@grammyjs/commands 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/command.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare class Command<C extends Context = Context> implements MiddlewareO
17
17
  *
18
18
  * @param name Default command name
19
19
  * @param description Default command description
20
- * @param options Options object that shuold apply to this command only
20
+ * @param options Options object that should apply to this command only
21
21
  * @access package
22
22
  */
23
23
  constructor(name: string | RegExp, description: string, options?: Partial<CommandOptions>);
@@ -76,7 +76,7 @@ export declare class Command<C extends Context = Context> implements MiddlewareO
76
76
  * ```ts
77
77
  * bot
78
78
  * .filter(
79
- * Command.hasCommand(/\/delete_(.*)/),
79
+ * Command.hasCommand(/delete_(.*)/),
80
80
  * (ctx) => ctx.reply(`Deleting ${ctx.message?.text?.split("_")[1]}`)
81
81
  * )
82
82
  * ```
@@ -96,7 +96,7 @@ export declare class Command<C extends Context = Context> implements MiddlewareO
96
96
  * .localize("pt", "iniciar", "Inicia a configuração do bot")
97
97
  * ```
98
98
  *
99
- * @param languageCode Language this translation applies to
99
+ * @param languageCode Language this translation applies to. @see https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
100
100
  * @param name Localized command name
101
101
  * @param description Localized command description
102
102
  */
package/out/command.js CHANGED
@@ -19,7 +19,7 @@ class Command {
19
19
  *
20
20
  * @param name Default command name
21
21
  * @param description Default command description
22
- * @param options Options object that shuold apply to this command only
22
+ * @param options Options object that should apply to this command only
23
23
  * @access package
24
24
  */
25
25
  constructor(name, description, options = {}) {
@@ -125,7 +125,7 @@ class Command {
125
125
  * ```ts
126
126
  * bot
127
127
  * .filter(
128
- * Command.hasCommand(/\/delete_(.*)/),
128
+ * Command.hasCommand(/delete_(.*)/),
129
129
  * (ctx) => ctx.reply(`Deleting ${ctx.message?.text?.split("_")[1]}`)
130
130
  * )
131
131
  * ```
@@ -171,13 +171,13 @@ class Command {
171
171
  * .localize("pt", "iniciar", "Inicia a configuração do bot")
172
172
  * ```
173
173
  *
174
- * @param languageCode Language this translation applies to
174
+ * @param languageCode Language this translation applies to. @see https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
175
175
  * @param name Localized command name
176
176
  * @param description Localized command description
177
177
  */
178
178
  localize(languageCode, name, description) {
179
179
  this._languages.set(languageCode, {
180
- name: new RegExp(name),
180
+ name: name,
181
181
  description,
182
182
  });
183
183
  return this;
@@ -210,7 +210,9 @@ class Command {
210
210
  toObject(languageCode = "default") {
211
211
  const localizedName = this.getLocalizedName(languageCode);
212
212
  return {
213
- command: localizedName instanceof RegExp ? "" : localizedName,
213
+ command: localizedName instanceof RegExp
214
+ ? localizedName.source
215
+ : localizedName,
214
216
  description: this.getLocalizedDescription(languageCode),
215
217
  };
216
218
  }
package/out/commands.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Command, MaybeArray } from "./command.js";
2
- import { Api, BotCommand, BotCommandScope, Context, Middleware } from "./deps.node.js";
2
+ import { Api, BotCommand, BotCommandScope, CommandContext, Context, Middleware } from "./deps.node.js";
3
3
  import { CommandOptions } from "./types.js";
4
- type SetMyCommandsParams = {
4
+ export type SetMyCommandsParams = {
5
5
  /**
6
6
  * Scope
7
+ * @param language_code two letter abbreviation in ISO_639 standard: https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes
7
8
  */
8
9
  scope?: BotCommandScope;
9
10
  language_code?: string;
@@ -40,7 +41,7 @@ export declare class Commands<C extends Context> {
40
41
  * @param options Extra options that should apply only to this command
41
42
  * @returns An instance of the `Command` class
42
43
  */
43
- command(name: string | RegExp, description: string, handler: MaybeArray<Middleware<C>>, options?: Partial<CommandOptions>): Command<C>;
44
+ command(name: string | RegExp, description: string, handler: MaybeArray<Middleware<CommandContext<C>>>, options?: Partial<CommandOptions>): Command<C>;
44
45
  /**
45
46
  * Registers a new command with no handlers.
46
47
  * @param name Default command name
@@ -51,7 +52,6 @@ export declare class Commands<C extends Context> {
51
52
  command(name: string | RegExp, description: string, options?: Partial<CommandOptions>): Command<C>;
52
53
  /**
53
54
  * Serializes the commands into multiple objects that can each be passed to a `setMyCommands` call.
54
- *
55
55
  * @returns One item for each combination of command + scope + language
56
56
  */
57
57
  toArgs(): SetMyCommandsParams[];
@@ -81,4 +81,3 @@ export declare class Commands<C extends Context> {
81
81
  toString(): string;
82
82
  middleware(): import("grammy").MiddlewareFn<C>;
83
83
  }
84
- export {};
package/out/commands.js CHANGED
@@ -77,7 +77,6 @@ class Commands {
77
77
  }
78
78
  /**
79
79
  * Serializes the commands into multiple objects that can each be passed to a `setMyCommands` call.
80
- *
81
80
  * @returns One item for each combination of command + scope + language
82
81
  */
83
82
  toArgs() {
@@ -112,6 +111,7 @@ class Commands {
112
111
  language_code: language === "default" ? undefined : language,
113
112
  commands: this._commands
114
113
  .filter((command) => command.scopes.length)
114
+ .filter((command) => typeof command.name === "string")
115
115
  .map((command) => command.toObject(language)),
116
116
  });
117
117
  }
package/out/context.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Commands } from "./commands.js";
2
2
  import { Context, NextFunction } from "./deps.node.js";
3
3
  import { JaroWinklerOptions } from "./jaro-winkler.js";
4
+ import { SetMyCommandsParams } from "./mod.js";
4
5
  export interface CommandsFlavor<C extends Context = Context> extends Context {
5
6
  /**
6
7
  * Sets the provided commands for the current chat.
@@ -9,7 +10,7 @@ export interface CommandsFlavor<C extends Context = Context> extends Context {
9
10
  * @param commands List of available commands
10
11
  * @returns Promise with the result of the operations
11
12
  */
12
- setMyCommands: (commands: Commands<C>) => Promise<void>;
13
+ setMyCommands: (commands: Commands<C>, ...moreCommands: Commands<C>[]) => Promise<void>;
13
14
  /**
14
15
  * Returns the nearest command to the user input.
15
16
  * If no command is found, returns `null`.
@@ -24,3 +25,11 @@ export interface CommandsFlavor<C extends Context = Context> extends Context {
24
25
  * Installs the commands flavor into the context.
25
26
  */
26
27
  export declare function commands<C extends Context>(): (ctx: CommandsFlavor<C>, next: NextFunction) => Promise<void>;
28
+ /**
29
+ * Iterates over an array of commands params, merging commands when two commandsParams
30
+ * are from the same language.
31
+ *
32
+ * @param commandParams an array of commands params coming from multiple Commands instances
33
+ * @returns an array containing all commands to be set on ctx
34
+ */
35
+ export declare function _mergeMyCommandsParams(commandParams: SetMyCommandsParams[][]): SetMyCommandsParams[];
package/out/context.js CHANGED
@@ -1,18 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.commands = void 0;
3
+ exports._mergeMyCommandsParams = exports.commands = void 0;
4
4
  const jaro_winkler_js_1 = require("./jaro-winkler.js");
5
5
  /**
6
6
  * Installs the commands flavor into the context.
7
7
  */
8
8
  function commands() {
9
9
  return (ctx, next) => {
10
- ctx.setMyCommands = async (commands) => {
10
+ ctx.setMyCommands = async (commands, ...moreCommands) => {
11
11
  if (!ctx.chat) {
12
12
  throw new Error("cannot call `ctx.setMyCommands` on an update with no `chat` property");
13
13
  }
14
- await Promise.all(commands
15
- .toSingleScopeArgs({ type: "chat", chat_id: ctx.chat.id })
14
+ const commandsParams = [commands].concat(moreCommands).map((commands) => commands.toSingleScopeArgs({
15
+ type: "chat",
16
+ chat_id: ctx.chat.id,
17
+ }));
18
+ const mergedCommands = _mergeMyCommandsParams(commandsParams);
19
+ await Promise.all(mergedCommands
16
20
  .map((args) => ctx.api.raw.setMyCommands(args)));
17
21
  };
18
22
  ctx.getNearestCommand = (commands, options) => {
@@ -27,3 +31,36 @@ function commands() {
27
31
  };
28
32
  }
29
33
  exports.commands = commands;
34
+ /**
35
+ * Iterates over an array of commands params, merging commands when two commandsParams
36
+ * are from the same language.
37
+ *
38
+ * @param commandParams an array of commands params coming from multiple Commands instances
39
+ * @returns an array containing all commands to be set on ctx
40
+ */
41
+ function _mergeMyCommandsParams(commandParams) {
42
+ if (!commandParams.flat().length)
43
+ return [];
44
+ return commandParams
45
+ .flat()
46
+ .sort((a, b) => {
47
+ if (!a.language_code)
48
+ return -1;
49
+ if (!b.language_code)
50
+ return 1;
51
+ return a.language_code.localeCompare(b.language_code);
52
+ })
53
+ .reduce((result, current, i, arr) => {
54
+ if (i === 0 || current.language_code !== arr[i - 1].language_code) {
55
+ result.push(current);
56
+ return result;
57
+ }
58
+ else {
59
+ result[result.length - 1].commands = result[result.length - 1]
60
+ .commands
61
+ .concat(current.commands);
62
+ return result;
63
+ }
64
+ }, []);
65
+ }
66
+ exports._mergeMyCommandsParams = _mergeMyCommandsParams;
@@ -1,2 +1,2 @@
1
- export { Api, Bot, type ChatTypeContext, type ChatTypeMiddleware, type CommandMiddleware, Composer, Context, type Middleware, type MiddlewareObj, type NextFunction, } from "grammy";
1
+ export { Api, Bot, type ChatTypeContext, type ChatTypeMiddleware, type CommandContext, type CommandMiddleware, Composer, Context, type Middleware, type MiddlewareObj, type NextFunction, } from "grammy";
2
2
  export type { BotCommand, BotCommandScope, BotCommandScopeAllChatAdministrators, BotCommandScopeAllGroupChats, BotCommandScopeAllPrivateChats, Chat, } from "grammy/types";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grammyjs/commands",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "grammY Commands Plugin",
5
5
  "main": "out/mod.js",
6
6
  "scripts": {