@grammyjs/commands
Version:
grammY Commands Plugin
124 lines (123 loc) • 5.42 kB
TypeScript
import { CommandGroup } from "./command-group.js";
import { CommandMatch } from "./command.js";
import { BotCommandScopeChat, Context, NextFunction } from "./deps.node.js";
import { SetMyCommandsParams } from "./mod.js";
import { BotCommandEntity } from "./types.js";
import { JaroWinklerOptions } from "./utils/jaro-winkler.js";
import { SetBotCommandsOptions } from "./utils/set-bot-commands.js";
export interface CommandsFlavor<C extends Context = Context> extends Context {
/**
* Sets the provided commands for the current chat.
* Cannot be called on updates that don't have a `chat` property.
*
* [!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
*
* @example
* ```typescript
* bot.hears("sudo", (ctx) =>
* ctx.setMyCommands(userCommands, adminCommands));
* bot.hears("logout", (ctx) =>
* ctx.setMyCommands(userCommands));
* bot.hears("example", (ctx) =>
* ctx.setMyCommands([aCommands, bCommands, cCommands]));
* ```
*
* @param commands List of available commands
* @returns Promise with the result of the operations
*/
setMyCommands: (commands: CommandGroup<C> | CommandGroup<C>[], options?: SetBotCommandsOptions) => Promise<void>;
/**
* Returns the nearest command to the user input.
* If no command is found, returns `null`.
*
* @param commands List of available commands
* @param options Options for the Jaro-Winkler algorithm
* @returns The nearest command or `null`
*/
getNearestCommand: (commands: CommandGroup<C> | CommandGroup<C>[], options?: Omit<Partial<JaroWinklerOptions>, "language">) => string | null;
/**
* @param commands
* @returns command entities hydrated with the custom prefixes
*/
getCommandEntities: (commands: CommandGroup<C> | CommandGroup<C>[]) => BotCommandEntity[];
/**
* The matched command and the rest of the input.
*
* When matched command is a RegExp, a `match` property exposes the result of the RegExp match.
*/
commandMatch: CommandMatch;
}
/**
* Installs the commands flavor into the context.
*/
export declare function commands<C extends Context>(): (ctx: CommandsFlavor<C>, next: NextFunction) => Promise<void>;
/**
* Static class for getting and manipulating {@link SetMyCommandsParams}.
* The main function is {@link from}
*/
export declare class MyCommandParams {
/**
* Merges and serialize one or more Commands instances into a single array
* of commands params that can be used to set the commands menu displayed to the user.
* @example
```ts
const adminCommands = new CommandGroup();
const userCommands = new CommandGroup();
adminCommands
.command("do a",
"a description",
(ctx) => ctx.doA());
userCommands
.command("do b",
"b description",
(ctx) => ctx.doB());
const mergedParams =
MyCommandParams.from([a, b], someChatId);
```
* @param commands An array of one or more Commands instances.
* @returns an array of {@link SetMyCommandsParams} grouped by language
*/
static from<C extends Context>(commands: CommandGroup<C>[], chat_id: BotCommandScopeChat["chat_id"]): {
commandsParams: SetMyCommandsParams[];
uncompliantCommands: import("./command-group.js").UncompliantCommand[];
};
/**
* Serializes one or multiple {@link CommandGroup} instances, each one into their respective
* single scoped SetMyCommandsParams version.
* @example
```ts
const adminCommands = new CommandGroup();
// add to scope, localize, etc
const userCommands = new CommandGroup();
// add to scope, localize, etc
const [
singleScopedAdminParams,
singleScopedUserParams
] = MyCommandsParams.serialize([adminCommands,userCommands])
```
* @param commandsArr an array of one or more commands instances
* @param chat_id the chat id relative to the message update, coming from the ctx object.
* @returns an array of scoped {@link SetMyCommandsParams} mapped from their respective Commands instances
*/
static _serialize<C extends Context>(commandsArr: CommandGroup<C>[], chat_id: BotCommandScopeChat["chat_id"]): {
commandParams: SetMyCommandsParams[];
uncompliantCommands: import("./command-group.js").UncompliantCommand[];
}[];
/**
* Lexicographically sorts commandParams based on their language code.
* @returns the sorted array
*/
static _sortByLanguage(params: SetMyCommandsParams[]): SetMyCommandsParams[];
/**
* Iterates over an array of CommandsParams
* merging their respective {@link SetMyCommandsParams.commands}
* when they are from the same language, separating when they are not.
*
* @param params a flattened array of commands params coming from one or more Commands instances
* @returns an array containing all commands grouped by language
*/
private static mergeByLanguage;
}