Languages update

This commit is contained in:
Dmitry Anderson 2024-10-27 19:29:26 +01:00
parent e6ee3ee695
commit c6b48dd0d6
18 changed files with 36 additions and 149 deletions

View File

@ -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

View File

@ -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<string, LangPack>([
[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
}
}

View File

@ -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

View File

@ -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

View File

@ -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,
}
},
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,4 @@
# Captcha
captcha-already-in-chat = You are already in chat captcha-already-in-chat = You are already in chat
captcha-passed = Captcha passed! Now you can join the community: { invite_link } 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. captcha-failed = You didn't pass the captcha. You can try again after { timeout_mins } minutes.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -0,0 +1,4 @@
# Captcha
captcha-already-in-chat = Вы уже в чате
captcha-passed = Капча пройдена! Теперь вы можете присоединиться к нашему сообществу: { invite_link }
captcha-failed = Капча не пройдена. Вы сможете попробовать снова через { timeout_mins } минут.

View File

@ -0,0 +1,4 @@
# Captcha
captcha-already-in-chat = Ви вже в чаті
captcha-passed = Капча пройдена! Капча пройдена! Тепер ви можете приєднатися до нашої спільноти: { invite_link }
captcha-failed = Капча не пройдена. Ви зможете спробувати ще раз через { timeout_mins } хвилин.

View File

@ -1,7 +1,6 @@
import { Filter } from "https://deno.land/x/grammy/mod.ts"; import { Filter } from "https://deno.land/x/grammy/mod.ts";
import { BotConfig } from "../../cfg/config.ts"; import { BotConfig } from "../../cfg/config.ts";
import { Ctx } from "../ctx.ts"; import { Ctx } from "../ctx.ts";
import { LangManager } from "../lang/export.ts";
import { Kysely } from 'npm:kysely'; import { Kysely } from 'npm:kysely';
import { Database } from "../../repo/scheme.ts"; import { Database } from "../../repo/scheme.ts";
import { checkUserRestrictions } from "../../core/users.ts"; import { checkUserRestrictions } from "../../core/users.ts";
@ -66,16 +65,15 @@ const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) =>
}).execute() }).execute()
}) })
ctx.reply(LangManager.getLang(ctx.from!.language_code) await ctx.reply(ctx.t("captcha-passed",
.replies.captcha.passed(link.invite_link)) { invite_link: link.invite_link }))
ctx.session.captcha_data = undefined ctx.session.captcha_data = undefined
} }
const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => { const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => {
if (user.isChatParticipant) { if (user.isChatParticipant) {
ctx.reply(LangManager.getLang(ctx.from!.language_code) await ctx.reply(ctx.t("captcha-already-in-chat"))
.replies.captcha.already_in_chat)
} else if (user.activeInviteLink) { } else if (user.activeInviteLink) {
await ctx.reply(user.activeInviteLink) await ctx.reply(user.activeInviteLink)
} else if (user.isCaptchaSolved) { } else if (user.isCaptchaSolved) {
@ -109,8 +107,8 @@ const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely
ctx.session.captcha_data!.tries_failed++ ctx.session.captcha_data!.tries_failed++
await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id) await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id)
if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) { if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) {
ctx.reply(LangManager.getLang(ctx.msg.from.language_code) await ctx.reply(ctx.t("captcha-already-in-chat",
.replies.captcha.failed(TIMEOUT_AFTER_FAIL_MINS)) {timeout_mins: TIMEOUT_AFTER_FAIL_MINS}))
await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id) await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id)
// TODO: Add timeout // TODO: Add timeout
} }

View File

@ -8,6 +8,8 @@ import { checkUser, checkUserOnNewChatMember, onMemberLeftChat, checkUserOnStart
import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts"; import { checkCaptchaSolution, initUserCaptcha } from "./captcha.ts";
import { handleSafebooruQuery } from "./safebooru.ts"; import { handleSafebooruQuery } from "./safebooru.ts";
const DEFAULT_LOCALE = "en"
interface ChatMemberUpdateStatus { interface ChatMemberUpdateStatus {
joined: boolean, joined: boolean,
left: boolean, left: boolean,
@ -36,6 +38,7 @@ export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) =
bot.on('message', async (ctx, next) => { bot.on('message', async (ctx, next) => {
if (ctx.from?.is_bot) return if (ctx.from?.is_bot) return
console.log(`Chat ID: ${ctx.msg.chat.id} From: ${ctx.from.id} Message: ${ctx.msg.text}`) 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() await next()
}) })
@ -59,6 +62,8 @@ export const init = (bot: Bot<Ctx>, db: Kysely<Database>, cfg: CompiledConfig) =
bot.on('chat_member', async (ctx, next) => { bot.on('chat_member', async (ctx, next) => {
if (ctx.from.is_bot) return if (ctx.from.is_bot) return
if (ctx.chat.id !== botCfg.chat_id) 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 { old_chat_member, new_chat_member } = ctx.chatMember
const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member) const chatMemberUpdateStatus = getChatMemberUpdateStatus(old_chat_member, new_chat_member)

View File

@ -20,8 +20,8 @@ export const handleSafebooruQuery = async (
const resp = await fetch(reqURL) const resp = await fetch(reqURL)
if (!resp.ok) { if (!resp.ok) {
console.log(`Error response from safebooru. URL: ${reqURL} Status: ${resp.status} ${resp.text}`) console.log(`Error response from safebooru. URL: ${reqURL} Status: ${resp.status} ${resp.text}`)
const results: InlineQueryResult[] = [] // TODO: Return error to user
await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 }) await ctx.api.answerInlineQuery(ctx.inlineQuery.id, [], { cache_time: 1 })
return return
} }
const posts: SafebooruPostData[] = await resp.json() const posts: SafebooruPostData[] = await resp.json()
@ -32,12 +32,12 @@ export const handleSafebooruQuery = async (
} }
const results: InlineQueryResult[] = [] const results: InlineQueryResult[] = []
posts.map((post) => { for (const post of posts) {
results.push(InlineQueryResultBuilder.photo(`id-${post.id}`, post.file_url, { results.push(InlineQueryResultBuilder.photo(`id-${post.id}`, post.file_url, {
thumbnail_url: post.preview_url, thumbnail_url: post.preview_url,
caption: `[source](https://${SAFEBOORU_HOST}/index.php?page=post&s=view&id=${post.id})`, caption: `[source](https://${SAFEBOORU_HOST}/index.php?page=post&s=view&id=${post.id})`,
parse_mode: "MarkdownV2", parse_mode: "MarkdownV2",
})) }))
}) }
await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 }) await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 })
} }