Skip to content

Commit 480432f

Browse files
committed
Use @grammyjs/commands to set bot commands
1 parent 6a83982 commit 480432f

File tree

4 files changed

+69
-101
lines changed

4 files changed

+69
-101
lines changed

locales/en.ftl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
## Commands
22

3-
start-command-description = Start the bot
4-
language-command-description = Change language
5-
setcommands-command-description = Set bot commands
3+
start =
4+
.description = Start the bot
5+
language =
6+
.description = Change language
7+
setcommands =
8+
.description = Set bot commands
69
710
## Welcome Feature
811

package-lock.json

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"dependencies": {
2727
"@grammyjs/auto-chat-action": "0.1.1",
28+
"@grammyjs/commands": "1.0.5",
2829
"@grammyjs/hydrate": "1.4.1",
2930
"@grammyjs/i18n": "1.1.2",
3031
"@grammyjs/parse-mode": "1.11.1",
Lines changed: 35 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,49 @@
11
import type { Context } from '#root/bot/context.js'
2-
import type { BotCommand, LanguageCode } from '@grammyjs/types'
2+
import type { LanguageCode } from '@grammyjs/types'
33
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
2916
}
3017

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+
}
3325
}
3426

3527
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)
3732

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)
5037

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)
8840

89-
await Promise.all(requests)
90-
}
41+
const commands = new CommandGroup()
42+
.add(start)
43+
.add(language)
44+
.add(setcommands)
9145

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)
10647

10748
return ctx.reply(ctx.t('admin-commands-updated'))
10849
}

0 commit comments

Comments
 (0)