This commit is contained in:
leca 2024-08-12 18:39:16 +03:00
parent 8be4673006
commit daa1f1e1d4
2 changed files with 15 additions and 38 deletions

View File

@ -73,17 +73,17 @@ client.on("room.message", async (roomId, event) => {
switch (current_action) {
case "wait_start":
let a = answer.split(" ")
let a = answer.split(" ");
if (a[0] !== "!start") return;
if (a[1] !== "ru" && a[1] !== "en") return;
await setUserLanguage(roomId, a[1]);
i18n.locale = a[1]
i18n.locale = a[1];
await setUserState(roomId, "location");
await client.sendText(roomId, i18n.t(["setup", "location"]));
break;
case "location":
let number = parseInt(answer)
let number = parseInt(answer);
if (!number) {
let cities = await findCity(answer);
await client.sendText(roomId, i18n.t(["setup", "choosecity"], {
@ -116,7 +116,7 @@ client.on("room.message", async (roomId, event) => {
country5: cities[4].country,
lat5: cities[4].lat,
lng5: cities[4].lng,
}))
}));
} else {
await processRequest(client, roomId, current_action, number, 'range');
}
@ -133,7 +133,7 @@ client.on("room.message", async (roomId, event) => {
await processRequest(client, roomId, current_action, answer, 'age');
break;
case "age":
answer = parseInt(answer)
answer = parseInt(answer);
if (!answer) {
await client.sendText(roomId, i18n.t(["setup", "age"]));
return;
@ -141,7 +141,6 @@ client.on("room.message", async (roomId, event) => {
if (answer < 14) {
await client.sendText(roomId, i18n.t(["errors", "tooyoung"]));
await client.leaveRoom(roomId);
await fullEraseUser(roomId);
return;
@ -152,7 +151,6 @@ client.on("room.message", async (roomId, event) => {
answer = answer.toLowerCase().trim();
if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
await client.sendText(roomId, i18n.t(["errors", "twosexes"]));
return;
}
await processRequest(client, roomId, current_action, answer[0], 'interest');
@ -161,7 +159,6 @@ client.on("room.message", async (roomId, event) => {
answer = answer.toLowerCase().trim();
if (answer != "male" && answer != "female" && answer != "both") {
await client.sendText(roomId, i18n.t(["errors", "didntunderstand"]));
return;
}
await processRequest(client, roomId, current_action, answer[0], 'description');
@ -172,14 +169,12 @@ client.on("room.message", async (roomId, event) => {
case "pictures":
if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') {
await client.sendText(roomId, i18n.t(["setup", "done"]));
await setUserState(roomId, 'view_profiles');
await showRandomProfileToUser(client, roomId);
} else {
let pictures_count = parseInt(await getAmountOfUserPictures(roomId));
if (pictures_count >= maxAmountOfPhotoesPerUser) {
await client.sendText(roomId, i18n.t(["errors", "toomuch"]));
await setUserState(roomId, 'view_profiles');
await showRandomProfileToUser(client, roomId);
} else {
@ -189,12 +184,9 @@ client.on("room.message", async (roomId, event) => {
let pictures_count = await getAmountOfUserPictures(roomId);
if (pictures_count < maxAmountOfPhotoesPerUser) {
await client.sendText(roomId, i18n.t(["setup", "more"], { amount: String(maxAmountOfPhotoesPerUser - pictures_count) }));
} else {
await client.sendText(roomId, i18n.t(["setup", "enough"]));
await client.sendText(roomId, i18n.t(["setup", "done"]));
await setUserState(roomId, 'view_profiles');
await showRandomProfileToUser(client, roomId);
}
@ -208,21 +200,17 @@ client.on("room.message", async (roomId, event) => {
let value = await checkForMutualLike(roomId, currently_viewing);
if (value) {
await client.sendText(currently_viewing, i18n.t(["general", "newlikes"]));
await client.sendText(roomId, i18n.t(["general", "newlikes"]));
}
} else if (answer == '💌' || answer == '3') {
await setUserState(roomId, 'send_message');
await client.sendText(roomId, i18n.t(["general", "message"]));
return;
} else if (answer == '🏠️' || answer == '4') {
await client.sendText(roomId, i18n.t(["general", "menu"]));
await setUserState(roomId, 'menu');
return;
}
await showRandomProfileToUser(client, roomId);
break;
case 'send_message':
@ -238,7 +226,6 @@ client.on("room.message", async (roomId, event) => {
if (await insertMessageIntoDB(roomId, recipient, msgtype, content)) {
await client.sendText(recipient, i18n.t(["general", "newmessage"]));
await client.sendText(roomId, i18n.t(["general", "messagesent"]));
} else {
await client.sendText(roomId, i18n.t(["errors", "alreadymessaged"]));
}
@ -254,13 +241,11 @@ client.on("room.message", async (roomId, event) => {
case '2':
await showNewLikes(client, roomId);
await client.sendText(roomId, i18n.t(["general", "menu"]));
break;
case '3':
let unreadMessages = await getUnreadMessages(roomId);
if (!unreadMessages || unreadMessages.length == 0) {
await client.sendText(roomId, i18n.t(["general", "nonewmessages"]));
return;
}
await client.sendText(roomId, i18n.t(["general", "msg"]));
@ -274,7 +259,7 @@ client.on("room.message", async (roomId, event) => {
let msgtype;
if (message.type == "p") {
msgtype = "m.image"
msgtype = "m.image";
} else if (message.type == "v") {
msgtype = "m.video";
}
@ -291,15 +276,12 @@ client.on("room.message", async (roomId, event) => {
break;
default:
await client.sendText(roomId, i18n.t(["errors", "didntunderstand"]));
await client.sendText(roomId, i18n.t(["general", "menu"]));
break;
}
break;
default:
await client.sendText(roomId, i18n.t(["errors", "didntunderstand"]));
return;
}
} catch (e) {
@ -311,7 +293,7 @@ client.on("room.event", async (roomId, event) => {
try {
if (event.type === "m.room.member" && event.content?.membership === "leave") {
await fullEraseUser(roomId);
logInfo(`Bot has left a room with ID ${roomId}`)
logInfo(`Bot has left a room with ID ${roomId}`);
client.leaveRoom(roomId);
}
} catch (e) {
@ -328,29 +310,27 @@ client.on("room.invite", async (roomId, event) => {
if (!isDM) {
await client.sendText(roomId, i18n.t(["errors", "notadm"]));
await client.leaveRoom(roomId)
await client.leaveRoom(roomId);
}
logInfo(`Bot has joined a room with ID ${roomId}`)
logInfo(`Bot has joined a room with ID ${roomId}`);
let mx_id = event.sender
let mx_id = event.sender;
let is_profile_exists = (await db.query("SELECT * FROM users WHERE room_id = $1", [roomId])).rowCount > 0;
if (!is_profile_exists) {
await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "wait_start"])
await db.query("INSERT INTO users(mx_id, room_id, current_action) VALUES ($1, $2, $3)", [mx_id, roomId, "wait_start"]);
i18n.locale = "en";
await client.sendText(roomId, i18n.t(["general", "welcome"]));
i18n.locale = "ru";
await client.sendText(roomId, i18n.t(["general", "welcome"]));
} else {
setUserState(roomId, 'view_profiles')
setUserState(roomId, 'view_profiles');
}
} catch (e) {
logError(e)
logError(e);
}
});

View File

@ -30,10 +30,9 @@ const i18n = new I18n({
i18n.defaultLocale = "en";
const requiresLengths = {
"country": 64,
"city": 64,
"location": 32,
"description": 512,
"name": 32,
"name": 32
};
const processRequest = async (client, roomId, question, answer, nextQuestion) => {
@ -60,8 +59,6 @@ const showRandomProfileToUser = async (client, roomId) => {
i18n.locale = preferredLanguage;
let chosenProfile = await selectProfilesForUser(roomId);
console.log(chosenProfile);
if (!chosenProfile) {
await client.sendText(roomId, i18n.t(["errors", "noprofiles"]));