Skip to content

Commit

Permalink
bot/modules/language: convert to TS (#626)
Browse files Browse the repository at this point in the history
Convert bot/modules/language to TypeScript.
  • Loading branch information
webwarrior-ws authored Jan 23, 2025
1 parent 4a5d20b commit 67cf255
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
13 changes: 0 additions & 13 deletions bot/modules/language/actions.js

This file was deleted.

16 changes: 16 additions & 0 deletions bot/modules/language/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { User } from '../../../models';
import { MainContext } from '../../start';

export const setLanguage = async (ctx: MainContext) => {
const tgId = (ctx.update as any).callback_query.from.id;
const user = await User.findOne({ tg_id: tgId });
if (user === null) return;
const code = ctx.match?.[1];
if (code === undefined)
throw new Error("setLanguage: code is undefined");
ctx.deleteMessage();
user.lang = code;
ctx.i18n.locale(code);
await user.save();
await ctx.reply(ctx.i18n.t('operation_successful'));
};
21 changes: 0 additions & 21 deletions bot/modules/language/commands.js

This file was deleted.

23 changes: 23 additions & 0 deletions bot/modules/language/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from 'path';
import fs from 'fs';
import { getLanguageFlag } from '../../../util';
import { logger } from '../../../logger';
import { showFlagsMessage } from './messages';
import { ILanguage } from '../../../util/languagesModel';
import { MainContext } from '../../start';

export const setlang = async (ctx: MainContext) => {
try {
const flags: ILanguage[] = [];
fs.readdirSync(path.join(__dirname, '../../../../locales')).forEach(file => {
const lang = file.split('.')[0];
const flag = getLanguageFlag(lang);
if (flag !== undefined) {
flags.push(flag);
}
});
await showFlagsMessage(ctx, flags, ctx.user.lang);
} catch (error) {
logger.error(error);
}
};
8 changes: 0 additions & 8 deletions bot/modules/language/index.js

This file was deleted.

10 changes: 10 additions & 0 deletions bot/modules/language/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { userMiddleware } = require('../../middleware/user');
import * as commands from './commands';
import * as actions from './actions';
import { Telegraf } from 'telegraf';
import { MainContext } from '../../start';

exports.configure = (bot: Telegraf) => {
bot.command('setlang', userMiddleware, ctx => commands.setlang(ctx as unknown as MainContext));
bot.action(/^setLanguage_([a-z]{2})$/, userMiddleware, ctx => actions.setLanguage(ctx as unknown as MainContext));
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { logger } = require('../../../logger');
import { logger } from '../../../logger';
import { ILanguage } from '../../../util/languagesModel';
import { MainContext } from '../../start';

exports.showFlagsMessage = async (ctx, flags, code) => {
export const showFlagsMessage = async (ctx: MainContext, flags: ILanguage[], code: string) => {
try {
const buttons = [];
while (flags.length > 0) {
Expand Down

0 comments on commit 67cf255

Please sign in to comment.