Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot/modules/language: convert to TS #626

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading