gramio 0.4.5 → 0.4.8

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/dist/index.cjs CHANGED
@@ -428,7 +428,7 @@ class Bot {
428
428
  __Derives;
429
429
  "~" = this._;
430
430
  filters = {
431
- context: (name2) => (context) => context.is(name2)
431
+ context: (name) => (context) => context.is(name)
432
432
  };
433
433
  /** Options provided to instance */
434
434
  options;
@@ -651,18 +651,27 @@ class Bot {
651
651
  return this;
652
652
  }
653
653
  decorate(nameOrRecordValue, value) {
654
- for (const contextName of Object.keys(contexts.contextsMappings)) {
654
+ for (const contextName of Object.keys(
655
+ contexts.contextsMappings
656
+ )) {
655
657
  if (typeof nameOrRecordValue === "string")
656
- Object.defineProperty(contexts.contextsMappings[contextName].prototype, name, {
657
- value
658
- });
658
+ Object.defineProperty(
659
+ contexts.contextsMappings[contextName].prototype,
660
+ nameOrRecordValue,
661
+ {
662
+ value,
663
+ configurable: true
664
+ }
665
+ );
659
666
  else
660
667
  Object.defineProperties(
661
- // @ts-expect-error
662
668
  contexts.contextsMappings[contextName].prototype,
663
669
  Object.keys(nameOrRecordValue).reduce(
664
670
  (acc, key) => {
665
- acc[key] = { value: nameOrRecordValue[key] };
671
+ acc[key] = {
672
+ value: nameOrRecordValue[key],
673
+ configurable: true
674
+ };
666
675
  return acc;
667
676
  },
668
677
  {}
@@ -1002,15 +1011,23 @@ class Bot {
1002
1011
  * return context.send(`You message is /start ${context.args}`);
1003
1012
  * });
1004
1013
  */
1005
- command(command, handler, options) {
1006
- if (command.startsWith("/"))
1007
- throw new Error("Do not use / in command name");
1014
+ command(command, handler) {
1015
+ const normalizedCommands = typeof command === "string" ? [command] : Array.from(command);
1016
+ for (const cmd of normalizedCommands) {
1017
+ if (cmd.startsWith("/"))
1018
+ throw new Error(`Do not use / in command name (${cmd})`);
1019
+ }
1008
1020
  return this.on(["message", "business_message"], (context, next) => {
1009
1021
  if (context.entities?.some((entity) => {
1010
1022
  if (entity.type !== "bot_command" || entity.offset > 0) return false;
1011
- const cmd = context.text?.slice(1, entity.length)?.replace(`@${this.info.username}`, "");
1023
+ const cmd = context.text?.slice(1, entity.length)?.replace(
1024
+ this.info?.username ? `@${this.info.username}` : /@[a-zA-Z0-9_]+/,
1025
+ ""
1026
+ );
1012
1027
  context.args = context.text?.slice(entity.length).trim() || null;
1013
- return cmd === command;
1028
+ return normalizedCommands.some(
1029
+ (normalizedCommand) => cmd === normalizedCommand
1030
+ );
1014
1031
  }))
1015
1032
  return handler(context);
1016
1033
  return next();
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@ import { CallbackData } from '@gramio/callback-data';
2
2
  export * from '@gramio/callback-data';
3
3
  import { Context, UpdateName, ContextType, BotLike, Attachment } from '@gramio/contexts';
4
4
  export * from '@gramio/contexts';
5
- import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, SetWebhookParams, TelegramUpdate, TelegramReactionTypeEmojiEmoji, SetMyCommandsParams, TelegramBotCommand } from '@gramio/types';
5
+ import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, SetWebhookParams, TelegramUpdate, TelegramReactionTypeEmojiEmoji } from '@gramio/types';
6
6
  export * from '@gramio/types';
7
7
  import * as middleware_io from 'middleware-io';
8
8
  import { Composer as Composer$1, Middleware, CaughtMiddlewareHandler, NextMiddleware } from 'middleware-io';
@@ -977,9 +977,9 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
977
977
  * return context.send(`You message is /start ${context.args}`);
978
978
  * });
979
979
  */
980
- command(command: string, handler: (context: ContextType<typeof this, "message"> & {
980
+ command(command: MaybeArray<string>, handler: (context: ContextType<typeof this, "message"> & {
981
981
  args: string | null;
982
- }) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
982
+ }) => unknown): this;
983
983
  /** Currently not isolated!!! */
984
984
  group(grouped: (bot: typeof this) => AnyBot): typeof this;
985
985
  /**
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import { CallbackData } from '@gramio/callback-data';
2
2
  export * from '@gramio/callback-data';
3
3
  import { Context, UpdateName, ContextType, BotLike, Attachment } from '@gramio/contexts';
4
4
  export * from '@gramio/contexts';
5
- import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, SetWebhookParams, TelegramUpdate, TelegramReactionTypeEmojiEmoji, SetMyCommandsParams, TelegramBotCommand } from '@gramio/types';
5
+ import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, SetWebhookParams, TelegramUpdate, TelegramReactionTypeEmojiEmoji } from '@gramio/types';
6
6
  export * from '@gramio/types';
7
7
  import * as middleware_io from 'middleware-io';
8
8
  import { Composer as Composer$1, Middleware, CaughtMiddlewareHandler, NextMiddleware } from 'middleware-io';
@@ -977,9 +977,9 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
977
977
  * return context.send(`You message is /start ${context.args}`);
978
978
  * });
979
979
  */
980
- command(command: string, handler: (context: ContextType<typeof this, "message"> & {
980
+ command(command: MaybeArray<string>, handler: (context: ContextType<typeof this, "message"> & {
981
981
  args: string | null;
982
- }) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
982
+ }) => unknown): this;
983
983
  /** Currently not isolated!!! */
984
984
  group(grouped: (bot: typeof this) => AnyBot): typeof this;
985
985
  /**
package/dist/index.js CHANGED
@@ -430,7 +430,7 @@ class Bot {
430
430
  __Derives;
431
431
  "~" = this._;
432
432
  filters = {
433
- context: (name2) => (context) => context.is(name2)
433
+ context: (name) => (context) => context.is(name)
434
434
  };
435
435
  /** Options provided to instance */
436
436
  options;
@@ -653,18 +653,27 @@ class Bot {
653
653
  return this;
654
654
  }
655
655
  decorate(nameOrRecordValue, value) {
656
- for (const contextName of Object.keys(contextsMappings)) {
656
+ for (const contextName of Object.keys(
657
+ contextsMappings
658
+ )) {
657
659
  if (typeof nameOrRecordValue === "string")
658
- Object.defineProperty(contextsMappings[contextName].prototype, name, {
659
- value
660
- });
660
+ Object.defineProperty(
661
+ contextsMappings[contextName].prototype,
662
+ nameOrRecordValue,
663
+ {
664
+ value,
665
+ configurable: true
666
+ }
667
+ );
661
668
  else
662
669
  Object.defineProperties(
663
- // @ts-expect-error
664
670
  contextsMappings[contextName].prototype,
665
671
  Object.keys(nameOrRecordValue).reduce(
666
672
  (acc, key) => {
667
- acc[key] = { value: nameOrRecordValue[key] };
673
+ acc[key] = {
674
+ value: nameOrRecordValue[key],
675
+ configurable: true
676
+ };
668
677
  return acc;
669
678
  },
670
679
  {}
@@ -1004,15 +1013,23 @@ class Bot {
1004
1013
  * return context.send(`You message is /start ${context.args}`);
1005
1014
  * });
1006
1015
  */
1007
- command(command, handler, options) {
1008
- if (command.startsWith("/"))
1009
- throw new Error("Do not use / in command name");
1016
+ command(command, handler) {
1017
+ const normalizedCommands = typeof command === "string" ? [command] : Array.from(command);
1018
+ for (const cmd of normalizedCommands) {
1019
+ if (cmd.startsWith("/"))
1020
+ throw new Error(`Do not use / in command name (${cmd})`);
1021
+ }
1010
1022
  return this.on(["message", "business_message"], (context, next) => {
1011
1023
  if (context.entities?.some((entity) => {
1012
1024
  if (entity.type !== "bot_command" || entity.offset > 0) return false;
1013
- const cmd = context.text?.slice(1, entity.length)?.replace(`@${this.info.username}`, "");
1025
+ const cmd = context.text?.slice(1, entity.length)?.replace(
1026
+ this.info?.username ? `@${this.info.username}` : /@[a-zA-Z0-9_]+/,
1027
+ ""
1028
+ );
1014
1029
  context.args = context.text?.slice(entity.length).trim() || null;
1015
- return cmd === command;
1030
+ return normalizedCommands.some(
1031
+ (normalizedCommand) => cmd === normalizedCommand
1032
+ );
1016
1033
  }))
1017
1034
  return handler(context);
1018
1035
  return next();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gramio",
3
3
  "type": "module",
4
- "version": "0.4.5",
4
+ "version": "0.4.8",
5
5
  "description": "Powerful, extensible and really type-safe Telegram Bot API framework",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.js",