-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bot/modules/language: convert to TS (#626)
Convert bot/modules/language to TypeScript.
- Loading branch information
1 parent
4a5d20b
commit 67cf255
Showing
7 changed files
with
53 additions
and
44 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
6 changes: 4 additions & 2 deletions
6
bot/modules/language/messages.js → bot/modules/language/messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters