From c6b48dd0d6250d46d6463c15c49b9dca63294d35 Mon Sep 17 00:00:00 2001 From: Dmitry Anderson <4nd3r5z0n@gmail.com> Date: Sun, 27 Oct 2024 19:29:26 +0100 Subject: [PATCH] Languages update --- bot/lang/en.ts | 14 -------------- bot/lang/export.ts | 30 ------------------------------ bot/lang/hr.ts | 9 --------- bot/lang/hu.ts | 14 -------------- bot/lang/lang_pack.ts | 20 -------------------- bot/lang/pl.ts | 9 --------- bot/lang/rs.ts | 14 -------------- bot/lang/ru.ts | 14 -------------- bot/lang/ua.ts | 14 -------------- bot/locales/en.ftl | 1 + bot/locales/hu.ftl | 4 ++++ bot/locales/pl.ftl | 5 +++++ bot/locales/rs.ftl | 4 ++++ bot/locales/ru.ftl | 4 ++++ bot/locales/ua.ftl | 4 ++++ bot/normal_mode/captcha.ts | 12 +++++------- bot/normal_mode/init.ts | 5 +++++ bot/normal_mode/safebooru.ts | 8 ++++---- 18 files changed, 36 insertions(+), 149 deletions(-) delete mode 100644 bot/lang/en.ts delete mode 100644 bot/lang/export.ts delete mode 100644 bot/lang/hr.ts delete mode 100644 bot/lang/hu.ts delete mode 100644 bot/lang/lang_pack.ts delete mode 100644 bot/lang/pl.ts delete mode 100644 bot/lang/rs.ts delete mode 100644 bot/lang/ru.ts delete mode 100644 bot/lang/ua.ts diff --git a/bot/lang/en.ts b/bot/lang/en.ts deleted file mode 100644 index 9189ce5..0000000 --- a/bot/lang/en.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { LANG_EN, LangPack } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_EN, - btn_labels: null, - replies: { - captcha: { - already_in_chat: "You are already in chat", - passed: (invite_link: string) => `Captcha passed! Now u can join to the community: ${invite_link}`, - failed: (timeout_mins: number) => `U didn't pass the captcha. U can try again after ${timeout_mins} minutes.`, - } - }, -} -export default pack diff --git a/bot/lang/export.ts b/bot/lang/export.ts deleted file mode 100644 index 5972869..0000000 --- a/bot/lang/export.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { LangPack } from "./lang_pack.ts" -import en_pack from "./en.ts" -import ru_pack from "./ru.ts" -import rs_pack from "./rs.ts" -import hr_pack from "./hr.ts" -import hu_pack from "./hu.ts" -import pl_pack from "./pl.ts" -import ua_pack from "./pl.ts" - - -export * from "./lang_pack.ts" - -export class LangManager { - static defaultLang: LangPack = en_pack - static langsMap = new Map([ - [en_pack.code, en_pack], - [ru_pack.code, ru_pack], - [rs_pack.code, rs_pack], - [hr_pack.code, hr_pack], - [hu_pack.code, hu_pack], - [pl_pack.code, pl_pack], - [ua_pack.code, ua_pack], - ]) - - static getDefaultLang(): LangPack { return this.defaultLang } - static getLang(langCode?: string): LangPack { - return this.langsMap.get(langCode || this.defaultLang.code) - || this.defaultLang - } -} \ No newline at end of file diff --git a/bot/lang/hr.ts b/bot/lang/hr.ts deleted file mode 100644 index 27a26cc..0000000 --- a/bot/lang/hr.ts +++ /dev/null @@ -1,9 +0,0 @@ -import pack_rs from "./rs.ts" -import { LANG_HR, LangPack } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_HR, - btn_labels: null, - replies: pack_rs.replies, -} -export default pack \ No newline at end of file diff --git a/bot/lang/hu.ts b/bot/lang/hu.ts deleted file mode 100644 index 9089a0b..0000000 --- a/bot/lang/hu.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { LANG_EN, LangPack } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_EN, - btn_labels: null, - replies: { - captcha: { - already_in_chat: "Már a csevegésben vagy.", - passed: (invite_link: string) => `Captcha sikeresen teljesítve! Most már csatlakozhat közösségünkhöz: ${invite_link}`, - failed: (timeout_mins: number) => `A captcha nem sikerült. 15 perc múlva próbálkozhat ${timeout_mins} újra.`, - } - }, -} -export default pack diff --git a/bot/lang/lang_pack.ts b/bot/lang/lang_pack.ts deleted file mode 100644 index 54c915b..0000000 --- a/bot/lang/lang_pack.ts +++ /dev/null @@ -1,20 +0,0 @@ -export const LANG_RU = "ru" -export const LANG_EN = "en" -export const LANG_UA = "ua" -export const LANG_HU = "hu" -export const LANG_RS = "rs" -export const LANG_HR = "hr" -export const LANG_PL = "pl" - - -export interface LangPack { - code: string, - btn_labels: null, - replies: { - captcha: { - already_in_chat: string, - passed: (invite_link: string) => string, - failed: (timeout_mins: number) => string, - } - }, -} \ No newline at end of file diff --git a/bot/lang/pl.ts b/bot/lang/pl.ts deleted file mode 100644 index c7cf29c..0000000 --- a/bot/lang/pl.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { LANG_PL, LangPack } from "./lang_pack.ts"; -import lang_en from "./en.ts" - -const pack: LangPack = { - code: LANG_PL, - btn_labels: null, - replies: lang_en.replies, -} -export default pack \ No newline at end of file diff --git a/bot/lang/rs.ts b/bot/lang/rs.ts deleted file mode 100644 index 364c1dc..0000000 --- a/bot/lang/rs.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { LangPack, LANG_RS } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_RS, - btn_labels: null, - replies: { - captcha: { - already_in_chat: "Ti si već u chat-u", - passed: (invite_link: string) => `Uspeo si sa captch-om! Sada mozes predruziti se nasoj zajednici: ${invite_link}`, - failed: (timeout_mins: number) => `Nisi prošao captch-u. Mozes pokušati ponovo za ${timeout_mins} minuta.`, - } - }, -} -export default pack \ No newline at end of file diff --git a/bot/lang/ru.ts b/bot/lang/ru.ts deleted file mode 100644 index 0fd9f00..0000000 --- a/bot/lang/ru.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { LANG_RU, LangPack } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_RU, - btn_labels: null, - replies: { - captcha: { - already_in_chat: "Вы уже в чате", - passed: (invite_link: string) => `Капча пройдена! Теперь вы можете присоединиться к нашему сообществу: ${invite_link}`, - failed: (timeout_mins: number) => `Капча не пройдена. Вы сможете попробовать снова через ${timeout_mins} минут.`, - } - }, -} -export default pack \ No newline at end of file diff --git a/bot/lang/ua.ts b/bot/lang/ua.ts deleted file mode 100644 index b0d8898..0000000 --- a/bot/lang/ua.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { LANG_UA, LangPack } from "./lang_pack.ts"; - -const pack: LangPack = { - code: LANG_UA, - btn_labels: null, - replies: { - captcha: { - already_in_chat: "Ви вже в чаті", - passed: (invite_link: string) => `Капча пройдена! Тепер ви можете приєднатися до нашої спільноти: ${invite_link}`, - failed: (timeout_mins: number) => `Капча не пройдена. Ви зможете спробувати ще раз через ${timeout_mins} хвилин.`, - } - }, -} -export default pack \ No newline at end of file diff --git a/bot/locales/en.ftl b/bot/locales/en.ftl index b3251b1..2c77d63 100644 --- a/bot/locales/en.ftl +++ b/bot/locales/en.ftl @@ -1,3 +1,4 @@ +# Captcha captcha-already-in-chat = You are already in chat captcha-passed = Captcha passed! Now you can join the community: { invite_link } captcha-failed = You didn't pass the captcha. You can try again after { timeout_mins } minutes. diff --git a/bot/locales/hu.ftl b/bot/locales/hu.ftl index e69de29..56da55c 100644 --- a/bot/locales/hu.ftl +++ b/bot/locales/hu.ftl @@ -0,0 +1,4 @@ +# Captcha +captcha-already-in-chat = Már a csevegésben vagy +captcha-passed = Captcha sikeresen teljesítve! Most már csatlakozhat közösségünkhöz: { invite_link } +captcha-failed = A captcha nem sikerült. 15 perc múlva próbálkozhat { timeout_mins } újra. diff --git a/bot/locales/pl.ftl b/bot/locales/pl.ftl index e69de29..c03ada8 100644 --- a/bot/locales/pl.ftl +++ b/bot/locales/pl.ftl @@ -0,0 +1,5 @@ +# Captcha +captcha-already-in-chat = Już jesteś na czacie +captcha-passed = Captcha pomyślnie zakończona! Teraz możesz dołączyć do naszej społeczności: { invite_link } +captcha-failed = Captcha nieudana. Spróbuj ponownie za { timeout_mins } minut. + diff --git a/bot/locales/rs.ftl b/bot/locales/rs.ftl index e69de29..db21a93 100644 --- a/bot/locales/rs.ftl +++ b/bot/locales/rs.ftl @@ -0,0 +1,4 @@ +# Captcha +captcha-already-in-chat = Ti si već u chat-u +captcha-passed = Uspeo si sa captch-om! Sada mozes predruziti se nasoj zajednici: { invite_link } +captcha-failed = Nisi prošao captch-u. Mozes pokušati ponovo za { timeout_mins } minuta. diff --git a/bot/locales/ru.ftl b/bot/locales/ru.ftl index e69de29..bad7855 100644 --- a/bot/locales/ru.ftl +++ b/bot/locales/ru.ftl @@ -0,0 +1,4 @@ +# Captcha +captcha-already-in-chat = Вы уже в чате +captcha-passed = Капча пройдена! Теперь вы можете присоединиться к нашему сообществу: { invite_link } +captcha-failed = Капча не пройдена. Вы сможете попробовать снова через { timeout_mins } минут. diff --git a/bot/locales/ua.ftl b/bot/locales/ua.ftl index e69de29..7976725 100644 --- a/bot/locales/ua.ftl +++ b/bot/locales/ua.ftl @@ -0,0 +1,4 @@ +# Captcha +captcha-already-in-chat = Ви вже в чаті +captcha-passed = Капча пройдена! Капча пройдена! Тепер ви можете приєднатися до нашої спільноти: { invite_link } +captcha-failed = Капча не пройдена. Ви зможете спробувати ще раз через { timeout_mins } хвилин. diff --git a/bot/normal_mode/captcha.ts b/bot/normal_mode/captcha.ts index 4b7250a..864eb28 100644 --- a/bot/normal_mode/captcha.ts +++ b/bot/normal_mode/captcha.ts @@ -1,7 +1,6 @@ import { Filter } from "https://deno.land/x/grammy/mod.ts"; import { BotConfig } from "../../cfg/config.ts"; import { Ctx } from "../ctx.ts"; -import { LangManager } from "../lang/export.ts"; import { Kysely } from 'npm:kysely'; import { Database } from "../../repo/scheme.ts"; import { checkUserRestrictions } from "../../core/users.ts"; @@ -66,16 +65,15 @@ const captchaPassed = async (ctx: Ctx, db: Kysely, cfg: BotConfig) => }).execute() }) - ctx.reply(LangManager.getLang(ctx.from!.language_code) - .replies.captcha.passed(link.invite_link)) + await ctx.reply(ctx.t("captcha-passed", + { invite_link: link.invite_link })) ctx.session.captcha_data = undefined } const initUserCaptcha = async (ctx: Ctx, db: Kysely, user: CheckUserOnStartOut, cfg: BotConfig) => { if (user.isChatParticipant) { - ctx.reply(LangManager.getLang(ctx.from!.language_code) - .replies.captcha.already_in_chat) + await ctx.reply(ctx.t("captcha-already-in-chat")) } else if (user.activeInviteLink) { await ctx.reply(user.activeInviteLink) } else if (user.isCaptchaSolved) { @@ -109,8 +107,8 @@ const checkCaptchaSolution = async (ctx: Filter, db: Kysely ctx.session.captcha_data!.tries_failed++ await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id) if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) { - ctx.reply(LangManager.getLang(ctx.msg.from.language_code) - .replies.captcha.failed(TIMEOUT_AFTER_FAIL_MINS)) + await ctx.reply(ctx.t("captcha-already-in-chat", + {timeout_mins: TIMEOUT_AFTER_FAIL_MINS})) await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id) // TODO: Add timeout } diff --git a/bot/normal_mode/init.ts b/bot/normal_mode/init.ts index f69ed87..709530a 100644 --- a/bot/normal_mode/init.ts +++ b/bot/normal_mode/init.ts @@ -8,6 +8,8 @@ import { checkUser, checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts"; import { handleSafebooruQuery } from "./safebooru.ts"; +const DEFAULT_LOCALE = "en" + interface ChatMemberUpdateStatus { joined: boolean, left: boolean, @@ -36,6 +38,7 @@ export const init = (bot: Bot, db: Kysely, cfg: CompiledConfig) = bot.on('message', async (ctx, next) => { if (ctx.from?.is_bot) return console.log(`Chat ID: ${ctx.msg.chat.id} From: ${ctx.from.id} Message: ${ctx.msg.text}`) + ctx.i18n.useLocale(ctx.from.language_code || DEFAULT_LOCALE) await next() }) @@ -59,6 +62,8 @@ export const init = (bot: Bot, db: Kysely, cfg: CompiledConfig) = bot.on('chat_member', async (ctx, next) => { if (ctx.from.is_bot) return if (ctx.chat.id !== botCfg.chat_id) return + + ctx.i18n.useLocale(ctx.from.language_code || DEFAULT_LOCALE) const { old_chat_member, new_chat_member } = ctx.chatMember const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member) diff --git a/bot/normal_mode/safebooru.ts b/bot/normal_mode/safebooru.ts index c2fbb36..7f4d5ca 100644 --- a/bot/normal_mode/safebooru.ts +++ b/bot/normal_mode/safebooru.ts @@ -20,8 +20,8 @@ export const handleSafebooruQuery = async ( const resp = await fetch(reqURL) if (!resp.ok) { console.log(`Error response from safebooru. URL: ${reqURL} Status: ${resp.status} ${resp.text}`) - const results: InlineQueryResult[] = [] - await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 }) + // TODO: Return error to user + await ctx.api.answerInlineQuery(ctx.inlineQuery.id, [], { cache_time: 1 }) return } const posts: SafebooruPostData[] = await resp.json() @@ -32,12 +32,12 @@ export const handleSafebooruQuery = async ( } const results: InlineQueryResult[] = [] - posts.map((post) => { + for (const post of posts) { results.push(InlineQueryResultBuilder.photo(`id-${post.id}`, post.file_url, { thumbnail_url: post.preview_url, caption: `[source](https://${SAFEBOORU_HOST}/index.php?page=post&s=view&id=${post.id})`, parse_mode: "MarkdownV2", })) - }) + } await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 }) }