diff --git a/src/index.js b/src/index.js index 746efb9..564151d 100644 --- a/src/index.js +++ b/src/index.js @@ -58,11 +58,6 @@ import { db } from "./db.js"; import fs from "fs"; import { I18n } from "i18n-js"; -const i18n = new I18n({ - en: JSON.parse(fs.readFileSync("./translations/en.json")), - ru: JSON.parse(fs.readFileSync("./translations/ru.json")) -}); -i18n.defaultLocale = "en"; const config = readConfig(); @@ -79,6 +74,11 @@ client.on("room.message", async (roomId, event) => { try { if (event.sender === await client.getUserId()) return; + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + if (!await doesUserExist(roomId)) { i18n.locale = 'en' await client.sendText(roomId, i18n.t(["errors", "usernotexists"])); @@ -333,6 +333,11 @@ client.on("room.invite", async (roomId, event) => { await client.leaveRoom(roomId); } + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + logInfo(`Bot has joined a room with ID ${roomId}`); let mx_id = event.sender; diff --git a/src/interactions.js b/src/interactions.js index 5cb99d4..59e1c0a 100644 --- a/src/interactions.js +++ b/src/interactions.js @@ -32,12 +32,7 @@ const maxAmountOfPhotoesPerUser = config.maxAmountOfPhotoesPerUser; import { I18n } from "i18n-js"; -const i18n = new I18n({ - en: JSON.parse(fs.readFileSync("./translations/en.json")), - ru: JSON.parse(fs.readFileSync("./translations/ru.json")) -}); -i18n.defaultLocale = "en"; const requiresLengths = { "location": 32, @@ -47,6 +42,11 @@ const requiresLengths = { const processRequest = async (client, roomId, question, answer, nextQuestion) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + let preferredLanguage = await getUserLanguage(roomId); if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; i18n.locale = preferredLanguage; @@ -60,6 +60,7 @@ const processRequest = async (client, roomId, question, answer, nextQuestion) => await db.query(`UPDATE users SET ${q[0]} = $1 WHERE room_id = $2`, [answer, roomId]); else await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]); + await client.sendText(roomId, i18n.t(["general", "setopt"], { opt: answer })); if (nextQuestion == "menu") await client.sendText(roomId, i18n.t(["general", "menu"])); @@ -98,6 +99,11 @@ ${chosenProfile.description}`; } const showRandomProfileToUser = async (client, roomId) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + let preferredLanguage = await getUserLanguage(roomId); if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; i18n.locale = preferredLanguage; @@ -112,10 +118,18 @@ const showRandomProfileToUser = async (client, roomId) => { await showProfile(client, roomId, chosenProfile); await setUserCurrentlyViewingProfile(roomId, chosenProfile.room_id); + + + await client.sendText(roomId, i18n.t(["general", "rate"])); }; const showProfileToUser = async (client, roomId, profileId) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + let preferredLanguage = await getUserLanguage(roomId); if (!preferredLanguage) preferredLanguage = i18n.defaultLocale; i18n.locale = preferredLanguage; @@ -140,6 +154,11 @@ const showNewLikes = async (client, roomId) => { }; const wait_start = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + let a = answer.split(" "); if (a[0] !== "!start") return; if (a[1] !== "ru" && a[1] !== "en") return; @@ -150,6 +169,11 @@ const wait_start = async (client, roomId, current_action, answer, update) => { } const loc = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + let number = parseInt(answer); if (!number) { let cities = await findCity(answer); @@ -190,6 +214,11 @@ const loc = async (client, roomId, current_action, answer, update) => { } const range = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + answer = parseInt(answer.split(" ")[0]); if (!answer && answer != 0) { await client.sendText(roomId, i18n.t(["setup", "range"])); @@ -199,6 +228,11 @@ const range = async (client, roomId, current_action, answer, update) => { } const age = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + answer = parseInt(answer); if (!answer) { await client.sendText(roomId, i18n.t(["setup", "age"])); @@ -215,6 +249,11 @@ const age = async (client, roomId, current_action, answer, update) => { } const sex = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + answer = answer.toLowerCase().trim(); if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") { await client.sendText(roomId, i18n.t(["errors", "twosexes"])); @@ -224,6 +263,11 @@ const sex = async (client, roomId, current_action, answer, update) => { } const interest = async (client, roomId, current_action, answer, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + answer = answer.toLowerCase().trim(); if (answer != "male" && answer != "female" && answer != "both") { await client.sendText(roomId, i18n.t(["errors", "didntunderstand"])); @@ -233,6 +277,11 @@ const interest = async (client, roomId, current_action, answer, update) => { } const pictures = async (client, roomId, current_action, event, update) => { + const i18n = new I18n({ + en: JSON.parse(fs.readFileSync("./translations/en.json")), + ru: JSON.parse(fs.readFileSync("./translations/ru.json")) + }); + const msgtype = event.content.msgtype; if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') { await client.sendText(roomId, i18n.t(["setup", "done"])); diff --git a/translations/en.json b/translations/en.json index 1a28b9b..2b7f3a5 100644 --- a/translations/en.json +++ b/translations/en.json @@ -2,7 +2,7 @@ "general": { "welcome": "Hello! I am a bot for new meetings in Matrix!\nTo start, please, type \"!start \", where language are 'en' or 'ru'.\nI'll ask some questions to fill your profile. You can change that info later, if you will.\nThen, I'll start showing you another people's profiles.\nYou can like other profile, skip or send a message to the person.\nIf you mutually liked each other, you'll be given each other's MXID to start a DM.\nMy source code can be found on gitea: https://git.foxarmy.org/leca/heart2heart.\n\nGood luck!", "rate": "Decide wether you like (👍️, ❤️ or 1), dislike (👎️, 💔 or 2) or wanna send a messsage (💌 or 3). Write '🏠️' or 4 to quit to menu. Any other response counts as dislike.", - "menu": "Pick an action:\n0) Show your profile\n1) Start watching profiles\n2) Check for mutual likes\n3) Check for messages\n4) Change location\n5) Change range\n6) Change name\n7) Change age\n8) Change sex\n9) Change interest\n10) Change description\n11) Change pictures\n12) Change langage", + "menu": "Pick an action:\n0) Show your profile\n1) Start watching profiles\n2) Check for mutual likes\n3) Check for messages\n4) Change location\n5) Change range\n6) Change name\n7) Change age\n8) Change sex\n9) Change interest\n10) Change description\n11) Change pictures\n12) Change language", "newlikes": "You have new mutual like(s)! Go into menu (🏠️) to see!", "showalike": "This person and you mutually liked each other!", "mxid": "Go drop them a line! Their MXID is: %{mxid}",