@grammyjs/commands 0.5.1 → 0.6.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/commands.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Command } from "./command.js";
2
- import { Api, BotCommand, BotCommandScope, Context } from "./deps.node.js";
1
+ import { Command, MaybeArray } from "./command.js";
2
+ import { Api, BotCommand, BotCommandScope, Context, Middleware } from "./deps.node.js";
3
3
  import { CommandOptions } from "./types.js";
4
4
  type SetMyCommandsParams = {
5
5
  /**
@@ -33,8 +33,17 @@ export declare class Commands<C extends Context> {
33
33
  private _populateComposer;
34
34
  private _populateMetadata;
35
35
  /**
36
- * Registers a new command and returns it.
37
- * @param name Command name
36
+ * Registers a new command with a default handler.
37
+ * @param name Default command name
38
+ * @param description Default command description
39
+ * @param handler Default command handler
40
+ * @param options Extra options that should apply only to this command
41
+ * @returns An instance of the `Command` class
42
+ */
43
+ command(name: string | RegExp, description: string, handler: MaybeArray<Middleware<C>>, options?: Partial<CommandOptions>): Command<C>;
44
+ /**
45
+ * Registers a new command with no handlers.
46
+ * @param name Default command name
38
47
  * @param description Default command description
39
48
  * @param options Extra options that should apply only to this command
40
49
  * @returns An instance of the `Command` class
package/out/commands.js CHANGED
@@ -3,6 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Commands = void 0;
4
4
  const command_js_1 = require("./command.js");
5
5
  const deps_node_js_1 = require("./deps.node.js");
6
+ const isMiddleware = (obj) => {
7
+ if (!obj)
8
+ return false;
9
+ if (Array.isArray(obj))
10
+ return obj.every(isMiddleware);
11
+ const objType = typeof obj;
12
+ switch (objType) {
13
+ case "function":
14
+ return true;
15
+ case "object":
16
+ return Object.keys(obj).includes("middleware");
17
+ }
18
+ return false;
19
+ };
6
20
  /**
7
21
  * Central class that manages all registered commands.
8
22
  * This is the starting point for the plugin, and this is what you should pass to `bot.use` so your commands get properly registered.
@@ -47,15 +61,17 @@ class Commands {
47
61
  }
48
62
  });
49
63
  }
50
- /**
51
- * Registers a new command and returns it.
52
- * @param name Command name
53
- * @param description Default command description
54
- * @param options Extra options that should apply only to this command
55
- * @returns An instance of the `Command` class
56
- */
57
- command(name, description, options = this._commandOptions) {
64
+ command(name, description, handlerOrOptions, _options) {
65
+ var _a;
66
+ const handler = isMiddleware(handlerOrOptions)
67
+ ? handlerOrOptions
68
+ : undefined;
69
+ const options = handler
70
+ ? _options !== null && _options !== void 0 ? _options : this._commandOptions
71
+ : (_a = handlerOrOptions) !== null && _a !== void 0 ? _a : this._commandOptions;
58
72
  const command = new command_js_1.Command(name, description, options);
73
+ if (handler)
74
+ command.addToScope({ type: "default" }, handler);
59
75
  this._commands.push(command);
60
76
  return command;
61
77
  }
package/out/mod.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { Command } from "./command.js";
1
2
  export * from "./commands.js";
2
3
  export * from "./context.js";
3
4
  export type { CommandOptions } from "./types.js";
package/out/mod.js CHANGED
@@ -14,5 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Command = void 0;
18
+ var command_js_1 = require("./command.js");
19
+ Object.defineProperty(exports, "Command", { enumerable: true, get: function () { return command_js_1.Command; } });
17
20
  __exportStar(require("./commands.js"), exports);
18
21
  __exportStar(require("./context.js"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grammyjs/commands",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "grammY Commands Plugin",
5
5
  "main": "out/mod.js",
6
6
  "scripts": {