@mtcute/dispatcher 0.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.
Files changed (190) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +26 -0
  3. package/cjs/callback-data-builder.d.ts +43 -0
  4. package/cjs/callback-data-builder.js +99 -0
  5. package/cjs/callback-data-builder.js.map +1 -0
  6. package/cjs/context/base.d.ts +8 -0
  7. package/cjs/context/base.js +3 -0
  8. package/cjs/context/base.js.map +1 -0
  9. package/cjs/context/callback-query.d.ts +27 -0
  10. package/cjs/context/callback-query.js +56 -0
  11. package/cjs/context/callback-query.js.map +1 -0
  12. package/cjs/context/chat-join-request.d.ts +16 -0
  13. package/cjs/context/chat-join-request.js +34 -0
  14. package/cjs/context/chat-join-request.js.map +1 -0
  15. package/cjs/context/chosen-inline-result.d.ts +21 -0
  16. package/cjs/context/chosen-inline-result.js +35 -0
  17. package/cjs/context/chosen-inline-result.js.map +1 -0
  18. package/cjs/context/index.d.ts +8 -0
  19. package/cjs/context/index.js +24 -0
  20. package/cjs/context/index.js.map +1 -0
  21. package/cjs/context/inline-query.d.ts +14 -0
  22. package/cjs/context/inline-query.js +22 -0
  23. package/cjs/context/inline-query.js.map +1 -0
  24. package/cjs/context/message.d.ts +72 -0
  25. package/cjs/context/message.js +142 -0
  26. package/cjs/context/message.js.map +1 -0
  27. package/cjs/context/parse.d.ts +1 -0
  28. package/cjs/context/parse.js +34 -0
  29. package/cjs/context/parse.js.map +1 -0
  30. package/cjs/context/pre-checkout-query.d.ts +16 -0
  31. package/cjs/context/pre-checkout-query.js +26 -0
  32. package/cjs/context/pre-checkout-query.js.map +1 -0
  33. package/cjs/dispatcher.d.ts +641 -0
  34. package/cjs/dispatcher.js +765 -0
  35. package/cjs/dispatcher.js.map +1 -0
  36. package/cjs/filters/bots.d.ts +70 -0
  37. package/cjs/filters/bots.js +129 -0
  38. package/cjs/filters/bots.js.map +1 -0
  39. package/cjs/filters/bundle.d.ts +10 -0
  40. package/cjs/filters/bundle.js +27 -0
  41. package/cjs/filters/bundle.js.map +1 -0
  42. package/cjs/filters/chat.d.ts +27 -0
  43. package/cjs/filters/chat.js +55 -0
  44. package/cjs/filters/chat.js.map +1 -0
  45. package/cjs/filters/group.d.ts +25 -0
  46. package/cjs/filters/group.js +72 -0
  47. package/cjs/filters/group.js.map +1 -0
  48. package/cjs/filters/index.d.ts +3 -0
  49. package/cjs/filters/index.js +29 -0
  50. package/cjs/filters/index.js.map +1 -0
  51. package/cjs/filters/logic.d.ts +29 -0
  52. package/cjs/filters/logic.js +114 -0
  53. package/cjs/filters/logic.js.map +1 -0
  54. package/cjs/filters/message.d.ts +295 -0
  55. package/cjs/filters/message.js +150 -0
  56. package/cjs/filters/message.js.map +1 -0
  57. package/cjs/filters/state.d.ts +15 -0
  58. package/cjs/filters/state.js +32 -0
  59. package/cjs/filters/state.js.map +1 -0
  60. package/cjs/filters/text.d.ts +64 -0
  61. package/cjs/filters/text.js +132 -0
  62. package/cjs/filters/text.js.map +1 -0
  63. package/cjs/filters/types.d.ts +91 -0
  64. package/cjs/filters/types.js +6 -0
  65. package/cjs/filters/types.js.map +1 -0
  66. package/cjs/filters/updates.d.ts +46 -0
  67. package/cjs/filters/updates.js +46 -0
  68. package/cjs/filters/updates.js.map +1 -0
  69. package/cjs/filters/user.d.ts +24 -0
  70. package/cjs/filters/user.js +76 -0
  71. package/cjs/filters/user.js.map +1 -0
  72. package/cjs/handler.d.ts +31 -0
  73. package/cjs/handler.js +4 -0
  74. package/cjs/handler.js.map +1 -0
  75. package/cjs/index.d.ts +8 -0
  76. package/cjs/index.js +25 -0
  77. package/cjs/index.js.map +1 -0
  78. package/cjs/package.json +3 -0
  79. package/cjs/propagation.d.ts +21 -0
  80. package/cjs/propagation.js +26 -0
  81. package/cjs/propagation.js.map +1 -0
  82. package/cjs/state/index.d.ts +3 -0
  83. package/cjs/state/index.js +20 -0
  84. package/cjs/state/index.js.map +1 -0
  85. package/cjs/state/key.d.ts +23 -0
  86. package/cjs/state/key.js +45 -0
  87. package/cjs/state/key.js.map +1 -0
  88. package/cjs/state/storage.d.ts +75 -0
  89. package/cjs/state/storage.js +17 -0
  90. package/cjs/state/storage.js.map +1 -0
  91. package/cjs/state/update-state.d.ts +151 -0
  92. package/cjs/state/update-state.js +211 -0
  93. package/cjs/state/update-state.js.map +1 -0
  94. package/cjs/wizard.d.ts +60 -0
  95. package/cjs/wizard.js +103 -0
  96. package/cjs/wizard.js.map +1 -0
  97. package/esm/callback-data-builder.d.ts +43 -0
  98. package/esm/callback-data-builder.js +95 -0
  99. package/esm/callback-data-builder.js.map +1 -0
  100. package/esm/context/base.d.ts +8 -0
  101. package/esm/context/base.js +2 -0
  102. package/esm/context/base.js.map +1 -0
  103. package/esm/context/callback-query.d.ts +27 -0
  104. package/esm/context/callback-query.js +52 -0
  105. package/esm/context/callback-query.js.map +1 -0
  106. package/esm/context/chat-join-request.d.ts +16 -0
  107. package/esm/context/chat-join-request.js +30 -0
  108. package/esm/context/chat-join-request.js.map +1 -0
  109. package/esm/context/chosen-inline-result.d.ts +21 -0
  110. package/esm/context/chosen-inline-result.js +31 -0
  111. package/esm/context/chosen-inline-result.js.map +1 -0
  112. package/esm/context/index.d.ts +8 -0
  113. package/esm/context/index.js +8 -0
  114. package/esm/context/index.js.map +1 -0
  115. package/esm/context/inline-query.d.ts +14 -0
  116. package/esm/context/inline-query.js +18 -0
  117. package/esm/context/inline-query.js.map +1 -0
  118. package/esm/context/message.d.ts +72 -0
  119. package/esm/context/message.js +138 -0
  120. package/esm/context/message.js.map +1 -0
  121. package/esm/context/parse.d.ts +1 -0
  122. package/esm/context/parse.js +30 -0
  123. package/esm/context/parse.js.map +1 -0
  124. package/esm/context/pre-checkout-query.d.ts +16 -0
  125. package/esm/context/pre-checkout-query.js +22 -0
  126. package/esm/context/pre-checkout-query.js.map +1 -0
  127. package/esm/dispatcher.d.ts +641 -0
  128. package/esm/dispatcher.js +761 -0
  129. package/esm/dispatcher.js.map +1 -0
  130. package/esm/filters/bots.d.ts +70 -0
  131. package/esm/filters/bots.js +125 -0
  132. package/esm/filters/bots.js.map +1 -0
  133. package/esm/filters/bundle.d.ts +10 -0
  134. package/esm/filters/bundle.js +11 -0
  135. package/esm/filters/bundle.js.map +1 -0
  136. package/esm/filters/chat.d.ts +27 -0
  137. package/esm/filters/chat.js +50 -0
  138. package/esm/filters/chat.js.map +1 -0
  139. package/esm/filters/group.d.ts +25 -0
  140. package/esm/filters/group.js +67 -0
  141. package/esm/filters/group.js.map +1 -0
  142. package/esm/filters/index.d.ts +3 -0
  143. package/esm/filters/index.js +3 -0
  144. package/esm/filters/index.js.map +1 -0
  145. package/esm/filters/logic.d.ts +29 -0
  146. package/esm/filters/logic.js +107 -0
  147. package/esm/filters/logic.js.map +1 -0
  148. package/esm/filters/message.d.ts +295 -0
  149. package/esm/filters/message.js +130 -0
  150. package/esm/filters/message.js.map +1 -0
  151. package/esm/filters/state.d.ts +15 -0
  152. package/esm/filters/state.js +27 -0
  153. package/esm/filters/state.js.map +1 -0
  154. package/esm/filters/text.d.ts +64 -0
  155. package/esm/filters/text.js +124 -0
  156. package/esm/filters/text.js.map +1 -0
  157. package/esm/filters/types.d.ts +91 -0
  158. package/esm/filters/types.js +5 -0
  159. package/esm/filters/types.js.map +1 -0
  160. package/esm/filters/updates.d.ts +46 -0
  161. package/esm/filters/updates.js +39 -0
  162. package/esm/filters/updates.js.map +1 -0
  163. package/esm/filters/user.d.ts +24 -0
  164. package/esm/filters/user.js +70 -0
  165. package/esm/filters/user.js.map +1 -0
  166. package/esm/handler.d.ts +31 -0
  167. package/esm/handler.js +3 -0
  168. package/esm/handler.js.map +1 -0
  169. package/esm/index.d.ts +8 -0
  170. package/esm/index.js +9 -0
  171. package/esm/index.js.map +1 -0
  172. package/esm/propagation.d.ts +21 -0
  173. package/esm/propagation.js +23 -0
  174. package/esm/propagation.js.map +1 -0
  175. package/esm/state/index.d.ts +3 -0
  176. package/esm/state/index.js +4 -0
  177. package/esm/state/index.js.map +1 -0
  178. package/esm/state/key.d.ts +23 -0
  179. package/esm/state/key.js +41 -0
  180. package/esm/state/key.js.map +1 -0
  181. package/esm/state/storage.d.ts +75 -0
  182. package/esm/state/storage.js +13 -0
  183. package/esm/state/storage.js.map +1 -0
  184. package/esm/state/update-state.d.ts +151 -0
  185. package/esm/state/update-state.js +206 -0
  186. package/esm/state/update-state.js.map +1 -0
  187. package/esm/wizard.d.ts +60 -0
  188. package/esm/wizard.js +99 -0
  189. package/esm/wizard.js.map +1 -0
  190. package/package.json +21 -0
@@ -0,0 +1,29 @@
1
+ import { ExtractBaseMany, ExtractMod, Invert, UnionToIntersection, UpdateFilter } from './types.js';
2
+ /**
3
+ * Filter that matches any update
4
+ */
5
+ export declare const any: UpdateFilter<any>;
6
+ /**
7
+ * Invert a filter by applying a NOT logical operation:
8
+ * `not(fn) = NOT fn`
9
+ *
10
+ * > **Note**: This also inverts type modification, i.e.
11
+ * > if the base is `{ field: string | number | null }`
12
+ * > and the modification is `{ field: string }`,
13
+ * > then the negated filter will have
14
+ * > inverted modification `{ field: number | null }`
15
+ *
16
+ * @param fn Filter to negate
17
+ */
18
+ export declare function not<Base, Mod, State extends object>(fn: UpdateFilter<Base, Mod, State>): UpdateFilter<Base, Invert<Base, Mod>, State>;
19
+ export declare function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>): UpdateFilter<Base1 & Base2, Mod1 & Mod2, State1 | State2>;
20
+ export declare function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>): UpdateFilter<Base1 & Base2 & Base3, Mod1 & Mod2 & Mod3, State1 | State2 | State3>;
21
+ export declare function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>): UpdateFilter<Base1 & Base2 & Base3 & Base4, Mod1 & Mod2 & Mod3 & Mod4, State1 | State2 | State3 | State4>;
22
+ export declare function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object, Base5, Mod5, State5 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>, fn5: UpdateFilter<Base5, Mod5, State5>): UpdateFilter<Base1 & Base2 & Base3 & Base4 & Base5, Mod1 & Mod2 & Mod3 & Mod4 & Mod5, State1 | State2 | State3 | State4 | State5>;
23
+ export declare function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object, Base5, Mod5, State5 extends object, Base6, Mod6, State6 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>, fn5: UpdateFilter<Base5, Mod5, State5>, fn6: UpdateFilter<Base6, Mod6, State6>): UpdateFilter<Base1 & Base2 & Base3 & Base4 & Base5 & Base6, Mod1 & Mod2 & Mod3 & Mod4 & Mod5 & Mod6, State1 | State2 | State3 | State4 | State5 | State6>;
24
+ export declare function and<Filters extends UpdateFilter<any, any>[]>(...fns: Filters): UpdateFilter<ExtractBaseMany<Filters>, UnionToIntersection<ExtractMod<Filters[number]>>>;
25
+ export declare function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>): UpdateFilter<Base1 & Base2, Mod1 | Mod2, State1 | State2>;
26
+ export declare function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>): UpdateFilter<Base1 & Base2 & Base3, Mod1 | Mod2 | Mod3, State1 | State2 | State3>;
27
+ export declare function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>): UpdateFilter<Base1 & Base2 & Base3 & Base4, Mod1 | Mod2 | Mod3 | Mod4, State1 | State2 | State3 | State4>;
28
+ export declare function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object, Base5, Mod5, State5 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>, fn5: UpdateFilter<Base5, Mod5, State5>): UpdateFilter<Base1 & Base2 & Base3 & Base4 & Base5, Mod1 | Mod2 | Mod3 | Mod4 | Mod5, State1 | State2 | State3 | State4 | State5>;
29
+ export declare function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object, Base3, Mod3, State3 extends object, Base4, Mod4, State4 extends object, Base5, Mod5, State5 extends object, Base6, Mod6, State6 extends object>(fn1: UpdateFilter<Base1, Mod1, State1>, fn2: UpdateFilter<Base2, Mod2, State2>, fn3: UpdateFilter<Base3, Mod3, State3>, fn4: UpdateFilter<Base4, Mod4, State4>, fn5: UpdateFilter<Base5, Mod5, State5>, fn6: UpdateFilter<Base6, Mod6, State6>): UpdateFilter<Base1 & Base2 & Base3 & Base4 & Base5 & Base6, Mod1 | Mod2 | Mod3 | Mod4 | Mod5 | Mod6, State1 | State2 | State3 | State4 | State5 | State6>;
@@ -0,0 +1,107 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ // ^^ will be looked into in MTQ-29
3
+ /**
4
+ * Filter that matches any update
5
+ */
6
+ export const any = () => true;
7
+ /**
8
+ * Invert a filter by applying a NOT logical operation:
9
+ * `not(fn) = NOT fn`
10
+ *
11
+ * > **Note**: This also inverts type modification, i.e.
12
+ * > if the base is `{ field: string | number | null }`
13
+ * > and the modification is `{ field: string }`,
14
+ * > then the negated filter will have
15
+ * > inverted modification `{ field: number | null }`
16
+ *
17
+ * @param fn Filter to negate
18
+ */
19
+ export function not(fn) {
20
+ return (upd, state) => {
21
+ const res = fn(upd, state);
22
+ if (typeof res === 'boolean')
23
+ return !res;
24
+ return res.then((r) => !r);
25
+ };
26
+ }
27
+ /**
28
+ * Combine multiple filters by applying an AND logical
29
+ * operation between every one of them:
30
+ * `and(fn1, fn2, ..., fnN) = fn1 AND fn2 AND ... AND fnN`
31
+ *
32
+ * > **Note**: This also combines type modifications, i.e.
33
+ * > if the 1st has modification `{ field1: string }`
34
+ * > and the 2nd has modification `{ field2: number }`,
35
+ * > then the combined filter will have
36
+ * > combined modification `{ field1: string, field2: number }`
37
+ *
38
+ * > **Note**: Due to TypeScript limitations (or more likely my lack of brain cells),
39
+ * > state type is only correctly inferred for up to 6 filters.
40
+ * > If you need more, either provide type explicitly (e.g. `filters.state<SomeState>(...)`),
41
+ * > or combine multiple `and` calls.
42
+ *
43
+ * @param fns Filters to combine
44
+ */
45
+ export function and(...fns) {
46
+ return (upd, state) => {
47
+ let i = 0;
48
+ const max = fns.length;
49
+ const next = () => {
50
+ if (i === max)
51
+ return true;
52
+ const res = fns[i++](upd, state);
53
+ if (typeof res === 'boolean') {
54
+ if (!res)
55
+ return false;
56
+ return next();
57
+ }
58
+ return res.then((r) => {
59
+ if (!r)
60
+ return false;
61
+ return next();
62
+ });
63
+ };
64
+ return next();
65
+ };
66
+ }
67
+ /**
68
+ * Combine multiple filters by applying an OR logical
69
+ * operation between every one of them:
70
+ * `or(fn1, fn2, ..., fnN) = fn1 OR fn2 OR ... OR fnN`
71
+ *
72
+ * > **Note**: This also combines type modifications in a union, i.e.
73
+ * > if the 1st has modification `{ field1: string }`
74
+ * > and the 2nd has modification `{ field2: number }`,
75
+ * > then the combined filter will have
76
+ * > modification `{ field1: string } | { field2: number }`.
77
+ *
78
+ * > **Note**: Due to TypeScript limitations (or more likely my lack of brain cells),
79
+ * > state type is only correctly inferred for up to 6 filters.
80
+ * > If you need more, either provide type explicitly (e.g. `filters.state<SomeState>(...)`),
81
+ * > or combine multiple `and` calls.
82
+ *
83
+ * @param fns Filters to combine
84
+ */
85
+ export function or(...fns) {
86
+ return (upd, state) => {
87
+ let i = 0;
88
+ const max = fns.length;
89
+ const next = () => {
90
+ if (i === max)
91
+ return false;
92
+ const res = fns[i++](upd, state);
93
+ if (typeof res === 'boolean') {
94
+ if (res)
95
+ return true;
96
+ return next();
97
+ }
98
+ return res.then((r) => {
99
+ if (r)
100
+ return true;
101
+ return next();
102
+ });
103
+ };
104
+ return next();
105
+ };
106
+ }
107
+ //# sourceMappingURL=logic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logic.js","sourceRoot":"","sources":["../../../src/filters/logic.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,mCAAmC;AAMnC;;GAEG;AACH,MAAM,CAAC,MAAM,GAAG,GAAsB,GAAG,EAAE,CAAC,IAAI,CAAA;AAEhD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,GAAG,CACf,EAAkC;IAElC,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;QAClB,MAAM,GAAG,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QAE1B,IAAI,OAAO,GAAG,KAAK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B,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;YAEhC,IAAI,OAAO,GAAG,KAAK,SAAS,EAAE;gBAC1B,IAAI,GAAG;oBAAE,OAAO,IAAI,CAAA;gBAEpB,OAAO,IAAI,EAAE,CAAA;aAChB;YAED,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAU,EAAE,EAAE;gBAC3B,IAAI,CAAC;oBAAE,OAAO,IAAI,CAAA;gBAElB,OAAO,IAAI,EAAE,CAAA;YACjB,CAAC,CAAC,CAAA;QACN,CAAC,CAAA;QAED,OAAO,IAAI,EAAE,CAAA;IACjB,CAAC,CAAA;AACL,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\n\nimport { MaybeAsync } from '@mtcute/client'\n\nimport { ExtractBaseMany, ExtractMod, Invert, UnionToIntersection, UpdateFilter } from './types.js'\n\n/**\n * Filter that matches any update\n */\nexport const any: UpdateFilter<any> = () => true\n\n/**\n * Invert a filter by applying a NOT logical operation:\n * `not(fn) = NOT fn`\n *\n * > **Note**: This also inverts type modification, i.e.\n * > if the base is `{ field: string | number | null }`\n * > and the modification is `{ field: string }`,\n * > then the negated filter will have\n * > inverted modification `{ field: number | null }`\n *\n * @param fn Filter to negate\n */\nexport function not<Base, Mod, State extends object>(\n fn: UpdateFilter<Base, Mod, State>,\n): UpdateFilter<Base, Invert<Base, Mod>, State> {\n return (upd, state) => {\n const res = fn(upd, state)\n\n if (typeof res === 'boolean') return !res\n\n return res.then((r) => !r)\n }\n}\n\n// i couldn't come up with proper types for these 😭\n// if you know how to do this better - PRs are welcome!\n\nexport function and<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n): UpdateFilter<Base1 & Base2, Mod1 & Mod2, State1 | State2>\nexport function and<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n): UpdateFilter<Base1 & Base2 & Base3, Mod1 & Mod2 & Mod3, State1 | State2 | State3>\nexport function and<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n): UpdateFilter<Base1 & Base2 & Base3 & Base4, Mod1 & Mod2 & Mod3 & Mod4, State1 | State2 | State3 | State4>\nexport function and<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n Base5,\n Mod5,\n State5 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n fn5: UpdateFilter<Base5, Mod5, State5>,\n): UpdateFilter<\n Base1 & Base2 & Base3 & Base4 & Base5,\n Mod1 & Mod2 & Mod3 & Mod4 & Mod5,\n State1 | State2 | State3 | State4 | State5\n>\nexport function and<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n Base5,\n Mod5,\n State5 extends object,\n Base6,\n Mod6,\n State6 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n fn5: UpdateFilter<Base5, Mod5, State5>,\n fn6: UpdateFilter<Base6, Mod6, State6>,\n): UpdateFilter<\n Base1 & Base2 & Base3 & Base4 & Base5 & Base6,\n Mod1 & Mod2 & Mod3 & Mod4 & Mod5 & Mod6,\n State1 | State2 | State3 | State4 | State5 | State6\n>\nexport function and<Filters extends UpdateFilter<any, any>[]>(\n ...fns: Filters\n): UpdateFilter<ExtractBaseMany<Filters>, UnionToIntersection<ExtractMod<Filters[number]>>>\n\n/**\n * Combine multiple filters by applying an AND logical\n * operation between every one of them:\n * `and(fn1, fn2, ..., fnN) = fn1 AND fn2 AND ... AND fnN`\n *\n * > **Note**: This also combines type modifications, i.e.\n * > if the 1st has modification `{ field1: string }`\n * > and the 2nd has modification `{ field2: number }`,\n * > then the combined filter will have\n * > combined modification `{ field1: string, field2: number }`\n *\n * > **Note**: Due to TypeScript limitations (or more likely my lack of brain cells),\n * > state type is only correctly inferred for up to 6 filters.\n * > If you need more, either provide type explicitly (e.g. `filters.state<SomeState>(...)`),\n * > or combine multiple `and` calls.\n *\n * @param fns Filters to combine\n */\nexport function and(...fns: UpdateFilter<any, any, any>[]): UpdateFilter<any, any, any> {\n return (upd, state) => {\n let i = 0\n const max = fns.length\n\n const next = (): MaybeAsync<boolean> => {\n if (i === max) return true\n\n const res = fns[i++](upd, state)\n\n if (typeof res === 'boolean') {\n if (!res) return false\n\n return next()\n }\n\n return res.then((r: boolean) => {\n if (!r) return false\n\n return next()\n })\n }\n\n return next()\n }\n}\n\nexport function or<Base1, Mod1, State1 extends object, Base2, Mod2, State2 extends object>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n): UpdateFilter<Base1 & Base2, Mod1 | Mod2, State1 | State2>\n\nexport function or<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n): UpdateFilter<Base1 & Base2 & Base3, Mod1 | Mod2 | Mod3, State1 | State2 | State3>\n\nexport function or<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n): UpdateFilter<Base1 & Base2 & Base3 & Base4, Mod1 | Mod2 | Mod3 | Mod4, State1 | State2 | State3 | State4>\n\nexport function or<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n Base5,\n Mod5,\n State5 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n fn5: UpdateFilter<Base5, Mod5, State5>,\n): UpdateFilter<\n Base1 & Base2 & Base3 & Base4 & Base5,\n Mod1 | Mod2 | Mod3 | Mod4 | Mod5,\n State1 | State2 | State3 | State4 | State5\n>\n\nexport function or<\n Base1,\n Mod1,\n State1 extends object,\n Base2,\n Mod2,\n State2 extends object,\n Base3,\n Mod3,\n State3 extends object,\n Base4,\n Mod4,\n State4 extends object,\n Base5,\n Mod5,\n State5 extends object,\n Base6,\n Mod6,\n State6 extends object,\n>(\n fn1: UpdateFilter<Base1, Mod1, State1>,\n fn2: UpdateFilter<Base2, Mod2, State2>,\n fn3: UpdateFilter<Base3, Mod3, State3>,\n fn4: UpdateFilter<Base4, Mod4, State4>,\n fn5: UpdateFilter<Base5, Mod5, State5>,\n fn6: UpdateFilter<Base6, Mod6, State6>,\n): UpdateFilter<\n Base1 & Base2 & Base3 & Base4 & Base5 & Base6,\n Mod1 | Mod2 | Mod3 | Mod4 | Mod5 | Mod6,\n State1 | State2 | State3 | State4 | State5 | State6\n>\n\n/**\n * Combine multiple filters by applying an OR logical\n * operation between every one of them:\n * `or(fn1, fn2, ..., fnN) = fn1 OR fn2 OR ... OR fnN`\n *\n * > **Note**: This also combines type modifications in a union, i.e.\n * > if the 1st has modification `{ field1: string }`\n * > and the 2nd has modification `{ field2: number }`,\n * > then the combined filter will have\n * > modification `{ field1: string } | { field2: number }`.\n *\n * > **Note**: Due to TypeScript limitations (or more likely my lack of brain cells),\n * > state type is only correctly inferred for up to 6 filters.\n * > If you need more, either provide type explicitly (e.g. `filters.state<SomeState>(...)`),\n * > or combine multiple `and` calls.\n *\n * @param fns Filters to combine\n */\nexport function or(...fns: UpdateFilter<any, any, any>[]): UpdateFilter<any, any, any> {\n return (upd, state) => {\n let i = 0\n const max = fns.length\n\n const next = (): MaybeAsync<boolean> => {\n if (i === max) return false\n\n const res = fns[i++](upd, state)\n\n if (typeof res === 'boolean') {\n if (res) return true\n\n return next()\n }\n\n return res.then((r: boolean) => {\n if (r) return true\n\n return next()\n })\n }\n\n return next()\n }\n}\n"]}
@@ -0,0 +1,295 @@
1
+ import { Chat, MaybeArray, Message, MessageAction, MessageReplyInfo, RawDocument, Sticker, StickerSourceType, StickerType, User, Video } from '@mtcute/client';
2
+ import { MessageContext } from '../index.js';
3
+ import { Modify, UpdateFilter } from './types.js';
4
+ /**
5
+ * Filter incoming messages.
6
+ *
7
+ * Messages sent to yourself (i.e. Saved Messages) are also "incoming"
8
+ */
9
+ export declare const incoming: UpdateFilter<Message, {
10
+ isOutgoing: false;
11
+ }>;
12
+ /**
13
+ * Filter outgoing messages.
14
+ *
15
+ * Messages sent to yourself (i.e. Saved Messages) are **not** "outgoing"
16
+ */
17
+ export declare const outgoing: UpdateFilter<Message, {
18
+ isOutgoing: true;
19
+ }>;
20
+ /**
21
+ * Filter messages that are replies to some other message
22
+ */
23
+ export declare const reply: UpdateFilter<Message, {
24
+ replyToMessage: MessageReplyInfo;
25
+ }>;
26
+ /**
27
+ * Filter messages containing some media
28
+ */
29
+ export declare const media: UpdateFilter<Message, {
30
+ media: Exclude<Message['media'], null>;
31
+ }>;
32
+ /**
33
+ * Filter messages containing media of given type
34
+ */
35
+ export declare const mediaOf: <T extends "photo" | "location" | "poll" | "contact" | "game" | "sticker" | "dice" | "audio" | "voice" | "document" | "video" | "live_location" | "web_page" | "venue" | "invoice">(type: T) => UpdateFilter<Message, {
36
+ media: Extract<import("@mtcute/client").Photo, {
37
+ type: T;
38
+ }> | Extract<import("@mtcute/client").Dice, {
39
+ type: T;
40
+ }> | Extract<import("@mtcute/client").Contact, {
41
+ type: T;
42
+ }> | Extract<import("@mtcute/client").Audio, {
43
+ type: T;
44
+ }> | Extract<import("@mtcute/client").Voice, {
45
+ type: T;
46
+ }> | Extract<Sticker, {
47
+ type: T;
48
+ }> | Extract<import("@mtcute/client").Document, {
49
+ type: T;
50
+ }> | Extract<Video, {
51
+ type: T;
52
+ }> | Extract<import("@mtcute/client").Location, {
53
+ type: T;
54
+ }> | Extract<import("@mtcute/client").LiveLocation, {
55
+ type: T;
56
+ }> | Extract<import("@mtcute/client").Game, {
57
+ type: T;
58
+ }> | Extract<import("@mtcute/client").WebPage, {
59
+ type: T;
60
+ }> | Extract<import("@mtcute/client").Venue, {
61
+ type: T;
62
+ }> | Extract<import("@mtcute/client").Poll, {
63
+ type: T;
64
+ }> | Extract<import("@mtcute/client").Invoice, {
65
+ type: T;
66
+ }>;
67
+ }>;
68
+ /** Filter messages containing a photo */
69
+ export declare const photo: UpdateFilter<Message, {
70
+ media: import("@mtcute/client").Photo;
71
+ }>;
72
+ /** Filter messages containing a dice */
73
+ export declare const dice: UpdateFilter<Message, {
74
+ media: import("@mtcute/client").Dice;
75
+ }>;
76
+ /** Filter messages containing a contact */
77
+ export declare const contact: UpdateFilter<Message, {
78
+ media: import("@mtcute/client").Contact;
79
+ }>;
80
+ /** Filter messages containing an audio file */
81
+ export declare const audio: UpdateFilter<Message, {
82
+ media: import("@mtcute/client").Audio;
83
+ }>;
84
+ /** Filter messages containing a voice message (audio-only) */
85
+ export declare const voice: UpdateFilter<Message, {
86
+ media: import("@mtcute/client").Voice;
87
+ }>;
88
+ /** Filter messages containing a sticker */
89
+ export declare const sticker: UpdateFilter<Message, {
90
+ media: Sticker;
91
+ }>;
92
+ /** Filter messages containing a document (a file) */
93
+ export declare const document: UpdateFilter<Message, {
94
+ media: import("@mtcute/client").Document;
95
+ }>;
96
+ /** Filter messages containing any video (videos, round messages and animations) */
97
+ export declare const anyVideo: UpdateFilter<Message, {
98
+ media: Video;
99
+ }>;
100
+ /** Filter messages containing a static location */
101
+ export declare const location: UpdateFilter<Message, {
102
+ media: import("@mtcute/client").Location;
103
+ }>;
104
+ /** Filter messages containing a live location */
105
+ export declare const liveLocation: UpdateFilter<Message, {
106
+ media: import("@mtcute/client").LiveLocation;
107
+ }>;
108
+ /** Filter messages containing a game */
109
+ export declare const game: UpdateFilter<Message, {
110
+ media: import("@mtcute/client").Game;
111
+ }>;
112
+ /** Filter messages containing a web page */
113
+ export declare const webpage: UpdateFilter<Message, {
114
+ media: import("@mtcute/client").WebPage;
115
+ }>;
116
+ /** Filter messages containing a venue */
117
+ export declare const venue: UpdateFilter<Message, {
118
+ media: import("@mtcute/client").Venue;
119
+ }>;
120
+ /** Filter messages containing a poll */
121
+ export declare const poll: UpdateFilter<Message, {
122
+ media: import("@mtcute/client").Poll;
123
+ }>;
124
+ /** Filter messages containing an invoice */
125
+ export declare const invoice: UpdateFilter<Message, {
126
+ media: import("@mtcute/client").Invoice;
127
+ }>;
128
+ /**
129
+ * Filter messages containing any location (live or static).
130
+ */
131
+ export declare const anyLocation: UpdateFilter<Message, {
132
+ media: Location;
133
+ }>;
134
+ /**
135
+ * Filter messages containing a document
136
+ *
137
+ * This will also match media like audio, video, voice
138
+ * that also use Documents
139
+ */
140
+ export declare const anyDocument: UpdateFilter<Message, {
141
+ media: RawDocument;
142
+ }>;
143
+ /**
144
+ * Filter messages containing a simple video.
145
+ *
146
+ * This does not include round messages and animations
147
+ */
148
+ export declare const video: UpdateFilter<Message, {
149
+ media: Modify<Video, {
150
+ isRound: false;
151
+ isAnimation: false;
152
+ }>;
153
+ }>;
154
+ /**
155
+ * Filter messages containing an animation.
156
+ *
157
+ * > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)
158
+ * > are also considered animations.
159
+ */
160
+ export declare const animation: UpdateFilter<Message, {
161
+ media: Modify<Video, {
162
+ isRound: false;
163
+ isAnimation: true;
164
+ }>;
165
+ }>;
166
+ /**
167
+ * Filter messages containing a round message (aka video note).
168
+ */
169
+ export declare const roundMessage: UpdateFilter<Message, {
170
+ media: Modify<Video, {
171
+ isRound: true;
172
+ isAnimation: false;
173
+ }>;
174
+ }>;
175
+ /**
176
+ * Filter messages containing a sticker by its type
177
+ */
178
+ export declare const stickerByType: (type: StickerType) => UpdateFilter<Message, {
179
+ media: Sticker;
180
+ }>;
181
+ /**
182
+ * Filter messages containing a sticker by its source file type
183
+ */
184
+ export declare const stickerBySourceType: (type: StickerSourceType) => UpdateFilter<Message, {
185
+ media: Sticker;
186
+ }>;
187
+ /**
188
+ * Filter text-only messages non-service messages
189
+ */
190
+ export declare const text: UpdateFilter<Message, {
191
+ media: null;
192
+ isService: false;
193
+ }>;
194
+ /**
195
+ * Filter service messages
196
+ */
197
+ export declare const service: UpdateFilter<Message, {
198
+ isService: true;
199
+ }>;
200
+ /**
201
+ * Filter service messages by action type
202
+ */
203
+ export declare const action: <T extends "chat_created" | "channel_created" | "chat_migrate_to" | "channel_migrate_from" | "message_pinned" | "history_cleared" | "game_score" | "contact_joined" | "title_changed" | "photo_changed" | "photo_deleted" | "users_added" | "user_left" | "user_removed" | "user_joined_link" | "payment_received" | "payment_sent" | "call" | "screenshot_taken" | "bot_allowed" | "geo_proximity" | "group_call_started" | "group_call_ended" | "group_call_scheduled" | "group_call_invite" | "ttl_changed" | "topic_created" | "topic_edited" | "custom" | "theme_changed" | "user_joined_approved" | "webview_sent" | "webview_received" | "premium_gifted" | "photo_suggested" | "peer_chosen" | "wallpaper_changed">(type: MaybeArray<T>) => UpdateFilter<Message, {
204
+ action: Extract<import("@mtcute/client").ActionChatCreated, {
205
+ type: T;
206
+ }> | Extract<import("@mtcute/client").ActionChannelCreated, {
207
+ type: T;
208
+ }> | Extract<import("@mtcute/client").ActionChatMigrateTo, {
209
+ type: T;
210
+ }> | Extract<import("@mtcute/client").ActionChannelMigrateFrom, {
211
+ type: T;
212
+ }> | Extract<import("@mtcute/client").ActionMessagePinned, {
213
+ type: T;
214
+ }> | Extract<import("@mtcute/client").ActionHistoryCleared, {
215
+ type: T;
216
+ }> | Extract<import("@mtcute/client").ActionGameScore, {
217
+ type: T;
218
+ }> | Extract<import("@mtcute/client").ActionContactJoined, {
219
+ type: T;
220
+ }> | Extract<import("@mtcute/client").ActionTitleChanged, {
221
+ type: T;
222
+ }> | Extract<import("@mtcute/client").ActionPhotoChanged, {
223
+ type: T;
224
+ }> | Extract<import("@mtcute/client").ActionPhotoDeleted, {
225
+ type: T;
226
+ }> | Extract<import("@mtcute/client").ActionUsersAdded, {
227
+ type: T;
228
+ }> | Extract<import("@mtcute/client").ActionUserLeft, {
229
+ type: T;
230
+ }> | Extract<import("@mtcute/client").ActionUserRemoved, {
231
+ type: T;
232
+ }> | Extract<import("@mtcute/client").ActionUserJoinedLink, {
233
+ type: T;
234
+ }> | Extract<import("@mtcute/client").ActionPaymentReceived, {
235
+ type: T;
236
+ }> | Extract<import("@mtcute/client").ActionPaymentSent, {
237
+ type: T;
238
+ }> | Extract<import("@mtcute/client").ActionCall, {
239
+ type: T;
240
+ }> | Extract<import("@mtcute/client").ActionScreenshotTaken, {
241
+ type: T;
242
+ }> | Extract<import("@mtcute/client").ActionBotAllowed, {
243
+ type: T;
244
+ }> | Extract<import("@mtcute/client").ActionGeoProximity, {
245
+ type: T;
246
+ }> | Extract<import("@mtcute/client").ActionGroupCallStarted, {
247
+ type: T;
248
+ }> | Extract<import("@mtcute/client").ActionGroupCallEnded, {
249
+ type: T;
250
+ }> | Extract<import("@mtcute/client").ActionGroupCallScheduled, {
251
+ type: T;
252
+ }> | Extract<import("@mtcute/client").ActionGroupInvite, {
253
+ type: T;
254
+ }> | Extract<import("@mtcute/client").ActionTtlChanged, {
255
+ type: T;
256
+ }> | Extract<import("@mtcute/client").ActionTopicCreated, {
257
+ type: T;
258
+ }> | Extract<import("@mtcute/client").ActionTopicEdited, {
259
+ type: T;
260
+ }> | Extract<import("@mtcute/client").ActionCustom, {
261
+ type: T;
262
+ }> | Extract<import("@mtcute/client").ActionThemeChanged, {
263
+ type: T;
264
+ }> | Extract<import("@mtcute/client").ActionUserJoinedApproved, {
265
+ type: T;
266
+ }> | Extract<import("@mtcute/client").ActionWebviewDataSent, {
267
+ type: T;
268
+ }> | Extract<import("@mtcute/client").ActionWebviewDataReceived, {
269
+ type: T;
270
+ }> | Extract<import("@mtcute/client").ActionPremiumGifted, {
271
+ type: T;
272
+ }> | Extract<import("@mtcute/client").ActionPhotoSuggested, {
273
+ type: T;
274
+ }> | Extract<import("@mtcute/client").ActionPeerChosen, {
275
+ type: T;
276
+ }> | Extract<import("@mtcute/client").ActionWallpaperChanged, {
277
+ type: T;
278
+ }>;
279
+ sender: T extends "history_cleared" | "contact_joined" | "user_removed" | "user_joined_link" | "bot_allowed" ? User : User | Chat;
280
+ }>;
281
+ export declare const sender: <T extends "user" | "chat">(type: T) => UpdateFilter<Message, {
282
+ sender: Extract<User, {
283
+ type: T;
284
+ }> | Extract<Chat, {
285
+ type: T;
286
+ }>;
287
+ }>;
288
+ /**
289
+ * Filter that matches messages that are replies to some other message.
290
+ *
291
+ * Optionally, you can pass a filter that will be applied to the replied message.
292
+ */
293
+ export declare const replyTo: <Mod, State extends object>(filter?: UpdateFilter<Message, Mod, State> | undefined) => UpdateFilter<MessageContext, {
294
+ getReplyTo: () => Promise<Message & Mod>;
295
+ }, State>;
@@ -0,0 +1,130 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ // ^^ will be looked into in MTQ-29
3
+ import { RawDocument, RawLocation, } from '@mtcute/client';
4
+ /**
5
+ * Filter incoming messages.
6
+ *
7
+ * Messages sent to yourself (i.e. Saved Messages) are also "incoming"
8
+ */
9
+ export const incoming = (msg) => !msg.isOutgoing;
10
+ /**
11
+ * Filter outgoing messages.
12
+ *
13
+ * Messages sent to yourself (i.e. Saved Messages) are **not** "outgoing"
14
+ */
15
+ export const outgoing = (msg) => msg.isOutgoing;
16
+ /**
17
+ * Filter messages that are replies to some other message
18
+ */
19
+ export const reply = (msg) => msg.replyToMessage !== null;
20
+ /**
21
+ * Filter messages containing some media
22
+ */
23
+ export const media = (msg) => msg.media !== null;
24
+ /**
25
+ * Filter messages containing media of given type
26
+ */
27
+ export const mediaOf = (type) => (msg) => msg.media?.type === type;
28
+ /** Filter messages containing a photo */
29
+ export const photo = mediaOf('photo');
30
+ /** Filter messages containing a dice */
31
+ export const dice = mediaOf('dice');
32
+ /** Filter messages containing a contact */
33
+ export const contact = mediaOf('contact');
34
+ /** Filter messages containing an audio file */
35
+ export const audio = mediaOf('audio');
36
+ /** Filter messages containing a voice message (audio-only) */
37
+ export const voice = mediaOf('voice');
38
+ /** Filter messages containing a sticker */
39
+ export const sticker = mediaOf('sticker');
40
+ /** Filter messages containing a document (a file) */
41
+ export const document = mediaOf('document');
42
+ /** Filter messages containing any video (videos, round messages and animations) */
43
+ export const anyVideo = mediaOf('video');
44
+ /** Filter messages containing a static location */
45
+ export const location = mediaOf('location');
46
+ /** Filter messages containing a live location */
47
+ export const liveLocation = mediaOf('live_location');
48
+ /** Filter messages containing a game */
49
+ export const game = mediaOf('game');
50
+ /** Filter messages containing a web page */
51
+ export const webpage = mediaOf('web_page');
52
+ /** Filter messages containing a venue */
53
+ export const venue = mediaOf('venue');
54
+ /** Filter messages containing a poll */
55
+ export const poll = mediaOf('poll');
56
+ /** Filter messages containing an invoice */
57
+ export const invoice = mediaOf('invoice');
58
+ /**
59
+ * Filter messages containing any location (live or static).
60
+ */
61
+ export const anyLocation = (msg) => msg.media instanceof RawLocation;
62
+ /**
63
+ * Filter messages containing a document
64
+ *
65
+ * This will also match media like audio, video, voice
66
+ * that also use Documents
67
+ */
68
+ export const anyDocument = (msg) => msg.media instanceof RawDocument;
69
+ /**
70
+ * Filter messages containing a simple video.
71
+ *
72
+ * This does not include round messages and animations
73
+ */
74
+ export const video = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && !msg.media.isRound;
75
+ /**
76
+ * Filter messages containing an animation.
77
+ *
78
+ * > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)
79
+ * > are also considered animations.
80
+ */
81
+ export const animation = (msg) => msg.media?.type === 'video' && msg.media.isAnimation && !msg.media.isRound;
82
+ /**
83
+ * Filter messages containing a round message (aka video note).
84
+ */
85
+ export const roundMessage = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && msg.media.isRound;
86
+ /**
87
+ * Filter messages containing a sticker by its type
88
+ */
89
+ export const stickerByType = (type) => (msg) => msg.media?.type === 'sticker' && msg.media.stickerType === type;
90
+ /**
91
+ * Filter messages containing a sticker by its source file type
92
+ */
93
+ export const stickerBySourceType = (type) => (msg) => msg.media?.type === 'sticker' && msg.media.sourceType === type;
94
+ /**
95
+ * Filter text-only messages non-service messages
96
+ */
97
+ export const text = (msg) => msg.media === null && !msg.isService;
98
+ /**
99
+ * Filter service messages
100
+ */
101
+ export const service = (msg) => msg.isService;
102
+ /**
103
+ * Filter service messages by action type
104
+ */
105
+ export const action = (type) => {
106
+ if (Array.isArray(type)) {
107
+ const index = {};
108
+ type.forEach((it) => (index[it] = true));
109
+ return (msg) => msg.action?.type in index;
110
+ }
111
+ return (msg) => msg.action?.type === type;
112
+ };
113
+ export const sender = (type) => (msg) => msg.sender.type === type;
114
+ /**
115
+ * Filter that matches messages that are replies to some other message.
116
+ *
117
+ * Optionally, you can pass a filter that will be applied to the replied message.
118
+ */
119
+ export const replyTo = (filter) => async (msg, state) => {
120
+ if (!msg.replyToMessage?.id)
121
+ return false;
122
+ const reply = await msg.getReplyTo();
123
+ if (!reply)
124
+ return false;
125
+ msg.getReplyTo = () => Promise.resolve(reply);
126
+ if (!filter)
127
+ return true;
128
+ return filter(reply, state);
129
+ };
130
+ //# sourceMappingURL=message.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/filters/message.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,mCAAmC;AACnC,OAAO,EAOH,WAAW,EACX,WAAW,GAMd,MAAM,gBAAgB,CAAA;AAKvB;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAiD,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,CAAA;AAE9F;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAgD,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAA;AAE5F;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAgE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,cAAc,KAAK,IAAI,CAAA;AAEtH;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAsE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,KAAK,IAAI,CAAA;AAEnH;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAChB,CAA6B,IAAO,EAA4E,EAAE,CAC9G,CAAC,GAAG,EAAE,EAAE,CACJ,GAAG,CAAC,KAAK,EAAE,IAAI,KAAK,IAAI,CAAA;AAEpC,yCAAyC;AACzC,MAAM,CAAC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AACrC,wCAAwC;AACxC,MAAM,CAAC,M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MAAM;QAAE,OAAO,IAAI,CAAA;IAExB,OAAO,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;AAC/B,CAAC,CAAA","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n// ^^ will be looked into in MTQ-29\nimport {\n Chat,\n MaybeArray,\n Message,\n MessageAction,\n MessageMediaType,\n MessageReplyInfo,\n RawDocument,\n RawLocation,\n Sticker,\n StickerSourceType,\n StickerType,\n User,\n Video,\n} from '@mtcute/client'\n\nimport { MessageContext } from '../index.js'\nimport { Modify, UpdateFilter } from './types.js'\n\n/**\n * Filter incoming messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are also \"incoming\"\n */\nexport const incoming: UpdateFilter<Message, { isOutgoing: false }> = (msg) => !msg.isOutgoing\n\n/**\n * Filter outgoing messages.\n *\n * Messages sent to yourself (i.e. Saved Messages) are **not** \"outgoing\"\n */\nexport const outgoing: UpdateFilter<Message, { isOutgoing: true }> = (msg) => msg.isOutgoing\n\n/**\n * Filter messages that are replies to some other message\n */\nexport const reply: UpdateFilter<Message, { replyToMessage: MessageReplyInfo }> = (msg) => msg.replyToMessage !== null\n\n/**\n * Filter messages containing some media\n */\nexport const media: UpdateFilter<Message, { media: Exclude<Message['media'], null> }> = (msg) => msg.media !== null\n\n/**\n * Filter messages containing media of given type\n */\nexport const mediaOf =\n <T extends MessageMediaType>(type: T): UpdateFilter<Message, { media: Extract<Message['media'], { type: T }> }> =>\n (msg) =>\n msg.media?.type === type\n\n/** Filter messages containing a photo */\nexport const photo = mediaOf('photo')\n/** Filter messages containing a dice */\nexport const dice = mediaOf('dice')\n/** Filter messages containing a contact */\nexport const contact = mediaOf('contact')\n/** Filter messages containing an audio file */\nexport const audio = mediaOf('audio')\n/** Filter messages containing a voice message (audio-only) */\nexport const voice = mediaOf('voice')\n/** Filter messages containing a sticker */\nexport const sticker = mediaOf('sticker')\n/** Filter messages containing a document (a file) */\nexport const document = mediaOf('document')\n/** Filter messages containing any video (videos, round messages and animations) */\nexport const anyVideo = mediaOf('video')\n/** Filter messages containing a static location */\nexport const location = mediaOf('location')\n/** Filter messages containing a live location */\nexport const liveLocation = mediaOf('live_location')\n/** Filter messages containing a game */\nexport const game = mediaOf('game')\n/** Filter messages containing a web page */\nexport const webpage = mediaOf('web_page')\n/** Filter messages containing a venue */\nexport const venue = mediaOf('venue')\n/** Filter messages containing a poll */\nexport const poll = mediaOf('poll')\n/** Filter messages containing an invoice */\nexport const invoice = mediaOf('invoice')\n\n/**\n * Filter messages containing any location (live or static).\n */\nexport const anyLocation: UpdateFilter<Message, { media: Location }> = (msg) => msg.media instanceof RawLocation\n\n/**\n * Filter messages containing a document\n *\n * This will also match media like audio, video, voice\n * that also use Documents\n */\nexport const anyDocument: UpdateFilter<Message, { media: RawDocument }> = (msg) => msg.media instanceof RawDocument\n\n/**\n * Filter messages containing a simple video.\n *\n * This does not include round messages and animations\n */\nexport const video: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing an animation.\n *\n * > **Note**: Legacy GIFs (i.e. documents with `image/gif` MIME)\n * > are also considered animations.\n */\nexport const animation: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: false\n isAnimation: true\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && msg.media.isAnimation && !msg.media.isRound\n\n/**\n * Filter messages containing a round message (aka video note).\n */\nexport const roundMessage: UpdateFilter<\n Message,\n {\n media: Modify<\n Video,\n {\n isRound: true\n isAnimation: false\n }\n >\n }\n> = (msg) => msg.media?.type === 'video' && !msg.media.isAnimation && msg.media.isRound\n\n/**\n * Filter messages containing a sticker by its type\n */\nexport const stickerByType =\n (type: StickerType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.stickerType === type\n\n/**\n * Filter messages containing a sticker by its source file type\n */\nexport const stickerBySourceType =\n (type: StickerSourceType): UpdateFilter<Message, { media: Sticker }> =>\n (msg) =>\n msg.media?.type === 'sticker' && msg.media.sourceType === type\n\n/**\n * Filter text-only messages non-service messages\n */\nexport const text: UpdateFilter<\n Message,\n {\n media: null\n isService: false\n }\n> = (msg) => msg.media === null && !msg.isService\n\n/**\n * Filter service messages\n */\nexport const service: UpdateFilter<Message, { isService: true }> = (msg) => msg.isService\n\n/**\n * Filter service messages by action type\n */\nexport const action = <T extends Exclude<MessageAction, null>['type']>(\n type: MaybeArray<T>,\n): UpdateFilter<\n Message,\n {\n action: Extract<MessageAction, { type: T }>\n sender: T extends 'user_joined_link' | 'user_removed' | 'history_cleared' | 'contact_joined' | 'bot_allowed'\n ? User\n : User | Chat\n }\n> => {\n if (Array.isArray(type)) {\n const index: Partial<Record<T, true>> = {}\n type.forEach((it) => (index[it] = true))\n\n return (msg) => (msg.action?.type as any) in index\n }\n\n return (msg) => msg.action?.type === type\n}\n\nexport const sender =\n <T extends Message['sender']['type']>(\n type: T,\n ): UpdateFilter<Message, { sender: Extract<Message['sender'], { type: T }> }> =>\n (msg) =>\n msg.sender.type === type\n\n/**\n * Filter that matches messages that are replies to some other message.\n *\n * Optionally, you can pass a filter that will be applied to the replied message.\n */\nexport const replyTo =\n <Mod, State extends object>(\n filter?: UpdateFilter<Message, Mod, State>,\n ): UpdateFilter<MessageContext, { getReplyTo: () => Promise<Message & Mod> }, State> =>\n async (msg, state) => {\n if (!msg.replyToMessage?.id) return false\n\n const reply = await msg.getReplyTo()\n if (!reply) return false\n\n msg.getReplyTo = () => Promise.resolve(reply)\n\n if (!filter) return true\n\n return filter(reply, state)\n }\n"]}
@@ -0,0 +1,15 @@
1
+ import { MaybeAsync } from '@mtcute/client';
2
+ import { UpdateFilter } from './types.js';
3
+ /**
4
+ * Create a filter for the cases when the state is empty
5
+ */
6
+ export declare const stateEmpty: UpdateFilter<any>;
7
+ /**
8
+ * Create a filter based on state predicate
9
+ *
10
+ * If state exists and matches `predicate`, update passes
11
+ * this filter, otherwise it doesn't
12
+ *
13
+ * @param predicate State predicate
14
+ */
15
+ export declare const state: <T extends object>(predicate: (state: T) => MaybeAsync<boolean>) => UpdateFilter<any, {}, T>;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Create a filter for the cases when the state is empty
3
+ */
4
+ export const stateEmpty = async (upd, state) => {
5
+ if (!state)
6
+ return false;
7
+ return !(await state.get());
8
+ };
9
+ /**
10
+ * Create a filter based on state predicate
11
+ *
12
+ * If state exists and matches `predicate`, update passes
13
+ * this filter, otherwise it doesn't
14
+ *
15
+ * @param predicate State predicate
16
+ */
17
+ export const state = (predicate) => {
18
+ return async (upd, state) => {
19
+ if (!state)
20
+ return false;
21
+ const data = await state.get();
22
+ if (!data)
23
+ return false;
24
+ return predicate(data);
25
+ };
26
+ };
27
+ //# sourceMappingURL=state.js.map