UNPKG

@grammyjs/commands

Version:
146 lines (145 loc) 5.96 kB
import { Command, CommandsFlavor } from "./mod.js"; import { Api, BotCommand, BotCommandScope, BotCommandScopeChat, CommandContext, Context, type LanguageCode, Middleware } from "./deps.node.js"; import type { BotCommandX, CommandOptions } from "./types.js"; import { type MaybeArray } from "./utils/array.js"; import { SetBotCommandsOptions } from "./utils/set-bot-commands.js"; import { JaroWinklerOptions } from "./utils/jaro-winkler.js"; /** * Interface for grouping {@link BotCommand}s that might (or not) * be related to each other by scope and/or language. */ export interface SetMyCommandsParams { /** If defined: scope on which the commands will take effect */ scope?: BotCommandScope; /** If defined: Language on which the commands will take effect. * Two letter abbreviation in ISO_639 standard: https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes */ language_code?: LanguageCode; /** Commands that can be each one passed to a SetMyCommands Call */ commands: (BotCommand & { hasHandler?: boolean; })[]; } /** * Interface to represent uncompliance of a command * with the Bot API */ export interface UncompliantCommand { /** Name of the uncompliant command */ name: string; /** Reason why the command was considered uncompliant */ reasons: string[]; /** Language in which the command is uncompliant */ language: LanguageCode | "default"; } /** * Central class that manages all registered commands. * This is the starting point for the plugin, and this is what you should pass to `bot.use` so your commands get properly registered. * * @example * ```typescript * const myCommands = new CommandGroup() * commands.command("start", "start the bot configuration", (ctx) => ctx.reply("Hello there!")) * * // Registers the commands with the bot instance. * bot.use(myCommands) * ``` */ export declare class CommandGroup<C extends Context> { private _languages; private _scopes; private _commands; private _cachedComposer; private _cachedComposerInvalidated; private _commandOptions; constructor(options?: Partial<CommandOptions>); private _addCommandToScope; private _populateMetadata; /** * Registers a new command with a default handler. * @param name Default command name * @param description Default command description * @param handler Default command handler * @param options Extra options that should apply only to this command * @returns An instance of the `Command` class */ command(name: string | RegExp, description: string, handler: MaybeArray<Middleware<CommandContext<C>>>, options?: Partial<CommandOptions>): Command<C>; /** * Registers a new command with no handlers. * @param name Default command name * @param description Default command description * @param options Extra options that should apply only to this command * @returns An instance of the `Command` class */ command(name: string | RegExp, description: string, options?: Partial<CommandOptions>): Command<C>; /** * Registers a Command that was created by it's own. * * @param command the command or list of commands being added to the group */ add(command: Command<C> | Command<C>[]): this; /** * Serializes the commands into multiple objects that can each be passed to a `setMyCommands` call. * * @returns One item for each combination of command + scope + language */ toArgs(chat_id?: BotCommandScopeChat["chat_id"]): { scopes: SetMyCommandsParams[]; uncompliantCommands: UncompliantCommand[]; }; /** * Serializes the commands of a single scope into objects that can each be passed to a `setMyCommands` call. * * @param scope Selected scope to be serialized * @returns One item per command per language */ toSingleScopeArgs(scope: BotCommandScope): { commandParams: SetMyCommandsParams[]; uncompliantCommands: UncompliantCommand[]; }; /** * Registers all commands to be displayed by clients according to their scopes and languages * Calls `setMyCommands` for each language of each scope of each command. * * [!IMPORTANT] * Calling this method with upperCased command names registered, will throw * @see https://core.telegram.org/bots/api#botcommand * @see https://core.telegram.org/method/bots.setBotCommands * * @param Instance of `bot` or { api: bot.api } */ setCommands({ api }: { api: Api; }, options?: Partial<SetBotCommandsOptions>): Promise<void>; /** * Serialize all register commands into a more detailed object * including it's name, prefix and language, and more data * * @param filterLanguage if undefined, it returns all names * else get only the locales for the given filterLanguage * fallbacks to "default" * * @returns an array of {@link BotCommandX} * * Note: mainly used to serialize for {@link FuzzyMatch} */ toElementals(filterLanguage?: LanguageCode | "default"): BotCommandX[]; /** * @returns A JSON serialized version of all the currently registered commands */ toString(): string; middleware(): import("grammy").MiddlewareFn<C>; /** * @returns all {@link Command}s contained in the instance */ get commands(): Command<C>[]; /** * @returns all prefixes registered in this instance */ get prefixes(): string[]; } type HaveCommandLike<C extends Context = Context, CF extends CommandsFlavor<C> = CommandsFlavor<C>> = C & CF & { commandSuggestion: string | null; }; export declare function commandNotFound<CF extends CommandsFlavor<C>, C extends Context = Context>(commands: CommandGroup<C> | CommandGroup<C>[], opts?: Omit<Partial<JaroWinklerOptions>, "language">): (ctx: C) => ctx is HaveCommandLike<C, CF>; export {};