botify-wa 0.2.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 (422) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +101 -0
  3. package/bin/botify.js +198 -0
  4. package/package.json +61 -0
  5. package/src/Bot.js +327 -0
  6. package/src/Context.js +185 -0
  7. package/src/PluginManager.js +131 -0
  8. package/src/config/index.js +2 -0
  9. package/src/config/parser.js +114 -0
  10. package/src/config/schema.js +106 -0
  11. package/src/index.js +18 -0
  12. package/src/loader.js +87 -0
  13. package/src/middlewares/antiSpam.js +75 -0
  14. package/src/middlewares/cooldown.js +81 -0
  15. package/src/middlewares/index.js +4 -0
  16. package/src/middlewares/logger.js +47 -0
  17. package/src/middlewares/permission.js +60 -0
  18. package/src/templates/config.bt +37 -0
  19. package/src/templates/index.js +34 -0
  20. package/src/templates/plugins/info.js +25 -0
  21. package/src/templates/plugins/menu.js +30 -0
  22. package/src/templates/plugins/ping.js +13 -0
  23. package/vendor/core/WAProto/GenerateStatics.sh +3 -0
  24. package/vendor/core/WAProto/WAProto.proto +6035 -0
  25. package/vendor/core/WAProto/fix-imports.js +81 -0
  26. package/vendor/core/WAProto/index.d.ts +2 -0
  27. package/vendor/core/WAProto/index.js +132326 -0
  28. package/vendor/core/lib/Defaults/index.d.ts +74 -0
  29. package/vendor/core/lib/Defaults/index.d.ts.map +1 -0
  30. package/vendor/core/lib/Defaults/index.js +140 -0
  31. package/vendor/core/lib/Defaults/index.js.map +1 -0
  32. package/vendor/core/lib/Modded/message_builder.js +2330 -0
  33. package/vendor/core/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  34. package/vendor/core/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  35. package/vendor/core/lib/Signal/Group/ciphertext-message.js +12 -0
  36. package/vendor/core/lib/Signal/Group/ciphertext-message.js.map +1 -0
  37. package/vendor/core/lib/Signal/Group/group-session-builder.d.ts +15 -0
  38. package/vendor/core/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  39. package/vendor/core/lib/Signal/Group/group-session-builder.js +30 -0
  40. package/vendor/core/lib/Signal/Group/group-session-builder.js.map +1 -0
  41. package/vendor/core/lib/Signal/Group/group_cipher.d.ts +17 -0
  42. package/vendor/core/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  43. package/vendor/core/lib/Signal/Group/group_cipher.js +82 -0
  44. package/vendor/core/lib/Signal/Group/group_cipher.js.map +1 -0
  45. package/vendor/core/lib/Signal/Group/index.d.ts +12 -0
  46. package/vendor/core/lib/Signal/Group/index.d.ts.map +1 -0
  47. package/vendor/core/lib/Signal/Group/index.js +12 -0
  48. package/vendor/core/lib/Signal/Group/index.js.map +1 -0
  49. package/vendor/core/lib/Signal/Group/keyhelper.d.ts +11 -0
  50. package/vendor/core/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  51. package/vendor/core/lib/Signal/Group/keyhelper.js +18 -0
  52. package/vendor/core/lib/Signal/Group/keyhelper.js.map +1 -0
  53. package/vendor/core/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  54. package/vendor/core/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  55. package/vendor/core/lib/Signal/Group/sender-chain-key.js +26 -0
  56. package/vendor/core/lib/Signal/Group/sender-chain-key.js.map +1 -0
  57. package/vendor/core/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  58. package/vendor/core/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  59. package/vendor/core/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  60. package/vendor/core/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  61. package/vendor/core/lib/Signal/Group/sender-key-message.d.ts +19 -0
  62. package/vendor/core/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  63. package/vendor/core/lib/Signal/Group/sender-key-message.js +66 -0
  64. package/vendor/core/lib/Signal/Group/sender-key-message.js.map +1 -0
  65. package/vendor/core/lib/Signal/Group/sender-key-name.d.ts +18 -0
  66. package/vendor/core/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  67. package/vendor/core/lib/Signal/Group/sender-key-name.js +48 -0
  68. package/vendor/core/lib/Signal/Group/sender-key-name.js.map +1 -0
  69. package/vendor/core/lib/Signal/Group/sender-key-record.d.ts +31 -0
  70. package/vendor/core/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  71. package/vendor/core/lib/Signal/Group/sender-key-record.js +41 -0
  72. package/vendor/core/lib/Signal/Group/sender-key-record.js.map +1 -0
  73. package/vendor/core/lib/Signal/Group/sender-key-state.d.ts +39 -0
  74. package/vendor/core/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  75. package/vendor/core/lib/Signal/Group/sender-key-state.js +84 -0
  76. package/vendor/core/lib/Signal/Group/sender-key-state.js.map +1 -0
  77. package/vendor/core/lib/Signal/Group/sender-message-key.d.ts +12 -0
  78. package/vendor/core/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  79. package/vendor/core/lib/Signal/Group/sender-message-key.js +26 -0
  80. package/vendor/core/lib/Signal/Group/sender-message-key.js.map +1 -0
  81. package/vendor/core/lib/Signal/libsignal.d.ts +5 -0
  82. package/vendor/core/lib/Signal/libsignal.d.ts.map +1 -0
  83. package/vendor/core/lib/Signal/libsignal.js +403 -0
  84. package/vendor/core/lib/Signal/libsignal.js.map +1 -0
  85. package/vendor/core/lib/Signal/lid-mapping.d.ts +19 -0
  86. package/vendor/core/lib/Signal/lid-mapping.d.ts.map +1 -0
  87. package/vendor/core/lib/Signal/lid-mapping.js +271 -0
  88. package/vendor/core/lib/Signal/lid-mapping.js.map +1 -0
  89. package/vendor/core/lib/Socket/Client/index.d.ts +3 -0
  90. package/vendor/core/lib/Socket/Client/index.d.ts.map +1 -0
  91. package/vendor/core/lib/Socket/Client/index.js +3 -0
  92. package/vendor/core/lib/Socket/Client/index.js.map +1 -0
  93. package/vendor/core/lib/Socket/Client/types.d.ts +16 -0
  94. package/vendor/core/lib/Socket/Client/types.d.ts.map +1 -0
  95. package/vendor/core/lib/Socket/Client/types.js +11 -0
  96. package/vendor/core/lib/Socket/Client/types.js.map +1 -0
  97. package/vendor/core/lib/Socket/Client/websocket.d.ts +13 -0
  98. package/vendor/core/lib/Socket/Client/websocket.d.ts.map +1 -0
  99. package/vendor/core/lib/Socket/Client/websocket.js +54 -0
  100. package/vendor/core/lib/Socket/Client/websocket.js.map +1 -0
  101. package/vendor/core/lib/Socket/business.d.ts +536 -0
  102. package/vendor/core/lib/Socket/business.d.ts.map +1 -0
  103. package/vendor/core/lib/Socket/business.js +422 -0
  104. package/vendor/core/lib/Socket/business.js.map +1 -0
  105. package/vendor/core/lib/Socket/chats.d.ts +111 -0
  106. package/vendor/core/lib/Socket/chats.d.ts.map +1 -0
  107. package/vendor/core/lib/Socket/chats.js +991 -0
  108. package/vendor/core/lib/Socket/chats.js.map +1 -0
  109. package/vendor/core/lib/Socket/communities.d.ts +645 -0
  110. package/vendor/core/lib/Socket/communities.d.ts.map +1 -0
  111. package/vendor/core/lib/Socket/communities.js +431 -0
  112. package/vendor/core/lib/Socket/communities.js.map +1 -0
  113. package/vendor/core/lib/Socket/dugong.d.ts +130 -0
  114. package/vendor/core/lib/Socket/dugong.d.ts.map +1 -0
  115. package/vendor/core/lib/Socket/dugong.js +768 -0
  116. package/vendor/core/lib/Socket/dugong.js.map +1 -0
  117. package/vendor/core/lib/Socket/groups.d.ts +150 -0
  118. package/vendor/core/lib/Socket/groups.d.ts.map +1 -0
  119. package/vendor/core/lib/Socket/groups.js +327 -0
  120. package/vendor/core/lib/Socket/groups.js.map +1 -0
  121. package/vendor/core/lib/Socket/index.d.ts +636 -0
  122. package/vendor/core/lib/Socket/index.d.ts.map +1 -0
  123. package/vendor/core/lib/Socket/index.js +15 -0
  124. package/vendor/core/lib/Socket/index.js.map +1 -0
  125. package/vendor/core/lib/Socket/messages-recv.d.ts +238 -0
  126. package/vendor/core/lib/Socket/messages-recv.d.ts.map +1 -0
  127. package/vendor/core/lib/Socket/messages-recv.js +1308 -0
  128. package/vendor/core/lib/Socket/messages-recv.js.map +1 -0
  129. package/vendor/core/lib/Socket/messages-send.d.ts +579 -0
  130. package/vendor/core/lib/Socket/messages-send.d.ts.map +1 -0
  131. package/vendor/core/lib/Socket/messages-send.js +1595 -0
  132. package/vendor/core/lib/Socket/messages-send.js.map +1 -0
  133. package/vendor/core/lib/Socket/mex.d.ts +3 -0
  134. package/vendor/core/lib/Socket/mex.d.ts.map +1 -0
  135. package/vendor/core/lib/Socket/mex.js +48 -0
  136. package/vendor/core/lib/Socket/mex.js.map +1 -0
  137. package/vendor/core/lib/Socket/newsletter.d.ts +177 -0
  138. package/vendor/core/lib/Socket/newsletter.d.ts.map +1 -0
  139. package/vendor/core/lib/Socket/newsletter.js +456 -0
  140. package/vendor/core/lib/Socket/newsletter.js.map +1 -0
  141. package/vendor/core/lib/Socket/socket.d.ts +55 -0
  142. package/vendor/core/lib/Socket/socket.d.ts.map +1 -0
  143. package/vendor/core/lib/Socket/socket.js +936 -0
  144. package/vendor/core/lib/Socket/socket.js.map +1 -0
  145. package/vendor/core/lib/Types/Auth.d.ts +116 -0
  146. package/vendor/core/lib/Types/Auth.d.ts.map +1 -0
  147. package/vendor/core/lib/Types/Auth.js +2 -0
  148. package/vendor/core/lib/Types/Auth.js.map +1 -0
  149. package/vendor/core/lib/Types/Bussines.d.ts +37 -0
  150. package/vendor/core/lib/Types/Bussines.d.ts.map +1 -0
  151. package/vendor/core/lib/Types/Bussines.js +2 -0
  152. package/vendor/core/lib/Types/Bussines.js.map +1 -0
  153. package/vendor/core/lib/Types/Call.d.ts +15 -0
  154. package/vendor/core/lib/Types/Call.d.ts.map +1 -0
  155. package/vendor/core/lib/Types/Call.js +2 -0
  156. package/vendor/core/lib/Types/Call.js.map +1 -0
  157. package/vendor/core/lib/Types/Chat.d.ts +123 -0
  158. package/vendor/core/lib/Types/Chat.d.ts.map +1 -0
  159. package/vendor/core/lib/Types/Chat.js +8 -0
  160. package/vendor/core/lib/Types/Chat.js.map +1 -0
  161. package/vendor/core/lib/Types/Contact.d.ts +24 -0
  162. package/vendor/core/lib/Types/Contact.d.ts.map +1 -0
  163. package/vendor/core/lib/Types/Contact.js +2 -0
  164. package/vendor/core/lib/Types/Contact.js.map +1 -0
  165. package/vendor/core/lib/Types/Events.d.ts +237 -0
  166. package/vendor/core/lib/Types/Events.d.ts.map +1 -0
  167. package/vendor/core/lib/Types/Events.js +2 -0
  168. package/vendor/core/lib/Types/Events.js.map +1 -0
  169. package/vendor/core/lib/Types/GroupMetadata.d.ts +68 -0
  170. package/vendor/core/lib/Types/GroupMetadata.d.ts.map +1 -0
  171. package/vendor/core/lib/Types/GroupMetadata.js +2 -0
  172. package/vendor/core/lib/Types/GroupMetadata.js.map +1 -0
  173. package/vendor/core/lib/Types/Label.d.ts +47 -0
  174. package/vendor/core/lib/Types/Label.d.ts.map +1 -0
  175. package/vendor/core/lib/Types/Label.js +25 -0
  176. package/vendor/core/lib/Types/Label.js.map +1 -0
  177. package/vendor/core/lib/Types/LabelAssociation.d.ts +30 -0
  178. package/vendor/core/lib/Types/LabelAssociation.d.ts.map +1 -0
  179. package/vendor/core/lib/Types/LabelAssociation.js +7 -0
  180. package/vendor/core/lib/Types/LabelAssociation.js.map +1 -0
  181. package/vendor/core/lib/Types/Message.d.ts +422 -0
  182. package/vendor/core/lib/Types/Message.d.ts.map +1 -0
  183. package/vendor/core/lib/Types/Message.js +11 -0
  184. package/vendor/core/lib/Types/Message.js.map +1 -0
  185. package/vendor/core/lib/Types/Newsletter.d.ts +135 -0
  186. package/vendor/core/lib/Types/Newsletter.d.ts.map +1 -0
  187. package/vendor/core/lib/Types/Newsletter.js +31 -0
  188. package/vendor/core/lib/Types/Newsletter.js.map +1 -0
  189. package/vendor/core/lib/Types/Product.d.ts +79 -0
  190. package/vendor/core/lib/Types/Product.d.ts.map +1 -0
  191. package/vendor/core/lib/Types/Product.js +2 -0
  192. package/vendor/core/lib/Types/Product.js.map +1 -0
  193. package/vendor/core/lib/Types/Signal.d.ts +76 -0
  194. package/vendor/core/lib/Types/Signal.d.ts.map +1 -0
  195. package/vendor/core/lib/Types/Signal.js +2 -0
  196. package/vendor/core/lib/Types/Signal.js.map +1 -0
  197. package/vendor/core/lib/Types/Socket.d.ts +158 -0
  198. package/vendor/core/lib/Types/Socket.d.ts.map +1 -0
  199. package/vendor/core/lib/Types/Socket.js +3 -0
  200. package/vendor/core/lib/Types/Socket.js.map +1 -0
  201. package/vendor/core/lib/Types/State.d.ts +39 -0
  202. package/vendor/core/lib/Types/State.d.ts.map +1 -0
  203. package/vendor/core/lib/Types/State.js +13 -0
  204. package/vendor/core/lib/Types/State.js.map +1 -0
  205. package/vendor/core/lib/Types/USync.d.ts +26 -0
  206. package/vendor/core/lib/Types/USync.d.ts.map +1 -0
  207. package/vendor/core/lib/Types/USync.js +2 -0
  208. package/vendor/core/lib/Types/USync.js.map +1 -0
  209. package/vendor/core/lib/Types/index.d.ts +66 -0
  210. package/vendor/core/lib/Types/index.d.ts.map +1 -0
  211. package/vendor/core/lib/Types/index.js +33 -0
  212. package/vendor/core/lib/Types/index.js.map +1 -0
  213. package/vendor/core/lib/Utils/auth-utils.d.ts +19 -0
  214. package/vendor/core/lib/Utils/auth-utils.d.ts.map +1 -0
  215. package/vendor/core/lib/Utils/auth-utils.js +290 -0
  216. package/vendor/core/lib/Utils/auth-utils.js.map +1 -0
  217. package/vendor/core/lib/Utils/browser-utils.d.ts +4 -0
  218. package/vendor/core/lib/Utils/browser-utils.d.ts.map +1 -0
  219. package/vendor/core/lib/Utils/browser-utils.js +28 -0
  220. package/vendor/core/lib/Utils/browser-utils.js.map +1 -0
  221. package/vendor/core/lib/Utils/business.d.ts +23 -0
  222. package/vendor/core/lib/Utils/business.d.ts.map +1 -0
  223. package/vendor/core/lib/Utils/business.js +231 -0
  224. package/vendor/core/lib/Utils/business.js.map +1 -0
  225. package/vendor/core/lib/Utils/chat-utils.d.ts +70 -0
  226. package/vendor/core/lib/Utils/chat-utils.d.ts.map +1 -0
  227. package/vendor/core/lib/Utils/chat-utils.js +812 -0
  228. package/vendor/core/lib/Utils/chat-utils.js.map +1 -0
  229. package/vendor/core/lib/Utils/crypto.d.ts +37 -0
  230. package/vendor/core/lib/Utils/crypto.d.ts.map +1 -0
  231. package/vendor/core/lib/Utils/crypto.js +118 -0
  232. package/vendor/core/lib/Utils/crypto.js.map +1 -0
  233. package/vendor/core/lib/Utils/decode-wa-message.d.ts +48 -0
  234. package/vendor/core/lib/Utils/decode-wa-message.d.ts.map +1 -0
  235. package/vendor/core/lib/Utils/decode-wa-message.js +283 -0
  236. package/vendor/core/lib/Utils/decode-wa-message.js.map +1 -0
  237. package/vendor/core/lib/Utils/event-buffer.d.ts +34 -0
  238. package/vendor/core/lib/Utils/event-buffer.d.ts.map +1 -0
  239. package/vendor/core/lib/Utils/event-buffer.js +574 -0
  240. package/vendor/core/lib/Utils/event-buffer.js.map +1 -0
  241. package/vendor/core/lib/Utils/generics.d.ts +91 -0
  242. package/vendor/core/lib/Utils/generics.d.ts.map +1 -0
  243. package/vendor/core/lib/Utils/generics.js +384 -0
  244. package/vendor/core/lib/Utils/generics.js.map +1 -0
  245. package/vendor/core/lib/Utils/history.d.ts +22 -0
  246. package/vendor/core/lib/Utils/history.d.ts.map +1 -0
  247. package/vendor/core/lib/Utils/history.js +131 -0
  248. package/vendor/core/lib/Utils/history.js.map +1 -0
  249. package/vendor/core/lib/Utils/identity-change-handler.d.ts +37 -0
  250. package/vendor/core/lib/Utils/identity-change-handler.d.ts.map +1 -0
  251. package/vendor/core/lib/Utils/identity-change-handler.js +49 -0
  252. package/vendor/core/lib/Utils/identity-change-handler.js.map +1 -0
  253. package/vendor/core/lib/Utils/index.d.ts +23 -0
  254. package/vendor/core/lib/Utils/index.d.ts.map +1 -0
  255. package/vendor/core/lib/Utils/index.js +23 -0
  256. package/vendor/core/lib/Utils/index.js.map +1 -0
  257. package/vendor/core/lib/Utils/link-preview.d.ts +21 -0
  258. package/vendor/core/lib/Utils/link-preview.d.ts.map +1 -0
  259. package/vendor/core/lib/Utils/link-preview.js +85 -0
  260. package/vendor/core/lib/Utils/link-preview.js.map +1 -0
  261. package/vendor/core/lib/Utils/logger.d.ts +13 -0
  262. package/vendor/core/lib/Utils/logger.d.ts.map +1 -0
  263. package/vendor/core/lib/Utils/logger.js +3 -0
  264. package/vendor/core/lib/Utils/logger.js.map +1 -0
  265. package/vendor/core/lib/Utils/lt-hash.d.ts +8 -0
  266. package/vendor/core/lib/Utils/lt-hash.d.ts.map +1 -0
  267. package/vendor/core/lib/Utils/lt-hash.js +8 -0
  268. package/vendor/core/lib/Utils/lt-hash.js.map +1 -0
  269. package/vendor/core/lib/Utils/make-mutex.d.ts +9 -0
  270. package/vendor/core/lib/Utils/make-mutex.d.ts.map +1 -0
  271. package/vendor/core/lib/Utils/make-mutex.js +33 -0
  272. package/vendor/core/lib/Utils/make-mutex.js.map +1 -0
  273. package/vendor/core/lib/Utils/message-retry-manager.d.ts +110 -0
  274. package/vendor/core/lib/Utils/message-retry-manager.d.ts.map +1 -0
  275. package/vendor/core/lib/Utils/message-retry-manager.js +225 -0
  276. package/vendor/core/lib/Utils/message-retry-manager.js.map +1 -0
  277. package/vendor/core/lib/Utils/messages-media.d.ts +130 -0
  278. package/vendor/core/lib/Utils/messages-media.d.ts.map +1 -0
  279. package/vendor/core/lib/Utils/messages-media.js +782 -0
  280. package/vendor/core/lib/Utils/messages-media.js.map +1 -0
  281. package/vendor/core/lib/Utils/messages.d.ts +91 -0
  282. package/vendor/core/lib/Utils/messages.d.ts.map +1 -0
  283. package/vendor/core/lib/Utils/messages.js +1240 -0
  284. package/vendor/core/lib/Utils/messages.js.map +1 -0
  285. package/vendor/core/lib/Utils/noise-handler.d.ts +20 -0
  286. package/vendor/core/lib/Utils/noise-handler.d.ts.map +1 -0
  287. package/vendor/core/lib/Utils/noise-handler.js +201 -0
  288. package/vendor/core/lib/Utils/noise-handler.js.map +1 -0
  289. package/vendor/core/lib/Utils/offline-node-processor.d.ts +17 -0
  290. package/vendor/core/lib/Utils/offline-node-processor.d.ts.map +1 -0
  291. package/vendor/core/lib/Utils/offline-node-processor.js +40 -0
  292. package/vendor/core/lib/Utils/offline-node-processor.js.map +1 -0
  293. package/vendor/core/lib/Utils/pre-key-manager.d.ts +28 -0
  294. package/vendor/core/lib/Utils/pre-key-manager.d.ts.map +1 -0
  295. package/vendor/core/lib/Utils/pre-key-manager.js +106 -0
  296. package/vendor/core/lib/Utils/pre-key-manager.js.map +1 -0
  297. package/vendor/core/lib/Utils/process-message.d.ts +60 -0
  298. package/vendor/core/lib/Utils/process-message.d.ts.map +1 -0
  299. package/vendor/core/lib/Utils/process-message.js +528 -0
  300. package/vendor/core/lib/Utils/process-message.js.map +1 -0
  301. package/vendor/core/lib/Utils/reporting-utils.d.ts +11 -0
  302. package/vendor/core/lib/Utils/reporting-utils.d.ts.map +1 -0
  303. package/vendor/core/lib/Utils/reporting-utils.js +258 -0
  304. package/vendor/core/lib/Utils/reporting-utils.js.map +1 -0
  305. package/vendor/core/lib/Utils/rich-messages.d.ts +277 -0
  306. package/vendor/core/lib/Utils/rich-messages.d.ts.map +1 -0
  307. package/vendor/core/lib/Utils/rich-messages.js +1144 -0
  308. package/vendor/core/lib/Utils/rich-messages.js.map +1 -0
  309. package/vendor/core/lib/Utils/signal.d.ts +34 -0
  310. package/vendor/core/lib/Utils/signal.d.ts.map +1 -0
  311. package/vendor/core/lib/Utils/signal.js +159 -0
  312. package/vendor/core/lib/Utils/signal.js.map +1 -0
  313. package/vendor/core/lib/Utils/stanza-ack.d.ts +11 -0
  314. package/vendor/core/lib/Utils/stanza-ack.d.ts.map +1 -0
  315. package/vendor/core/lib/Utils/stanza-ack.js +38 -0
  316. package/vendor/core/lib/Utils/stanza-ack.js.map +1 -0
  317. package/vendor/core/lib/Utils/sticker-pack.d.ts +10 -0
  318. package/vendor/core/lib/Utils/sticker-pack.js +111 -0
  319. package/vendor/core/lib/Utils/sync-action-utils.d.ts +19 -0
  320. package/vendor/core/lib/Utils/sync-action-utils.d.ts.map +1 -0
  321. package/vendor/core/lib/Utils/sync-action-utils.js +48 -0
  322. package/vendor/core/lib/Utils/sync-action-utils.js.map +1 -0
  323. package/vendor/core/lib/Utils/tc-token-utils.d.ts +12 -0
  324. package/vendor/core/lib/Utils/tc-token-utils.d.ts.map +1 -0
  325. package/vendor/core/lib/Utils/tc-token-utils.js +18 -0
  326. package/vendor/core/lib/Utils/tc-token-utils.js.map +1 -0
  327. package/vendor/core/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  328. package/vendor/core/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  329. package/vendor/core/lib/Utils/use-multi-file-auth-state.js +121 -0
  330. package/vendor/core/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  331. package/vendor/core/lib/Utils/validate-connection.d.ts +11 -0
  332. package/vendor/core/lib/Utils/validate-connection.d.ts.map +1 -0
  333. package/vendor/core/lib/Utils/validate-connection.js +200 -0
  334. package/vendor/core/lib/Utils/validate-connection.js.map +1 -0
  335. package/vendor/core/lib/WABinary/constants.d.ts +28 -0
  336. package/vendor/core/lib/WABinary/constants.d.ts.map +1 -0
  337. package/vendor/core/lib/WABinary/constants.js +1301 -0
  338. package/vendor/core/lib/WABinary/constants.js.map +1 -0
  339. package/vendor/core/lib/WABinary/decode.d.ts +7 -0
  340. package/vendor/core/lib/WABinary/decode.d.ts.map +1 -0
  341. package/vendor/core/lib/WABinary/decode.js +262 -0
  342. package/vendor/core/lib/WABinary/decode.js.map +1 -0
  343. package/vendor/core/lib/WABinary/encode.d.ts +3 -0
  344. package/vendor/core/lib/WABinary/encode.d.ts.map +1 -0
  345. package/vendor/core/lib/WABinary/encode.js +220 -0
  346. package/vendor/core/lib/WABinary/encode.js.map +1 -0
  347. package/vendor/core/lib/WABinary/generic-utils.d.ts +17 -0
  348. package/vendor/core/lib/WABinary/generic-utils.d.ts.map +1 -0
  349. package/vendor/core/lib/WABinary/generic-utils.js +191 -0
  350. package/vendor/core/lib/WABinary/generic-utils.js.map +1 -0
  351. package/vendor/core/lib/WABinary/index.d.ts +6 -0
  352. package/vendor/core/lib/WABinary/index.d.ts.map +1 -0
  353. package/vendor/core/lib/WABinary/index.js +6 -0
  354. package/vendor/core/lib/WABinary/index.js.map +1 -0
  355. package/vendor/core/lib/WABinary/jid-utils.d.ts +48 -0
  356. package/vendor/core/lib/WABinary/jid-utils.d.ts.map +1 -0
  357. package/vendor/core/lib/WABinary/jid-utils.js +96 -0
  358. package/vendor/core/lib/WABinary/jid-utils.js.map +1 -0
  359. package/vendor/core/lib/WABinary/types.d.ts +19 -0
  360. package/vendor/core/lib/WABinary/types.d.ts.map +1 -0
  361. package/vendor/core/lib/WABinary/types.js +2 -0
  362. package/vendor/core/lib/WABinary/types.js.map +1 -0
  363. package/vendor/core/lib/WAM/BinaryInfo.d.ts +9 -0
  364. package/vendor/core/lib/WAM/BinaryInfo.d.ts.map +1 -0
  365. package/vendor/core/lib/WAM/BinaryInfo.js +10 -0
  366. package/vendor/core/lib/WAM/BinaryInfo.js.map +1 -0
  367. package/vendor/core/lib/WAM/constants.d.ts +40 -0
  368. package/vendor/core/lib/WAM/constants.d.ts.map +1 -0
  369. package/vendor/core/lib/WAM/constants.js +22853 -0
  370. package/vendor/core/lib/WAM/constants.js.map +1 -0
  371. package/vendor/core/lib/WAM/encode.d.ts +3 -0
  372. package/vendor/core/lib/WAM/encode.d.ts.map +1 -0
  373. package/vendor/core/lib/WAM/encode.js +150 -0
  374. package/vendor/core/lib/WAM/encode.js.map +1 -0
  375. package/vendor/core/lib/WAM/index.d.ts +4 -0
  376. package/vendor/core/lib/WAM/index.d.ts.map +1 -0
  377. package/vendor/core/lib/WAM/index.js +4 -0
  378. package/vendor/core/lib/WAM/index.js.map +1 -0
  379. package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  380. package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  381. package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  382. package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  383. package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  384. package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  385. package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  386. package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  387. package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  388. package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  389. package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  390. package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  391. package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  392. package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  393. package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  394. package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  395. package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  396. package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  397. package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  398. package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  399. package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  400. package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  401. package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  402. package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  403. package/vendor/core/lib/WAUSync/Protocols/index.d.ts +5 -0
  404. package/vendor/core/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  405. package/vendor/core/lib/WAUSync/Protocols/index.js +5 -0
  406. package/vendor/core/lib/WAUSync/Protocols/index.js.map +1 -0
  407. package/vendor/core/lib/WAUSync/USyncQuery.d.ts +29 -0
  408. package/vendor/core/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  409. package/vendor/core/lib/WAUSync/USyncQuery.js +94 -0
  410. package/vendor/core/lib/WAUSync/USyncQuery.js.map +1 -0
  411. package/vendor/core/lib/WAUSync/USyncUser.d.ts +13 -0
  412. package/vendor/core/lib/WAUSync/USyncUser.d.ts.map +1 -0
  413. package/vendor/core/lib/WAUSync/USyncUser.js +23 -0
  414. package/vendor/core/lib/WAUSync/USyncUser.js.map +1 -0
  415. package/vendor/core/lib/WAUSync/index.d.ts +4 -0
  416. package/vendor/core/lib/WAUSync/index.d.ts.map +1 -0
  417. package/vendor/core/lib/WAUSync/index.js +4 -0
  418. package/vendor/core/lib/WAUSync/index.js.map +1 -0
  419. package/vendor/core/lib/index.d.ts +13 -0
  420. package/vendor/core/lib/index.d.ts.map +1 -0
  421. package/vendor/core/lib/index.js +13 -0
  422. package/vendor/core/lib/index.js.map +1 -0
package/src/Bot.js ADDED
@@ -0,0 +1,327 @@
1
+ import makeWASocket, {
2
+ DisconnectReason,
3
+ useMultiFileAuthState,
4
+ } from "../vendor/core/lib/index.js";
5
+ import qrcode from "qrcode-terminal";
6
+ import pino from "pino";
7
+ import { PluginManager } from "./PluginManager.js";
8
+ import { Context } from "./Context.js";
9
+ import { loadConfig, mergeConfig } from "./config/index.js";
10
+ import { logger } from "./middlewares/logger.js";
11
+ import { cooldown } from "./middlewares/cooldown.js";
12
+ import { permission } from "./middlewares/permission.js";
13
+ import { antiSpam } from "./middlewares/antiSpam.js";
14
+
15
+ /**
16
+ * Bot
17
+ * ---
18
+ * The main class of the Botify framework. Handles:
19
+ * - config.bt loading + merging with programmatic options (code wins)
20
+ * - auth/session (via multi-file auth state)
21
+ * - connection lifecycle + auto-reconnect (with exponential backoff)
22
+ * - built-in middlewares: logger, permission, cooldown, antiSpam
23
+ * (auto-registered in the correct order; user middlewares run after)
24
+ * - command routing through PluginManager
25
+ * - a small event API on top of the underlying WhatsApp connection
26
+ * - centralized error handling for command/middleware failures
27
+ *
28
+ * Everything low-level (encryption, sockets, WA protocol) lives in
29
+ * vendor/core — Botify only adds structure on top.
30
+ */
31
+ export class Bot {
32
+ /**
33
+ * @param {{
34
+ * sessionPath?: string,
35
+ * prefix?: string,
36
+ * logLevel?: "trace"|"debug"|"info"|"warn"|"error"|"fatal"|"silent",
37
+ * printQR?: boolean,
38
+ * socketConfig?: object,
39
+ * owners?: string[],
40
+ *
41
+ * defaultCooldown?: number, // ms; applied when command omits cooldown
42
+ * cooldownMessage?: string | ((ctx, remainingMs) => string),
43
+ *
44
+ * antiSpam?: {
45
+ * enabled?: boolean,
46
+ * windowMs?: number,
47
+ * maxMessages?: number,
48
+ * message?: string | ((ctx) => string),
49
+ * },
50
+ *
51
+ * permissionMessages?: {
52
+ * owner?: string | ((ctx) => string),
53
+ * admin?: string | ((ctx) => string),
54
+ * },
55
+ *
56
+ * logger?: {
57
+ * enabled?: boolean,
58
+ * showPn?: boolean,
59
+ * logFn?: (line: string) => void,
60
+ * format?: (ctx) => string | Promise<string>,
61
+ * },
62
+ *
63
+ * reconnectBaseDelay?: number, // ms, default 1000
64
+ * reconnectMaxDelay?: number, // ms, default 30000
65
+ *
66
+ * configDir?: string, // directory to look for config.bt (default: cwd)
67
+ * }} [options]
68
+ */
69
+ constructor(options = {}) {
70
+ // ── 1. Load config.bt then merge (code options override file) ────────────
71
+ const fileConfig = loadConfig(options.configDir);
72
+ const cfg = mergeConfig(fileConfig, options);
73
+
74
+ this.options = {
75
+ sessionPath: cfg.sessionPath ?? "./session",
76
+ prefix: cfg.prefix ?? "!",
77
+ printQR: cfg.printQR ?? true,
78
+ logLevel: cfg.logLevel ?? "error",
79
+ socketConfig: cfg.socketConfig ?? {},
80
+ owners: cfg.owners ?? [],
81
+
82
+ defaultCooldown: cfg.defaultCooldown ?? 0,
83
+ cooldownMessage: cfg.cooldownMessage,
84
+
85
+ antiSpam: {
86
+ enabled: cfg.antiSpam?.enabled ?? false,
87
+ windowMs: cfg.antiSpam?.windowMs ?? 5_000,
88
+ maxMessages: cfg.antiSpam?.maxMessages ?? 5,
89
+ message: cfg.antiSpam?.message,
90
+ },
91
+
92
+ permissionMessages: {
93
+ owner: cfg.permissionMessages?.owner,
94
+ admin: cfg.permissionMessages?.admin,
95
+ },
96
+
97
+ logger: {
98
+ enabled: cfg.logger?.enabled ?? true,
99
+ showPn: cfg.logger?.showPn ?? true,
100
+ logFn: cfg.logger?.logFn,
101
+ format: cfg.logger?.format,
102
+ },
103
+
104
+ reconnectBaseDelay: cfg.reconnectBaseDelay ?? 1_000,
105
+ reconnectMaxDelay: cfg.reconnectMaxDelay ?? 30_000,
106
+ };
107
+
108
+ this.plugins = new PluginManager();
109
+ this.sock = null;
110
+
111
+ /** @type {Map<string, Function[]>} */
112
+ this._listeners = new Map();
113
+ /** @type {Function[]} */
114
+ this._errorHandlers = [];
115
+ this._reconnectAttempts = 0;
116
+ this._reconnectTimer = null;
117
+
118
+ // ── 2. Register built-in middlewares in the correct order ────────────────
119
+ //
120
+ // Order matters:
121
+ // 1. logger — log first so every attempt is recorded, even blocked ones
122
+ // 2. antiSpam — global rate limit before any per-command check
123
+ // 3. permission — owner / admin gate
124
+ // 4. cooldown — per-user per-command throttle
125
+ // (user middlewares added via bot.use() run after all of the above)
126
+
127
+ this.plugins.use(
128
+ logger({
129
+ enabled: this.options.logger.enabled,
130
+ showPn: this.options.logger.showPn,
131
+ logFn: this.options.logger.logFn,
132
+ format: this.options.logger.format,
133
+ })
134
+ );
135
+
136
+ if (this.options.antiSpam.enabled) {
137
+ this.plugins.use(
138
+ antiSpam({
139
+ windowMs: this.options.antiSpam.windowMs,
140
+ maxMessages: this.options.antiSpam.maxMessages,
141
+ message: this.options.antiSpam.message,
142
+ })
143
+ );
144
+ }
145
+
146
+ this.plugins.use(
147
+ permission({
148
+ ownerMessage: this.options.permissionMessages.owner,
149
+ adminMessage: this.options.permissionMessages.admin,
150
+ emitEvent: (ctx, reason) => this._emit("noPermission", ctx, reason),
151
+ })
152
+ );
153
+
154
+ this.plugins.use(
155
+ cooldown({
156
+ defaultCooldown: this.options.defaultCooldown,
157
+ message: this.options.cooldownMessage,
158
+ emitEvent: (ctx, remainingMs) => this._emit("cooldown", ctx, remainingMs),
159
+ })
160
+ );
161
+ }
162
+
163
+ // ── Public API ─────────────────────────────────────────────────────────────
164
+
165
+ /** Register a command. Shortcut for bot.plugins.command(...) */
166
+ command(name, handler, opts) {
167
+ this.plugins.command(name, handler, opts);
168
+ return this;
169
+ }
170
+
171
+ /**
172
+ * Register a user middleware.
173
+ * Runs AFTER all built-in middlewares (logger → antiSpam → permission → cooldown).
174
+ */
175
+ use(fn) {
176
+ this.plugins.use(fn);
177
+ return this;
178
+ }
179
+
180
+ /**
181
+ * Listen to a Botify-level event:
182
+ * "ready", "message", "disconnect", "unknownCommand",
183
+ * "cooldown", "noPermission", "qr"
184
+ * Raw core connection events are still available via bot.sock.ev.
185
+ */
186
+ on(event, handler) {
187
+ if (!this._listeners.has(event)) this._listeners.set(event, []);
188
+ this._listeners.get(event).push(handler);
189
+ return this;
190
+ }
191
+
192
+ /**
193
+ * Register a global error handler. Called whenever a command handler or
194
+ * middleware throws. Multiple handlers can be registered; all run.
195
+ * If none are registered, errors are logged to console.
196
+ * @param {(error: unknown, ctx: import('./Context.js').Context) => any} fn
197
+ */
198
+ onError(fn) {
199
+ this._errorHandlers.push(fn);
200
+ return this;
201
+ }
202
+
203
+ // ── Internals ──────────────────────────────────────────────────────────────
204
+
205
+ _emit(event, ...args) {
206
+ for (const fn of this._listeners.get(event) ?? []) fn(...args);
207
+ }
208
+
209
+ async _handleError(error, ctx) {
210
+ if (this._errorHandlers.length === 0) {
211
+ console.error("[botify] Unhandled error in command/middleware:", error);
212
+ return;
213
+ }
214
+ for (const fn of this._errorHandlers) {
215
+ try {
216
+ await fn(error, ctx);
217
+ } catch (handlerError) {
218
+ console.error("[botify] Error inside onError handler:", handlerError);
219
+ }
220
+ }
221
+ }
222
+
223
+ /** Boot the bot: load/create session, connect, wire up listeners. */
224
+ async start() {
225
+ if (this._reconnectTimer) {
226
+ clearTimeout(this._reconnectTimer);
227
+ this._reconnectTimer = null;
228
+ }
229
+
230
+ const { state, saveCreds } = await useMultiFileAuthState(
231
+ this.options.sessionPath
232
+ );
233
+
234
+ const logger_ =
235
+ this.options.socketConfig.logger ??
236
+ pino({ level: this.options.logLevel });
237
+
238
+ this.sock = makeWASocket({
239
+ auth: state,
240
+ ...this.options.socketConfig,
241
+ logger: logger_,
242
+ });
243
+
244
+ this.sock.ev.on("creds.update", saveCreds);
245
+
246
+ this.sock.ev.on("connection.update", (update) => {
247
+ const { connection, lastDisconnect, qr } = update;
248
+
249
+ if (qr) {
250
+ if (this.options.printQR) {
251
+ qrcode.generate(qr, { small: true });
252
+ console.log("Scan the QR code above with WhatsApp (Linked Devices).");
253
+ }
254
+ this._emit("qr", qr);
255
+ }
256
+
257
+ if (connection === "open") {
258
+ this._reconnectAttempts = 0;
259
+ this._emit("ready", this.sock);
260
+ }
261
+
262
+ if (connection === "close") {
263
+ const statusCode = lastDisconnect?.error?.output?.statusCode;
264
+ const shouldReconnect = statusCode !== DisconnectReason.loggedOut;
265
+
266
+ if (shouldReconnect) {
267
+ const delay = Math.min(
268
+ this.options.reconnectBaseDelay * 2 ** this._reconnectAttempts,
269
+ this.options.reconnectMaxDelay
270
+ );
271
+ this._reconnectAttempts++;
272
+
273
+ this._emit("disconnect", {
274
+ statusCode,
275
+ willReconnect: true,
276
+ reconnectDelayMs: delay,
277
+ attempt: this._reconnectAttempts,
278
+ });
279
+
280
+ this._reconnectTimer = setTimeout(() => this.start(), delay);
281
+ } else {
282
+ this._emit("disconnect", { statusCode, willReconnect: false });
283
+ }
284
+ }
285
+ });
286
+
287
+ this.sock.ev.on("messages.upsert", async ({ messages, type }) => {
288
+ if (type !== "notify") return;
289
+
290
+ for (const raw of messages) {
291
+ if (!raw.message) continue;
292
+
293
+ const ctx = new Context(this.sock, raw, {
294
+ prefix: this.options.prefix,
295
+ owners: this.options.owners,
296
+ });
297
+
298
+ this._emit("message", ctx);
299
+
300
+ if (!ctx.command) continue;
301
+
302
+ const result = await this.plugins.dispatch(ctx.command, ctx);
303
+
304
+ switch (result.status) {
305
+ case "not_found":
306
+ this._emit("unknownCommand", ctx);
307
+ break;
308
+ case "error":
309
+ await this._handleError(result.error, ctx);
310
+ break;
311
+ // "ok" and "stopped" need no further action
312
+ }
313
+ }
314
+ });
315
+
316
+ return this.sock;
317
+ }
318
+
319
+ /** Gracefully close the socket. */
320
+ async stop() {
321
+ if (this._reconnectTimer) {
322
+ clearTimeout(this._reconnectTimer);
323
+ this._reconnectTimer = null;
324
+ }
325
+ await this.sock?.end?.(undefined);
326
+ }
327
+ }
package/src/Context.js ADDED
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Context
3
+ * -------
4
+ * A friendly, per-message object passed to every command/middleware.
5
+ * Wraps the raw connection message + gives shortcuts like ctx.reply().
6
+ */
7
+ export class Context {
8
+ /**
9
+ * @param {import('../vendor/core/lib/index.js').WASocket} sock
10
+ * @param {import('../vendor/core/lib/index.js').proto.IWebMessageInfo} rawMessage
11
+ * @param {{ prefix: string, owners?: string[] }} botConfig
12
+ */
13
+ constructor(sock, rawMessage, botConfig) {
14
+ this.sock = sock;
15
+ this.raw = rawMessage;
16
+ this.botConfig = botConfig;
17
+
18
+ const key = rawMessage.key;
19
+
20
+ // WhatsApp is rolling out "LID" (Linked ID, @lid) as an alternate,
21
+ // non-phone-number identifier — mainly in DMs and communities, for
22
+ // privacy. When that's active, the "primary" field (participant /
23
+ // remoteJid) may hold the @lid form while the real phone-number JID
24
+ // sits in the matching "Alt" field (participantAlt / remoteJidAlt),
25
+ // or vice versa. We fall back through all four so `sender`/`from`
26
+ // are never silently empty just because WhatsApp picked a different
27
+ // field for this account/chat.
28
+ this.from = key.remoteJid || key.remoteJidAlt || "";
29
+ this.sender =
30
+ key.participant || key.remoteJid || key.participantAlt || key.remoteJidAlt || "";
31
+ this.fromMe = !!key.fromMe;
32
+
33
+ /**
34
+ * Kind of chat this message came from:
35
+ * "group" | "channel" (newsletter) | "broadcast" | "private"
36
+ */
37
+ this.chatType = detectChatType(this.from);
38
+ /** Convenience boolean, derived from chatType (kept for backwards compat). */
39
+ this.isGroup = this.chatType === "group";
40
+
41
+ /** Display name of the sender as WhatsApp reports it (may be stale/unset). */
42
+ this.pushName = rawMessage.pushName || "Unknown";
43
+
44
+ /** The sender's @lid identifier, if WhatsApp provided one — else null. */
45
+ this.senderLid = [key.participant, key.participantAlt, key.remoteJid, key.remoteJidAlt]
46
+ .find((jid) => isLid(jid)) ?? null;
47
+ /** The sender's real phone-number JID (@s.whatsapp.net), if available — else null. */
48
+ this.senderPn = [key.participant, key.participantAlt, key.remoteJid, key.remoteJidAlt]
49
+ .find((jid) => isPn(jid)) ?? null;
50
+
51
+ /**
52
+ * Sender's phone number with no @domain suffix, e.g. "6281234567890".
53
+ * Prefers the real phone-number JID over a LID (a LID's numeric part
54
+ * is NOT a phone number, so it's only used here as a last resort).
55
+ */
56
+ this.senderNumber = (this.senderPn ?? this.senderLid ?? this.sender).split("@")[0] ?? "";
57
+
58
+ this.text = extractText(rawMessage);
59
+ const parsed = parseCommand(this.text, botConfig.prefix);
60
+ this.command = parsed.command;
61
+ this.args = parsed.args;
62
+
63
+ /** Whether the sender is listed in Bot({ owners: [...] }). */
64
+ this.isOwner = (botConfig.owners ?? []).includes(this.sender);
65
+
66
+ /**
67
+ * Cache for the group metadata fetch, shared by isGroupAdmin() and
68
+ * getGroupName() so a single incoming message never fetches it twice.
69
+ * undefined = not fetched yet, null = fetched but unavailable/failed.
70
+ * @type {object | null | undefined}
71
+ */
72
+ this._groupMetadataCache = undefined;
73
+ }
74
+
75
+ /** Reply in the same chat, quoting the triggering message. */
76
+ async reply(text, opts = {}) {
77
+ return this.sock.sendMessage(
78
+ this.from,
79
+ { text, ...opts },
80
+ { quoted: this.raw }
81
+ );
82
+ }
83
+
84
+ /** Send any core-compatible message content to the same chat. */
85
+ async send(content, opts = {}) {
86
+ return this.sock.sendMessage(this.from, content, opts);
87
+ }
88
+
89
+ /** React to the triggering message with an emoji. */
90
+ async react(emoji) {
91
+ return this.sock.sendMessage(this.from, {
92
+ react: { text: emoji, key: this.raw.key },
93
+ });
94
+ }
95
+
96
+ /**
97
+ * Fetches (and caches) this chat's group metadata. Returns `null` outside
98
+ * of groups, or if the fetch fails (e.g. bot was removed from the group).
99
+ */
100
+ async _getGroupMetadata() {
101
+ if (this.chatType !== "group") return null;
102
+ if (this._groupMetadataCache !== undefined) return this._groupMetadataCache;
103
+
104
+ try {
105
+ this._groupMetadataCache = await this.sock.groupMetadata(this.from);
106
+ } catch {
107
+ this._groupMetadataCache = null;
108
+ }
109
+ return this._groupMetadataCache;
110
+ }
111
+
112
+ /**
113
+ * Whether the sender is an admin (or superadmin) of the current group.
114
+ * Always `false` outside of groups.
115
+ */
116
+ async isGroupAdmin() {
117
+ const metadata = await this._getGroupMetadata();
118
+ if (!metadata) return false;
119
+
120
+ const ids = [this.sender, this.senderLid, this.senderPn].filter(Boolean);
121
+ const participant = metadata.participants.find((p) => ids.includes(p.id));
122
+ return (
123
+ !!participant &&
124
+ (participant.admin === "admin" || participant.admin === "superadmin")
125
+ );
126
+ }
127
+
128
+ /**
129
+ * The current group's display name (its "subject"). Returns `null`
130
+ * outside of groups or if metadata couldn't be fetched.
131
+ */
132
+ async getGroupName() {
133
+ const metadata = await this._getGroupMetadata();
134
+ return metadata?.subject ?? null;
135
+ }
136
+
137
+ /**
138
+ * A human-readable one-liner describing where this message came from,
139
+ * handy for logging: e.g. `group "Koalisi Community"` or `private chat`.
140
+ */
141
+ async describeChat() {
142
+ if (this.chatType === "group") {
143
+ const name = await this.getGroupName();
144
+ return `group "${name ?? this.from}"`;
145
+ }
146
+ if (this.chatType === "channel") return "channel";
147
+ if (this.chatType === "broadcast") return "broadcast list";
148
+ return "private chat";
149
+ }
150
+ }
151
+
152
+ function isLid(jid) {
153
+ return typeof jid === "string" && jid.endsWith("@lid");
154
+ }
155
+
156
+ function isPn(jid) {
157
+ return typeof jid === "string" && jid.endsWith("@s.whatsapp.net");
158
+ }
159
+
160
+ function detectChatType(jid) {
161
+ if (jid.endsWith("@g.us")) return "group";
162
+ if (jid.endsWith("@newsletter")) return "channel";
163
+ if (jid.endsWith("@broadcast")) return "broadcast";
164
+ return "private";
165
+ }
166
+
167
+ function extractText(msg) {
168
+ const m = msg.message;
169
+ if (!m) return "";
170
+ return (
171
+ m.conversation ??
172
+ m.extendedTextMessage?.text ??
173
+ m.imageMessage?.caption ??
174
+ m.videoMessage?.caption ??
175
+ ""
176
+ );
177
+ }
178
+
179
+ function parseCommand(text, prefix) {
180
+ if (!text || !text.startsWith(prefix)) {
181
+ return { command: null, args: [] };
182
+ }
183
+ const [command, ...args] = text.slice(prefix.length).trim().split(/\s+/);
184
+ return { command: command?.toLowerCase() ?? null, args };
185
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * PluginManager
3
+ * -------------
4
+ * Registers commands and middleware, then dispatches incoming messages
5
+ * to the right handler.
6
+ *
7
+ * Cooldown and permission logic have been moved to built-in middlewares
8
+ * (src/middlewares/cooldown.js and src/middlewares/permission.js).
9
+ * PluginManager's job is now strictly:
10
+ * 1. Store command definitions.
11
+ * 2. Attach the matched command entry to ctx._commandEntry so middlewares
12
+ * can read per-command opts (cooldown ms, owner flag, admin flag).
13
+ * 3. Run the middleware chain.
14
+ * 4. Call the handler if the chain wasn't stopped.
15
+ * 5. Return a typed result object — never throw.
16
+ */
17
+ export class PluginManager {
18
+ constructor() {
19
+ /**
20
+ * @type {Map<string, {
21
+ * handler: Function,
22
+ * description?: string,
23
+ * cooldown?: number,
24
+ * owner?: boolean,
25
+ * admin?: boolean
26
+ * }>}
27
+ */
28
+ this.commands = new Map();
29
+ /** @type {Array<Function>} */
30
+ this.middlewares = [];
31
+ }
32
+
33
+ /**
34
+ * Register a command.
35
+ * @param {string} name - command name without prefix (e.g. "ping")
36
+ * @param {(ctx: import('./Context.js').Context) => any} handler
37
+ * @param {{
38
+ * description?: string,
39
+ * aliases?: string[],
40
+ * cooldown?: number, // ms — read by the cooldown middleware
41
+ * owner?: boolean, // read by the permission middleware
42
+ * admin?: boolean // read by the permission middleware
43
+ * }} [opts]
44
+ */
45
+ command(name, handler, opts = {}) {
46
+ const entry = {
47
+ handler,
48
+ description: opts.description ?? "",
49
+ cooldown: opts.cooldown ?? 0,
50
+ owner: opts.owner ?? false,
51
+ admin: opts.admin ?? false,
52
+ };
53
+
54
+ this.commands.set(name.toLowerCase(), entry);
55
+ for (const alias of opts.aliases ?? []) {
56
+ this.commands.set(alias.toLowerCase(), entry);
57
+ }
58
+ return this;
59
+ }
60
+
61
+ /**
62
+ * Register a middleware that runs before command dispatch.
63
+ * Return `false` from a middleware to stop the chain.
64
+ * @param {(ctx: import('./Context.js').Context) => any} fn
65
+ */
66
+ use(fn) {
67
+ this.middlewares.push(fn);
68
+ return this;
69
+ }
70
+
71
+ /** List all unique registered commands (deduplicates aliases). */
72
+ list() {
73
+ const seen = new Set();
74
+ const out = [];
75
+ for (const [key, entry] of this.commands.entries()) {
76
+ if (seen.has(entry)) continue;
77
+ seen.add(entry);
78
+ const aliases = [...this.commands.entries()]
79
+ .filter(([k, v]) => v === entry && k !== key)
80
+ .map(([k]) => k);
81
+ out.push({
82
+ name: key,
83
+ aliases,
84
+ description: entry.description,
85
+ cooldown: entry.cooldown,
86
+ owner: entry.owner,
87
+ admin: entry.admin,
88
+ });
89
+ }
90
+ return out;
91
+ }
92
+
93
+ has(name) {
94
+ return this.commands.has(name.toLowerCase());
95
+ }
96
+
97
+ /**
98
+ * Attach command entry, run middlewares, then call the handler.
99
+ * Never throws — errors are caught and returned as a result object.
100
+ *
101
+ * @param {string} name
102
+ * @param {import('./Context.js').Context} ctx
103
+ * @returns {Promise<
104
+ * | { status: "not_found" }
105
+ * | { status: "stopped" }
106
+ * | { status: "ok" }
107
+ * | { status: "error", error: unknown }
108
+ * >}
109
+ */
110
+ async dispatch(name, ctx) {
111
+ const key = name.toLowerCase();
112
+ const entry = this.commands.get(key);
113
+ if (!entry) return { status: "not_found" };
114
+
115
+ try {
116
+ // Attach command metadata to ctx so middlewares (cooldown, permission)
117
+ // can read per-command options without needing a registry reference.
118
+ ctx._commandEntry = entry;
119
+
120
+ for (const mw of this.middlewares) {
121
+ const result = await mw(ctx);
122
+ if (result === false) return { status: "stopped" };
123
+ }
124
+
125
+ await entry.handler(ctx);
126
+ return { status: "ok" };
127
+ } catch (error) {
128
+ return { status: "error", error };
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,2 @@
1
+ export { parse, loadConfig } from "./parser.js";
2
+ export { mergeConfig } from "./schema.js";