add translation using i18n
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/db.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/db.js
									
									
									
									
									
								
							@@ -156,6 +156,14 @@ const markMessageAsRead = async (roomId, recipient) => {
 | 
			
		||||
    return (await db.query("UPDATE messages SET read = TRUE WHERE sender = $1 AND recipient = $2", [roomId, recipient]));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const setUserLanguage = async (roomId, language) => {
 | 
			
		||||
    await db.query("UPDATE users SET language = $1 WHERE room_id = $2", [language, roomId]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getUserLanguage = async (roomId) => {
 | 
			
		||||
    return (await db.query("SELECT language FROM users WHERE room_id = $1", [roomId])).rows[0].language;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export {
 | 
			
		||||
    eraseUser,
 | 
			
		||||
    appendUserPictures,
 | 
			
		||||
@@ -176,5 +184,7 @@ export {
 | 
			
		||||
    uploadMediaAsMessage,
 | 
			
		||||
    insertMessageIntoDB,
 | 
			
		||||
    getUnreadMessages,
 | 
			
		||||
    markMessageAsRead
 | 
			
		||||
    markMessageAsRead,
 | 
			
		||||
    setUserLanguage,
 | 
			
		||||
    getUserLanguage
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										102
									
								
								src/index.js
									
									
									
									
									
								
							
							
						
						
									
										102
									
								
								src/index.js
									
									
									
									
									
								
							@@ -13,7 +13,6 @@ import {
 | 
			
		||||
    logError,
 | 
			
		||||
    logInfo,
 | 
			
		||||
    readConfig,
 | 
			
		||||
    readMessages,
 | 
			
		||||
    uploadMediaFromEvent
 | 
			
		||||
} from './utils.js';
 | 
			
		||||
 | 
			
		||||
@@ -31,16 +30,25 @@ import {
 | 
			
		||||
    insertMessageIntoDB,
 | 
			
		||||
    markMessageAsRead,
 | 
			
		||||
    setUserState,
 | 
			
		||||
    uploadMediaAsMessage
 | 
			
		||||
    uploadMediaAsMessage,
 | 
			
		||||
    setUserLanguage,
 | 
			
		||||
    getUserLanguage
 | 
			
		||||
} from './db.js';
 | 
			
		||||
 | 
			
		||||
import { processRequest, showRandomProfileToUser, showNewLikes } from "./interactions.js";
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
const messages = readMessages();
 | 
			
		||||
 | 
			
		||||
const homeserverUrl = config.homeserverURL;
 | 
			
		||||
const accessToken = config.token;
 | 
			
		||||
@@ -60,11 +68,19 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
        let answer = event.content.body;
 | 
			
		||||
        let msgtype = event.content.msgtype
 | 
			
		||||
 | 
			
		||||
        let preferredLanguage = await getUserLanguage(roomId);
 | 
			
		||||
        if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
 | 
			
		||||
        i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
        switch (current_action) {
 | 
			
		||||
            case "wait_start":
 | 
			
		||||
                if (answer !== "!start") return;
 | 
			
		||||
                let a = answer.split(" ")
 | 
			
		||||
                if (a[0] !== "!start") return;
 | 
			
		||||
                if (a[1] !== "ru" && a[1] !== "en") return;
 | 
			
		||||
                await setUserLanguage(roomId, a[1]);
 | 
			
		||||
                await setUserState(roomId, "country");
 | 
			
		||||
                await client.sendText(roomId, messages.setup.country);
 | 
			
		||||
                await client.sendText(roomId, i18n.t(["setup", "country"]));
 | 
			
		||||
                
 | 
			
		||||
                break;
 | 
			
		||||
            case "country":
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer, 'city');
 | 
			
		||||
@@ -78,12 +94,13 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
            case "age":
 | 
			
		||||
                answer = parseInt(answer)
 | 
			
		||||
                if (!answer) {
 | 
			
		||||
                    await client.sendText(roomId, messages.errors.didntunderstand);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["general", "age"]));
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (answer < 14) {
 | 
			
		||||
                    await client.sendText(roomId, messages.errors.tooyoung);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["errors", "tooyoung"]));
 | 
			
		||||
 | 
			
		||||
                    await client.leaveRoom(roomId);
 | 
			
		||||
                    await eraseUser(roomId);
 | 
			
		||||
                    return;
 | 
			
		||||
@@ -93,7 +110,8 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
            case "sex":
 | 
			
		||||
                answer = answer.toLowerCase().trim();
 | 
			
		||||
                if (answer.toLowerCase() != "male" && answer.toLowerCase() != "female") {
 | 
			
		||||
                    await client.sendText(roomId, messages.errors.twosexes);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["errors", "twosexes"]));
 | 
			
		||||
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer[0], 'interest');
 | 
			
		||||
@@ -101,7 +119,8 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
            case "interest":
 | 
			
		||||
                answer = answer.toLowerCase().trim();
 | 
			
		||||
                if (answer != "male" && answer != "female" && answer != "both") {
 | 
			
		||||
                    await client.sendText(roomId, messages.errors.didntunderstand);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["errors", "didntunderstand"]));
 | 
			
		||||
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                await processRequest(client, roomId, current_action, answer[0], 'description');
 | 
			
		||||
@@ -111,13 +130,15 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                break;
 | 
			
		||||
            case "pictures":
 | 
			
		||||
                if (event.content?.msgtype !== 'm.image' && event.content?.msgtype !== 'm.video') {
 | 
			
		||||
                    await client.sendText(roomId, messages.setup.done);
 | 
			
		||||
                    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, messages.errors.toomuch);
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["errors", "toomuch"]));
 | 
			
		||||
 | 
			
		||||
                        await setUserState(roomId, 'view_profiles');
 | 
			
		||||
                        await showRandomProfileToUser(client, roomId);
 | 
			
		||||
                    } else {
 | 
			
		||||
@@ -126,10 +147,13 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                        await appendUserPictures(roomId, mxc, type);
 | 
			
		||||
                        let pictures_count = await getAmountOfUserPictures(roomId);
 | 
			
		||||
                        if (pictures_count < maxAmountOfPhotoesPerUser) {
 | 
			
		||||
                            await client.sendText(roomId, messages.setup.more + String(maxAmountOfPhotoesPerUser - pictures_count));
 | 
			
		||||
                            await client.sendText(roomId, i18n.t(["setup", "more"], { amount: String(maxAmountOfPhotoesPerUser - pictures_count) }));
 | 
			
		||||
 | 
			
		||||
                        } else {
 | 
			
		||||
                            await client.sendText(roomId, messages.setup.enough);
 | 
			
		||||
                            await client.sendText(roomId, messages.setup.done);
 | 
			
		||||
                            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);
 | 
			
		||||
                        }
 | 
			
		||||
@@ -142,12 +166,15 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                    await appendUserLikes(roomId, currently_viewing);
 | 
			
		||||
                    let value = await checkForMutualLike(roomId, currently_viewing);
 | 
			
		||||
                    if (value) {
 | 
			
		||||
                        await client.sendText(roomId, messages.general.newlikes);
 | 
			
		||||
                        await client.sendText(currently_viewing, messages.general.newlikes);
 | 
			
		||||
                        await client.sendText(roomId, 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, messages.general.message);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["general", "message"]));
 | 
			
		||||
 | 
			
		||||
                    return;
 | 
			
		||||
                } else if (answer == '🏠️' || answer == '4') {
 | 
			
		||||
                    await client.sendText(roomId, messages.general.menu);
 | 
			
		||||
@@ -168,10 +195,13 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                    content = answer;
 | 
			
		||||
                }
 | 
			
		||||
                if (await insertMessageIntoDB(roomId, recipient, msgtype, content)) {
 | 
			
		||||
                    await client.sendText(recipient, messages.general.newmessage);
 | 
			
		||||
                    await client.sendText(roomId, messages.general.messagesent);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["general", "newmessage"]));
 | 
			
		||||
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["general", "messagesent"]));
 | 
			
		||||
 | 
			
		||||
                } else {
 | 
			
		||||
                    await client.sendText(roomId, messages.errors.alreadymessaged);
 | 
			
		||||
                    await client.sendText(roomId, i18n.t(["general", "alreadymessaged"]));
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                await setUserState(roomId, 'view_profiles');
 | 
			
		||||
                await showRandomProfileToUser(client, roomId);
 | 
			
		||||
@@ -184,17 +214,21 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                        break;
 | 
			
		||||
                    case '2':
 | 
			
		||||
                        await showNewLikes(client, roomId);
 | 
			
		||||
                        await client.sendText(roomId, messages.general.menu);
 | 
			
		||||
                        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, messages.general.nonewmessages);
 | 
			
		||||
                            await client.sendText(roomId, i18n.t(["general", "nonewmessages"]));
 | 
			
		||||
 | 
			
		||||
                            return;
 | 
			
		||||
                        }
 | 
			
		||||
                        await client.sendText(roomId, "Messages:");
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "msg"]));
 | 
			
		||||
 | 
			
		||||
                        for (let message of unreadMessages) {
 | 
			
		||||
                            await client.sendText(roomId, message.mx_id + messages.general.showmessage);
 | 
			
		||||
                            await client.sendText(roomId, i18n.t(["general", "showmessage"], { user: message.mx_id }));
 | 
			
		||||
 | 
			
		||||
                            if (message.type == "t") {
 | 
			
		||||
                                await client.sendText(roomId, message.content)
 | 
			
		||||
                            } else if (message.type == "p" || message.type == "v") {
 | 
			
		||||
@@ -214,16 +248,19 @@ client.on("room.message", async (roomId, event) => {
 | 
			
		||||
                            }
 | 
			
		||||
                            await markMessageAsRead(message.sender, roomId);
 | 
			
		||||
                        }
 | 
			
		||||
                        await client.sendText(roomId, messages.general.menu);
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "menu"]));
 | 
			
		||||
                        break;
 | 
			
		||||
                    default:
 | 
			
		||||
                        await client.sendText(roomId, messages.errors.didntunderstand);
 | 
			
		||||
                        await client.sendText(roomId, messages.general.menu);
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "didntunderstand"]));
 | 
			
		||||
 | 
			
		||||
                        await client.sendText(roomId, i18n.t(["general", "menu"]));
 | 
			
		||||
 | 
			
		||||
                        break;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                await client.sendText(roomId, messages.errors.didntunderstand);
 | 
			
		||||
                await client.sendText(roomId, i18n.t(["general", "didntunderstand"]));
 | 
			
		||||
 | 
			
		||||
                return;
 | 
			
		||||
        }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
@@ -249,7 +286,8 @@ client.on("room.invite", async (roomId, event) => {
 | 
			
		||||
        let isDM = members.length == 2 ? true : false;
 | 
			
		||||
 | 
			
		||||
        if (!isDM) {
 | 
			
		||||
            await client.sendText(roomId, messages.errors.notadm);
 | 
			
		||||
            await client.sendText(roomId, i18n.t(["errors", "notadm"]));
 | 
			
		||||
 | 
			
		||||
            await client.leaveRoom(roomId)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -262,7 +300,11 @@ client.on("room.invite", async (roomId, event) => {
 | 
			
		||||
        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 client.sendText(roomId, messages.general.welcome);
 | 
			
		||||
            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')
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ import {
 | 
			
		||||
    logError,
 | 
			
		||||
    logInfo,
 | 
			
		||||
    readConfig,
 | 
			
		||||
    readMessages
 | 
			
		||||
} from './utils.js';
 | 
			
		||||
 | 
			
		||||
import {
 | 
			
		||||
@@ -14,9 +13,19 @@ import {
 | 
			
		||||
    getAllLikesForUser,
 | 
			
		||||
    checkForMutualLike,
 | 
			
		||||
    markLikeAsRead,
 | 
			
		||||
    getUserLanguage
 | 
			
		||||
} from "./db.js";
 | 
			
		||||
 | 
			
		||||
const messages = readMessages();
 | 
			
		||||
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 requiresLengths = {
 | 
			
		||||
    "country": 64,
 | 
			
		||||
@@ -26,20 +35,32 @@ const requiresLengths = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const processRequest = async (client, roomId, question, answer, nextQuestion) => {
 | 
			
		||||
 | 
			
		||||
    let preferredLanguage = await getUserLanguage(roomId);
 | 
			
		||||
    if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
 | 
			
		||||
    i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
    if (answer.length > requiresLengths[question]) {
 | 
			
		||||
        await client.sendText(roomId, messages.errors.toobig);
 | 
			
		||||
        await client.sendText(roomId, i18n.t(["errors", "toobig"]));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    await db.query(`UPDATE users SET ${question} = $1 WHERE room_id = $2`, [answer, roomId]);
 | 
			
		||||
    await client.sendText(roomId, `Set your ${question} setting to "${answer}".`);
 | 
			
		||||
    await client.sendText(roomId, messages.setup[nextQuestion]); //next question
 | 
			
		||||
    await client.sendText(roomId, i18n.t(["general", "setopt"], { opt: answer }));
 | 
			
		||||
 | 
			
		||||
    await client.sendText(roomId, i18n.t(["setup", nextQuestion]));
 | 
			
		||||
 | 
			
		||||
    setUserState(roomId, nextQuestion);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const showRandomProfileToUser = async (client, roomId) => {
 | 
			
		||||
    let preferredLanguage = await getUserLanguage(roomId);
 | 
			
		||||
    if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
 | 
			
		||||
    i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
    let chosenProfile = await selectProfilesForUser(roomId);
 | 
			
		||||
    if (!chosenProfile) {
 | 
			
		||||
        await client.sendText(roomId, messages.errors.noprofiles);
 | 
			
		||||
        await client.sendText(roomId, i18n.t(["errors", "noprofiles"]));
 | 
			
		||||
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    let message =
 | 
			
		||||
@@ -71,13 +92,18 @@ ${chosenProfile.description}`;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const showProfileToUser = async (client, roomId, profileId) => {
 | 
			
		||||
    let preferredLanguage = await getUserLanguage(roomId);
 | 
			
		||||
    if (!preferredLanguage) preferredLanguage = i18n.defaultLocale;
 | 
			
		||||
    i18n.locale = preferredLanguage;
 | 
			
		||||
 | 
			
		||||
    let profileInfo = await getProfileInfo(profileId);
 | 
			
		||||
    let message =
 | 
			
		||||
        `${profileInfo.country}, ${profileInfo.city}.
 | 
			
		||||
${profileInfo.name}, ${profileInfo.sex == 'm' ? 'male' : 'female'}, ${profileInfo.age}.
 | 
			
		||||
${profileInfo.description}`;
 | 
			
		||||
 | 
			
		||||
    await client.sendText(roomId, messages.general.showalike);
 | 
			
		||||
    await client.sendText(roomId, i18n.t(["general", "showalike"]));
 | 
			
		||||
 | 
			
		||||
    await client.sendText(roomId, message);
 | 
			
		||||
 | 
			
		||||
    if (profileInfo.media) {
 | 
			
		||||
@@ -95,8 +121,8 @@ ${profileInfo.description}`;
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    await client.sendText(roomId, i18n.t(["general", "mxid"], { mxid: profileInfo.mx_id }));
 | 
			
		||||
 | 
			
		||||
    await client.sendText(roomId, messages.general.mxid + profileInfo.mx_id);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const showNewLikes = async (client, roomId) => {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								src/utils.js
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/utils.js
									
									
									
									
									
								
							@@ -8,7 +8,6 @@ import {
 | 
			
		||||
} from "matrix-bot-sdk";
 | 
			
		||||
 | 
			
		||||
const configPath = './config.json';
 | 
			
		||||
const messagesPath = './messages.json';
 | 
			
		||||
 | 
			
		||||
const logError = (message) => {
 | 
			
		||||
    let time = new Date;
 | 
			
		||||
@@ -40,14 +39,6 @@ const readConfig = () => {
 | 
			
		||||
    return JSON.parse(fs.readFileSync(configPath));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const readMessages = () => {
 | 
			
		||||
    if (!fs.existsSync(messagesPath)) {
 | 
			
		||||
        logError("No 'messages.json' file found. Please, ensure that you are up to date by syncing using 'git pull' command.");
 | 
			
		||||
        process.exit(-1);
 | 
			
		||||
    }
 | 
			
		||||
    return JSON.parse(fs.readFileSync(messagesPath));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const uploadMediaFromEvent = async (client, event) => {
 | 
			
		||||
    const message = new MessageEvent(event);
 | 
			
		||||
    const fileEvent = new MessageEvent(message.raw);
 | 
			
		||||
@@ -69,4 +60,4 @@ const convertMsgType = (msgtype) => {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export { readMessages, readConfig, logError, logInfo, uploadMediaFromEvent, convertMsgType };
 | 
			
		||||
export { readConfig, logError, logInfo, uploadMediaFromEvent, convertMsgType };
 | 
			
		||||
		Reference in New Issue
	
	Block a user