@mtkruto/node 0.1.135 → 0.1.138

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.
Files changed (39) hide show
  1. package/esm/0_deps.d.ts +1 -1
  2. package/esm/0_deps.js +1 -1
  3. package/esm/4_constants.d.ts +1 -1
  4. package/esm/4_constants.js +1 -1
  5. package/esm/client/0_utilities.d.ts +38 -23
  6. package/esm/client/0_utilities.js +27 -0
  7. package/esm/client/1_composer.d.ts +4 -4
  8. package/esm/client/1_composer.js +3 -29
  9. package/esm/client/4_client.d.ts +4 -4
  10. package/esm/client/4_client.js +3 -29
  11. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.d.ts +2 -0
  12. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js +2 -0
  13. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.d.ts +26 -0
  14. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.js +32 -0
  15. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.d.ts +41 -0
  16. package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.js +113 -0
  17. package/esm/storage/1_storage_local_storage.js +2 -2
  18. package/esm/storage/1_storage_session_storage.js +2 -2
  19. package/esm/types/6_update.d.ts +70 -8
  20. package/package.json +1 -2
  21. package/script/0_deps.d.ts +1 -1
  22. package/script/0_deps.js +6 -6
  23. package/script/4_constants.d.ts +1 -1
  24. package/script/4_constants.js +1 -1
  25. package/script/client/0_utilities.d.ts +38 -23
  26. package/script/client/0_utilities.js +29 -1
  27. package/script/client/1_composer.d.ts +4 -4
  28. package/script/client/1_composer.js +3 -29
  29. package/script/client/4_client.d.ts +4 -4
  30. package/script/client/4_client.js +2 -28
  31. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.d.ts +2 -0
  32. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js +18 -0
  33. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.d.ts +26 -0
  34. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.js +36 -0
  35. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.d.ts +41 -0
  36. package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.js +117 -0
  37. package/script/storage/1_storage_local_storage.js +2 -2
  38. package/script/storage/1_storage_session_storage.js +2 -2
  39. package/script/types/6_update.d.ts +70 -8
@@ -9,7 +9,13 @@ import { Message } from "./4_message.js";
9
9
  import { CallbackQuery } from "./5_callback_query.js";
10
10
  import { Chat } from "./5_chat.js";
11
11
  /**
12
- * The client's connection state was changed.
12
+ * A client's connection state was changed.
13
+ *
14
+ * ```
15
+ * client.on("connectionState", (ctx) => {
16
+ * console.log("The client's connection state is now:", ctx.connectionState);
17
+ * });
18
+ * ```
13
19
  * @unlisted
14
20
  */
15
21
  export interface UpdateConnectionState {
@@ -17,7 +23,18 @@ export interface UpdateConnectionState {
17
23
  connectionState: ConnectionState;
18
24
  }
19
25
  /**
20
- * The client's authorization state was changed.
26
+ * A client's authorization state was changed.
27
+ *
28
+ * ```
29
+ * client.on("authorizationState", (ctx) => {
30
+ * if (ctx.authorizationState.authorized) {
31
+ * const me = await ctx.client.getMe();
32
+ * console.log("The client is now authorized as", me.firstName);
33
+ * } else {
34
+ * console.log("The client is no longer authorized.")
35
+ * }
36
+ * });
37
+ * ```
21
38
  * @unlisted
22
39
  */
23
40
  export interface UpdateAuthorizationState {
@@ -26,6 +43,23 @@ export interface UpdateAuthorizationState {
26
43
  }
27
44
  /**
28
45
  * A message was sent or received.
46
+ *
47
+ * ```
48
+ * // Handle text messages
49
+ * client.on("message:text", (ctx) => {
50
+ * const receivedOrSent = ctx.message.out ? "sent" : "received";
51
+ * console.log("Just", receivedOrSent, "a text message:", ctx.message.text);
52
+ * });
53
+ *
54
+ * // Handle other messages
55
+ * client.on("message", (ctx) => {
56
+ * if (ctx.message.out) {
57
+ * console.log("Just sent a message.");
58
+ * }
59
+ * });
60
+ * ```
61
+ *
62
+ * Note that updates on outgoing messages are disabled by default for bots.
29
63
  * @unlisted
30
64
  */
31
65
  export interface UpdateNewMessage {
@@ -34,6 +68,13 @@ export interface UpdateNewMessage {
34
68
  }
35
69
  /**
36
70
  * A message was edited.
71
+ *
72
+ * ```
73
+ * client.on("editedMessage", (ctx) => {
74
+ * console.log("A message was just edited.");
75
+ * // ctx.editedMessage
76
+ * });
77
+ * ```
37
78
  * @unlisted
38
79
  */
39
80
  export interface UpdateEditedMessage {
@@ -42,6 +83,14 @@ export interface UpdateEditedMessage {
42
83
  }
43
84
  /**
44
85
  * One or more messages were deleted.
86
+ *
87
+ * ```
88
+ * client.on("deletedMessages", (ctx) => {
89
+ * for (const deletedMessage of ctx.deletedMessages) {
90
+ * console.log(deletedMessage);
91
+ * }
92
+ * });
93
+ * ```
45
94
  * @unlisted
46
95
  */
47
96
  export interface UpdateDeletedMessages {
@@ -49,7 +98,13 @@ export interface UpdateDeletedMessages {
49
98
  deletedMessages: MessageIdentifier[];
50
99
  }
51
100
  /**
52
- * A callback query was received. Bot-only.
101
+ * A callback query was made (a user presses an inline button). Bot-only.
102
+ *
103
+ * ```
104
+ * client.on("callbackQuery", async (ctx) => {
105
+ * await ctx.answerCallbackQuery(ctx.callbackQuery.data, { alert: true });
106
+ * });
107
+ * ```
53
108
  * @unlisted
54
109
  */
55
110
  export interface UpdateCallbackQuery {
@@ -58,6 +113,13 @@ export interface UpdateCallbackQuery {
58
113
  }
59
114
  /**
60
115
  * An inline query was received. Bot-only.
116
+ *
117
+ * ```
118
+ * client.on("inlineQuery", (ctx) => {
119
+ * const { from, query } = ctx.inlineQuery;
120
+ * console.log("User", from.id, "sent an inline query:", query);
121
+ * });
122
+ * ```
61
123
  * @unlisted
62
124
  */
63
125
  export interface UpdateInlineQuery {
@@ -65,15 +127,15 @@ export interface UpdateInlineQuery {
65
127
  inlineQuery: InlineQuery;
66
128
  }
67
129
  /**
68
- * An inline result was chosen. Bot-only.
130
+ * An inline query result was chosen. Bot-only.
69
131
  * @unlisted
70
132
  */
71
133
  export interface UpdateChosenInlineResult {
72
- /** The chosen inline result. */
134
+ /** The chosen inline query result. */
73
135
  chosenInlineResult: ChosenInlineResult;
74
136
  }
75
137
  /**
76
- * A new chat was added. User-only.
138
+ * A new chat was added to the chat list. User-only.
77
139
  * @unlisted
78
140
  */
79
141
  export interface UpdateNewChat {
@@ -81,14 +143,14 @@ export interface UpdateNewChat {
81
143
  newChat: Chat;
82
144
  }
83
145
  /**
84
- * A chat was deleted. User-only.
146
+ * A chat in the chat list was edited. User-only.
85
147
  * @unlisted
86
148
  */
87
149
  export interface UpdateEditedChat {
88
150
  editedChat: Chat;
89
151
  }
90
152
  /**
91
- * A chat was deleted. User-only.
153
+ * A chat was removed from the chat list. User-only.
92
154
  * @unlisted
93
155
  */
94
156
  export interface UpdateDeletedChat {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mtkruto/node",
3
- "version": "0.1.135",
3
+ "version": "0.1.138",
4
4
  "description": "MTKruto for Node.js",
5
5
  "author": "Roj <rojvv@icloud.com>",
6
6
  "repository": {
@@ -20,7 +20,6 @@
20
20
  "test": "node test_runner.js"
21
21
  },
22
22
  "dependencies": {
23
- "async-mutex": "0.4.0",
24
23
  "object-inspect": "1.12.3",
25
24
  "@deno/shim-deno": "~0.17.0",
26
25
  "@deno/shim-crypto": "~0.3.1",
@@ -5,7 +5,7 @@ export { contentType } from "./deps/deno.land/std@0.210.0/media_types/content_ty
5
5
  export declare function extension(mimeType: string): string;
6
6
  export { ctr256, factorize, ige256Decrypt, ige256Encrypt, init as initTgCrypto } from "./deps/deno.land/x/tgcrypto@0.3.3/mod.js";
7
7
  export { gunzip, gzip } from "./deps/raw.githubusercontent.com/MTKruto/compress/main/gzip/gzip.js";
8
- export { Mutex, type MutexInterface } from "async-mutex";
8
+ export { Mutex } from "./deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js";
9
9
  export { Parser } from "./deps/deno.land/x/html_parser@v0.1.3/src/mod.js";
10
10
  import { debug as debug_ } from "./deps/raw.githubusercontent.com/MTKruto/debug/main/mod.js";
11
11
  export declare const debug: typeof debug_;
package/script/0_deps.js CHANGED
@@ -53,10 +53,10 @@ Object.defineProperty(exports, "initTgCrypto", { enumerable: true, get: function
53
53
  var gzip_js_1 = require("./deps/raw.githubusercontent.com/MTKruto/compress/main/gzip/gzip.js");
54
54
  Object.defineProperty(exports, "gunzip", { enumerable: true, get: function () { return gzip_js_1.gunzip; } });
55
55
  Object.defineProperty(exports, "gzip", { enumerable: true, get: function () { return gzip_js_1.gzip; } });
56
- var async_mutex_1 = require("async-mutex");
57
- Object.defineProperty(exports, "Mutex", { enumerable: true, get: function () { return async_mutex_1.Mutex; } });
58
- var mod_js_2 = require("./deps/deno.land/x/html_parser@v0.1.3/src/mod.js");
59
- Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return mod_js_2.Parser; } });
60
- const mod_js_3 = require("./deps/raw.githubusercontent.com/MTKruto/debug/main/mod.js");
61
- const debug = (v) => (0, mod_js_3.debug)(v);
56
+ var mod_js_2 = require("./deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js");
57
+ Object.defineProperty(exports, "Mutex", { enumerable: true, get: function () { return mod_js_2.Mutex; } });
58
+ var mod_js_3 = require("./deps/deno.land/x/html_parser@v0.1.3/src/mod.js");
59
+ Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return mod_js_3.Parser; } });
60
+ const mod_js_4 = require("./deps/raw.githubusercontent.com/MTKruto/debug/main/mod.js");
61
+ const debug = (v) => (0, mod_js_4.debug)(v);
62
62
  exports.debug = debug;
@@ -4,7 +4,7 @@ export type PublicKeys = readonly [bigint, [bigint, bigint]][];
4
4
  export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const INITIAL_DC: DC;
6
6
  export declare const LAYER = 169;
7
- export declare const APP_VERSION = "MTKruto 0.1.135";
7
+ export declare const APP_VERSION = "MTKruto 0.1.138";
8
8
  export declare const DEVICE_MODEL: string;
9
9
  export declare const LANG_CODE: string;
10
10
  export declare const LANG_PACK = "";
@@ -79,7 +79,7 @@ exports.PUBLIC_KEYS = Object.freeze([
79
79
  ]);
80
80
  exports.INITIAL_DC = "2";
81
81
  exports.LAYER = 169;
82
- exports.APP_VERSION = "MTKruto 0.1.135";
82
+ exports.APP_VERSION = "MTKruto 0.1.138";
83
83
  // @ts-ignore: lib
84
84
  exports.DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
85
85
  exports.LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,5 +1,5 @@
1
1
  import { enums, types } from "../2_tl.js";
2
- import { ChatP, Message, MessageTypes, UpdateMap, User } from "../3_types.js";
2
+ import { ChatP, MessageTypes, UpdateMap, User } from "../3_types.js";
3
3
  export declare const resolve: () => Promise<void>;
4
4
  export type With<T, K extends keyof T> = T & Required<Pick<T, K>>;
5
5
  export declare function isPtsUpdate(v: enums.Update): v is types.UpdateNewMessage | types.UpdateDeleteMessages | types.UpdateReadHistoryInbox | types.UpdateReadHistoryOutbox | types.UpdatePinnedChannelMessages | types.UpdatePinnedMessages | types.UpdateFolderPeers | types.UpdateChannelWebPage | types.UpdateEditMessage | types.UpdateReadMessagesContents | types.UpdateWebPage;
@@ -12,29 +12,44 @@ export declare function getFileContents(source: FileSource, fileName?: string):
12
12
  export declare function isHttpUrl(string: string): boolean;
13
13
  export declare function getUsername(string: string): string;
14
14
  export declare function getChatListId(chatList: string): 0 | 1;
15
- type MessageWith<T extends keyof MessageTypes, F extends number | string | symbol> = F extends keyof MessageTypes[T] ? MessageTypes[T] & {
16
- [P in F]-?: NonNullable<MessageTypes[T][P]>;
17
- } : MessageTypes[T];
18
- type Me<U extends keyof UpdateMap> = U extends "connectionState" | "authorizationState" ? {
19
- me?: User;
20
- } : {
21
- me: User;
22
- };
23
- type Msg<U extends keyof UpdateMap, T extends keyof MessageTypes, F extends number | string | symbol = ""> = U extends "message" | "editedMessage" ? {
24
- msg: MessageFilter<U, T, F>;
15
+ type AnyLevel1 = keyof UpdateMap;
16
+ type GetLevel1Type<L1 extends AnyLevel1> = UpdateMap[L1];
17
+ type GetAnyLevel2<L1 extends AnyLevel1> = L1 extends "message" | "editedMessage" ? keyof MessageTypes : never;
18
+ type AnyLevel2<L1 extends AnyLevel1 = AnyLevel1> = L1 extends unknown ? `${L1 extends "message" | "editedMessage" ? L1 | "" : L1}:${GetAnyLevel2<L1>}` : never;
19
+ type GetLevel2Type<L1 extends string, L2 extends string> = L2 extends keyof MessageTypes ? L1 extends "" ? {
20
+ [P in "message" | "editedMessage"]?: MessageTypes[L2];
21
+ } : L1 extends "message" | "editedMessage" ? {
22
+ [P in L1]: MessageTypes[L2];
23
+ } : never : never;
24
+ type AnyLevelX = AnyLevel1 | AnyLevel2;
25
+ type FilterCore<Q extends AnyLevelX = AnyLevelX> = Q extends AnyLevel1 ? GetLevel1Type<Q> : Q extends `${infer L1}:${infer L2}` ? GetLevel2Type<L1, L2> : 1;
26
+ type Chat<T> = "msg" extends keyof T ? T & {
25
27
  chat: ChatP;
26
- } : {
27
- msg?: Message;
28
- };
29
- type From<U extends keyof UpdateMap> = U extends "callbackQuery" | "inlineQuery" ? {
28
+ } : T;
29
+ type Msg<T> = "message" extends keyof T ? T & {
30
+ msg: NonNullable<T["message"]>;
31
+ } : "editedMessage" extends keyof T ? T & {
32
+ msg: NonNullable<T["editedMessage"]>;
33
+ } : "callbackQuery" extends keyof T ? "message" extends keyof T["callbackQuery"] ? T & {
34
+ msg: T["callbackQuery"]["message"];
35
+ } : T : T;
36
+ type From<T> = "callbackQuery" extends keyof T ? T & {
30
37
  from: User;
31
- } : {
38
+ } : "inlineQuery" extends keyof T ? T & {
39
+ from: User;
40
+ } : "message" extends keyof T ? T & {
41
+ from?: User;
42
+ } : "editedMessage" extends keyof T ? T & {
32
43
  from?: User;
33
- };
34
- type MessageFilter<U extends keyof UpdateMap, T extends keyof MessageTypes, F extends number | string | symbol> = U extends "message" ? {
35
- message: MessageWith<T, F>;
36
- } : U extends "editedMessage" ? {
37
- editedMessage: MessageWith<T, F>;
38
- } : UpdateMap[U];
39
- export type WithUpdate<C, U extends keyof UpdateMap, T extends keyof MessageTypes, F extends number | string | symbol = ""> = C & Me<U> & Msg<U, T, F> & From<U> & MessageFilter<U, T, F>;
44
+ } : T;
45
+ type SenderChat<T> = "message" extends keyof T ? T & {
46
+ senderChat?: ChatP;
47
+ } : "editedMessage" extends keyof T ? T & {
48
+ senderChat?: ChatP;
49
+ } : T;
50
+ type Shortcuts<T> = SenderChat<From<Chat<Msg<T>>>>;
51
+ type Filter<Q extends AnyLevelX> = Shortcuts<FilterCore<Q>>;
52
+ export type FilterQuery = AnyLevelX;
53
+ export type WithFilter<T, Q extends FilterQuery> = T & Filter<Q>;
54
+ export declare function match<Q extends FilterQuery, T extends object>(filter: Q, value: T): boolean;
40
55
  export {};
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getChatListId = exports.getUsername = exports.isHttpUrl = exports.getFileContents = exports.isChannelPtsUpdate = exports.isPtsUpdate = exports.resolve = void 0;
26
+ exports.match = exports.getChatListId = exports.getUsername = exports.isHttpUrl = exports.getFileContents = exports.isChannelPtsUpdate = exports.isPtsUpdate = exports.resolve = void 0;
27
27
  const dntShim = __importStar(require("../_dnt.shims.js"));
28
28
  const _0_deps_js_1 = require("../0_deps.js");
29
29
  const _1_utilities_js_1 = require("../1_utilities.js");
@@ -178,3 +178,31 @@ function getChatListId(chatList) {
178
178
  }
179
179
  }
180
180
  exports.getChatListId = getChatListId;
181
+ function match(filter, value) {
182
+ let [type, ...other] = filter.split(":");
183
+ if (type != "" && !(type in value)) {
184
+ return false;
185
+ }
186
+ if (type == "") {
187
+ if (other.length != 1) {
188
+ return false;
189
+ }
190
+ if ("message" in value) {
191
+ type = "message";
192
+ }
193
+ else if ("editedMessage" in value) {
194
+ type = "editedMessage";
195
+ }
196
+ else {
197
+ return false;
198
+ }
199
+ }
200
+ const field = other[0];
201
+ if (field) {
202
+ if (!(field in value[type])) {
203
+ return false;
204
+ }
205
+ }
206
+ return true;
207
+ }
208
+ exports.match = match;
@@ -1,5 +1,5 @@
1
- import { MessageTypes, Update, UpdateIntersection, UpdateMap, User } from "../3_types.js";
2
- import { WithUpdate } from "./0_utilities.js";
1
+ import { Update, UpdateIntersection, User } from "../3_types.js";
2
+ import { FilterQuery, WithFilter } from "./0_utilities.js";
3
3
  type MaybePromise<T> = T | Promise<T>;
4
4
  export type NextFunction = () => Promise<void>;
5
5
  export type MiddlewareFn<C> = (ctx: C, next: NextFunction) => MaybePromise<unknown>;
@@ -21,10 +21,10 @@ export declare class Composer<C extends {
21
21
  branch(predicate: (ctx: UpdateIntersection<C>) => MaybePromise<boolean>, trueHandler_: Middleware<UpdateIntersection<C>>, falseHandler_: Middleware<UpdateIntersection<C>>): Composer<UpdateIntersection<C>>;
22
22
  filter<D extends C>(predicate: (ctx: UpdateIntersection<C>) => ctx is D, ...middleware: Middleware<D>[]): Composer<D>;
23
23
  filter(predicate: (ctx: UpdateIntersection<C>) => MaybePromise<boolean>, ...middleware: Middleware<UpdateIntersection<C>>[]): Composer<C>;
24
- on<T extends keyof UpdateMap, F extends string, K extends keyof MessageTypes>(filter: T extends "message" | "editedMessage" ? T | [T, K, ...F[]] : T, ...middleawre: Middleware<WithUpdate<C, T, K, F>>[]): Composer<WithUpdate<C, T, K, F>>;
24
+ on<Q extends FilterQuery>(filter: Q, ...middleawre: Middleware<WithFilter<C, Q>>[]): Composer<UpdateIntersection<WithFilter<C, Q>>>;
25
25
  command(commands: string | RegExp | (string | RegExp)[] | {
26
26
  names: string | RegExp | (string | RegExp)[];
27
27
  prefixes: string | string[];
28
- }, ...middleawre: Middleware<WithUpdate<C, "message", "text">>[]): Composer<WithUpdate<C, "message", "text", string>>;
28
+ }, ...middleawre: Middleware<WithFilter<C, "message:text">>[]): Composer<UpdateIntersection<WithFilter<C, "message:text">>>;
29
29
  }
30
30
  export {};
@@ -13,7 +13,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
13
13
  var _Composer_handle, _Composer_prefixes;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Composer = exports.skip = exports.concat = exports.flatten = void 0;
16
- const _3_types_js_1 = require("../3_types.js");
16
+ const _0_utilities_js_1 = require("./0_utilities.js");
17
17
  function flatten(mw) {
18
18
  return typeof mw === "function" ? mw : (ctx, next) => mw.middleware()(ctx, next);
19
19
  }
@@ -75,34 +75,8 @@ class Composer {
75
75
  return composer;
76
76
  }
77
77
  on(filter, ...middleawre) {
78
- const type = typeof filter === "string" ? filter : filter[0];
79
- let keys = Array.isArray(filter) ? filter.slice(1) : [];
80
- let messageType = null;
81
- if (type == "message") {
82
- messageType = keys[0];
83
- keys = keys.slice(1);
84
- }
85
78
  return this.filter((ctx) => {
86
- if (type in ctx) {
87
- if (messageType != null) {
88
- // deno-lint-ignore ban-ts-comment
89
- // @ts-ignore
90
- (0, _3_types_js_1.assertMessageType)(ctx[type], messageType);
91
- }
92
- if (keys.length > 0) {
93
- for (const key of keys) {
94
- // deno-lint-ignore ban-ts-comment
95
- // @ts-ignore
96
- if (!(key in ctx[type])) {
97
- return false;
98
- }
99
- }
100
- }
101
- return true;
102
- }
103
- else {
104
- return false;
105
- }
79
+ return (0, _0_utilities_js_1.match)(filter, ctx);
106
80
  }, ...middleawre);
107
81
  }
108
82
  command(commands, ...middleawre) {
@@ -120,7 +94,7 @@ class Composer {
120
94
  }
121
95
  }
122
96
  }
123
- return this.on(["message", "text"]).filter((ctx) => {
97
+ return this.on("message:text").filter((ctx) => {
124
98
  const prefixes_ = prefixes.length == 0 ? [!ctx.me?.isBot ? "\\" : "/"] : prefixes;
125
99
  if (prefixes_.length == 0) {
126
100
  return false;
@@ -2,10 +2,10 @@ import { MaybePromise } from "../1_utilities.js";
2
2
  import { functions, ReadObject, types } from "../2_tl.js";
3
3
  import { Storage } from "../3_storage.js";
4
4
  import { DC } from "../3_transport.js";
5
- import { BotCommand, Chat, ChatAction, ChatID, ChatP, Document, InlineQueryResult, Message, MessageAnimation, MessageAudio, MessageContact, MessageDice, MessageDocument, MessageLocation, MessagePhoto, MessagePoll, MessageText, MessageTypes, MessageVenue, MessageVideo, MessageVideoNote, MessageVoice, NetworkStatistics, ParseMode, Reaction, Update, UpdateIntersection, UpdateMap, User } from "../3_types.js";
5
+ import { BotCommand, Chat, ChatAction, ChatID, ChatP, Document, InlineQueryResult, Message, MessageAnimation, MessageAudio, MessageContact, MessageDice, MessageDocument, MessageLocation, MessagePhoto, MessagePoll, MessageText, MessageVenue, MessageVideo, MessageVideoNote, MessageVoice, NetworkStatistics, ParseMode, Reaction, Update, UpdateIntersection, User } from "../3_types.js";
6
6
  import { Migrate } from "../4_errors.js";
7
7
  import { ClientAbstract } from "./0_client_abstract.js";
8
- import { FileSource, WithUpdate } from "./0_utilities.js";
8
+ import { FileSource, FilterQuery, WithFilter } from "./0_utilities.js";
9
9
  import { Composer, Middleware } from "./1_composer.js";
10
10
  import { AddReactionParams, AnswerCallbackQueryParams, AnswerInlineQueryParams, AuthorizeUserParams, ClientParams, DeleteMessageParams, DeleteMessagesParams, DownloadParams, EditMessageParams, ForwardMessagesParams, GetChatsParams, GetHistoryParams, GetMyCommandsParams, ReplyParams, SendAnimationParams, SendAudioParams, SendContactParams, SendDiceParams, SendDocumentParams, SendLocationParams, SendMessageParams, SendPhotoParams, SendPollParams, SendVenueParams, SendVideoNoteParams, SendVideoParams, SendVoiceParams, SetMyCommandsParams, SetReactionsParams, UploadParams } from "./3_params.js";
11
11
  export type NextFn<T = void> = () => Promise<T>;
@@ -339,11 +339,11 @@ export declare class Client<C extends Context = Context> extends ClientAbstract
339
339
  branch(predicate: (ctx: UpdateIntersection<C>) => MaybePromise<boolean>, trueHandler_: Middleware<UpdateIntersection<C>>, falseHandler_: Middleware<UpdateIntersection<C>>): Composer<UpdateIntersection<C>>;
340
340
  filter<D extends C>(predicate: (ctx: UpdateIntersection<C>) => ctx is D, ...middleware: Middleware<D>[]): Composer<D>;
341
341
  filter(predicate: (ctx: UpdateIntersection<C>) => MaybePromise<boolean>, ...middleware: Middleware<UpdateIntersection<C>>[]): Composer<C>;
342
- on<T extends keyof UpdateMap, F extends string, K extends keyof MessageTypes>(filter: T extends "message" | "editedMessage" ? T | [T, K, ...F[]] : T, ...middleawre: Middleware<WithUpdate<C, T, K, F>>[]): Composer<WithUpdate<C, T, K, F>>;
342
+ on<Q extends FilterQuery>(filter: Q, ...middleawre: Middleware<WithFilter<C, Q>>[]): Composer<UpdateIntersection<WithFilter<C, Q>>>;
343
343
  command(commands: string | RegExp | (string | RegExp)[] | {
344
344
  names: string | RegExp | (string | RegExp)[];
345
345
  prefixes: string | string[];
346
- }, ...middleawre: Middleware<WithUpdate<C, "message", "text">>[]): Composer<WithUpdate<C, "message", "text", string>>;
346
+ }, ...middleawre: Middleware<WithFilter<C, "message:text">>[]): Composer<UpdateIntersection<WithFilter<C, "message:text">>>;
347
347
  /**
348
348
  * Set the bot's description in the given language. Bot-only.
349
349
  *
@@ -2236,34 +2236,8 @@ class Client extends _0_client_abstract_js_1.ClientAbstract {
2236
2236
  return composer;
2237
2237
  }
2238
2238
  on(filter, ...middleawre) {
2239
- const type = typeof filter === "string" ? filter : filter[0];
2240
- let keys = Array.isArray(filter) ? filter.slice(1) : [];
2241
- let messageType = null;
2242
- if (type == "message") {
2243
- messageType = keys[0];
2244
- keys = keys.slice(1);
2245
- }
2246
2239
  return this.filter((ctx) => {
2247
- if (type in ctx) {
2248
- if (messageType != null) {
2249
- // deno-lint-ignore ban-ts-comment
2250
- // @ts-ignore
2251
- (0, _3_types_js_1.assertMessageType)(ctx[type], messageType);
2252
- }
2253
- if (keys.length > 0) {
2254
- for (const key of keys) {
2255
- // deno-lint-ignore ban-ts-comment
2256
- // @ts-ignore
2257
- if (!(key in ctx[type])) {
2258
- return false;
2259
- }
2260
- }
2261
- }
2262
- return true;
2263
- }
2264
- else {
2265
- return false;
2266
- }
2240
+ return (0, _0_utilities_js_1.match)(filter, ctx);
2267
2241
  }, ...middleawre);
2268
2242
  }
2269
2243
  command(commands, ...middleawre) {
@@ -2281,7 +2255,7 @@ class Client extends _0_client_abstract_js_1.ClientAbstract {
2281
2255
  }
2282
2256
  }
2283
2257
  }
2284
- return this.on(["message", "text"]).filter((ctx) => {
2258
+ return this.on("message:text").filter((ctx) => {
2285
2259
  const prefixes_ = prefixes.length == 0 ? [!ctx.me?.isBot ? "\\" : "/"] : prefixes;
2286
2260
  if (prefixes_.length == 0) {
2287
2261
  return false;
@@ -0,0 +1,2 @@
1
+ export * from "./mutex.js";
2
+ export * from "./semaphore.js";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./mutex.js"), exports);
18
+ __exportStar(require("./semaphore.js"), exports);
@@ -0,0 +1,26 @@
1
+ export interface MutexInterface {
2
+ acquire(): Promise<MutexInterface.Releaser>;
3
+ runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>;
4
+ waitForUnlock(): Promise<void>;
5
+ isLocked(): boolean;
6
+ release(): void;
7
+ cancel(): void;
8
+ }
9
+ export declare namespace MutexInterface {
10
+ interface Releaser {
11
+ (): void;
12
+ }
13
+ interface Worker<T> {
14
+ (): Promise<T> | T;
15
+ }
16
+ }
17
+ export declare class Mutex implements MutexInterface {
18
+ constructor(cancelError?: Error);
19
+ acquire(): Promise<MutexInterface.Releaser>;
20
+ runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>;
21
+ isLocked(): boolean;
22
+ waitForUnlock(): Promise<void>;
23
+ release(): void;
24
+ cancel(): void;
25
+ private _semaphore;
26
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Mutex = void 0;
4
+ const semaphore_js_1 = require("./semaphore.js");
5
+ class Mutex {
6
+ constructor(cancelError) {
7
+ Object.defineProperty(this, "_semaphore", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ this._semaphore = new semaphore_js_1.Semaphore(1, cancelError);
14
+ }
15
+ async acquire() {
16
+ const [, releaser] = await this._semaphore.acquire();
17
+ return releaser;
18
+ }
19
+ runExclusive(callback) {
20
+ return this._semaphore.runExclusive(() => callback());
21
+ }
22
+ isLocked() {
23
+ return this._semaphore.isLocked();
24
+ }
25
+ waitForUnlock() {
26
+ return this._semaphore.waitForUnlock();
27
+ }
28
+ release() {
29
+ if (this._semaphore.isLocked())
30
+ this._semaphore.release();
31
+ }
32
+ cancel() {
33
+ return this._semaphore.cancel();
34
+ }
35
+ }
36
+ exports.Mutex = Mutex;
@@ -0,0 +1,41 @@
1
+ export declare const E_CANCELED: Error;
2
+ export interface SemaphoreInterface {
3
+ acquire(weight?: number): Promise<[number, SemaphoreInterface.Releaser]>;
4
+ runExclusive<T>(callback: SemaphoreInterface.Worker<T>, weight?: number): Promise<T>;
5
+ waitForUnlock(weight?: number): Promise<void>;
6
+ isLocked(): boolean;
7
+ getValue(): number;
8
+ setValue(value: number): void;
9
+ release(weight?: number): void;
10
+ cancel(): void;
11
+ }
12
+ export declare namespace SemaphoreInterface {
13
+ interface Releaser {
14
+ (): void;
15
+ }
16
+ interface Worker<T> {
17
+ (value: number): Promise<T> | T;
18
+ }
19
+ }
20
+ export interface QueueEntry {
21
+ resolve(result: [number, SemaphoreInterface.Releaser]): void;
22
+ reject(error: unknown): void;
23
+ }
24
+ export declare class Semaphore implements SemaphoreInterface {
25
+ private _value;
26
+ private _cancelError;
27
+ constructor(_value: number, _cancelError?: Error);
28
+ acquire(weight?: number): Promise<[number, SemaphoreInterface.Releaser]>;
29
+ runExclusive<T>(callback: SemaphoreInterface.Worker<T>, weight?: number): Promise<T>;
30
+ waitForUnlock(weight?: number): Promise<void>;
31
+ isLocked(): boolean;
32
+ getValue(): number;
33
+ setValue(value: number): void;
34
+ release(weight?: number): void;
35
+ cancel(): void;
36
+ private _dispatch;
37
+ private _newReleaser;
38
+ private _drainUnlockWaiters;
39
+ private _weightedQueues;
40
+ private _weightedWaiters;
41
+ }