|
1 | 1 | import type { Context } from '#root/bot/context.js' |
2 | | -import type { BotCommand, LanguageCode } from '@grammyjs/types' |
| 2 | +import type { LanguageCode } from '@grammyjs/types' |
3 | 3 | import type { CommandContext } from 'grammy' |
4 | | -import { i18n, isMultipleLocales } from '#root/bot/i18n.js' |
5 | | - |
6 | | -function getLanguageCommand(localeCode: string): BotCommand { |
7 | | - return { |
8 | | - command: 'language', |
9 | | - description: i18n.t(localeCode, 'language-command-description'), |
10 | | - } |
11 | | -} |
12 | | - |
13 | | -function getPrivateChatCommands(localeCode: string): BotCommand[] { |
14 | | - return [ |
15 | | - { |
16 | | - command: 'start', |
17 | | - description: i18n.t(localeCode, 'start-command-description'), |
18 | | - }, |
19 | | - ] |
20 | | -} |
21 | | - |
22 | | -function getPrivateChatAdminCommands(localeCode: string): BotCommand[] { |
23 | | - return [ |
24 | | - { |
25 | | - command: 'setcommands', |
26 | | - description: i18n.t(localeCode, 'setcommands-command-description'), |
27 | | - }, |
28 | | - ] |
| 4 | +import { i18n } from '#root/bot/i18n.js' |
| 5 | +import { Command, CommandGroup } from '@grammyjs/commands' |
| 6 | + |
| 7 | +function addCommandLocalizations(command: Command) { |
| 8 | + i18n.locales.forEach((locale) => { |
| 9 | + command.localize( |
| 10 | + locale as LanguageCode, |
| 11 | + command.name, |
| 12 | + i18n.t(locale, `${command.name}.description`), |
| 13 | + ) |
| 14 | + }) |
| 15 | + return command |
29 | 16 | } |
30 | 17 |
|
31 | | -function getGroupChatCommands(_localeCode: string): BotCommand[] { |
32 | | - return [] |
| 18 | +function addCommandToChats(command: Command, chats: number[]) { |
| 19 | + for (const chatId of chats) { |
| 20 | + command.addToScope({ |
| 21 | + type: 'chat', |
| 22 | + chat_id: chatId, |
| 23 | + }) |
| 24 | + } |
33 | 25 | } |
34 | 26 |
|
35 | 27 | export async function setCommandsHandler(ctx: CommandContext<Context>) { |
36 | | - const DEFAULT_LANGUAGE_CODE = 'en' |
| 28 | + const start = new Command('start', i18n.t('en', 'start.description')) |
| 29 | + .addToScope({ type: 'all_private_chats' }) |
| 30 | + addCommandLocalizations(start) |
| 31 | + addCommandToChats(start, ctx.config.botAdmins) |
37 | 32 |
|
38 | | - // set private chat commands |
39 | | - await ctx.api.setMyCommands( |
40 | | - [ |
41 | | - ...getPrivateChatCommands(DEFAULT_LANGUAGE_CODE), |
42 | | - ...(isMultipleLocales ? [getLanguageCommand(DEFAULT_LANGUAGE_CODE)] : []), |
43 | | - ], |
44 | | - { |
45 | | - scope: { |
46 | | - type: 'all_private_chats', |
47 | | - }, |
48 | | - }, |
49 | | - ) |
| 33 | + const language = new Command('language', i18n.t('en', 'language.description')) |
| 34 | + .addToScope({ type: 'all_private_chats' }) |
| 35 | + addCommandLocalizations(language) |
| 36 | + addCommandToChats(language, ctx.config.botAdmins) |
50 | 37 |
|
51 | | - if (isMultipleLocales) { |
52 | | - const requests = i18n.locales.map(code => |
53 | | - ctx.api.setMyCommands( |
54 | | - [ |
55 | | - ...getPrivateChatCommands(code), |
56 | | - ...(isMultipleLocales |
57 | | - ? [getLanguageCommand(DEFAULT_LANGUAGE_CODE)] |
58 | | - : []), |
59 | | - ], |
60 | | - { |
61 | | - language_code: code as LanguageCode, |
62 | | - scope: { |
63 | | - type: 'all_private_chats', |
64 | | - }, |
65 | | - }, |
66 | | - ), |
67 | | - ) |
68 | | - |
69 | | - await Promise.all(requests) |
70 | | - } |
71 | | - |
72 | | - // set group chat commands |
73 | | - await ctx.api.setMyCommands(getGroupChatCommands(DEFAULT_LANGUAGE_CODE), { |
74 | | - scope: { |
75 | | - type: 'all_group_chats', |
76 | | - }, |
77 | | - }) |
78 | | - |
79 | | - if (isMultipleLocales) { |
80 | | - const requests = i18n.locales.map(code => |
81 | | - ctx.api.setMyCommands(getGroupChatCommands(code), { |
82 | | - language_code: code as LanguageCode, |
83 | | - scope: { |
84 | | - type: 'all_group_chats', |
85 | | - }, |
86 | | - }), |
87 | | - ) |
| 38 | + const setcommands = new Command('setcommands', i18n.t('en', 'setcommands.description')) |
| 39 | + addCommandToChats(setcommands, ctx.config.botAdmins) |
88 | 40 |
|
89 | | - await Promise.all(requests) |
90 | | - } |
| 41 | + const commands = new CommandGroup() |
| 42 | + .add(start) |
| 43 | + .add(language) |
| 44 | + .add(setcommands) |
91 | 45 |
|
92 | | - // set private chat commands for owner |
93 | | - await ctx.api.setMyCommands( |
94 | | - [ |
95 | | - ...getPrivateChatCommands(DEFAULT_LANGUAGE_CODE), |
96 | | - ...getPrivateChatAdminCommands(DEFAULT_LANGUAGE_CODE), |
97 | | - ...(isMultipleLocales ? [getLanguageCommand(DEFAULT_LANGUAGE_CODE)] : []), |
98 | | - ], |
99 | | - { |
100 | | - scope: { |
101 | | - type: 'chat', |
102 | | - chat_id: Number(ctx.config.botAdmins), |
103 | | - }, |
104 | | - }, |
105 | | - ) |
| 46 | + await commands.setCommands(ctx) |
106 | 47 |
|
107 | 48 | return ctx.reply(ctx.t('admin-commands-updated')) |
108 | 49 | } |
0 commit comments