@grammyjs/commands 0.0.1 → 0.3.1
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/README.md +33 -143
- package/out/command.d.ts +14 -10
- package/out/command.js +33 -39
- package/out/deps.node.d.ts +2 -2
- package/out/deps.node.js +2 -1
- package/out/plugin.d.ts +13 -4
- package/out/plugin.js +46 -43
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,178 +1,68 @@
|
|
|
1
1
|
## grammY Commands Plugin
|
|
2
2
|
|
|
3
|
-
This plugin provides a convenient way to define and manage commands for your grammY bot. It simplifies the process of
|
|
4
|
-
setting up commands with scopes and localization.
|
|
3
|
+
This plugin provides a convenient way to define and manage commands for your grammY bot. It simplifies the process of setting up commands with scopes and localization.
|
|
5
4
|
|
|
6
5
|
## Installation
|
|
7
6
|
|
|
8
|
-
On NPM, this plugin is available as `@grammyjs/commands`, and on Deno it's available at
|
|
9
|
-
[deno.land/x/grammy_commands](https://deno.land/x/grammy_commands).
|
|
10
|
-
|
|
11
|
-
You can install it for Node.js with this command:
|
|
12
7
|
|
|
13
8
|
```sh
|
|
14
9
|
npm i @grammyjs/commands
|
|
15
10
|
```
|
|
16
11
|
|
|
17
|
-
## Core Concepts
|
|
18
|
-
|
|
19
|
-
### The `Commands` class
|
|
20
|
-
|
|
21
|
-
The `Commands` class is the foundation of this plugin. It allows you to create commands by specifying their names and
|
|
22
|
-
descriptions, along with additional attributes such as scopes and localization.
|
|
23
|
-
|
|
24
|
-
### The `ctx.setMyCommands` method
|
|
25
|
-
|
|
26
|
-
The `ctx.setMyCommands` method, provided by the plugin, offers a convenient way to dynamically set the available
|
|
27
|
-
commands for the current chat. By invoking this method and passing an instance of the `Commands` class, you can override
|
|
28
|
-
the default command scopes and make all commands accessible within the current chat. This feature is particularly useful
|
|
29
|
-
when you need to customize the command set for specific chat contexts, ignoring the predefined scopes, which allows for
|
|
30
|
-
a more tailored and context-aware usage of bot commands.
|
|
31
|
-
|
|
32
12
|
## Usage
|
|
33
13
|
|
|
34
|
-
|
|
14
|
+
The main functionality of this plugin is to define your commands, localize them, and give them handlers for each [scope](https://core.telegram.org/bots/api#botcommandscope), like so:
|
|
35
15
|
|
|
36
|
-
To manage commands for multiple scopes at once, create an instance of the `Commands` class, add your commands to it, and
|
|
37
|
-
then call the `setFor` method of the class instance. Check out this example:
|
|
38
16
|
|
|
39
17
|
```typescript
|
|
40
|
-
import { Bot } from "
|
|
41
|
-
import { Commands } from "
|
|
42
|
-
|
|
43
|
-
const bot = new Bot("<your_bot_token>");
|
|
44
|
-
const cmds = new Commands();
|
|
18
|
+
import { Bot } from "grammy";
|
|
19
|
+
import { Commands } from "@grammyjs/commands";
|
|
45
20
|
|
|
46
|
-
|
|
21
|
+
const bot = new Bot("<telegram token>");
|
|
47
22
|
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Managing Commands For The Current Chat
|
|
52
|
-
|
|
53
|
-
To set the commands for the current chat, you can use the `ctx.setMyCommands` method of the `Context` object. For that,
|
|
54
|
-
you will need to install the `CommandsFlavor` to your context, and create an instance of the `Commands` class, to which
|
|
55
|
-
you add your commands to and then pass to `ctx.setMyCommands`. This will override the predefined scopes and make all
|
|
56
|
-
commands available to the current chat. Here's an example:
|
|
57
|
-
|
|
58
|
-
> Please note that this only works in updates which have a `chat` object. If `ctx.chat` is not defined, an error will be
|
|
59
|
-
> thrown ar runtime.
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
import { Bot, Context } from "https://deno.land/x/grammy/mod.ts";
|
|
63
|
-
import { Commands, CommandsFlavor } from "https://deno.land/x/grammy_commands/mod.ts";
|
|
64
|
-
|
|
65
|
-
type MyContext = CommandsFlavor<Context>;
|
|
66
|
-
const bot = new Bot<MyContext>("<your_bot_token>");
|
|
67
|
-
|
|
68
|
-
bot.on(":text", async (ctx: Context) => {
|
|
69
|
-
const cmds = new Commands();
|
|
70
|
-
|
|
71
|
-
// Define your commands...
|
|
72
|
-
|
|
73
|
-
await ctx.setMyCommands(cmds);
|
|
74
|
-
});
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### Defining Commands
|
|
78
|
-
|
|
79
|
-
To define a command, use the `command` method of the `CommandsPlugin` instance. The `command` method takes two
|
|
80
|
-
arguments: the command name and the command description. Here's an example:
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
commands.command("help", "Sends help");
|
|
84
|
-
```
|
|
23
|
+
const myCommands = new Commands();
|
|
85
24
|
|
|
86
|
-
|
|
25
|
+
myCommands.command("start", "Initializes bot configuration")
|
|
26
|
+
.localize("pt", "start", "Inicializa as configurações do bot")
|
|
27
|
+
.addToScope({ type: "all_private_chats" }, (ctx) => ctx.reply(`Hello, ${ctx.chat.first_name}!`))
|
|
28
|
+
.addToScope({ type: "all_group_chats" }, (ctx) => ctx.reply(`Hello, members of ${ctx.chat.title}!`));
|
|
87
29
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
you can pass your middleware as a third parameter to the `.command()` method. You can also use the `onChatType` method
|
|
91
|
-
of the `Command` class to define middleware specific to that chat type. Here are some examples:
|
|
30
|
+
// Calls `setMyCommands`
|
|
31
|
+
await myCommands.setCommands(bot);
|
|
92
32
|
|
|
93
|
-
|
|
33
|
+
// Registers the command handlers
|
|
34
|
+
bot.use(myCommands);
|
|
94
35
|
|
|
95
|
-
|
|
96
|
-
commands.command("start", "Starts the bot", (ctx) => {
|
|
97
|
-
ctx.reply(`Hello, ${ctx.chat?.first_name ?? "there"}!`);
|
|
98
|
-
});
|
|
36
|
+
bot.start();
|
|
99
37
|
```
|
|
100
38
|
|
|
101
|
-
|
|
39
|
+
It is very important that you call `bot.use` with your instance of the `Commands` class.
|
|
40
|
+
Otherwise, the command handlers will not be registered, and your bot will not respond to those commands.
|
|
102
41
|
|
|
103
|
-
|
|
104
|
-
commands.command("start", "Starts the bot")
|
|
105
|
-
.onChatType("private", (ctx) => ctx.reply(`Hello ${ctx.chat.first_name}!`))
|
|
106
|
-
.onChatType(["group", "supergroup", "channel"], (ctx) => ctx.reply(`Hello members of ${ctx.chat.title}!`))
|
|
107
|
-
.addToScope("default");
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### Localization
|
|
42
|
+
### Context shortcuts
|
|
111
43
|
|
|
112
|
-
|
|
113
|
-
|
|
44
|
+
This plugin provides a shortcut for setting the commands for the current chat.
|
|
45
|
+
To use it, you need to install the commands flavor and the plugin itself, like so:
|
|
114
46
|
|
|
115
47
|
```typescript
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
```
|
|
48
|
+
import { Bot, Context } from "grammy";
|
|
49
|
+
import { Commands, CommandsFlavor, commands } from "@grammyjs/commands";
|
|
119
50
|
|
|
120
|
-
|
|
51
|
+
type BotContext = CommandsFlavor;
|
|
121
52
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
`addToScope` method. Here's an example:
|
|
53
|
+
const bot = new Bot<BotContext>("<telegram_token>");
|
|
54
|
+
bot.use(commands());
|
|
125
55
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
.addToScope("default")
|
|
129
|
-
.addToScope("all_group_chats")
|
|
130
|
-
.addToScope("all_private_chats")
|
|
131
|
-
.addToScope("all_chat_administrators")
|
|
132
|
-
.addToScope("chat_administrators", "@grammyjs")
|
|
133
|
-
.addToScope("chat", "@SpecificUserChat");
|
|
134
|
-
```
|
|
56
|
+
bot.on("message", async (ctx) => {
|
|
57
|
+
const cmds = new Commands();
|
|
135
58
|
|
|
136
|
-
|
|
59
|
+
cmds.command("start", "Initializes bot configuration")
|
|
60
|
+
.localize("pt", "start", "Inicializa as configurações do bot");
|
|
137
61
|
|
|
138
|
-
|
|
139
|
-
override the defined scopes and make all commands available to the current chat. Here's an example:
|
|
62
|
+
await ctx.setMyCommands(cmds);
|
|
140
63
|
|
|
141
|
-
|
|
142
|
-
bot.on(":text", async (ctx) => {
|
|
143
|
-
await ctx.setMyCommands(commands);
|
|
64
|
+
return ctx.reply("Commands set!");
|
|
144
65
|
});
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
This will set all commands defined in the `commands` instance for the current chat.
|
|
148
|
-
|
|
149
|
-
## Examples
|
|
150
|
-
|
|
151
|
-
### Example 1: Help Command
|
|
152
|
-
|
|
153
|
-
This example shows how to define a basic help command:
|
|
154
|
-
|
|
155
|
-
```typescript
|
|
156
|
-
commands.command("help", "Sends help")
|
|
157
|
-
.localize("de-DE", "hilfe", "Sendet Hilfe")
|
|
158
|
-
.addToScope("default")
|
|
159
|
-
.addToScope("all_group_chats")
|
|
160
|
-
.addToScope("all_private_chats")
|
|
161
|
-
.addToScope("all_chat_administrators")
|
|
162
|
-
.addToScope("chat_administrators", "@grammyjs")
|
|
163
|
-
.addToScope("chat", "@LWJerri");
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Example 2: Stats Command
|
|
167
|
-
|
|
168
|
-
This example shows how to define a stats command for group chats:
|
|
169
|
-
|
|
170
|
-
```typescript
|
|
171
|
-
commands.command("stats", "Sends group stats")
|
|
172
|
-
.addToScope("all_group_chats")
|
|
173
|
-
.addToScope("all_chat_administrators")
|
|
174
|
-
.addToScope("chat_administrators", "@grammyjs")
|
|
175
|
-
.addToScope("chat", "@LWJerri");
|
|
176
|
-
```
|
|
177
66
|
|
|
178
|
-
|
|
67
|
+
bot.start()
|
|
68
|
+
```
|
package/out/command.d.ts
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import { BotCommand, BotCommandScope,
|
|
1
|
+
import { type BotCommand, type BotCommandScope, type BotCommandScopeAllChatAdministrators, type BotCommandScopeAllGroupChats, type BotCommandScopeAllPrivateChats, type ChatTypeMiddleware, type Context, type Middleware, type MiddlewareObj } from "./deps.node.js";
|
|
2
2
|
export type MaybeArray<T> = T | T[];
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
type BotCommandGroupsScope = BotCommandScopeAllGroupChats | BotCommandScopeAllChatAdministrators;
|
|
4
|
+
export declare class Command<C extends Context = Context> implements MiddlewareObj<C> {
|
|
5
|
+
private _scopes;
|
|
6
|
+
private _languages;
|
|
7
|
+
private _composer;
|
|
8
|
+
constructor(name: string, description: string);
|
|
9
|
+
get scopes(): BotCommandScope[];
|
|
6
10
|
get languages(): Map<string, {
|
|
7
11
|
name: string;
|
|
8
12
|
description: string;
|
|
9
13
|
}>;
|
|
10
|
-
get
|
|
14
|
+
get names(): string[];
|
|
11
15
|
get name(): string;
|
|
12
16
|
get description(): string;
|
|
13
|
-
addToScope(
|
|
14
|
-
addToScope(
|
|
15
|
-
addToScope(
|
|
16
|
-
addToScope(type: "chat_administrators", chatId: string | number): this;
|
|
17
|
+
addToScope(scope: BotCommandGroupsScope, ...middleware: ChatTypeMiddleware<C, "group" | "supergroup">[]): this;
|
|
18
|
+
addToScope(scope: BotCommandScopeAllPrivateChats, ...middleware: ChatTypeMiddleware<C, "private">[]): this;
|
|
19
|
+
addToScope(scope: BotCommandScope, ...middleware: Array<Middleware<C>>): this;
|
|
17
20
|
localize(languageCode: string, name: string, description: string): this;
|
|
18
21
|
getLocalizedName(languageCode: string): string;
|
|
19
22
|
getLocalizedDescription(languageCode: string): string;
|
|
20
|
-
onChatType<T extends Chat["type"]>(chatType: MaybeArray<T>, ...middleware: Array<CommandMiddleware<ChatTypeContext<C, T>>>): this;
|
|
21
23
|
toObject(languageCode?: string): BotCommand;
|
|
24
|
+
middleware(): import("grammy").MiddlewareFn<C>;
|
|
22
25
|
}
|
|
26
|
+
export {};
|
package/out/command.js
CHANGED
|
@@ -1,62 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _Command_scopes, _Command_languages;
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.Command = void 0;
|
|
10
4
|
const deps_node_js_1 = require("./deps.node.js");
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.
|
|
5
|
+
const isAdmin = (ctx) => ctx.getAuthor().then((author) => ["administrator", "creator"].includes(author.status));
|
|
6
|
+
class Command {
|
|
7
|
+
constructor(name, description) {
|
|
8
|
+
this._scopes = [];
|
|
9
|
+
this._languages = new Map();
|
|
10
|
+
this._composer = new deps_node_js_1.Composer();
|
|
11
|
+
this._languages.set("default", { name, description });
|
|
12
|
+
}
|
|
13
|
+
get scopes() {
|
|
14
|
+
return this._scopes;
|
|
21
15
|
}
|
|
22
16
|
get languages() {
|
|
23
|
-
return
|
|
17
|
+
return this._languages;
|
|
24
18
|
}
|
|
25
|
-
get
|
|
26
|
-
return
|
|
19
|
+
get names() {
|
|
20
|
+
return Array.from(this._languages.values()).map(({ name }) => name);
|
|
27
21
|
}
|
|
28
22
|
get name() {
|
|
29
|
-
return
|
|
23
|
+
return this._languages.get("default").name;
|
|
30
24
|
}
|
|
31
25
|
get description() {
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
addToScope(
|
|
35
|
-
|
|
36
|
-
.with({ type: "default" },
|
|
37
|
-
.with({ type:
|
|
38
|
-
.with({ type: "
|
|
39
|
-
.
|
|
40
|
-
|
|
41
|
-
|
|
26
|
+
return this._languages.get("default").description;
|
|
27
|
+
}
|
|
28
|
+
addToScope(scope, ...middleware) {
|
|
29
|
+
(0, deps_node_js_1.match)(scope)
|
|
30
|
+
.with({ type: "default" }, () => this._composer.command(this.names, ...middleware))
|
|
31
|
+
.with({ type: "all_chat_administrators" }, () => this._composer.filter(isAdmin).command(this.names, ...middleware))
|
|
32
|
+
.with({ type: "all_private_chats" }, () => this._composer.chatType("private").command(this.names, ...middleware))
|
|
33
|
+
.with({ type: "all_group_chats" }, () => this._composer.chatType(["group", "supergroup"]).command(this.names, ...middleware))
|
|
34
|
+
.with({ type: deps_node_js_1.P.union("chat", "chat_administrators"), chat_id: deps_node_js_1.P.not(deps_node_js_1.P.nullish).select() }, (chatId) => this._composer.filter((ctx) => { var _a; return ((_a = ctx.chat) === null || _a === void 0 ? void 0 : _a.id) === chatId; }).filter(isAdmin).command(this.names, ...middleware))
|
|
35
|
+
.with({ type: "chat_member", chat_id: deps_node_js_1.P.not(deps_node_js_1.P.nullish).select("chatId"), user_id: deps_node_js_1.P.not(deps_node_js_1.P.nullish).select("userId") }, ({ chatId, userId }) => this._composer.filter((ctx) => { var _a; return ((_a = ctx.chat) === null || _a === void 0 ? void 0 : _a.id) === chatId; })
|
|
36
|
+
.filter((ctx) => { var _a; return ((_a = ctx.from) === null || _a === void 0 ? void 0 : _a.id) === userId; })
|
|
37
|
+
.command(this.names, ...middleware));
|
|
38
|
+
this._scopes.push(scope);
|
|
42
39
|
return this;
|
|
43
40
|
}
|
|
44
41
|
localize(languageCode, name, description) {
|
|
45
|
-
|
|
42
|
+
this._languages.set(languageCode, { name, description });
|
|
46
43
|
return this;
|
|
47
44
|
}
|
|
48
45
|
getLocalizedName(languageCode) {
|
|
49
46
|
var _a, _b;
|
|
50
|
-
return (_b = (_a =
|
|
47
|
+
return (_b = (_a = this._languages.get(languageCode)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : this.name;
|
|
51
48
|
}
|
|
52
49
|
getLocalizedDescription(languageCode) {
|
|
53
50
|
var _a, _b;
|
|
54
|
-
return (_b = (_a =
|
|
55
|
-
}
|
|
56
|
-
onChatType(chatType, ...middleware) {
|
|
57
|
-
const names = Array.from(__classPrivateFieldGet(this, _Command_languages, "f").values()).map(({ name }) => name);
|
|
58
|
-
this.chatType(chatType).command(names, ...middleware);
|
|
59
|
-
return this;
|
|
51
|
+
return (_b = (_a = this._languages.get(languageCode)) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : this.description;
|
|
60
52
|
}
|
|
61
53
|
toObject(languageCode = "default") {
|
|
62
54
|
return {
|
|
@@ -64,6 +56,8 @@ class Command extends deps_node_js_1.Composer {
|
|
|
64
56
|
description: this.getLocalizedDescription(languageCode),
|
|
65
57
|
};
|
|
66
58
|
}
|
|
59
|
+
middleware() {
|
|
60
|
+
return this._composer.middleware();
|
|
61
|
+
}
|
|
67
62
|
}
|
|
68
63
|
exports.Command = Command;
|
|
69
|
-
_Command_scopes = new WeakMap(), _Command_languages = new WeakMap();
|
package/out/deps.node.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { Bot, type ChatTypeContext, type CommandMiddleware, Composer, Context, type Middleware, type NextFunction, } from "grammy";
|
|
2
|
-
export type { BotCommand, BotCommandScope, Chat } from "grammy/types";
|
|
1
|
+
export { Api, Bot, type ChatTypeContext, type ChatTypeMiddleware, type CommandMiddleware, Composer, Context, type Middleware, type MiddlewareObj, type NextFunction, } from "grammy";
|
|
2
|
+
export type { BotCommand, BotCommandScope, BotCommandScopeAllChatAdministrators, BotCommandScopeAllGroupChats, BotCommandScopeAllPrivateChats, Chat, } from "grammy/types";
|
|
3
3
|
export { match, P } from "ts-pattern";
|
package/out/deps.node.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.P = exports.match = exports.Context = exports.Composer = exports.Bot = void 0;
|
|
3
|
+
exports.P = exports.match = exports.Context = exports.Composer = exports.Bot = exports.Api = void 0;
|
|
4
4
|
// TODO: Replace with official deno module, once it arrives (https://github.com/gvergnaud/ts-pattern/pull/108)
|
|
5
5
|
var grammy_1 = require("grammy");
|
|
6
|
+
Object.defineProperty(exports, "Api", { enumerable: true, get: function () { return grammy_1.Api; } });
|
|
6
7
|
Object.defineProperty(exports, "Bot", { enumerable: true, get: function () { return grammy_1.Bot; } });
|
|
7
8
|
Object.defineProperty(exports, "Composer", { enumerable: true, get: function () { return grammy_1.Composer; } });
|
|
8
9
|
Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return grammy_1.Context; } });
|
package/out/plugin.d.ts
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { Command } from "./command.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Api, BotCommand, BotCommandScope, Context } from "./deps.node.js";
|
|
3
3
|
type SetMyCommandsParams = {
|
|
4
4
|
scope?: BotCommandScope;
|
|
5
5
|
language_code?: string;
|
|
6
6
|
commands: BotCommand[];
|
|
7
7
|
};
|
|
8
8
|
export declare class Commands<C extends Context> {
|
|
9
|
-
|
|
9
|
+
private _languages;
|
|
10
|
+
private _scopes;
|
|
11
|
+
private _commands;
|
|
12
|
+
private _composer;
|
|
10
13
|
constructor(commands?: Command<C>[]);
|
|
11
|
-
|
|
14
|
+
private _addCommandToScope;
|
|
15
|
+
private _populateComposer;
|
|
16
|
+
private _populateMetadata;
|
|
17
|
+
command(name: string, description: string): Command<C>;
|
|
12
18
|
toArgs(): SetMyCommandsParams[];
|
|
13
19
|
toSingleScopeArgs(scope: BotCommandScope): SetMyCommandsParams[];
|
|
14
|
-
|
|
20
|
+
setCommands({ api }: {
|
|
21
|
+
api: Api;
|
|
22
|
+
}): Promise<void>;
|
|
15
23
|
toJSON(): SetMyCommandsParams[];
|
|
16
24
|
toString(): string;
|
|
25
|
+
middleware(): import("grammy").MiddlewareFn<C>;
|
|
17
26
|
}
|
|
18
27
|
export {};
|
package/out/plugin.js
CHANGED
|
@@ -1,38 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var _Commands_instances, _Commands_languages, _Commands_scopes, _Commands_commands, _Commands_addCommandToScope, _Commands_populate;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.Commands = void 0;
|
|
16
4
|
const command_js_1 = require("./command.js");
|
|
17
5
|
const deps_node_js_1 = require("./deps.node.js");
|
|
18
6
|
class Commands {
|
|
19
7
|
constructor(commands = []) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
commands.forEach((command) =>
|
|
8
|
+
this._languages = new Set();
|
|
9
|
+
this._scopes = new Map();
|
|
10
|
+
this._commands = [];
|
|
11
|
+
this._composer = new deps_node_js_1.Composer();
|
|
12
|
+
commands.forEach((command) => this._commands.push(command));
|
|
25
13
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
_addCommandToScope(scope, command) {
|
|
15
|
+
var _a;
|
|
16
|
+
const commands = (_a = this._scopes.get(JSON.stringify(scope))) !== null && _a !== void 0 ? _a : [];
|
|
17
|
+
this._scopes.set(JSON.stringify(scope), commands.concat([command]));
|
|
18
|
+
}
|
|
19
|
+
_populateComposer() {
|
|
20
|
+
for (const command of this._commands) {
|
|
21
|
+
for (const args of command.languages.values()) {
|
|
22
|
+
this._composer.command(args.name, command.middleware());
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
_populateMetadata() {
|
|
27
|
+
this._languages.clear();
|
|
28
|
+
this._scopes.clear();
|
|
29
|
+
this._commands.forEach((command) => {
|
|
30
|
+
for (const scope of command.scopes) {
|
|
31
|
+
this._addCommandToScope(scope, command);
|
|
32
|
+
}
|
|
33
|
+
for (const language of command.languages.keys()) {
|
|
34
|
+
this._languages.add(language);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
command(name, description) {
|
|
39
|
+
const command = new command_js_1.Command(name, description);
|
|
40
|
+
this._commands.push(command);
|
|
29
41
|
return command;
|
|
30
42
|
}
|
|
31
43
|
toArgs() {
|
|
32
|
-
|
|
44
|
+
this._populateMetadata();
|
|
33
45
|
const params = [];
|
|
34
|
-
for (const [scope, commands] of
|
|
35
|
-
for (const language of
|
|
46
|
+
for (const [scope, commands] of this._scopes.entries()) {
|
|
47
|
+
for (const language of this._languages) {
|
|
36
48
|
params.push({
|
|
37
49
|
scope: JSON.parse(scope),
|
|
38
50
|
language_code: language === "default" ? undefined : language,
|
|
@@ -43,23 +55,21 @@ class Commands {
|
|
|
43
55
|
return params.filter((params) => params.commands.length > 0);
|
|
44
56
|
}
|
|
45
57
|
toSingleScopeArgs(scope) {
|
|
46
|
-
|
|
58
|
+
this._populateMetadata();
|
|
47
59
|
const params = [];
|
|
48
|
-
for (const language of
|
|
60
|
+
for (const language of this._languages) {
|
|
49
61
|
params.push({
|
|
50
62
|
scope,
|
|
51
|
-
language_code:
|
|
52
|
-
commands:
|
|
63
|
+
language_code: language === "default" ? undefined : language,
|
|
64
|
+
commands: this._commands
|
|
53
65
|
.filter((command) => command.scopes.length)
|
|
54
66
|
.map((command) => command.toObject(language)),
|
|
55
67
|
});
|
|
56
68
|
}
|
|
57
69
|
return params;
|
|
58
70
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const promises = argsArray.map((args) => bot.api.raw.setMyCommands(args));
|
|
62
|
-
return Promise.all(promises);
|
|
71
|
+
async setCommands({ api }) {
|
|
72
|
+
await Promise.all(this.toArgs().map((args) => api.raw.setMyCommands(args)));
|
|
63
73
|
}
|
|
64
74
|
toJSON() {
|
|
65
75
|
return this.toArgs();
|
|
@@ -67,18 +77,11 @@ class Commands {
|
|
|
67
77
|
toString() {
|
|
68
78
|
return JSON.stringify(this);
|
|
69
79
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
__classPrivateFieldSet(this, _Commands_languages, new Set(), "f");
|
|
76
|
-
__classPrivateFieldSet(this, _Commands_scopes, new Map(), "f");
|
|
77
|
-
__classPrivateFieldGet(this, _Commands_commands, "f").forEach((command) => {
|
|
78
|
-
command.scopes.forEach((scope) => __classPrivateFieldGet(this, _Commands_instances, "m", _Commands_addCommandToScope).call(this, scope, command));
|
|
79
|
-
Array.from(command.languages.keys()).forEach((language) => __classPrivateFieldGet(this, _Commands_languages, "f").add(language));
|
|
80
|
-
});
|
|
81
|
-
}, Symbol.for("Deno.customInspect"))]() {
|
|
80
|
+
middleware() {
|
|
81
|
+
this._populateComposer();
|
|
82
|
+
return this._composer.middleware();
|
|
83
|
+
}
|
|
84
|
+
[Symbol.for("Deno.customInspect")]() {
|
|
82
85
|
return this.toString();
|
|
83
86
|
}
|
|
84
87
|
[Symbol.for("nodejs.util.inspect.custom")]() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grammyjs/commands",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "grammY Commands Plugin",
|
|
5
5
|
"main": "out/mod.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,5 +21,7 @@
|
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"typescript": "^5.1.6"
|
|
23
23
|
},
|
|
24
|
-
"files": [
|
|
24
|
+
"files": [
|
|
25
|
+
"out"
|
|
26
|
+
]
|
|
25
27
|
}
|