mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 22:24:02 +00:00
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { getBotTranslations, type BotLocale } from './i18n'
|
|
import type { TelegramCommandName } from './i18n/types'
|
|
|
|
export interface TelegramCommandDefinition {
|
|
command: TelegramCommandName
|
|
description: string
|
|
}
|
|
|
|
export interface ScopedTelegramCommands {
|
|
scope: 'default' | 'all_private_chats' | 'all_group_chats' | 'all_chat_administrators'
|
|
commands: readonly TelegramCommandDefinition[]
|
|
}
|
|
|
|
const DEFAULT_COMMAND_NAMES = [
|
|
'help',
|
|
'household_status'
|
|
] as const satisfies readonly TelegramCommandName[]
|
|
const PRIVATE_CHAT_COMMAND_NAMES = [
|
|
...DEFAULT_COMMAND_NAMES,
|
|
'anon',
|
|
'cancel'
|
|
] as const satisfies readonly TelegramCommandName[]
|
|
const GROUP_CHAT_COMMAND_NAMES = DEFAULT_COMMAND_NAMES
|
|
const GROUP_ADMIN_COMMAND_NAMES = [
|
|
...GROUP_CHAT_COMMAND_NAMES,
|
|
'setup',
|
|
'bind_purchase_topic',
|
|
'bind_feedback_topic',
|
|
'pending_members',
|
|
'approve_member'
|
|
] as const satisfies readonly TelegramCommandName[]
|
|
|
|
function mapCommands(
|
|
locale: BotLocale,
|
|
names: readonly TelegramCommandName[]
|
|
): readonly TelegramCommandDefinition[] {
|
|
const descriptions = getBotTranslations(locale).commands
|
|
|
|
return names.map((command) => ({
|
|
command,
|
|
description: descriptions[command]
|
|
}))
|
|
}
|
|
|
|
export function getTelegramCommandScopes(locale: BotLocale): readonly ScopedTelegramCommands[] {
|
|
return [
|
|
{
|
|
scope: 'default',
|
|
commands: mapCommands(locale, DEFAULT_COMMAND_NAMES)
|
|
},
|
|
{
|
|
scope: 'all_private_chats',
|
|
commands: mapCommands(locale, PRIVATE_CHAT_COMMAND_NAMES)
|
|
},
|
|
{
|
|
scope: 'all_group_chats',
|
|
commands: mapCommands(locale, GROUP_CHAT_COMMAND_NAMES)
|
|
},
|
|
{
|
|
scope: 'all_chat_administrators',
|
|
commands: mapCommands(locale, GROUP_ADMIN_COMMAND_NAMES)
|
|
}
|
|
]
|
|
}
|
|
|
|
export function formatTelegramHelpText(locale: BotLocale): string {
|
|
const t = getBotTranslations(locale)
|
|
const defaultCommands = new Set<TelegramCommandName>(DEFAULT_COMMAND_NAMES)
|
|
const privateCommands = mapCommands(locale, PRIVATE_CHAT_COMMAND_NAMES)
|
|
const adminCommands = mapCommands(locale, GROUP_ADMIN_COMMAND_NAMES).filter(
|
|
(command) => !defaultCommands.has(command.command)
|
|
)
|
|
|
|
return [
|
|
t.help.intro,
|
|
t.help.privateChatHeading,
|
|
...privateCommands.map((command) => `/${command.command} - ${command.description}`),
|
|
t.help.groupAdminsHeading,
|
|
...adminCommands.map((command) => `/${command.command} - ${command.description}`)
|
|
].join('\n')
|
|
}
|