@grammyjs/commands 1.0.8 → 1.1.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.
@@ -1,5 +1,5 @@
1
1
  import { Command, CommandsFlavor } from "./mod.js";
2
- import { Api, BotCommand, BotCommandScope, CommandContext, Context, type LanguageCode, Middleware } from "./deps.node.js";
2
+ import { Api, BotCommand, BotCommandScope, BotCommandScopeChat, CommandContext, Context, type LanguageCode, Middleware } from "./deps.node.js";
3
3
  import type { BotCommandX, CommandOptions } from "./types.js";
4
4
  import { type MaybeArray } from "./utils/array.js";
5
5
  import { SetBotCommandsOptions } from "./utils/set-bot-commands.js";
@@ -83,7 +83,7 @@ export declare class CommandGroup<C extends Context> {
83
83
  *
84
84
  * @returns One item for each combination of command + scope + language
85
85
  */
86
- toArgs(): {
86
+ toArgs(chat_id?: BotCommandScopeChat["chat_id"]): {
87
87
  scopes: SetMyCommandsParams[];
88
88
  uncompliantCommands: UncompliantCommand[];
89
89
  };
@@ -78,7 +78,7 @@ class CommandGroup {
78
78
  *
79
79
  * @returns One item for each combination of command + scope + language
80
80
  */
81
- toArgs() {
81
+ toArgs(chat_id) {
82
82
  this._populateMetadata();
83
83
  const scopes = [];
84
84
  const uncompliantCommands = [];
@@ -98,7 +98,9 @@ class CommandGroup {
98
98
  });
99
99
  if (compliantScopedCommands.length) {
100
100
  scopes.push({
101
- scope: JSON.parse(scope),
101
+ scope: chat_id
102
+ ? { ...JSON.parse(scope), chat_id }
103
+ : { ...JSON.parse(scope) },
102
104
  language_code: language === "default" ? undefined : language,
103
105
  commands: compliantScopedCommands.map((command) => command.toObject(language)),
104
106
  });
package/out/context.d.ts CHANGED
@@ -84,40 +84,13 @@ export declare class MyCommandParams {
84
84
  commandsParams: SetMyCommandsParams[];
85
85
  uncompliantCommands: import("./command-group.js").UncompliantCommand[];
86
86
  };
87
- /**
88
- * Serializes one or multiple {@link CommandGroup} instances, each one into their respective
89
- * single scoped SetMyCommandsParams version.
90
- * @example
91
- ```ts
92
- const adminCommands = new CommandGroup();
93
- // add to scope, localize, etc
94
- const userCommands = new CommandGroup();
95
- // add to scope, localize, etc
96
- const [
97
- singleScopedAdminParams,
98
- singleScopedUserParams
99
- ] = MyCommandsParams.serialize([adminCommands,userCommands])
100
- ```
101
- * @param commandsArr an array of one or more commands instances
102
- * @param chat_id the chat id relative to the message update, coming from the ctx object.
103
- * @returns an array of scoped {@link SetMyCommandsParams} mapped from their respective Commands instances
104
- */
105
- static _serialize<C extends Context>(commandsArr: CommandGroup<C>[], chat_id: BotCommandScopeChat["chat_id"]): {
106
- commandParams: SetMyCommandsParams[];
107
- uncompliantCommands: import("./command-group.js").UncompliantCommand[];
108
- }[];
109
- /**
110
- * Lexicographically sorts commandParams based on their language code.
111
- * @returns the sorted array
112
- */
113
- static _sortByLanguage(params: SetMyCommandsParams[]): SetMyCommandsParams[];
114
87
  /**
115
88
  * Iterates over an array of CommandsParams
116
89
  * merging their respective {@link SetMyCommandsParams.commands}
117
- * when they are from the same language, separating when they are not.
90
+ * when they are from the same language and scope
118
91
  *
119
92
  * @param params a flattened array of commands params coming from one or more Commands instances
120
93
  * @returns an array containing all commands grouped by language
121
94
  */
122
- private static mergeByLanguage;
95
+ private static merge;
123
96
  }
package/out/context.js CHANGED
@@ -98,79 +98,41 @@ class MyCommandParams {
98
98
  * @returns an array of {@link SetMyCommandsParams} grouped by language
99
99
  */
100
100
  static from(commands, chat_id) {
101
- const serializedCommands = this._serialize(commands, chat_id);
101
+ const serializedCommands = commands.map((cmds) => cmds.toArgs(chat_id));
102
102
  const commandsParams = serializedCommands
103
- .map(({ commandParams }) => commandParams)
103
+ .map(({ scopes }) => scopes)
104
104
  .flat();
105
105
  const uncompliantCommands = serializedCommands
106
106
  .map(({ uncompliantCommands }) => uncompliantCommands)
107
107
  .flat();
108
108
  return {
109
- commandsParams: this.mergeByLanguage(commandsParams),
109
+ commandsParams: this.merge(commandsParams),
110
110
  uncompliantCommands,
111
111
  };
112
112
  }
113
- /**
114
- * Serializes one or multiple {@link CommandGroup} instances, each one into their respective
115
- * single scoped SetMyCommandsParams version.
116
- * @example
117
- ```ts
118
- const adminCommands = new CommandGroup();
119
- // add to scope, localize, etc
120
- const userCommands = new CommandGroup();
121
- // add to scope, localize, etc
122
- const [
123
- singleScopedAdminParams,
124
- singleScopedUserParams
125
- ] = MyCommandsParams.serialize([adminCommands,userCommands])
126
- ```
127
- * @param commandsArr an array of one or more commands instances
128
- * @param chat_id the chat id relative to the message update, coming from the ctx object.
129
- * @returns an array of scoped {@link SetMyCommandsParams} mapped from their respective Commands instances
130
- */
131
- static _serialize(commandsArr, chat_id) {
132
- return commandsArr.map((commands) => commands.toSingleScopeArgs({
133
- type: "chat",
134
- chat_id,
135
- }));
136
- }
137
- /**
138
- * Lexicographically sorts commandParams based on their language code.
139
- * @returns the sorted array
140
- */
141
- static _sortByLanguage(params) {
142
- return params.sort((a, b) => {
143
- if (!a.language_code)
144
- return -1;
145
- if (!b.language_code)
146
- return 1;
147
- return a.language_code.localeCompare(b.language_code);
148
- });
149
- }
150
113
  /**
151
114
  * Iterates over an array of CommandsParams
152
115
  * merging their respective {@link SetMyCommandsParams.commands}
153
- * when they are from the same language, separating when they are not.
116
+ * when they are from the same language and scope
154
117
  *
155
118
  * @param params a flattened array of commands params coming from one or more Commands instances
156
119
  * @returns an array containing all commands grouped by language
157
120
  */
158
- static mergeByLanguage(params) {
121
+ static merge(params) {
159
122
  if (!params.length)
160
123
  return [];
161
- const sorted = this._sortByLanguage(params);
162
- return sorted.reduce((result, current, i, arr) => {
163
- if (i === 0 || current.language_code !== arr[i - 1].language_code) {
164
- result.push(current);
165
- return result;
166
- }
167
- else {
168
- result[result.length - 1].commands = result[result.length - 1]
169
- .commands
170
- .concat(current.commands);
171
- return result;
124
+ const map = new Map();
125
+ params.forEach((curr) => {
126
+ if (!curr.scope)
127
+ return;
128
+ const key = `${curr.scope.type},${curr.language_code}`;
129
+ const old = map.get(key);
130
+ if (old) {
131
+ curr.commands = curr.commands.concat(old.commands);
172
132
  }
173
- }, []);
133
+ map.set(key, curr);
134
+ });
135
+ return [...map.values()];
174
136
  }
175
137
  }
176
138
  exports.MyCommandParams = MyCommandParams;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grammyjs/commands",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "grammY Commands Plugin",
5
5
  "main": "out/mod.js",
6
6
  "scripts": {