@grammyjs/commands 0.9.0 → 0.11.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.
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchesPattern = exports.isCommandOptions = exports.isMiddleware = exports.isAdmin = void 0;
4
+ function isAdmin(ctx) {
5
+ return ctx
6
+ .getAuthor()
7
+ .then((author) => ["administrator", "creator"].includes(author.status));
8
+ }
9
+ exports.isAdmin = isAdmin;
10
+ function isMiddleware(obj) {
11
+ if (!obj)
12
+ return false;
13
+ if (Array.isArray(obj))
14
+ return obj.every(isMiddleware);
15
+ const objType = typeof obj;
16
+ switch (objType) {
17
+ case "function":
18
+ return true;
19
+ case "object":
20
+ return Object.keys(obj).includes("middleware");
21
+ }
22
+ return false;
23
+ }
24
+ exports.isMiddleware = isMiddleware;
25
+ function isCommandOptions(obj) {
26
+ if (typeof obj !== "object" || !obj)
27
+ return false;
28
+ const { prefix, matchOnlyAtStart, targetedCommands, ignoreCase } = obj;
29
+ if (typeof prefix === "string")
30
+ return true;
31
+ if (typeof matchOnlyAtStart === "boolean")
32
+ return true;
33
+ if (targetedCommands &&
34
+ ["ignored", "optional", "required"].includes(targetedCommands))
35
+ return true;
36
+ if (typeof ignoreCase === "boolean")
37
+ return true;
38
+ return false;
39
+ }
40
+ exports.isCommandOptions = isCommandOptions;
41
+ function matchesPattern(value, pattern, ignoreCase = false) {
42
+ const transformedValue = ignoreCase ? value.toLowerCase() : value;
43
+ const transformedPattern = pattern instanceof RegExp && ignoreCase && !pattern.flags.includes("i")
44
+ ? new RegExp(pattern, pattern.flags + "i")
45
+ : pattern;
46
+ return typeof transformedPattern === "string"
47
+ ? transformedValue === transformedPattern
48
+ : transformedPattern.test(transformedValue);
49
+ }
50
+ exports.matchesPattern = matchesPattern;
@@ -0,0 +1,12 @@
1
+ import { UncompliantCommand } from "../command-group.js";
2
+ import { BotCommandScope } from "../deps.node.js";
3
+ export declare class InvalidScopeError extends Error {
4
+ constructor(scope: BotCommandScope);
5
+ }
6
+ export declare class CustomPrefixNotSupportedError extends Error {
7
+ readonly offendingCommands: string[];
8
+ constructor(message: string, offendingCommands: string[]);
9
+ }
10
+ export declare class UncompliantCommandsError extends Error {
11
+ constructor(commands: Array<UncompliantCommand>);
12
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UncompliantCommandsError = exports.CustomPrefixNotSupportedError = exports.InvalidScopeError = void 0;
4
+ class InvalidScopeError extends Error {
5
+ constructor(scope) {
6
+ super(`Invalid scope: ${scope}`);
7
+ this.name = "InvalidScopeError";
8
+ }
9
+ }
10
+ exports.InvalidScopeError = InvalidScopeError;
11
+ class CustomPrefixNotSupportedError extends Error {
12
+ constructor(message, offendingCommands) {
13
+ super(message);
14
+ this.offendingCommands = offendingCommands;
15
+ this.name = "CustomPrefixNotSupportedError";
16
+ }
17
+ }
18
+ exports.CustomPrefixNotSupportedError = CustomPrefixNotSupportedError;
19
+ class UncompliantCommandsError extends Error {
20
+ constructor(commands) {
21
+ const message = [
22
+ `Tried to set bot commands with one or more commands that do not comply with the Bot API requirements for command names. Offending command(s):`,
23
+ commands.map(({ name, reasons, language }) => `- (language: ${language}) ${name}: ${reasons.join(", ")}`)
24
+ .join("\n"),
25
+ "If you want to filter these commands out automatically, set `ignoreUncompliantCommands` to `true`",
26
+ ].join("\n");
27
+ super(message);
28
+ }
29
+ }
30
+ exports.UncompliantCommandsError = UncompliantCommandsError;
@@ -1,6 +1,6 @@
1
- import { Commands } from "./commands.js";
2
- import { Context, LanguageCode } from "./deps.node.js";
3
- import type { CommandElementals } from "./types.js";
1
+ import { CommandGroup } from "../command-group.js";
2
+ import { Context, LanguageCode } from "../deps.node.js";
3
+ import type { CommandElementals } from "../types.js";
4
4
  export declare function distance(s1: string, s2: string): number;
5
5
  export type JaroWinklerOptions = {
6
6
  ignoreCase?: boolean;
@@ -14,5 +14,5 @@ type CommandSimilarity = {
14
14
  };
15
15
  export declare function JaroWinklerDistance(s1: string, s2: string, options: Pick<Partial<JaroWinklerOptions>, "ignoreCase">): number;
16
16
  export declare function isLanguageCode(value: string | undefined): value is LanguageCode;
17
- export declare function fuzzyMatch<C extends Context>(userInput: string, commands: Commands<C>, options: Partial<JaroWinklerOptions>): CommandSimilarity | null;
17
+ export declare function fuzzyMatch<C extends Context>(userInput: string, commands: CommandGroup<C>, options: Partial<JaroWinklerOptions>): CommandSimilarity | null;
18
18
  export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fuzzyMatch = exports.isLanguageCode = exports.JaroWinklerDistance = exports.distance = void 0;
4
- const deps_node_js_1 = require("./deps.node.js");
4
+ const deps_node_js_1 = require("../deps.node.js");
5
5
  function distance(s1, s2) {
6
6
  if (s1.length === 0 || s2.length === 0) {
7
7
  return 0;
@@ -102,9 +102,7 @@ function fuzzyMatch(userInput, commands, options) {
102
102
  * https://en.wikipedia.org/wiki/IETF_language_tag
103
103
  */
104
104
  const possiblyISO639 = (_a = options.language) === null || _a === void 0 ? void 0 : _a.split("-")[0];
105
- const language = isLanguageCode(possiblyISO639)
106
- ? possiblyISO639
107
- : undefined;
105
+ const language = isLanguageCode(possiblyISO639) ? possiblyISO639 : undefined;
108
106
  const cmds = options.ignoreLocalization
109
107
  ? commands.toElementals()
110
108
  : commands.toElementals(language);
@@ -112,9 +110,7 @@ function fuzzyMatch(userInput, commands, options) {
112
110
  const similarity = JaroWinklerDistance(userInput, command.name, {
113
111
  ...options,
114
112
  });
115
- return similarity > best.similarity
116
- ? { command, similarity }
117
- : best;
113
+ return similarity > best.similarity ? { command, similarity } : best;
118
114
  }, { command: null, similarity: 0 });
119
115
  return bestMatch.similarity > similarityThreshold ? bestMatch : null;
120
116
  }
@@ -0,0 +1,24 @@
1
+ import { SetMyCommandsParams, UncompliantCommand } from "../command-group.js";
2
+ import { Api } from "../deps.node.js";
3
+ /**
4
+ * Options for the `setBotCommands` function.
5
+ */
6
+ export interface SetBotCommandsOptions {
7
+ /**
8
+ * Whether to remove invalid commands from the list of calls to the Bot API.
9
+ *
10
+ * If set to `false`, the method will throw an error if any of the commands
11
+ * is invalid according to the {@link https://core.telegram.org/bots/api#botcommand|official Bot API documentation}.
12
+ *
13
+ * Defaults to `false`.
14
+ */
15
+ ignoreUncompliantCommands?: boolean;
16
+ }
17
+ /**
18
+ * Performs validation and sets the provided commands for the bot.
19
+ * @param api Instance of the Api class for the bot the commands are being set for.
20
+ * @param commandParams List of commands to set.
21
+ * @param uncompliantCommands List of commands that do not comply with the Bot API rules.
22
+ * @param options Options object`
23
+ */
24
+ export declare function setBotCommands(api: Api, commandParams: SetMyCommandsParams[], uncompliantCommands: UncompliantCommand[], options?: Partial<SetBotCommandsOptions>): Promise<void>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setBotCommands = void 0;
4
+ const errors_js_1 = require("./errors.js");
5
+ /**
6
+ * Performs validation and sets the provided commands for the bot.
7
+ * @param api Instance of the Api class for the bot the commands are being set for.
8
+ * @param commandParams List of commands to set.
9
+ * @param uncompliantCommands List of commands that do not comply with the Bot API rules.
10
+ * @param options Options object`
11
+ */
12
+ async function setBotCommands(api, commandParams, uncompliantCommands, options) {
13
+ const { ignoreUncompliantCommands = false } = options !== null && options !== void 0 ? options : {};
14
+ if (uncompliantCommands.length && !ignoreUncompliantCommands) {
15
+ throw new errors_js_1.UncompliantCommandsError(uncompliantCommands);
16
+ }
17
+ await Promise.all(commandParams.map((args) => api.raw.setMyCommands(args)));
18
+ }
19
+ exports.setBotCommands = setBotCommands;
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
1
  {
2
- "name": "@grammyjs/commands",
3
- "version": "0.9.0",
4
- "description": "grammY Commands Plugin",
5
- "main": "out/mod.js",
6
- "scripts": {
7
- "backport": "deno task backport",
8
- "prepare": "deno task backport"
9
- },
10
- "keywords": [
11
- "grammY",
12
- "telegram",
13
- "bot",
14
- "commands"
15
- ],
16
- "author": "Roz <roz@rjmunhoz.me>",
17
- "license": "MIT",
18
- "dependencies": {
19
- "@grammyjs/types": "^3.8.1",
20
- "grammy": "^1.17.1",
21
- "ts-pattern": "^5.0.1"
22
- },
23
- "devDependencies": {
24
- "deno-bin": "^1.45.2",
25
- "typescript": "^5.1.6"
26
- },
27
- "files": [
28
- "out"
29
- ]
2
+ "name": "@grammyjs/commands",
3
+ "version": "0.11.0",
4
+ "description": "grammY Commands Plugin",
5
+ "main": "out/mod.js",
6
+ "scripts": {
7
+ "backport": "deno task backport",
8
+ "prepare": "deno task backport"
9
+ },
10
+ "keywords": [
11
+ "grammY",
12
+ "telegram",
13
+ "bot",
14
+ "commands"
15
+ ],
16
+ "author": "Roz <roz@rjmunhoz.me>",
17
+ "license": "MIT",
18
+ "dependencies": {
19
+ "@grammyjs/types": "^3.8.1",
20
+ "grammy": "^1.17.1",
21
+ "ts-pattern": "^5.0.1"
22
+ },
23
+ "devDependencies": {
24
+ "deno-bin": "^1.45.2",
25
+ "typescript": "^5.1.6"
26
+ },
27
+ "files": [
28
+ "out"
29
+ ]
30
30
  }
package/out/errors.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import { BotCommandScope } from "./deps.node.js";
2
- export declare class InvalidScopeError extends Error {
3
- constructor(scope: BotCommandScope);
4
- }
package/out/errors.js DELETED
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidScopeError = void 0;
4
- class InvalidScopeError extends Error {
5
- constructor(scope) {
6
- super(`Invalid scope: ${scope}`);
7
- this.name = "InvalidScopeError";
8
- }
9
- }
10
- exports.InvalidScopeError = InvalidScopeError;
package/out/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export type MaybeArray<T> = T | T[];
2
- export declare const ensureArray: <T>(value: MaybeArray<T>) => T[];
package/out/utils.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ensureArray = void 0;
4
- const ensureArray = (value) => Array.isArray(value) ? value : [value];
5
- exports.ensureArray = ensureArray;