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

View File

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