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.
- package/LICENSE +23 -0
- package/README.md +101 -0
- package/bin/botify.js +198 -0
- package/package.json +61 -0
- package/src/Bot.js +327 -0
- package/src/Context.js +185 -0
- package/src/PluginManager.js +131 -0
- package/src/config/index.js +2 -0
- package/src/config/parser.js +114 -0
- package/src/config/schema.js +106 -0
- package/src/index.js +18 -0
- package/src/loader.js +87 -0
- package/src/middlewares/antiSpam.js +75 -0
- package/src/middlewares/cooldown.js +81 -0
- package/src/middlewares/index.js +4 -0
- package/src/middlewares/logger.js +47 -0
- package/src/middlewares/permission.js +60 -0
- package/src/templates/config.bt +37 -0
- package/src/templates/index.js +34 -0
- package/src/templates/plugins/info.js +25 -0
- package/src/templates/plugins/menu.js +30 -0
- package/src/templates/plugins/ping.js +13 -0
- package/vendor/core/WAProto/GenerateStatics.sh +3 -0
- package/vendor/core/WAProto/WAProto.proto +6035 -0
- package/vendor/core/WAProto/fix-imports.js +81 -0
- package/vendor/core/WAProto/index.d.ts +2 -0
- package/vendor/core/WAProto/index.js +132326 -0
- package/vendor/core/lib/Defaults/index.d.ts +74 -0
- package/vendor/core/lib/Defaults/index.d.ts.map +1 -0
- package/vendor/core/lib/Defaults/index.js +140 -0
- package/vendor/core/lib/Defaults/index.js.map +1 -0
- package/vendor/core/lib/Modded/message_builder.js +2330 -0
- package/vendor/core/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/vendor/core/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/ciphertext-message.js +12 -0
- package/vendor/core/lib/Signal/Group/ciphertext-message.js.map +1 -0
- package/vendor/core/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/vendor/core/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/group-session-builder.js +30 -0
- package/vendor/core/lib/Signal/Group/group-session-builder.js.map +1 -0
- package/vendor/core/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/vendor/core/lib/Signal/Group/group_cipher.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/group_cipher.js +82 -0
- package/vendor/core/lib/Signal/Group/group_cipher.js.map +1 -0
- package/vendor/core/lib/Signal/Group/index.d.ts +12 -0
- package/vendor/core/lib/Signal/Group/index.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/index.js +12 -0
- package/vendor/core/lib/Signal/Group/index.js.map +1 -0
- package/vendor/core/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/vendor/core/lib/Signal/Group/keyhelper.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/keyhelper.js +18 -0
- package/vendor/core/lib/Signal/Group/keyhelper.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/vendor/core/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-chain-key.js +26 -0
- package/vendor/core/lib/Signal/Group/sender-chain-key.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/vendor/core/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/vendor/core/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/vendor/core/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-message.js +66 -0
- package/vendor/core/lib/Signal/Group/sender-key-message.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/vendor/core/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-name.js +48 -0
- package/vendor/core/lib/Signal/Group/sender-key-name.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/vendor/core/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-record.js +41 -0
- package/vendor/core/lib/Signal/Group/sender-key-record.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/vendor/core/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-key-state.js +84 -0
- package/vendor/core/lib/Signal/Group/sender-key-state.js.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/vendor/core/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
- package/vendor/core/lib/Signal/Group/sender-message-key.js +26 -0
- package/vendor/core/lib/Signal/Group/sender-message-key.js.map +1 -0
- package/vendor/core/lib/Signal/libsignal.d.ts +5 -0
- package/vendor/core/lib/Signal/libsignal.d.ts.map +1 -0
- package/vendor/core/lib/Signal/libsignal.js +403 -0
- package/vendor/core/lib/Signal/libsignal.js.map +1 -0
- package/vendor/core/lib/Signal/lid-mapping.d.ts +19 -0
- package/vendor/core/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/vendor/core/lib/Signal/lid-mapping.js +271 -0
- package/vendor/core/lib/Signal/lid-mapping.js.map +1 -0
- package/vendor/core/lib/Socket/Client/index.d.ts +3 -0
- package/vendor/core/lib/Socket/Client/index.d.ts.map +1 -0
- package/vendor/core/lib/Socket/Client/index.js +3 -0
- package/vendor/core/lib/Socket/Client/index.js.map +1 -0
- package/vendor/core/lib/Socket/Client/types.d.ts +16 -0
- package/vendor/core/lib/Socket/Client/types.d.ts.map +1 -0
- package/vendor/core/lib/Socket/Client/types.js +11 -0
- package/vendor/core/lib/Socket/Client/types.js.map +1 -0
- package/vendor/core/lib/Socket/Client/websocket.d.ts +13 -0
- package/vendor/core/lib/Socket/Client/websocket.d.ts.map +1 -0
- package/vendor/core/lib/Socket/Client/websocket.js +54 -0
- package/vendor/core/lib/Socket/Client/websocket.js.map +1 -0
- package/vendor/core/lib/Socket/business.d.ts +536 -0
- package/vendor/core/lib/Socket/business.d.ts.map +1 -0
- package/vendor/core/lib/Socket/business.js +422 -0
- package/vendor/core/lib/Socket/business.js.map +1 -0
- package/vendor/core/lib/Socket/chats.d.ts +111 -0
- package/vendor/core/lib/Socket/chats.d.ts.map +1 -0
- package/vendor/core/lib/Socket/chats.js +991 -0
- package/vendor/core/lib/Socket/chats.js.map +1 -0
- package/vendor/core/lib/Socket/communities.d.ts +645 -0
- package/vendor/core/lib/Socket/communities.d.ts.map +1 -0
- package/vendor/core/lib/Socket/communities.js +431 -0
- package/vendor/core/lib/Socket/communities.js.map +1 -0
- package/vendor/core/lib/Socket/dugong.d.ts +130 -0
- package/vendor/core/lib/Socket/dugong.d.ts.map +1 -0
- package/vendor/core/lib/Socket/dugong.js +768 -0
- package/vendor/core/lib/Socket/dugong.js.map +1 -0
- package/vendor/core/lib/Socket/groups.d.ts +150 -0
- package/vendor/core/lib/Socket/groups.d.ts.map +1 -0
- package/vendor/core/lib/Socket/groups.js +327 -0
- package/vendor/core/lib/Socket/groups.js.map +1 -0
- package/vendor/core/lib/Socket/index.d.ts +636 -0
- package/vendor/core/lib/Socket/index.d.ts.map +1 -0
- package/vendor/core/lib/Socket/index.js +15 -0
- package/vendor/core/lib/Socket/index.js.map +1 -0
- package/vendor/core/lib/Socket/messages-recv.d.ts +238 -0
- package/vendor/core/lib/Socket/messages-recv.d.ts.map +1 -0
- package/vendor/core/lib/Socket/messages-recv.js +1308 -0
- package/vendor/core/lib/Socket/messages-recv.js.map +1 -0
- package/vendor/core/lib/Socket/messages-send.d.ts +579 -0
- package/vendor/core/lib/Socket/messages-send.d.ts.map +1 -0
- package/vendor/core/lib/Socket/messages-send.js +1595 -0
- package/vendor/core/lib/Socket/messages-send.js.map +1 -0
- package/vendor/core/lib/Socket/mex.d.ts +3 -0
- package/vendor/core/lib/Socket/mex.d.ts.map +1 -0
- package/vendor/core/lib/Socket/mex.js +48 -0
- package/vendor/core/lib/Socket/mex.js.map +1 -0
- package/vendor/core/lib/Socket/newsletter.d.ts +177 -0
- package/vendor/core/lib/Socket/newsletter.d.ts.map +1 -0
- package/vendor/core/lib/Socket/newsletter.js +456 -0
- package/vendor/core/lib/Socket/newsletter.js.map +1 -0
- package/vendor/core/lib/Socket/socket.d.ts +55 -0
- package/vendor/core/lib/Socket/socket.d.ts.map +1 -0
- package/vendor/core/lib/Socket/socket.js +936 -0
- package/vendor/core/lib/Socket/socket.js.map +1 -0
- package/vendor/core/lib/Types/Auth.d.ts +116 -0
- package/vendor/core/lib/Types/Auth.d.ts.map +1 -0
- package/vendor/core/lib/Types/Auth.js +2 -0
- package/vendor/core/lib/Types/Auth.js.map +1 -0
- package/vendor/core/lib/Types/Bussines.d.ts +37 -0
- package/vendor/core/lib/Types/Bussines.d.ts.map +1 -0
- package/vendor/core/lib/Types/Bussines.js +2 -0
- package/vendor/core/lib/Types/Bussines.js.map +1 -0
- package/vendor/core/lib/Types/Call.d.ts +15 -0
- package/vendor/core/lib/Types/Call.d.ts.map +1 -0
- package/vendor/core/lib/Types/Call.js +2 -0
- package/vendor/core/lib/Types/Call.js.map +1 -0
- package/vendor/core/lib/Types/Chat.d.ts +123 -0
- package/vendor/core/lib/Types/Chat.d.ts.map +1 -0
- package/vendor/core/lib/Types/Chat.js +8 -0
- package/vendor/core/lib/Types/Chat.js.map +1 -0
- package/vendor/core/lib/Types/Contact.d.ts +24 -0
- package/vendor/core/lib/Types/Contact.d.ts.map +1 -0
- package/vendor/core/lib/Types/Contact.js +2 -0
- package/vendor/core/lib/Types/Contact.js.map +1 -0
- package/vendor/core/lib/Types/Events.d.ts +237 -0
- package/vendor/core/lib/Types/Events.d.ts.map +1 -0
- package/vendor/core/lib/Types/Events.js +2 -0
- package/vendor/core/lib/Types/Events.js.map +1 -0
- package/vendor/core/lib/Types/GroupMetadata.d.ts +68 -0
- package/vendor/core/lib/Types/GroupMetadata.d.ts.map +1 -0
- package/vendor/core/lib/Types/GroupMetadata.js +2 -0
- package/vendor/core/lib/Types/GroupMetadata.js.map +1 -0
- package/vendor/core/lib/Types/Label.d.ts +47 -0
- package/vendor/core/lib/Types/Label.d.ts.map +1 -0
- package/vendor/core/lib/Types/Label.js +25 -0
- package/vendor/core/lib/Types/Label.js.map +1 -0
- package/vendor/core/lib/Types/LabelAssociation.d.ts +30 -0
- package/vendor/core/lib/Types/LabelAssociation.d.ts.map +1 -0
- package/vendor/core/lib/Types/LabelAssociation.js +7 -0
- package/vendor/core/lib/Types/LabelAssociation.js.map +1 -0
- package/vendor/core/lib/Types/Message.d.ts +422 -0
- package/vendor/core/lib/Types/Message.d.ts.map +1 -0
- package/vendor/core/lib/Types/Message.js +11 -0
- package/vendor/core/lib/Types/Message.js.map +1 -0
- package/vendor/core/lib/Types/Newsletter.d.ts +135 -0
- package/vendor/core/lib/Types/Newsletter.d.ts.map +1 -0
- package/vendor/core/lib/Types/Newsletter.js +31 -0
- package/vendor/core/lib/Types/Newsletter.js.map +1 -0
- package/vendor/core/lib/Types/Product.d.ts +79 -0
- package/vendor/core/lib/Types/Product.d.ts.map +1 -0
- package/vendor/core/lib/Types/Product.js +2 -0
- package/vendor/core/lib/Types/Product.js.map +1 -0
- package/vendor/core/lib/Types/Signal.d.ts +76 -0
- package/vendor/core/lib/Types/Signal.d.ts.map +1 -0
- package/vendor/core/lib/Types/Signal.js +2 -0
- package/vendor/core/lib/Types/Signal.js.map +1 -0
- package/vendor/core/lib/Types/Socket.d.ts +158 -0
- package/vendor/core/lib/Types/Socket.d.ts.map +1 -0
- package/vendor/core/lib/Types/Socket.js +3 -0
- package/vendor/core/lib/Types/Socket.js.map +1 -0
- package/vendor/core/lib/Types/State.d.ts +39 -0
- package/vendor/core/lib/Types/State.d.ts.map +1 -0
- package/vendor/core/lib/Types/State.js +13 -0
- package/vendor/core/lib/Types/State.js.map +1 -0
- package/vendor/core/lib/Types/USync.d.ts +26 -0
- package/vendor/core/lib/Types/USync.d.ts.map +1 -0
- package/vendor/core/lib/Types/USync.js +2 -0
- package/vendor/core/lib/Types/USync.js.map +1 -0
- package/vendor/core/lib/Types/index.d.ts +66 -0
- package/vendor/core/lib/Types/index.d.ts.map +1 -0
- package/vendor/core/lib/Types/index.js +33 -0
- package/vendor/core/lib/Types/index.js.map +1 -0
- package/vendor/core/lib/Utils/auth-utils.d.ts +19 -0
- package/vendor/core/lib/Utils/auth-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/auth-utils.js +290 -0
- package/vendor/core/lib/Utils/auth-utils.js.map +1 -0
- package/vendor/core/lib/Utils/browser-utils.d.ts +4 -0
- package/vendor/core/lib/Utils/browser-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/browser-utils.js +28 -0
- package/vendor/core/lib/Utils/browser-utils.js.map +1 -0
- package/vendor/core/lib/Utils/business.d.ts +23 -0
- package/vendor/core/lib/Utils/business.d.ts.map +1 -0
- package/vendor/core/lib/Utils/business.js +231 -0
- package/vendor/core/lib/Utils/business.js.map +1 -0
- package/vendor/core/lib/Utils/chat-utils.d.ts +70 -0
- package/vendor/core/lib/Utils/chat-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/chat-utils.js +812 -0
- package/vendor/core/lib/Utils/chat-utils.js.map +1 -0
- package/vendor/core/lib/Utils/crypto.d.ts +37 -0
- package/vendor/core/lib/Utils/crypto.d.ts.map +1 -0
- package/vendor/core/lib/Utils/crypto.js +118 -0
- package/vendor/core/lib/Utils/crypto.js.map +1 -0
- package/vendor/core/lib/Utils/decode-wa-message.d.ts +48 -0
- package/vendor/core/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/vendor/core/lib/Utils/decode-wa-message.js +283 -0
- package/vendor/core/lib/Utils/decode-wa-message.js.map +1 -0
- package/vendor/core/lib/Utils/event-buffer.d.ts +34 -0
- package/vendor/core/lib/Utils/event-buffer.d.ts.map +1 -0
- package/vendor/core/lib/Utils/event-buffer.js +574 -0
- package/vendor/core/lib/Utils/event-buffer.js.map +1 -0
- package/vendor/core/lib/Utils/generics.d.ts +91 -0
- package/vendor/core/lib/Utils/generics.d.ts.map +1 -0
- package/vendor/core/lib/Utils/generics.js +384 -0
- package/vendor/core/lib/Utils/generics.js.map +1 -0
- package/vendor/core/lib/Utils/history.d.ts +22 -0
- package/vendor/core/lib/Utils/history.d.ts.map +1 -0
- package/vendor/core/lib/Utils/history.js +131 -0
- package/vendor/core/lib/Utils/history.js.map +1 -0
- package/vendor/core/lib/Utils/identity-change-handler.d.ts +37 -0
- package/vendor/core/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/vendor/core/lib/Utils/identity-change-handler.js +49 -0
- package/vendor/core/lib/Utils/identity-change-handler.js.map +1 -0
- package/vendor/core/lib/Utils/index.d.ts +23 -0
- package/vendor/core/lib/Utils/index.d.ts.map +1 -0
- package/vendor/core/lib/Utils/index.js +23 -0
- package/vendor/core/lib/Utils/index.js.map +1 -0
- package/vendor/core/lib/Utils/link-preview.d.ts +21 -0
- package/vendor/core/lib/Utils/link-preview.d.ts.map +1 -0
- package/vendor/core/lib/Utils/link-preview.js +85 -0
- package/vendor/core/lib/Utils/link-preview.js.map +1 -0
- package/vendor/core/lib/Utils/logger.d.ts +13 -0
- package/vendor/core/lib/Utils/logger.d.ts.map +1 -0
- package/vendor/core/lib/Utils/logger.js +3 -0
- package/vendor/core/lib/Utils/logger.js.map +1 -0
- package/vendor/core/lib/Utils/lt-hash.d.ts +8 -0
- package/vendor/core/lib/Utils/lt-hash.d.ts.map +1 -0
- package/vendor/core/lib/Utils/lt-hash.js +8 -0
- package/vendor/core/lib/Utils/lt-hash.js.map +1 -0
- package/vendor/core/lib/Utils/make-mutex.d.ts +9 -0
- package/vendor/core/lib/Utils/make-mutex.d.ts.map +1 -0
- package/vendor/core/lib/Utils/make-mutex.js +33 -0
- package/vendor/core/lib/Utils/make-mutex.js.map +1 -0
- package/vendor/core/lib/Utils/message-retry-manager.d.ts +110 -0
- package/vendor/core/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/vendor/core/lib/Utils/message-retry-manager.js +225 -0
- package/vendor/core/lib/Utils/message-retry-manager.js.map +1 -0
- package/vendor/core/lib/Utils/messages-media.d.ts +130 -0
- package/vendor/core/lib/Utils/messages-media.d.ts.map +1 -0
- package/vendor/core/lib/Utils/messages-media.js +782 -0
- package/vendor/core/lib/Utils/messages-media.js.map +1 -0
- package/vendor/core/lib/Utils/messages.d.ts +91 -0
- package/vendor/core/lib/Utils/messages.d.ts.map +1 -0
- package/vendor/core/lib/Utils/messages.js +1240 -0
- package/vendor/core/lib/Utils/messages.js.map +1 -0
- package/vendor/core/lib/Utils/noise-handler.d.ts +20 -0
- package/vendor/core/lib/Utils/noise-handler.d.ts.map +1 -0
- package/vendor/core/lib/Utils/noise-handler.js +201 -0
- package/vendor/core/lib/Utils/noise-handler.js.map +1 -0
- package/vendor/core/lib/Utils/offline-node-processor.d.ts +17 -0
- package/vendor/core/lib/Utils/offline-node-processor.d.ts.map +1 -0
- package/vendor/core/lib/Utils/offline-node-processor.js +40 -0
- package/vendor/core/lib/Utils/offline-node-processor.js.map +1 -0
- package/vendor/core/lib/Utils/pre-key-manager.d.ts +28 -0
- package/vendor/core/lib/Utils/pre-key-manager.d.ts.map +1 -0
- package/vendor/core/lib/Utils/pre-key-manager.js +106 -0
- package/vendor/core/lib/Utils/pre-key-manager.js.map +1 -0
- package/vendor/core/lib/Utils/process-message.d.ts +60 -0
- package/vendor/core/lib/Utils/process-message.d.ts.map +1 -0
- package/vendor/core/lib/Utils/process-message.js +528 -0
- package/vendor/core/lib/Utils/process-message.js.map +1 -0
- package/vendor/core/lib/Utils/reporting-utils.d.ts +11 -0
- package/vendor/core/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/reporting-utils.js +258 -0
- package/vendor/core/lib/Utils/reporting-utils.js.map +1 -0
- package/vendor/core/lib/Utils/rich-messages.d.ts +277 -0
- package/vendor/core/lib/Utils/rich-messages.d.ts.map +1 -0
- package/vendor/core/lib/Utils/rich-messages.js +1144 -0
- package/vendor/core/lib/Utils/rich-messages.js.map +1 -0
- package/vendor/core/lib/Utils/signal.d.ts +34 -0
- package/vendor/core/lib/Utils/signal.d.ts.map +1 -0
- package/vendor/core/lib/Utils/signal.js +159 -0
- package/vendor/core/lib/Utils/signal.js.map +1 -0
- package/vendor/core/lib/Utils/stanza-ack.d.ts +11 -0
- package/vendor/core/lib/Utils/stanza-ack.d.ts.map +1 -0
- package/vendor/core/lib/Utils/stanza-ack.js +38 -0
- package/vendor/core/lib/Utils/stanza-ack.js.map +1 -0
- package/vendor/core/lib/Utils/sticker-pack.d.ts +10 -0
- package/vendor/core/lib/Utils/sticker-pack.js +111 -0
- package/vendor/core/lib/Utils/sync-action-utils.d.ts +19 -0
- package/vendor/core/lib/Utils/sync-action-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/sync-action-utils.js +48 -0
- package/vendor/core/lib/Utils/sync-action-utils.js.map +1 -0
- package/vendor/core/lib/Utils/tc-token-utils.d.ts +12 -0
- package/vendor/core/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/vendor/core/lib/Utils/tc-token-utils.js +18 -0
- package/vendor/core/lib/Utils/tc-token-utils.js.map +1 -0
- package/vendor/core/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/vendor/core/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
- package/vendor/core/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/vendor/core/lib/Utils/use-multi-file-auth-state.js.map +1 -0
- package/vendor/core/lib/Utils/validate-connection.d.ts +11 -0
- package/vendor/core/lib/Utils/validate-connection.d.ts.map +1 -0
- package/vendor/core/lib/Utils/validate-connection.js +200 -0
- package/vendor/core/lib/Utils/validate-connection.js.map +1 -0
- package/vendor/core/lib/WABinary/constants.d.ts +28 -0
- package/vendor/core/lib/WABinary/constants.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/constants.js +1301 -0
- package/vendor/core/lib/WABinary/constants.js.map +1 -0
- package/vendor/core/lib/WABinary/decode.d.ts +7 -0
- package/vendor/core/lib/WABinary/decode.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/decode.js +262 -0
- package/vendor/core/lib/WABinary/decode.js.map +1 -0
- package/vendor/core/lib/WABinary/encode.d.ts +3 -0
- package/vendor/core/lib/WABinary/encode.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/encode.js +220 -0
- package/vendor/core/lib/WABinary/encode.js.map +1 -0
- package/vendor/core/lib/WABinary/generic-utils.d.ts +17 -0
- package/vendor/core/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/generic-utils.js +191 -0
- package/vendor/core/lib/WABinary/generic-utils.js.map +1 -0
- package/vendor/core/lib/WABinary/index.d.ts +6 -0
- package/vendor/core/lib/WABinary/index.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/index.js +6 -0
- package/vendor/core/lib/WABinary/index.js.map +1 -0
- package/vendor/core/lib/WABinary/jid-utils.d.ts +48 -0
- package/vendor/core/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/jid-utils.js +96 -0
- package/vendor/core/lib/WABinary/jid-utils.js.map +1 -0
- package/vendor/core/lib/WABinary/types.d.ts +19 -0
- package/vendor/core/lib/WABinary/types.d.ts.map +1 -0
- package/vendor/core/lib/WABinary/types.js +2 -0
- package/vendor/core/lib/WABinary/types.js.map +1 -0
- package/vendor/core/lib/WAM/BinaryInfo.d.ts +9 -0
- package/vendor/core/lib/WAM/BinaryInfo.d.ts.map +1 -0
- package/vendor/core/lib/WAM/BinaryInfo.js +10 -0
- package/vendor/core/lib/WAM/BinaryInfo.js.map +1 -0
- package/vendor/core/lib/WAM/constants.d.ts +40 -0
- package/vendor/core/lib/WAM/constants.d.ts.map +1 -0
- package/vendor/core/lib/WAM/constants.js +22853 -0
- package/vendor/core/lib/WAM/constants.js.map +1 -0
- package/vendor/core/lib/WAM/encode.d.ts +3 -0
- package/vendor/core/lib/WAM/encode.d.ts.map +1 -0
- package/vendor/core/lib/WAM/encode.js +150 -0
- package/vendor/core/lib/WAM/encode.js.map +1 -0
- package/vendor/core/lib/WAM/index.d.ts +4 -0
- package/vendor/core/lib/WAM/index.d.ts.map +1 -0
- package/vendor/core/lib/WAM/index.js +4 -0
- package/vendor/core/lib/WAM/index.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/vendor/core/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/vendor/core/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/index.d.ts +5 -0
- package/vendor/core/lib/WAUSync/Protocols/index.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/Protocols/index.js +5 -0
- package/vendor/core/lib/WAUSync/Protocols/index.js.map +1 -0
- package/vendor/core/lib/WAUSync/USyncQuery.d.ts +29 -0
- package/vendor/core/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/USyncQuery.js +94 -0
- package/vendor/core/lib/WAUSync/USyncQuery.js.map +1 -0
- package/vendor/core/lib/WAUSync/USyncUser.d.ts +13 -0
- package/vendor/core/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/USyncUser.js +23 -0
- package/vendor/core/lib/WAUSync/USyncUser.js.map +1 -0
- package/vendor/core/lib/WAUSync/index.d.ts +4 -0
- package/vendor/core/lib/WAUSync/index.d.ts.map +1 -0
- package/vendor/core/lib/WAUSync/index.js +4 -0
- package/vendor/core/lib/WAUSync/index.js.map +1 -0
- package/vendor/core/lib/index.d.ts +13 -0
- package/vendor/core/lib/index.d.ts.map +1 -0
- package/vendor/core/lib/index.js +13 -0
- 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
|
+
}
|