Compare commits

..

84 Commits

Author SHA1 Message Date
semantic-release-bot
9f3295cc0f chore(release): 1.0.0-dev.9 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.9](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.8...@revanced/bot-websocket-api@1.0.0-dev.9) (2024-08-03)

### Features

* **apis/websocket:** return `true` for data on a `TrainedMessage` packet ([65add4d](65add4dfee))
2024-08-03 19:53:18 +00:00
PalmDevs
4da6175cf5 fix(bots/discord): await when putting entries into db 2024-08-04 02:52:07 +07:00
PalmDevs
d90ad5c955 fix(packages/api): handle close event as a disconnect 2024-08-04 02:52:06 +07:00
PalmDevs
65add4dfee feat(apis/websocket): return true for data on a TrainedMessage packet 2024-08-04 02:52:04 +07:00
semantic-release-bot
2c2f6b76d4 chore(release): 1.0.0-dev.19 [skip ci]
# @revanced/discord-bot [1.0.0-dev.19](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.18...@revanced/discord-bot@1.0.0-dev.19) (2024-08-03)

### Bug Fixes

* **bots/discord:** correct whitelist logic ([49c29be](49c29bebfb))
2024-08-03 17:31:01 +00:00
PalmDevs
49c29bebfb fix(bots/discord): correct whitelist logic 2024-08-04 00:29:20 +07:00
semantic-release-bot
4e889d4991 chore(release): 1.0.0-dev.18 [skip ci]
# @revanced/discord-bot [1.0.0-dev.18](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.17...@revanced/discord-bot@1.0.0-dev.18) (2024-08-03)

### Bug Fixes

* **bots/discord:** set the `label` property correctly for message scans ([6d463df](6d463df586))
2024-08-03 15:24:18 +00:00
PalmDevs
6d463df586 fix(bots/discord): set the label property correctly for message scans 2024-08-03 22:22:42 +07:00
semantic-release-bot
2d8688bd4c chore(release): 1.0.0-dev.17 [skip ci]
# @revanced/discord-bot [1.0.0-dev.17](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.16...@revanced/discord-bot@1.0.0-dev.17) (2024-08-02)

### Bug Fixes

* **bots/discord/commands/eval:** evaluate in current context ([5925d90](5925d90209))
* **bots/discord:** send right response for after regexes ([a7688fa](a7688fa9b9))

### Features

* **bots/discord:** update example config file ([bc9951c](bc9951c9b5))
2024-08-02 18:40:39 +00:00
PalmDevs
bc9951c9b5 feat(bots/discord): update example config file 2024-08-03 01:36:19 +07:00
PalmDevs
a7688fa9b9 fix(bots/discord): send right response for after regexes 2024-08-03 01:31:45 +07:00
PalmDevs
5925d90209 fix(bots/discord/commands/eval): evaluate in current context 2024-08-03 01:31:44 +07:00
semantic-release-bot
5506518635 chore(release): 1.0.0-dev.16 [skip ci]
# @revanced/discord-bot [1.0.0-dev.16](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.15...@revanced/discord-bot@1.0.0-dev.16) (2024-08-02)

### Bug Fixes

* **bots/discord:** open database as read-write ([c366840](c36684091d))
* **bots/discord:** remove bad text channel checks ([f5939e2](f5939e2528))
* **bots/discord:** remove redundant footer for response embeds ([412e003](412e00317d))

### Features

* **bots/discord/commands:** add `reload` command ([6875b32](6875b32fd0))
2024-08-02 12:31:21 +00:00
PalmDevs
b9d08fff64 feat(bots/discord)!: add more attachment scan options 2024-08-02 19:26:20 +07:00
PalmDevs
6875b32fd0 feat(bots/discord/commands): add reload command 2024-08-02 19:26:20 +07:00
PalmDevs
c36684091d fix(bots/discord): open database as read-write 2024-08-02 19:26:19 +07:00
PalmDevs
f5939e2528 fix(bots/discord): remove bad text channel checks 2024-08-02 19:26:18 +07:00
PalmDevs
412e00317d fix(bots/discord): remove redundant footer for response embeds 2024-08-02 19:26:17 +07:00
PalmDevs
8fe78e424e fix(bots/discord)!: rename config replyToReplied to respondToReply 2024-08-02 19:26:12 +07:00
semantic-release-bot
9fe6b4ca70 chore(release): 1.0.0-dev.15 [skip ci]
# @revanced/discord-bot [1.0.0-dev.15](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.14...@revanced/discord-bot@1.0.0-dev.15) (2024-07-31)

### Bug Fixes

* **bots/discord:** import `config` from context ([763ef25](763ef253f9))

### Features

* **bots/discord:** add sticky messages ([bf66155](bf661556e1))
2024-07-31 19:31:21 +00:00
PalmDevs
bf661556e1 feat(bots/discord): add sticky messages 2024-08-01 02:29:49 +07:00
PalmDevs
763ef253f9 fix(bots/discord): import config from context 2024-07-31 21:25:12 +07:00
semantic-release-bot
561426028c chore(release): 1.0.0-dev.14 [skip ci]
# @revanced/discord-bot [1.0.0-dev.14](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.13...@revanced/discord-bot@1.0.0-dev.14) (2024-07-31)

### Bug Fixes

* **bots/discord:** always true check causing no messages to be scanned ([98ec37b](98ec37b5d1))
* other small issues ([bc437a5](bc437a5ec7))

### Features

* **bots/discord:** add more options for curing, fix default regex ([1a4ec1e](1a4ec1ece8))
* **bots/discord:** allow admins to bypass permission checks ([620f933](620f9339f0))
2024-07-31 12:36:12 +00:00
semantic-release-bot
b726c40fd4 chore(release): 1.0.0-dev.8 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.8](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.7...@revanced/bot-websocket-api@1.0.0-dev.8) (2024-07-31)

### Bug Fixes

* other small issues ([bc437a5](bc437a5ec7))
2024-07-31 12:35:31 +00:00
PalmDevs
bc437a5ec7 fix: other small issues 2024-07-31 19:34:12 +07:00
PalmDevs
620f9339f0 feat(bots/discord): allow admins to bypass permission checks 2024-07-31 19:34:11 +07:00
PalmDevs
1a4ec1ece8 feat(bots/discord): add more options for curing, fix default regex 2024-07-31 19:34:10 +07:00
PalmDevs
98ec37b5d1 fix(bots/discord): always true check causing no messages to be scanned 2024-07-31 19:34:10 +07:00
PalmDevs
711f57f4a1 fix(packages/api): null errors being thrown 2024-07-31 19:34:09 +07:00
PalmDevs
e29e9c3dd1 fix(packages/api): properly await failed packets 2024-07-31 19:34:08 +07:00
semantic-release-bot
b832311f7e chore(release): 1.0.0-dev.13 [skip ci]
# @revanced/discord-bot [1.0.0-dev.13](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.12...@revanced/discord-bot@1.0.0-dev.13) (2024-07-30)

### Bug Fixes

* **bots/discord:** broken regex when prefix set to special characters ([ab62e55](ab62e55e76))
2024-07-30 19:14:27 +00:00
semantic-release-bot
887ee85e41 chore(release): 1.0.0-dev.7 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.7](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.6...@revanced/bot-websocket-api@1.0.0-dev.7) (2024-07-30)
2024-07-30 19:13:45 +00:00
PalmDevs
c9b788dc51 build(Needs bump): do not minify builds 2024-07-31 02:12:31 +07:00
PalmDevs
ab62e55e76 fix(bots/discord): broken regex when prefix set to special characters 2024-07-31 02:12:30 +07:00
semantic-release-bot
8f83687b7c chore(release): 1.0.0-dev.12 [skip ci]
# @revanced/discord-bot [1.0.0-dev.12](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.11...@revanced/discord-bot@1.0.0-dev.12) (2024-07-30)

### Bug Fixes

* **bots/discord:** deployment runtime errors due to minification ([a60c60c](a60c60c0f9))
2024-07-30 18:42:20 +00:00
PalmDevs
a60c60c0f9 fix(bots/discord): deployment runtime errors due to minification 2024-07-31 01:40:45 +07:00
semantic-release-bot
95a122a225 chore(release): 1.0.0-dev.11 [skip ci]
# @revanced/discord-bot [1.0.0-dev.11](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.10...@revanced/discord-bot@1.0.0-dev.11) (2024-07-30)

### Bug Fixes

* **bots/discord:** reset counter when reconnected to api, redo message scan filter logic ([d234d79](d234d79310))
2024-07-30 17:23:25 +00:00
PalmDevs
d234d79310 fix(bots/discord): reset counter when reconnected to api, redo message scan filter logic 2024-07-31 00:22:02 +07:00
semantic-release-bot
3188f8dbed chore(release): 1.0.0-dev.10 [skip ci]
# @revanced/discord-bot [1.0.0-dev.10](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.9...@revanced/discord-bot@1.0.0-dev.10) (2024-07-30)

### Bug Fixes

* **bots/discord:** hanging process when disconnecting from API too many times ([d31616e](d31616ebcb))
2024-07-30 14:32:44 +00:00
semantic-release-bot
9b9bb1e1e6 chore(release): 1.0.0-dev.6 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.5...@revanced/bot-websocket-api@1.0.0-dev.6) (2024-07-30)

### Bug Fixes

* **bots/discord:** hanging process when disconnecting from API too many times ([d31616e](d31616ebcb))
2024-07-30 14:32:03 +00:00
PalmDevs
d31616ebcb fix(bots/discord): hanging process when disconnecting from API too many times 2024-07-30 21:30:54 +07:00
semantic-release-bot
2efedc47df chore(release): 1.0.0-dev.9 [skip ci]
# @revanced/discord-bot [1.0.0-dev.9](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.8...@revanced/discord-bot@1.0.0-dev.9) (2024-07-30)

### Features

* **bots/discord:** framework changes and new features ([646ec8d](646ec8da87))
2024-07-30 14:17:08 +00:00
PalmDevs
646ec8da87 feat(bots/discord): framework changes and new features
- Migrated to a new command framework which looks better and works better
- Fixed commands not being bundled correctly
- Added message (prefix) commands with argument validation
- Added a new CommandErrorType, for invalid arguments
- `/eval` is now a bit safer
- Corrected colors for the coinflip embed
- `/stop` now works even when the bot is not connected to the API
2024-07-30 21:15:36 +07:00
PalmDevs
a848a9c896 feat(packages/api): add force disconnecting and disconnected getter in APIClient 2024-07-30 21:14:20 +07:00
semantic-release-bot
8168f79ac6 chore(release): 1.0.0-dev.8 [skip ci]
# @revanced/discord-bot [1.0.0-dev.8](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.7...@revanced/discord-bot@1.0.0-dev.8) (2024-07-28)

### Bug Fixes

* **bots/discord:** cross-device link build errors ([38c0699](38c06997b4))

### Features

* **bots/discord:** blacklist and whitelist for filters ([cdb6001](cdb6001955))
2024-07-28 14:15:40 +00:00
PalmDevs
38c06997b4 fix(bots/discord): cross-device link build errors 2024-07-28 21:14:07 +07:00
PalmDevs
cdb6001955 feat(bots/discord): blacklist and whitelist for filters 2024-07-28 20:43:25 +07:00
semantic-release-bot
e748a4da92 chore(release): 1.0.0-dev.7 [skip ci]
# @revanced/discord-bot [1.0.0-dev.7](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.6...@revanced/discord-bot@1.0.0-dev.7) (2024-07-25)

### Bug Fixes

* **bot/discord:** start remove preset timeout for `role-preset` command ([cbf9116](cbf91162e2))
* **bots/discord:** only check for member permissions when specified while correcting responses ([b79a1c7](b79a1c7575))
* **bots/discord:** set timeout for eligible mutes to unmute faster ([1f5c5a9](1f5c5a92a6))

### Features

* **bots/discord:** add `replyToReplied` option in response config ([27662ed](27662ed91a))
2024-07-25 18:38:09 +00:00
PalmDevs
300e5cff3b ci(bots/discord): fix freezing after generating db schemas 2024-07-26 01:36:52 +07:00
PalmDevs
6685ffb855 chore: update lockfile 2024-07-26 01:30:33 +07:00
PalmDevs
cbf91162e2 fix(bot/discord): start remove preset timeout for role-preset command 2024-07-26 01:25:52 +07:00
PalmDevs
bd906fbf54 fix(bots/discord)!: remove guilds config in favor of upcoming impl 2024-07-26 01:25:51 +07:00
PalmDevs
27662ed91a feat(bots/discord): add replyToReplied option in response config 2024-07-26 01:25:50 +07:00
PalmDevs
d0acab1915 feat(bots/discord)!: add admin config 2024-07-26 01:25:49 +07:00
PalmDevs
e86180fe29 feat(bots/discord)!: allow message scan response to be message payloads 2024-07-26 01:25:48 +07:00
PalmDevs
1f5c5a92a6 fix(bots/discord): set timeout for eligible mutes to unmute faster 2024-07-26 01:25:47 +07:00
PalmDevs
b79a1c7575 fix(bots/discord): only check for member permissions when specified while correcting responses 2024-07-26 01:25:46 +07:00
PalmDevs
3559ed1cb5 ci(bots/discord): patch drizzle-kit to stop using node, decreases image size 2024-07-26 01:25:45 +07:00
semantic-release-bot
042b155b5e chore(release): 1.0.0-dev.6 [skip ci]
# @revanced/discord-bot [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.5...@revanced/discord-bot@1.0.0-dev.6) (2024-07-23)

### Bug Fixes

* **bots/discord:** ci issues causing database to not be auto generated ([673aa18](673aa189be))
2024-07-23 20:59:49 +00:00
PalmDevs
673aa189be fix(bots/discord): ci issues causing database to not be auto generated 2024-07-24 03:57:33 +07:00
PalmDevs
c503a86c53 ci(release): also update bun lockfile to prevent install freezes 2024-07-24 03:36:32 +07:00
semantic-release-bot
1bd973ea6c chore(release): 1.0.0-dev.5 [skip ci]
# @revanced/discord-bot [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.4...@revanced/discord-bot@1.0.0-dev.5) (2024-07-23)
2024-07-23 20:31:04 +00:00
semantic-release-bot
4bb965e9ff chore(release): 1.0.0-dev.5 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.4...@revanced/bot-websocket-api@1.0.0-dev.5) (2024-07-23)
2024-07-23 20:30:32 +00:00
PalmDevs
a8ceeb29ae chore: update lockfile 2024-07-24 03:29:32 +07:00
PalmDevs
96a6540434 build(Needs bump): revert building with bun explicitly
Building with only Bun causes compatibility issues, like Drizzle Kit not being to generate any schema for the database of the Discord bot.
2024-07-24 03:25:30 +07:00
PalmDevs
e02c86a9c4 ci(release): add time limit for job 2024-07-24 03:05:54 +07:00
semantic-release-bot
e82f2ab34b chore(release): 1.0.0-dev.4 [skip ci]
# @revanced/discord-bot [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.3...@revanced/discord-bot@1.0.0-dev.4) (2024-07-23)

### Bug Fixes

* **bots/discord:** wrong database schema path ([875bd20](875bd209b2))
2024-07-23 20:04:31 +00:00
PalmDevs
2a6f3c3013 chore: update lockfile 2024-07-24 03:03:19 +07:00
PalmDevs
875bd209b2 fix(bots/discord): wrong database schema path 2024-07-24 01:12:30 +07:00
semantic-release-bot
2b601b1a1d chore(release): 1.0.0-dev.3 [skip ci]
# @revanced/discord-bot [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.2...@revanced/discord-bot@1.0.0-dev.3) (2024-07-23)

### Bug Fixes

* **bots/discord:** revert dist denesting, fixes config not found ([0d4898d](0d4898dae8))
2024-07-23 15:42:43 +00:00
semantic-release-bot
164570d176 chore(release): 1.0.0-dev.4 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.3...@revanced/bot-websocket-api@1.0.0-dev.4) (2024-07-23)

### Bug Fixes

* **apis/websocket:** hardcoded paths in tesseract worker builds ([38e00eb](38e00eb4e5))
2024-07-23 15:42:10 +00:00
PalmDevs
0d4898dae8 fix(bots/discord): revert dist denesting, fixes config not found 2024-07-23 22:41:15 +07:00
PalmDevs
38e00eb4e5 fix(apis/websocket): hardcoded paths in tesseract worker builds 2024-07-23 22:38:23 +07:00
semantic-release-bot
3117af5497 chore(release): 1.0.0-dev.2 [skip ci]
# @revanced/discord-bot [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.1...@revanced/discord-bot@1.0.0-dev.2) (2024-07-23)

### Features

* **bots/discord:** don't nest builds in src directory, autogen db when missing ([4834685](4834685186))
2024-07-23 14:16:41 +00:00
PalmDevs
6a87464b40 chore(bots/discord): fix build script oversight 2024-07-23 21:15:07 +07:00
semantic-release-bot
5db076aee5 chore(release): 1.0.0-dev.3 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.2...@revanced/bot-websocket-api@1.0.0-dev.3) (2024-07-23)

### Bug Fixes

* **apis/websocket:** build and runtime issues ([89d8ab1](89d8ab1ee5))
2024-07-23 14:05:57 +00:00
PalmDevs
12f5aaf70f chore: update dependencies 2024-07-23 21:01:05 +07:00
PalmDevs
94d0dcc32b fix(packages/api): misleading errors being thrown 2024-07-23 21:01:04 +07:00
PalmDevs
4834685186 feat(bots/discord): don't nest builds in src directory, autogen db when missing 2024-07-23 21:01:04 +07:00
PalmDevs
ffe0a3ddf5 chore(bots/discord): change config to a js file 2024-07-23 21:01:03 +07:00
PalmDevs
89d8ab1ee5 fix(apis/websocket): build and runtime issues 2024-07-23 21:01:02 +07:00
semantic-release-bot
f142c2b82e chore(release): 1.0.0-dev.2 [skip ci]
# @revanced/bot-websocket-api [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.1...@revanced/bot-websocket-api@1.0.0-dev.2) (2024-07-22)

### Bug Fixes

* **apis/websocket:** also include tesseract core files in build ([7dfbf6c](7dfbf6c92c))
2024-07-22 17:46:37 +00:00
PalmDevs
7dfbf6c92c fix(apis/websocket): also include tesseract core files in build 2024-07-23 00:44:08 +07:00
semantic-release-bot
55631b220f chore(release): 1.0.0-dev.1 [skip ci]
# @revanced/discord-bot 1.0.0-dev.1 (2024-07-22)

### Bug Fixes

* **bots/discord/commands/mute:** use existing `parseDuration` util function' ([3e07429](3e07429664))
* **bots/discord/commands/unmute:** fix option description and embeds ([7fdf8c0](7fdf8c0dc7))
* **bots/discord/commands:** minor issues ([3b2596e](3b2596e748))
* **bots/discord/commands:** refactor and add checks ([a2bf3ea](a2bf3eade9))
* **bots/discord/database:** fix schema for role presets ([4aa138a](4aa138a9af))
* **bots/discord/scripts:** unintentional escaping on windows ([09dc706](09dc70632d))
* **bots/discord/utils/discord/embeds:** set thumbnail on moderation embeds ([b056291](b056291ad0))
* **bots/discord/utils/discord/rolePresets:** correct property access for presets ([4c6ad11](4c6ad11be3))
* **bots/discord/utils/discord:** add `moderation` module related functions ([7e8270f](7e8270f7d2))
* **bots/discord/utils/duration:** fix empty string returning with duration is 0 ([83c314e](83c314ef5f))
* **bots/discord:** apply active role presets if members rejoin ([f50b26b](f50b26b82d))
* **bots/discord:** check token before connecting to bot api ([f3e4408](f3e4408aa2))
* **bots/discord:** clear role presets after they expire ([faa81f4](faa81f4d88))
* **bots/discord:** connect to discord API even if initial bot API connection fails ([6658b58](6658b582db))
* **bots/discord:** do decancer after resetting nickname ([0303fe3](0303fe3e36))
* **bots/discord:** follow-up if reply is already sent when error ([f75060b](f75060bc9c))
* **bots/discord:** messed up file name for `unmute` command ([399dca7](399dca7153))
* **bots/discord:** owners cannot bypass checks on some commands ([39cba97](39cba97341))
* **bots/discord:** remove auto-generated files ([fb8af00](fb8af00866))
* **bots/discord:** remove usage of macros ([7f27c56](7f27c5607c))
* **bots/discord:** remove useless feature ([d830e48](d830e48bc2))
* **bots/discord:** use `APIEmbed` for response config ([35b9448](35b944800a))
* **bots/discord:** use env for initializing database ([af3759c](af3759caf4))
* **bots/discord:** wrong command file path being imported ([fa0159c](fa0159c3a8))
* config file not being read ([474a8be](474a8be4af))
* **discord-bot:** also execute slash commands ([f0d45b2](f0d45b2c92))
* **discord-bot:** check for role position ([d332043](d332043b1a))
* **discord-bot:** check if the member has the role ([9bff68c](9bff68c8c4))
* **discord-bot:** not executing slash commands ([aa08087](aa0808768b))
* **discord-bot:** only send lowercased text ([7803758](78037580dc))
* dislike button not working properly ([85eba55](85eba55424))
* fix deprecation ([4373ede](4373ede855))
* fix the fiter for the interaction collector ([a9ff003](a9ff00394a))
* ignore message if there's no content ([3cbebc2](3cbebc2842))
* move modules to `/bots` ([cd7156e](cd7156e792))
* trainAI not using the bin location ([bd29943](bd2994388b))

### chore

* fix more build issues ([77fefb9](77fefb9bef))

### Features

* add wit.ai support ([1909e2c](1909e2c421))
* **bots/discord/commands/reply:** send stacktrace when failed ([9f1ac37](9f1ac37927))
* **bots/discord/commands:** add `ban` and `unban` commands ([dc4863d](dc4863dc20))
* **bots/discord/commands:** add `eval` command ([e64d1da](e64d1da00c))
* **bots/discord/commands:** add `mute` and `unmute` commands ([c0fa2fe](c0fa2fe1c3))
* **bots/discord/commands:** add `purge` and `role-preset` commands ([fb01ce5](fb01ce5740))
* **bots/discord/commands:** allow process exception in `exception-test` ([ca47535](ca475356ad))
* **bots/discord/utils/discord/embeds:** expose `applyCommonEmbedStyles` fn ([2d794ed](2d794ede7d))
* **bots/discord/utils/embeds:** make title parameter nullable ([ee885ca](ee885ca758))
* **bots/discord/utils/fs:** use `recursive` option for listing files ([da21e1a](da21e1a6f7))
* **bots/discord/utils:** add duration utility ([a9add9e](a9add9ea9a))
* **bots/discord/utils:** add functions for role presets ([fb32a04](fb32a04ad3))
* **bots/discord/utils:** allow loading commands from custom dir ([8b690b8](8b690b879b))
* **bots/discord:** add `api.disconnectRetryInterval` config ([2f86586](2f86586179))
* **bots/discord:** add `moderation.roles` config to be used in `moderation` commands ([39d5b3a](39d5b3a479))
* **bots/discord:** add `ocrTriggers` resp config, embed footer scan mode ([744a56a](744a56a4fd))
* **bots/discord:** add a better way to manage databases ([a68d726](a68d726875))
* **bots/discord:** add more fallbacks for decancering ([2e1e009](2e1e009b42))
* **bots/discord:** add source ([f9d50a0](f9d50a0a6b))
* **bots/discord:** improve logs ([6abb740](6abb740994))
* **bots/discord:** sanitize `BasicDatabase` inputs ([fd76e0a](fd76e0af72))
* **bots/discord:** support nickname decancering ([1723e8c](1723e8cacf))
* **bots/discord:** switch to `drizzle-orm` ([e204b7b](e204b7b756))
* **bots/discord:** update config ([197d2ac](197d2acea8))
* discord bot scanning messages ([d1bd3b2](d1bd3b2b7e))
* **discord-bot:** a way to train AI ([355a508](355a50803a))
* **discord-bot:** command handler and train cmd ([6aee8a4](6aee8a4c63))
* **discord-bot:** event handler ([0ad5ece](0ad5ece085))
* GODEL AI ([0ba525c](0ba525c4a5))
* initalize discord bot ([bb4a5a7](bb4a5a77ee))
* initialize helper client ([7f9ca77](7f9ca77e03))
* message buttons for training ([6551ca9](6551ca9dad))
* platform specific responses ([18e57b0](18e57b0c32))
* prettier and eslint ([1c27ccb](1c27ccb17c))
* refactor and new features ([#7](https://github.com/revanced/revanced-helper/issues/7)) ([8b9f45d](8b9f45dc22))
* run bots in one process ([d26d533](d26d533174))
* training and replies changed ([715aa91](715aa918cf))
* **utils/discord/embeds:** allow adding extra fields for moderation embeds ([49ce9a7](49ce9a7ca3))

### BREAKING CHANGES

* In `@revanced/discord-bot`, its environment variable
                 `DATABASE_URL` has been renamed to `DATABASE_PATH`
                 and the `file:` prefix is no longer needed
2024-07-22 16:04:34 +00:00
70 changed files with 2082 additions and 913 deletions

View File

@@ -14,6 +14,7 @@ jobs:
permissions: permissions:
contents: read contents: read
packages: write packages: write
timeout-minutes: 10
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4

View File

@@ -1,3 +1,49 @@
# @revanced/bot-websocket-api [1.0.0-dev.9](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.8...@revanced/bot-websocket-api@1.0.0-dev.9) (2024-08-03)
### Features
* **apis/websocket:** return `true` for data on a `TrainedMessage` packet ([65add4d](https://github.com/revanced/revanced-helper/commit/65add4dfeed2fa067c2c8e2377f7d01d505ade54))
# @revanced/bot-websocket-api [1.0.0-dev.8](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.7...@revanced/bot-websocket-api@1.0.0-dev.8) (2024-07-31)
### Bug Fixes
* other small issues ([bc437a5](https://github.com/revanced/revanced-helper/commit/bc437a5ec7ce1d339094d608e2a61ac5f460c163))
# @revanced/bot-websocket-api [1.0.0-dev.7](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.6...@revanced/bot-websocket-api@1.0.0-dev.7) (2024-07-30)
# @revanced/bot-websocket-api [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.5...@revanced/bot-websocket-api@1.0.0-dev.6) (2024-07-30)
### Bug Fixes
* **bots/discord:** hanging process when disconnecting from API too many times ([d31616e](https://github.com/revanced/revanced-helper/commit/d31616ebcba6f1dcd8bde183bcb8d1adb1501b61))
# @revanced/bot-websocket-api [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.4...@revanced/bot-websocket-api@1.0.0-dev.5) (2024-07-23)
# @revanced/bot-websocket-api [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.3...@revanced/bot-websocket-api@1.0.0-dev.4) (2024-07-23)
### Bug Fixes
* **apis/websocket:** hardcoded paths in tesseract worker builds ([38e00eb](https://github.com/revanced/revanced-helper/commit/38e00eb4e59c763bd74d27b9b9b482ea66e4dcf4))
# @revanced/bot-websocket-api [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.2...@revanced/bot-websocket-api@1.0.0-dev.3) (2024-07-23)
### Bug Fixes
* **apis/websocket:** build and runtime issues ([89d8ab1](https://github.com/revanced/revanced-helper/commit/89d8ab1ee58278a9a96cdc31c679d0a0a0d865af))
# @revanced/bot-websocket-api [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/bot-websocket-api@1.0.0-dev.1...@revanced/bot-websocket-api@1.0.0-dev.2) (2024-07-22)
### Bug Fixes
* **apis/websocket:** also include tesseract core files in build ([7dfbf6c](https://github.com/revanced/revanced-helper/commit/7dfbf6c92c49100954fa4aca471dce4ab9fd9565))
# @revanced/bot-websocket-api 1.0.0-dev.1 (2024-07-22) # @revanced/bot-websocket-api 1.0.0-dev.1 (2024-07-22)

View File

@@ -6,7 +6,7 @@ FROM base AS build
WORKDIR /build WORKDIR /build
COPY . . COPY . .
RUN bun install --frozen-lockfile RUN bun install --frozen-lockfile
RUN cd apis/websocket && bun --bun run build RUN cd apis/websocket && bun run build
FROM base AS release FROM base AS release

View File

@@ -2,7 +2,7 @@
"name": "@revanced/bot-websocket-api", "name": "@revanced/bot-websocket-api",
"type": "module", "type": "module",
"private": true, "private": true,
"version": "1.0.0-dev.1", "version": "1.0.0-dev.9",
"description": "🧦 WebSocket API server for bots assisting ReVanced", "description": "🧦 WebSocket API server for bots assisting ReVanced",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@@ -1,23 +1,32 @@
import { createLogger } from '@revanced/bot-shared' import { createLogger } from '@revanced/bot-shared'
import { cp } from 'fs/promises' import { cp, rm } from 'fs/promises'
async function build(): Promise<void> {
const logger = createLogger() const logger = createLogger()
logger.info('Building Tesseract.js worker...') logger.info('Cleaning previous build...')
await Bun.build({ await rm('./dist', { recursive: true })
entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'],
target: 'bun',
outdir: './dist/worker',
})
logger.info('Building WebSocket API...') logger.info('Building WebSocket API...')
await Bun.build({ await Bun.build({
entrypoints: ['./src/index.ts'], entrypoints: ['./src/index.ts'],
outdir: './dist', outdir: './dist',
target: 'bun', target: 'bun',
sourcemap: 'external',
}) })
}
await build() logger.info('Building Tesseract.js worker...')
await Bun.build({
entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'],
external: ['tesseract.js-core/*'],
target: 'bun',
outdir: './dist/worker',
sourcemap: 'external',
})
// Tesseract.js is really bad for minification
// It forcefully requires this core module to be present which contains the WASM files
logger.info('Copying Tesseract.js Core...')
await cp('../../node_modules/tesseract.js-core', './dist/node_modules/tesseract.js-core', { recursive: true })
logger.info('Copying config...')
await cp('config.json', 'dist/config.json') await cp('config.json', 'dist/config.json')

View File

@@ -53,11 +53,11 @@ export interface WitMessageResponse {
}> }>
} }
const TesseractWorkerPath = joinPath(import.meta.dir, 'worker', 'index.js') const TesseractWorkerDirPath = joinPath(import.meta.dir, 'worker')
const TesseractCompiledWorkerExists = await pathExists(TesseractWorkerPath) const TesseractWorkerPath = joinPath(TesseractWorkerDirPath, 'index.js')
export const tesseract = await createTesseractWorker( export const tesseract = await createTesseractWorker(
'eng', 'eng',
OEM.DEFAULT, OEM.DEFAULT,
TesseractCompiledWorkerExists ? { workerPath: TesseractWorkerPath } : undefined, (await pathExists(TesseractWorkerDirPath)) ? { workerPath: TesseractWorkerPath } : undefined,
) )

View File

@@ -18,7 +18,7 @@ const trainMessageEventHandler: EventHandler<ClientOperation.TrainMessage> = asy
client.send( client.send(
{ {
op: ServerOperation.TrainedMessage, op: ServerOperation.TrainedMessage,
d: null, d: true,
}, },
nextSeq, nextSeq,
) )

View File

@@ -21,6 +21,9 @@
}, },
"useNodejsImportProtocol": { "useNodejsImportProtocol": {
"level": "off" "level": "off"
},
"useNumberNamespace": {
"level": "off"
} }
} }
} }

View File

@@ -174,13 +174,11 @@ dist
# Finder (MacOS) folder config # Finder (MacOS) folder config
.DS_Store .DS_Store
# Config
config.ts
# DB # DB
*.db *.db
*.sqlite *.sqlite
*.sqlite3 *.sqlite3
.drizzle
# Auto-generated files # Auto-generated files
src/commands/index.ts src/commands/index.ts

253
bots/discord/CHANGELOG.md Normal file
View File

@@ -0,0 +1,253 @@
# @revanced/discord-bot [1.0.0-dev.19](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.18...@revanced/discord-bot@1.0.0-dev.19) (2024-08-03)
### Bug Fixes
* **bots/discord:** correct whitelist logic ([49c29be](https://github.com/revanced/revanced-helper/commit/49c29bebfbe348ae4e2cc1b3a83bfa41eb26ccd1))
# @revanced/discord-bot [1.0.0-dev.18](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.17...@revanced/discord-bot@1.0.0-dev.18) (2024-08-03)
### Bug Fixes
* **bots/discord:** set the `label` property correctly for message scans ([6d463df](https://github.com/revanced/revanced-helper/commit/6d463df586dee5dd8fe8d6cff1c5316f7809b32a))
# @revanced/discord-bot [1.0.0-dev.17](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.16...@revanced/discord-bot@1.0.0-dev.17) (2024-08-02)
### Bug Fixes
* **bots/discord/commands/eval:** evaluate in current context ([5925d90](https://github.com/revanced/revanced-helper/commit/5925d902095acef5f6396ca03583a9cbb0862498))
* **bots/discord:** send right response for after regexes ([a7688fa](https://github.com/revanced/revanced-helper/commit/a7688fa9b91919a87f74071b502cd0a87cd1c1fa))
### Features
* **bots/discord:** update example config file ([bc9951c](https://github.com/revanced/revanced-helper/commit/bc9951c9b5e007c3e1b3076aa0966ccf29bb18bc))
# @revanced/discord-bot [1.0.0-dev.16](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.15...@revanced/discord-bot@1.0.0-dev.16) (2024-08-02)
### Bug Fixes
* **bots/discord:** open database as read-write ([c366840](https://github.com/revanced/revanced-helper/commit/c36684091dddf67880505dc459e4334a8a5492f4))
* **bots/discord:** remove bad text channel checks ([f5939e2](https://github.com/revanced/revanced-helper/commit/f5939e25288fea2022fdeec9085ecb9ffada6111))
* **bots/discord:** remove redundant footer for response embeds ([412e003](https://github.com/revanced/revanced-helper/commit/412e00317d1eaca23e9c1375e16f94a5f2fa8d86))
### Features
* **bots/discord/commands:** add `reload` command ([6875b32](https://github.com/revanced/revanced-helper/commit/6875b32fd0c6ce3034da9dc6c704d425afb26f2e))
# @revanced/discord-bot [1.0.0-dev.15](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.14...@revanced/discord-bot@1.0.0-dev.15) (2024-07-31)
### Bug Fixes
* **bots/discord:** import `config` from context ([763ef25](https://github.com/revanced/revanced-helper/commit/763ef253f9d4ff70a8b79969a7f4f41cba7f3c59))
### Features
* **bots/discord:** add sticky messages ([bf66155](https://github.com/revanced/revanced-helper/commit/bf661556e131bf0ef24e47f658fbcd701960e312))
# @revanced/discord-bot [1.0.0-dev.14](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.13...@revanced/discord-bot@1.0.0-dev.14) (2024-07-31)
### Bug Fixes
* **bots/discord:** always true check causing no messages to be scanned ([98ec37b](https://github.com/revanced/revanced-helper/commit/98ec37b5d18cade85270ab83b0ed0abe41244dd9))
* other small issues ([bc437a5](https://github.com/revanced/revanced-helper/commit/bc437a5ec7ce1d339094d608e2a61ac5f460c163))
### Features
* **bots/discord:** add more options for curing, fix default regex ([1a4ec1e](https://github.com/revanced/revanced-helper/commit/1a4ec1ece80becd9156582cc490f6681cb2a1f39))
* **bots/discord:** allow admins to bypass permission checks ([620f933](https://github.com/revanced/revanced-helper/commit/620f9339f0737b79d72c66d90ffa42ea3f987710))
# @revanced/discord-bot [1.0.0-dev.13](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.12...@revanced/discord-bot@1.0.0-dev.13) (2024-07-30)
### Bug Fixes
* **bots/discord:** broken regex when prefix set to special characters ([ab62e55](https://github.com/revanced/revanced-helper/commit/ab62e55e76005f5999d7413d1158e54053f28d1f))
# @revanced/discord-bot [1.0.0-dev.12](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.11...@revanced/discord-bot@1.0.0-dev.12) (2024-07-30)
### Bug Fixes
* **bots/discord:** deployment runtime errors due to minification ([a60c60c](https://github.com/revanced/revanced-helper/commit/a60c60c0f994a4c256b7d0582e99a1731209cf49))
# @revanced/discord-bot [1.0.0-dev.11](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.10...@revanced/discord-bot@1.0.0-dev.11) (2024-07-30)
### Bug Fixes
* **bots/discord:** reset counter when reconnected to api, redo message scan filter logic ([d234d79](https://github.com/revanced/revanced-helper/commit/d234d79310caed9c43e14a905f9ef46a110e071d))
# @revanced/discord-bot [1.0.0-dev.10](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.9...@revanced/discord-bot@1.0.0-dev.10) (2024-07-30)
### Bug Fixes
* **bots/discord:** hanging process when disconnecting from API too many times ([d31616e](https://github.com/revanced/revanced-helper/commit/d31616ebcba6f1dcd8bde183bcb8d1adb1501b61))
# @revanced/discord-bot [1.0.0-dev.9](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.8...@revanced/discord-bot@1.0.0-dev.9) (2024-07-30)
### Features
* **bots/discord:** framework changes and new features ([646ec8d](https://github.com/revanced/revanced-helper/commit/646ec8da87617e6c8f48a89e8054e2cba91da549))
# @revanced/discord-bot [1.0.0-dev.8](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.7...@revanced/discord-bot@1.0.0-dev.8) (2024-07-28)
### Bug Fixes
* **bots/discord:** cross-device link build errors ([38c0699](https://github.com/revanced/revanced-helper/commit/38c06997b4d0f7bb3f1e62618a5e3f088c522e30))
### Features
* **bots/discord:** blacklist and whitelist for filters ([cdb6001](https://github.com/revanced/revanced-helper/commit/cdb600195520dba33110c40841629259e317055e))
# @revanced/discord-bot [1.0.0-dev.7](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.6...@revanced/discord-bot@1.0.0-dev.7) (2024-07-25)
### Bug Fixes
* **bot/discord:** start remove preset timeout for `role-preset` command ([cbf9116](https://github.com/revanced/revanced-helper/commit/cbf91162e27dd4c1ecb976927ab708f1d882abca))
* **bots/discord:** only check for member permissions when specified while correcting responses ([b79a1c7](https://github.com/revanced/revanced-helper/commit/b79a1c7575e94c3e62654c87775cac497be4a50a))
* **bots/discord:** set timeout for eligible mutes to unmute faster ([1f5c5a9](https://github.com/revanced/revanced-helper/commit/1f5c5a92a639973b83a1204355538936e69a4454))
### Features
* **bots/discord:** add `replyToReplied` option in response config ([27662ed](https://github.com/revanced/revanced-helper/commit/27662ed91a79bfac7d3f091834e859a7b57366ce))
# @revanced/discord-bot [1.0.0-dev.6](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.5...@revanced/discord-bot@1.0.0-dev.6) (2024-07-23)
### Bug Fixes
* **bots/discord:** ci issues causing database to not be auto generated ([673aa18](https://github.com/revanced/revanced-helper/commit/673aa189bef1009a3e32ba3b1291a5ee84f2def3))
# @revanced/discord-bot [1.0.0-dev.5](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.4...@revanced/discord-bot@1.0.0-dev.5) (2024-07-23)
# @revanced/discord-bot [1.0.0-dev.4](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.3...@revanced/discord-bot@1.0.0-dev.4) (2024-07-23)
### Bug Fixes
* **bots/discord:** wrong database schema path ([875bd20](https://github.com/revanced/revanced-helper/commit/875bd209b252566414bf89349839cabc01697e1c))
# @revanced/discord-bot [1.0.0-dev.3](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.2...@revanced/discord-bot@1.0.0-dev.3) (2024-07-23)
### Bug Fixes
* **bots/discord:** revert dist denesting, fixes config not found ([0d4898d](https://github.com/revanced/revanced-helper/commit/0d4898dae8b26f8466d3f6b8f62875866f581644))
# @revanced/discord-bot [1.0.0-dev.2](https://github.com/revanced/revanced-helper/compare/@revanced/discord-bot@1.0.0-dev.1...@revanced/discord-bot@1.0.0-dev.2) (2024-07-23)
### Features
* **bots/discord:** don't nest builds in src directory, autogen db when missing ([4834685](https://github.com/revanced/revanced-helper/commit/48346851864c4d4b6276388644dd24ce16222b3e))
# @revanced/discord-bot 1.0.0-dev.1 (2024-07-22)
### Bug Fixes
* **bots/discord/commands/mute:** use existing `parseDuration` util function' ([3e07429](https://github.com/revanced/revanced-helper/commit/3e07429664f7dbb6ce653083e0adb1a232737fde))
* **bots/discord/commands/unmute:** fix option description and embeds ([7fdf8c0](https://github.com/revanced/revanced-helper/commit/7fdf8c0dc722e21fe5a3ad6ef8d3a306ef85f532))
* **bots/discord/commands:** minor issues ([3b2596e](https://github.com/revanced/revanced-helper/commit/3b2596e748cf2cde1500ef2ded55f0faabc2c272))
* **bots/discord/commands:** refactor and add checks ([a2bf3ea](https://github.com/revanced/revanced-helper/commit/a2bf3eade99b46f9ffb55d45b8caf1bcf3d22a9b))
* **bots/discord/database:** fix schema for role presets ([4aa138a](https://github.com/revanced/revanced-helper/commit/4aa138a9af8db7093ef637470fcfdea1f5341236))
* **bots/discord/scripts:** unintentional escaping on windows ([09dc706](https://github.com/revanced/revanced-helper/commit/09dc70632da0597fdb26677acee3f6fccbb2b9b5))
* **bots/discord/utils/discord/embeds:** set thumbnail on moderation embeds ([b056291](https://github.com/revanced/revanced-helper/commit/b056291ad0f2e2eac5eec8aa71f15dbc769aa0f9))
* **bots/discord/utils/discord/rolePresets:** correct property access for presets ([4c6ad11](https://github.com/revanced/revanced-helper/commit/4c6ad11be30c1d6af97c4ae40fc62d05fa7bdd57))
* **bots/discord/utils/discord:** add `moderation` module related functions ([7e8270f](https://github.com/revanced/revanced-helper/commit/7e8270f7d260322e1950e058b221ab088bd595d0))
* **bots/discord/utils/duration:** fix empty string returning with duration is 0 ([83c314e](https://github.com/revanced/revanced-helper/commit/83c314ef5f721abc355272db0e4c182dcfe5d943))
* **bots/discord:** apply active role presets if members rejoin ([f50b26b](https://github.com/revanced/revanced-helper/commit/f50b26b82d66c88fd1dbb8c07d77c177c0e781df))
* **bots/discord:** check token before connecting to bot api ([f3e4408](https://github.com/revanced/revanced-helper/commit/f3e4408aa28fb6a9d21365af8c1bea3d07b481de))
* **bots/discord:** clear role presets after they expire ([faa81f4](https://github.com/revanced/revanced-helper/commit/faa81f4d887eaeae809651f5b68187d033a260f2))
* **bots/discord:** connect to discord API even if initial bot API connection fails ([6658b58](https://github.com/revanced/revanced-helper/commit/6658b582dbeba7e072a7a04c4efa255e7f634aef))
* **bots/discord:** do decancer after resetting nickname ([0303fe3](https://github.com/revanced/revanced-helper/commit/0303fe3e367c07e92f831365d5548ca5b03435b2))
* **bots/discord:** follow-up if reply is already sent when error ([f75060b](https://github.com/revanced/revanced-helper/commit/f75060bc9cda44902cf872def73c116a6df039d7))
* **bots/discord:** messed up file name for `unmute` command ([399dca7](https://github.com/revanced/revanced-helper/commit/399dca71538fe5c8831977694a97058254a17578))
* **bots/discord:** owners cannot bypass checks on some commands ([39cba97](https://github.com/revanced/revanced-helper/commit/39cba973418027ba6ed67e1ae5ab5c6458807562))
* **bots/discord:** remove auto-generated files ([fb8af00](https://github.com/revanced/revanced-helper/commit/fb8af008661bf37389e01cba19d64a8b4fc82139))
* **bots/discord:** remove usage of macros ([7f27c56](https://github.com/revanced/revanced-helper/commit/7f27c5607ceeeef56d67097e88f68caa1b8791b3))
* **bots/discord:** remove useless feature ([d830e48](https://github.com/revanced/revanced-helper/commit/d830e48bc2de7aa457eab3a5f96ae652a93178f9))
* **bots/discord:** use `APIEmbed` for response config ([35b9448](https://github.com/revanced/revanced-helper/commit/35b944800a3943c187d5b0e0d3e465ad7d2056fe))
* **bots/discord:** use env for initializing database ([af3759c](https://github.com/revanced/revanced-helper/commit/af3759caf428fada3b3f4a51852543d6fb280018))
* **bots/discord:** wrong command file path being imported ([fa0159c](https://github.com/revanced/revanced-helper/commit/fa0159c3a8dd4dad8778ccdb75b9e7c02ebbb64f))
* config file not being read ([474a8be](https://github.com/revanced/revanced-helper/commit/474a8be4af4eb2bae6e80a893439d846ad4f7503))
* **discord-bot:** also execute slash commands ([f0d45b2](https://github.com/revanced/revanced-helper/commit/f0d45b2c926ed753e2d21f2e06e24d7e6c43880a))
* **discord-bot:** check for role position ([d332043](https://github.com/revanced/revanced-helper/commit/d332043b1a4bb7ac9698a2fc912832e184130b4b))
* **discord-bot:** check if the member has the role ([9bff68c](https://github.com/revanced/revanced-helper/commit/9bff68c8c40c692764e4dec15a058e35059efbc9))
* **discord-bot:** not executing slash commands ([aa08087](https://github.com/revanced/revanced-helper/commit/aa0808768b90844c5fbd3e75d9f2d01c723b0151))
* **discord-bot:** only send lowercased text ([7803758](https://github.com/revanced/revanced-helper/commit/78037580dc92883f5ca21157e45268850cb5db90))
* dislike button not working properly ([85eba55](https://github.com/revanced/revanced-helper/commit/85eba554247738066af72a8efd0de215ec1164dc))
* fix deprecation ([4373ede](https://github.com/revanced/revanced-helper/commit/4373ede855333f209676551162a525238656e1f8))
* fix the fiter for the interaction collector ([a9ff003](https://github.com/revanced/revanced-helper/commit/a9ff00394a73f68a6793c2b35ff184675ee5a72c))
* ignore message if there's no content ([3cbebc2](https://github.com/revanced/revanced-helper/commit/3cbebc284277808495e64cf0fb47c555924ad9c5))
* move modules to `/bots` ([cd7156e](https://github.com/revanced/revanced-helper/commit/cd7156e792e65777ad1ab5a6f5d828b9ef6a9754))
* trainAI not using the bin location ([bd29943](https://github.com/revanced/revanced-helper/commit/bd2994388bc65f720120ef49edb6ba8163260309))
### chore
* fix more build issues ([77fefb9](https://github.com/revanced/revanced-helper/commit/77fefb9bef286a22f40a4d76b79c64fcc5a2467f))
### Features
* add wit.ai support ([1909e2c](https://github.com/revanced/revanced-helper/commit/1909e2c42148d635dcd045c738d88f65c8be16e3))
* **bots/discord/commands/reply:** send stacktrace when failed ([9f1ac37](https://github.com/revanced/revanced-helper/commit/9f1ac379276c11da65235577a9c6717e01cb02eb))
* **bots/discord/commands:** add `ban` and `unban` commands ([dc4863d](https://github.com/revanced/revanced-helper/commit/dc4863dc208b3fede4d4def323306ab58daffe04))
* **bots/discord/commands:** add `eval` command ([e64d1da](https://github.com/revanced/revanced-helper/commit/e64d1da00cc2ba718da5a4b0da141fe86a0e48d2))
* **bots/discord/commands:** add `mute` and `unmute` commands ([c0fa2fe](https://github.com/revanced/revanced-helper/commit/c0fa2fe1c36acdc7c52cde277aa7da867065f55e))
* **bots/discord/commands:** add `purge` and `role-preset` commands ([fb01ce5](https://github.com/revanced/revanced-helper/commit/fb01ce57400130c93751a11573eb444c0ba103eb))
* **bots/discord/commands:** allow process exception in `exception-test` ([ca47535](https://github.com/revanced/revanced-helper/commit/ca475356ad95fec86e8e8b5bf4bbf17b70add5fe))
* **bots/discord/utils/discord/embeds:** expose `applyCommonEmbedStyles` fn ([2d794ed](https://github.com/revanced/revanced-helper/commit/2d794ede7d7a208bd3616c45e8e6d2a2cd83e9ed))
* **bots/discord/utils/embeds:** make title parameter nullable ([ee885ca](https://github.com/revanced/revanced-helper/commit/ee885ca7585a55fdc31e137ae29dc13a37ce2fb2))
* **bots/discord/utils/fs:** use `recursive` option for listing files ([da21e1a](https://github.com/revanced/revanced-helper/commit/da21e1a6f76deaeb477203b04263bd170863825b))
* **bots/discord/utils:** add duration utility ([a9add9e](https://github.com/revanced/revanced-helper/commit/a9add9ea9affb42bdfcb17cf4b268feec5729854))
* **bots/discord/utils:** add functions for role presets ([fb32a04](https://github.com/revanced/revanced-helper/commit/fb32a04ad38be8d0836dc99259b6ef05a0825830))
* **bots/discord/utils:** allow loading commands from custom dir ([8b690b8](https://github.com/revanced/revanced-helper/commit/8b690b879bb5c6023c8fc863afbd9fd1d02719bb))
* **bots/discord:** add `api.disconnectRetryInterval` config ([2f86586](https://github.com/revanced/revanced-helper/commit/2f8658617923c07f6847cbf1fdfc5f5379d95b6c))
* **bots/discord:** add `moderation.roles` config to be used in `moderation` commands ([39d5b3a](https://github.com/revanced/revanced-helper/commit/39d5b3a479b4d856aabe12cc31177c24f88ae23e))
* **bots/discord:** add `ocrTriggers` resp config, embed footer scan mode ([744a56a](https://github.com/revanced/revanced-helper/commit/744a56a4fdc8844e37959a88bcf81ee39fe726ef))
* **bots/discord:** add a better way to manage databases ([a68d726](https://github.com/revanced/revanced-helper/commit/a68d72687584332587455962b0202a306288057d))
* **bots/discord:** add more fallbacks for decancering ([2e1e009](https://github.com/revanced/revanced-helper/commit/2e1e009b4272495798313bd3bd61f258875c62e1))
* **bots/discord:** add source ([f9d50a0](https://github.com/revanced/revanced-helper/commit/f9d50a0a6bef8beaa428a0a555bfa4f879f685f1))
* **bots/discord:** improve logs ([6abb740](https://github.com/revanced/revanced-helper/commit/6abb7409945c10bd3af451fb45ef4b4d4ebe9489))
* **bots/discord:** sanitize `BasicDatabase` inputs ([fd76e0a](https://github.com/revanced/revanced-helper/commit/fd76e0af72fe28b414ae3b5e8d3886e58561e57e))
* **bots/discord:** support nickname decancering ([1723e8c](https://github.com/revanced/revanced-helper/commit/1723e8cacf96e8c6bdee22cfd30e89524fdcef74))
* **bots/discord:** switch to `drizzle-orm` ([e204b7b](https://github.com/revanced/revanced-helper/commit/e204b7b7566fd7fa423baef32977a8575d44a9e0))
* **bots/discord:** update config ([197d2ac](https://github.com/revanced/revanced-helper/commit/197d2acea89c38e43858d52736508d449152e804))
* discord bot scanning messages ([d1bd3b2](https://github.com/revanced/revanced-helper/commit/d1bd3b2b7e4985a64e9b070ab006cc6f3508c46e))
* **discord-bot:** a way to train AI ([355a508](https://github.com/revanced/revanced-helper/commit/355a50803adc85b5579155b55ddbba4fa0449237))
* **discord-bot:** command handler and train cmd ([6aee8a4](https://github.com/revanced/revanced-helper/commit/6aee8a4c63eb108800fcb0a23ca61f200d8f1f2a))
* **discord-bot:** event handler ([0ad5ece](https://github.com/revanced/revanced-helper/commit/0ad5ece08593c0db111fa4a592b42c6e0348fd1c))
* GODEL AI ([0ba525c](https://github.com/revanced/revanced-helper/commit/0ba525c4a5802106d582c75f713728accf2f151a))
* initalize discord bot ([bb4a5a7](https://github.com/revanced/revanced-helper/commit/bb4a5a77eefbc7ac88536f73a111df1050b235e7))
* initialize helper client ([7f9ca77](https://github.com/revanced/revanced-helper/commit/7f9ca77e0331ec143160ee51ed7c3aa9e4e70b9c))
* message buttons for training ([6551ca9](https://github.com/revanced/revanced-helper/commit/6551ca9dadc2e3ddfe98875e80ed61f7d71a1651))
* platform specific responses ([18e57b0](https://github.com/revanced/revanced-helper/commit/18e57b0c320732a937bb60db11c5d6794ed11522))
* prettier and eslint ([1c27ccb](https://github.com/revanced/revanced-helper/commit/1c27ccb17c85f0f6982db45de426181d2c231d0e))
* refactor and new features ([#7](https://github.com/revanced/revanced-helper/issues/7)) ([8b9f45d](https://github.com/revanced/revanced-helper/commit/8b9f45dc22de29dc2ccb1cfab9a026db00457e25))
* run bots in one process ([d26d533](https://github.com/revanced/revanced-helper/commit/d26d53317440c64fb775cea609a87d29be6c8b40))
* training and replies changed ([715aa91](https://github.com/revanced/revanced-helper/commit/715aa918cf84213c9b19591a398d7532eb3f232a))
* **utils/discord/embeds:** allow adding extra fields for moderation embeds ([49ce9a7](https://github.com/revanced/revanced-helper/commit/49ce9a7ca3d8558b73a9b94dfe7a01d809db6fff))
### BREAKING CHANGES
* In `@revanced/discord-bot`, its environment variable
`DATABASE_URL` has been renamed to `DATABASE_PATH`
and the `file:` prefix is no longer needed

View File

@@ -6,7 +6,7 @@ FROM base AS build
WORKDIR /build WORKDIR /build
COPY . . COPY . .
RUN bun install --frozen-lockfile RUN bun install --frozen-lockfile
RUN cd bots/discord && bun --bun run build RUN cd bots/discord && bun run build
FROM base AS release FROM base AS release

122
bots/discord/config.js Normal file
View File

@@ -0,0 +1,122 @@
// @ts-check
/**
* @type {import('./config.schema').Config}
*/
export default {
prefix: '!',
admin: {
users: ['USER_ID_HERE'],
roles: {
GUILD_ID_HERE: ['ROLE_ID_HERE'],
},
},
stickyMessages: {
GUILD_ID_HERE: {
CHANNEL_ID_HERE: {
message: {
content: 'This is a sticky message!',
},
timeout: 60000,
forceSendTimeout: 300000,
}
}
},
moderation: {
cure: {
minimumNameLength: 3,
removeCharactersRegex: /[^a-zA-Z0-9 \-_]/g,
defaultName: 'Server member',
},
roles: ['ROLE_ID_HERE'],
log: {
channel: 'CHANNEL_ID_HERE',
// Optional
thread: 'THREAD_ID_HERE',
},
},
rolePresets: {
guilds: {
GUILD_ID_HERE: {
preset: {
give: ['ROLE_ID_HERE'],
take: ['ROLE_ID_HERE'],
},
anotherPreset: {
give: ['ROLE_ID_HERE'],
take: ['ROLE_ID_HERE'],
},
},
},
checkExpiredEvery: 3600,
},
messageScan: {
scanBots: false,
scanOutsideGuilds: false,
filter: {
whitelist: {
channels: ['CHANNEL_ID_HERE'],
roles: ['ROLE_ID_HERE'],
users: ['USER_ID_HERE'],
},
blacklist: {
channels: ['CHANNEL_ID_HERE'],
roles: ['ROLE_ID_HERE'],
users: ['USER_ID_HERE'],
},
},
humanCorrections: {
falsePositiveLabel: 'false_positive',
allow: {
members: {
permissions: 8n,
roles: ['ROLE_ID_HERE'],
},
},
},
attachments: {
scanAttachments: true,
allowedMimeTypes: ['image/jpeg', 'image/png', 'image/webp', 'text/plain'],
maxTextFileSize: 512000
},
responses: [
{
filterOverride: {
whitelist: {
channels: ['CHANNEL_ID_HERE'],
roles: ['ROLE_ID_HERE'],
users: ['USER_ID_HERE'],
},
blacklist: {
channels: ['CHANNEL_ID_HERE'],
roles: ['ROLE_ID_HERE'],
users: ['USER_ID_HERE'],
},
},
triggers: {
text: [/^regexp?$/, { label: 'label', threshold: 0.85 }],
},
response: {
embeds: [
{
title: 'Embed title',
description: 'Embed description',
fields: [
{
name: 'Field name',
value: 'Field value',
},
],
},
],
},
},
],
},
logLevel: 'log',
api: {
url: 'ws://127.0.0.1:3000',
disconnectLimit: 3,
disconnectRetryInterval: 10000,
},
}

View File

@@ -1,11 +1,17 @@
import type { APIEmbed } from 'discord.js' import type { BaseMessageOptions } from 'discord.js'
export type Config = { export type Config = {
owners: string[] prefix?: string
guilds: string[] admin?: {
users?: string[]
roles?: Record<string, string[]>
}
stickyMessages?: Record<string, Record<string, StickyMessageConfig>>
moderation?: { moderation?: {
roles: string[] roles: string[]
cure?: { cure?: {
minimumNameLength?: number
removeCharactersRegex?: RegExp
defaultName: string defaultName: string
} }
log?: { log?: {
@@ -18,12 +24,16 @@ export type Config = {
guilds: Record<string, Record<string, RolePresetConfig>> guilds: Record<string, Record<string, RolePresetConfig>>
} }
messageScan?: { messageScan?: {
allowedAttachmentMimeTypes: string[] scanBots?: boolean
filter: { scanOutsideGuilds?: boolean
roles?: string[] attachments?: {
users?: string[] scanAttachments?: boolean
channels?: string[] allowedMimeTypes?: string[]
whitelist: boolean maxTextFileSize?: number
}
filter?: {
whitelist?: Filter
blacklist?: Filter
} }
humanCorrections: { humanCorrections: {
falsePositiveLabel: string falsePositiveLabel: string
@@ -45,6 +55,12 @@ export type Config = {
} }
} }
export type StickyMessageConfig = {
timeout: number
forceSendTimeout?: number
message: BaseMessageOptions
}
export type RolePresetConfig = { export type RolePresetConfig = {
give: string[] give: string[]
take: string[] take: string[]
@@ -57,6 +73,7 @@ export type ConfigMessageScanResponse = {
} }
filterOverride?: NonNullable<Config['messageScan']>['filter'] filterOverride?: NonNullable<Config['messageScan']>['filter']
response: ConfigMessageScanResponseMessage | null response: ConfigMessageScanResponseMessage | null
respondToReply?: boolean
} }
export type ConfigMessageScanResponseLabelConfig = { export type ConfigMessageScanResponseLabelConfig = {
@@ -70,4 +87,10 @@ export type ConfigMessageScanResponseLabelConfig = {
threshold: number threshold: number
} }
export type ConfigMessageScanResponseMessage = APIEmbed export type Filter = {
roles?: string[]
users?: string[]
channels?: string[]
}
export type ConfigMessageScanResponseMessage = BaseMessageOptions

View File

@@ -1,73 +0,0 @@
import type { Config } from './config.schema'
export default {
owners: ['USER_ID_HERE'],
guilds: ['GUILD_ID_HERE'],
moderation: {
cure: {
defaultName: 'Server member',
},
roles: ['ROLE_ID_HERE'],
log: {
channel: 'CHANNEL_ID_HERE',
// Optional
thread: 'THREAD_ID_HERE',
},
},
rolePresets: {
guilds: {
GUILD_ID_HERE: {
preset: {
give: ['ROLE_ID_HERE'],
take: ['ROLE_ID_HERE'],
},
anotherPreset: {
give: ['ROLE_ID_HERE'],
take: ['ROLE_ID_HERE'],
},
},
},
checkExpiredEvery: 3600,
},
messageScan: {
filter: {
channels: ['CHANNEL_ID_HERE'],
roles: ['ROLE_ID_HERE'],
users: ['USER_ID_HERE'],
whitelist: false,
},
humanCorrections: {
falsePositiveLabel: 'false_positive',
allow: {
members: {
permissions: 8n,
roles: ['ROLE_ID_HERE'],
},
},
},
allowedAttachmentMimeTypes: ['image/jpeg', 'image/png', 'image/webp'],
responses: [
{
triggers: {
text: [/^regexp?$/, { label: 'label', threshold: 0.85 }],
},
response: {
title: 'Embed title',
description: 'Embed description',
fields: [
{
name: 'Field name',
value: 'Field value',
},
],
},
},
],
},
logLevel: 'log',
api: {
url: 'ws://127.0.0.1:3000',
disconnectLimit: 3,
disconnectRetryInterval: 10000,
},
} satisfies Config as Config

View File

@@ -29,9 +29,8 @@ The distribution files will be placed inside the `dist` directory. Inside will i
To deploy the bot, you'll need to: To deploy the bot, you'll need to:
1. Replace the `config.ts` file with your own configuration _(optional)_ 1. [Build the bot as seen in the previous step](#-building)
2. [Build the bot as seen in the previous step](#-building) 2. Run the `reload-slash-commands` script
3. Run the `reload-slash-commands` script
This is to ensure all commands are registered, so they can be used. This is to ensure all commands are registered, so they can be used.
**It may take up to 2 hours until **global** commands are updated. This is a Discord limitation.** **It may take up to 2 hours until **global** commands are updated. This is a Discord limitation.**
@@ -40,7 +39,7 @@ To deploy the bot, you'll need to:
bun run scripts/reload-slash-commands.ts bun run scripts/reload-slash-commands.ts
``` ```
4. Copy contents of the `dist` directory 3. Copy contents of the `dist` directory
```sh ```sh
# For instance, we'll copy them both to /usr/src/discord-bot # For instance, we'll copy them both to /usr/src/discord-bot
@@ -48,18 +47,10 @@ To deploy the bot, you'll need to:
cp -R ./dist/* /usr/src/discord-bot cp -R ./dist/* /usr/src/discord-bot
``` ```
5. Copy the empty database (or use your own existing database) 4. Configure environment variables
```sh
# By default, the build script creates the database called "db.sqlite3"
# Unless you specify otherwise via the "DATABASE_PATH" environment variable
cp ./db.sqlite3 /usr/src/discord-bot
```
6. Configure environment variables
As seen in [`.env.example`](../.env.example). You can also optionally use a `.env` file which **Bun will automatically load**. As seen in [`.env.example`](../.env.example). You can also optionally use a `.env` file which **Bun will automatically load**.
7. Finally, run the bot 5. Finally, run the bot
```sh ```sh
cd /usr/src/discord-bot cd /usr/src/discord-bot

View File

@@ -35,8 +35,6 @@ export default {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("my-command") .setName("my-command")
.setDescription("My cool command") .setDescription("My cool command")
// Allowing this command to be used in DMs
.setDMPermission(true)
// DO NOT forget this line! // DO NOT forget this line!
.toJSON(), .toJSON(),

View File

@@ -3,6 +3,7 @@ import { defineConfig } from 'drizzle-kit'
export default defineConfig({ export default defineConfig({
dialect: 'sqlite', dialect: 'sqlite',
schema: './src/database/schemas.ts', schema: './src/database/schemas.ts',
out: './.drizzle',
dbCredentials: { dbCredentials: {
url: process.env['DATABASE_PATH'] ? `file:./${process.env['DATABASE_PATH']}` : 'file:./db.sqlite3', url: process.env['DATABASE_PATH'] ? `file:./${process.env['DATABASE_PATH']}` : 'file:./db.sqlite3',
}, },

View File

@@ -2,17 +2,16 @@
"name": "@revanced/discord-bot", "name": "@revanced/discord-bot",
"type": "module", "type": "module",
"private": true, "private": true,
"version": "0.1.0", "version": "1.0.0-dev.19",
"description": "🤖 Discord bot assisting ReVanced", "description": "🤖 Discord bot assisting ReVanced",
"main": "src/index.ts", "main": "src/index.ts",
"scripts": { "scripts": {
"register": "bun run scripts/reload-slash-commands.ts", "register": "bun run scripts/reload-slash-commands.ts",
"start": "bun prepare && bun run src/index.ts", "start": "bun prepare && bun run src/index.ts",
"dev": "bun prepare && bun --watch src/index.ts", "dev": "bun prepare && bun --watch src/index.ts",
"build:config": "bun build config.ts --outdir=dist", "build": "bun prepare && bun run scripts/build.ts",
"build": "bun prepare && bun build:config && bun build src/index.ts -e ./config.js --target=bun --outdir=dist/src",
"watch": "bun dev", "watch": "bun dev",
"prepare": "bun run scripts/generate-indexes.ts && drizzle-kit push" "prepare": "bun run scripts/generate-indexes.ts && bunx drizzle-kit generate --name=schema"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -0,0 +1,23 @@
import { createLogger } from '@revanced/bot-shared'
import { cp, rm } from 'fs/promises'
const logger = createLogger()
logger.warn('Cleaning previous build...')
await rm('./dist', { recursive: true })
logger.info('Building bot...')
await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist/src',
target: 'bun',
external: ['./config.js'],
sourcemap: 'external',
})
logger.info('Copying config...')
await cp('./config.js', './dist/config.js')
logger.info('Copying database schema...')
await cp('./.drizzle', './dist/.drizzle', { recursive: true })
await rm('./.drizzle', { recursive: true })

View File

@@ -1,50 +0,0 @@
import { REST } from '@discordjs/rest'
import { getMissingEnvironmentVariables } from '@revanced/bot-shared'
import { Routes } from 'discord-api-types/v9'
import type {
RESTGetCurrentApplicationResult,
RESTPutAPIApplicationCommandsResult,
RESTPutAPIApplicationGuildCommandsResult,
} from 'discord.js'
import { config, discord, logger } from '../src/context'
// Check if token exists
const missingEnvs = getMissingEnvironmentVariables(['DISCORD_TOKEN'])
if (missingEnvs.length) {
for (const env of missingEnvs) logger.fatal(`${env} is not defined in environment variables`)
process.exit(1)
}
// Group commands by global and guild
const { global: globalCommands = [], guild: guildCommands = [] } = Object.groupBy(Object.values(discord.commands), c =>
c.global ? 'global' : 'guild',
)
// Set commands
const rest = new REST({ version: '10' }).setToken(process.env['DISCORD_TOKEN']!)
try {
const app = (await rest.get(Routes.currentApplication())) as RESTGetCurrentApplicationResult
const data = (await rest.put(Routes.applicationCommands(app.id), {
body: globalCommands.map(({ data }) => {
if (!data.dm_permission) data.dm_permission = true
logger.warn(`Command ${data.name} has no dm_permission set, forcing to true as it is a global command`)
return data
}),
})) as RESTPutAPIApplicationCommandsResult
logger.info(`Reloaded ${data.length} global commands`)
for (const guildId of config.guilds) {
const data = (await rest.put(Routes.applicationGuildCommands(app.id, guildId), {
body: guildCommands.map(x => x.data),
})) as RESTPutAPIApplicationGuildCommandsResult
logger.info(`Reloaded ${data.length} guild commands for guild ${guildId}`)
}
} catch (e) {
logger.fatal(e)
}

View File

@@ -0,0 +1,594 @@
import { ApplicationCommandOptionType } from 'discord.js'
import { createErrorEmbed } from '$/utils/discord/embeds'
import { isAdmin } from '$/utils/discord/permissions'
import { config } from '../context'
import CommandError, { CommandErrorType } from './CommandError'
import type { Filter } from 'config.schema'
import type {
APIApplicationCommandChannelOption,
CacheType,
Channel,
ChatInputCommandInteraction,
CommandInteractionOption,
GuildMember,
Message,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Role,
User,
} from 'discord.js'
export default class Command<
Global extends boolean = false,
Options extends CommandOptionsOptions | undefined = undefined,
AllowMessageCommand extends boolean = false,
> {
name: string
description: string
requirements?: CommandRequirements
options?: Options
global?: Global
#execute: CommandExecuteFunction<Global, Options, AllowMessageCommand>
static OptionType = ApplicationCommandOptionType
constructor({
name,
description,
requirements,
options,
global,
execute,
}: CommandOptions<Global, Options, AllowMessageCommand>) {
this.name = name
this.description = description
this.requirements = requirements
this.options = options
this.global = global
this.#execute = execute
}
async onMessage(
context: typeof import('../context'),
msg: Message<If<Global, false, true>>,
args: CommandArguments,
): Promise<unknown> {
if (!this.global && !msg.inGuild())
return await msg.reply({
embeds: [createErrorEmbed('Cannot run this command', 'This command can only be used in a server.')],
})
const executor = this.global ? msg.author : await msg.guild?.members.fetch(msg.author.id)!
if (!(await this.canExecute(executor, msg.channelId)))
return await msg.reply({
embeds: [
createErrorEmbed(
'Cannot run this command',
'You do not meet the requirements to run this command.',
),
],
})
const options = this.options
? ((await this.#resolveMessageOptions(msg, this.options, args)) as CommandExecuteFunctionOptionsParameter<
NonNullable<Options>
>)
: undefined
// @ts-expect-error: Type mismatch (again!) because TypeScript is not smart enough
return await this.#execute({ ...context, executor }, msg, options)
}
async #resolveMessageOptions(msg: Message, options: CommandOptionsOptions, args: CommandArguments) {
const iterableOptions = Object.entries(options)
const _options = {} as unknown
for (let i = 0; i < iterableOptions.length; i++) {
const [name, option] = iterableOptions[i]!
const { type, required, description } = option
const isSubcommandLikeOption =
type === ApplicationCommandOptionType.Subcommand ||
type === ApplicationCommandOptionType.SubcommandGroup
const arg = args[i]
const expectedType = `${ApplicationCommandOptionType[type]}${required ? '' : '?'}`
const argExplainationString = `\n-# **${name}**: ${description}`
const choicesString =
'choices' in option && option.choices
? `\n\n-# **AVAILABLE CHOICES**\n${option.choices.map(({ value }) => `- ${value}`).join('\n')}`
: ''
if (isSubcommandLikeOption && !arg)
throw new CommandError(
CommandErrorType.MissingArgument,
`Missing required subcommand.\n\n-# **AVAILABLE SUBCOMMANDS**\n${iterableOptions.map(([name, { description }]) => `- **${name}**: ${description}`).join('\n')}`,
)
if (required && !arg)
throw new CommandError(
CommandErrorType.MissingArgument,
`Missing required argument **${name}** with type **${expectedType}**.${argExplainationString}${choicesString}`,
)
if (typeof arg === 'object' && arg.type !== type)
throw new CommandError(
CommandErrorType.InvalidArgument,
`Invalid type for argument **${name}**.${argExplainationString}\n\nExpected type: **${expectedType}**\nGot type: **${ApplicationCommandOptionType[arg.type]}**${choicesString}`,
)
if ('choices' in option && option.choices && !option.choices.some(({ value }) => value === arg))
throw new CommandError(
CommandErrorType.InvalidArgument,
`Invalid choice for argument **${name}**.\n${argExplainationString}\n\n${choicesString}\n`,
)
const argValue = typeof arg === 'string' ? arg : arg?.id
if (argValue && arg) {
if (isSubcommandLikeOption) {
const [subcommandName, subcommandOption] = iterableOptions.find(([name]) => name === argValue)!
// @ts-expect-error: Not smart enough, TypeScript :(
_options[subcommandName] = await this.#resolveMessageOptions(
msg,
(subcommandOption as CommandSubcommandLikeOption).options,
args.slice(i + 1),
)
break
}
if (
(type === ApplicationCommandOptionType.Channel ||
type === ApplicationCommandOptionType.User ||
type === ApplicationCommandOptionType.Role) &&
Number.isNaN(Number(argValue))
)
throw new CommandError(
CommandErrorType.InvalidArgument,
`Malformed ID for argument **${name}**.${argExplainationString}`,
)
if (
(type === ApplicationCommandOptionType.Number || type === ApplicationCommandOptionType.Integer) &&
Number.isNaN(Number(argValue))
) {
throw new CommandError(
CommandErrorType.InvalidArgument,
`Invalid number for argument **${name}**.${argExplainationString}`,
)
}
if (
type === ApplicationCommandOptionType.Boolean &&
!['true', 'false', 'yes', 'no', 'y', 'n', 't', 'f'].includes(argValue)
)
throw new CommandError(
CommandErrorType.InvalidArgument,
`Invalid boolean for argument **${name}**.${argExplainationString}`,
)
// @ts-expect-error: Not smart enough, TypeScript :(
_options[name] =
type === ApplicationCommandOptionType.Number || type === ApplicationCommandOptionType.Integer
? Number(argValue)
: type === ApplicationCommandOptionType.Boolean
? argValue[0] === 't' || argValue[0] === 'y'
: type === ApplicationCommandOptionType.Channel
? await msg.client.channels.fetch(argValue)
: type === ApplicationCommandOptionType.User
? await msg.client.users.fetch(argValue)
: type === ApplicationCommandOptionType.Role
? await msg.guild?.roles.fetch(argValue)
: argValue
}
}
return _options
}
async onInteraction(
context: typeof import('../context'),
interaction: ChatInputCommandInteraction,
): Promise<unknown> {
const { logger } = context
if (interaction.commandName !== this.name) {
logger.warn(`Command name mismatch, expected ${this.name}, but got ${interaction.commandName}!`)
return await interaction.reply({
embeds: [
createErrorEmbed(
'Internal command name mismatch',
'The interaction command name does not match the expected command name.',
),
],
})
}
if (!this.global && !interaction.inGuild()) {
logger.error(`Command ${this.name} cannot be run in DMs, but was registered as global`)
return await interaction.reply({
embeds: [createErrorEmbed('Cannot run this command', 'This command can only be used in a server.')],
ephemeral: true,
})
}
const executor = this.global ? interaction.user : await interaction.guild?.members.fetch(interaction.user.id)!
if (!(await this.canExecute(executor, interaction.channelId)))
return await interaction.reply({
embeds: [
createErrorEmbed(
'Cannot run this command',
'You do not meet the requirements to run this command.',
),
],
ephemeral: true,
})
const options = this.options
? ((await this.#resolveInteractionOptions(interaction)) as CommandExecuteFunctionOptionsParameter<
NonNullable<Options>
>)
: undefined
if (options === null)
return await interaction.reply({
embeds: [
createErrorEmbed(
'Internal command option type mismatch',
'The interaction command option type does not match the expected command option type.',
),
],
})
// @ts-expect-error: Type mismatch (again!) because TypeScript is not smart enough
return await this.#execute({ ...context, executor }, interaction, options)
}
async #resolveInteractionOptions(
interaction: ChatInputCommandInteraction,
options: readonly CommandInteractionOption[] = interaction.options.data,
) {
const _options = {} as unknown
if (this.options)
for (const { name, type, value } of options) {
if (this.options[name]?.type !== type) return null
if (
type === ApplicationCommandOptionType.Subcommand ||
type === ApplicationCommandOptionType.SubcommandGroup
) {
const subOptions = Object.entries((this.options[name] as CommandSubcommandLikeOption).options)
// @ts-expect-error: Not smart enough, TypeScript :(
_options[name] = await this.#resolveInteractionOptions(interaction, subOptions)
break
}
if (!value) continue
// @ts-expect-error: Not smart enough, TypeScript :(
_options[name] =
type === ApplicationCommandOptionType.Channel
? await interaction.client.channels.fetch(value as string)
: type === ApplicationCommandOptionType.User
? await interaction.client.users.fetch(value as string)
: type === ApplicationCommandOptionType.Role
? await interaction.guild?.roles.fetch(value as string)
: value
}
return _options
}
async canExecute(executor: User | GuildMember, channelId: string): Promise<boolean> {
if (!this.requirements) return false
const isExecutorAdmin = isAdmin(executor)
if (isExecutorAdmin) return true
const {
adminOnly,
channels,
roles,
permissions,
users,
mode = 'all',
defaultCondition = 'fail',
memberRequirementsForUsers = 'pass',
} = this.requirements
const member = this.global ? null : (executor as GuildMember)
const bDefCond = defaultCondition !== 'fail'
const bMemReqForUsers = memberRequirementsForUsers !== 'fail'
const conditions = [
adminOnly ? isExecutorAdmin : bDefCond,
channels ? channels.includes(channelId) : bDefCond,
member ? (roles ? roles.some(role => member.roles.cache.has(role)) : bDefCond) : bMemReqForUsers,
member ? (permissions ? member.permissions.has(permissions) : bDefCond) : bMemReqForUsers,
users ? users.includes(executor.id) : bDefCond,
]
if (mode === 'all' && conditions.some(condition => !condition)) return false
if (mode === 'any' && conditions.every(condition => !condition)) return false
return true
}
get json(): RESTPostAPIChatInputApplicationCommandsJSONBody & { contexts: Array<0 | 1 | 2> } {
return {
name: this.name,
description: this.description,
options: this.options ? this.#transformOptions(this.options) : undefined,
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object-interaction-context-types
contexts: this.global ? [0] : [0, 1],
}
}
#transformOptions(optionsObject: Record<string, CommandOption>) {
const options: RESTPostAPIChatInputApplicationCommandsJSONBody['options'] = []
for (const [name, option] of Object.entries(optionsObject)) {
options.push({
// biome-ignore lint/suspicious/noExplicitAny: Good enough work here
type: option.type as any,
name,
description: option.description,
required: option.required,
...(option.type === ApplicationCommandOptionType.Subcommand ||
option.type === ApplicationCommandOptionType.SubcommandGroup
? {
options: this.#transformOptions((option as CommandSubcommandLikeOption).options),
}
: {}),
...(option.type === ApplicationCommandOptionType.Channel ? { channel_types: option.types } : {}),
...(option.type === ApplicationCommandOptionType.Integer ||
option.type === ApplicationCommandOptionType.Number
? {
min_value: option.min,
max_value: option.max,
choices: option.choices,
autocomplete: option.autocomplete,
}
: {}),
...(option.type === ApplicationCommandOptionType.String
? {
min_length: option.minLength,
max_length: option.maxLength,
choices: option.choices,
autocomplete: option.autocomplete,
}
: {}),
})
}
return options
}
}
export class ModerationCommand<
Options extends CommandOptionsOptions,
AllowMessageCommand extends boolean = true,
> extends Command<false, Options, AllowMessageCommand> {
constructor(options: ExtendedCommandOptions<false, Options, AllowMessageCommand>) {
super({
...options,
requirements: {
...options.requirements,
defaultCondition: 'pass',
roles: (config.moderation?.roles ?? []).concat(options.requirements?.roles ?? []),
},
// @ts-expect-error: No thanks
allowMessageCommand: options.allowMessageCommand ?? true,
global: false,
})
}
}
export class AdminCommand<Options extends CommandOptionsOptions, AllowMessageCommand extends boolean> extends Command<
true,
Options,
AllowMessageCommand
> {
constructor(options: ExtendedCommandOptions<true, Options, AllowMessageCommand>) {
super({
...options,
requirements: {
...options.requirements,
adminOnly: true,
defaultCondition: 'pass',
},
global: true,
})
}
}
/* TODO:
APIApplicationCommandAttachmentOption
APIApplicationCommandMentionableOption
APIApplicationCommandRoleOption
*/
export interface CommandOptions<
Global extends boolean,
Options extends CommandOptionsOptions | undefined,
AllowMessageCommand extends boolean,
> {
name: string
description: string
requirements?: CommandRequirements
options?: Options
execute: CommandExecuteFunction<Global, Options, AllowMessageCommand>
global?: Global
allowMessageCommand?: AllowMessageCommand
}
export type CommandArguments = Array<string | CommandSpecialArgument>
export type CommandSpecialArgument = {
type: (typeof CommandSpecialArgumentType)[keyof typeof CommandSpecialArgumentType]
id: string
}
export const CommandSpecialArgumentType = {
Channel: ApplicationCommandOptionType.Channel,
Role: ApplicationCommandOptionType.Role,
User: ApplicationCommandOptionType.User,
}
type ExtendedCommandOptions<
Global extends boolean,
Options extends CommandOptionsOptions,
AllowMessageCommand extends boolean,
> = Omit<CommandOptions<Global, Options, AllowMessageCommand>, 'global'> & {
requirements?: Omit<CommandOptions<false, Options, AllowMessageCommand>['requirements'], 'defaultCondition'>
}
export type CommandOptionsOptions = Record<string, CommandOption>
type CommandExecuteFunction<
Global extends boolean,
Options extends CommandOptionsOptions | undefined,
AllowMessageCommand extends boolean,
> = (
context: CommandContext<Global>,
trigger: If<
AllowMessageCommand,
Message<InvertBoolean<Global>> | ChatInputCommandInteraction<If<Global, CacheType, 'raw' | 'cached'>>,
ChatInputCommandInteraction<If<Global, CacheType, 'raw' | 'cached'>>
>,
options: Options extends CommandOptionsOptions ? CommandExecuteFunctionOptionsParameter<Options> : never,
) => Promise<unknown> | unknown
type If<T extends boolean | undefined, U, V> = T extends true ? U : V
type InvertBoolean<T extends boolean> = If<T, false, true>
type CommandExecuteFunctionOptionsParameter<Options extends CommandOptionsOptions> = {
[K in keyof Options]: Options[K]['type'] extends
| ApplicationCommandOptionType.Subcommand
| ApplicationCommandOptionType.SubcommandGroup
? // @ts-expect-error: Shut up, it works
CommandExecuteFunctionOptionsParameter<Options[K]['options']> | undefined
: If<
Options[K]['required'],
CommandOptionValueMap[Options[K]['type']],
CommandOptionValueMap[Options[K]['type']] | undefined
>
}
type CommandContext<Global extends boolean> = typeof import('../context') & {
executor: CommandExecutor<Global>
}
type CommandOptionValueMap = {
[ApplicationCommandOptionType.Boolean]: boolean
[ApplicationCommandOptionType.Channel]: Channel
[ApplicationCommandOptionType.Integer]: number
[ApplicationCommandOptionType.Number]: number
[ApplicationCommandOptionType.String]: string
[ApplicationCommandOptionType.User]: User
[ApplicationCommandOptionType.Role]: Role
[ApplicationCommandOptionType.Subcommand]: never
[ApplicationCommandOptionType.SubcommandGroup]: never
}
type CommandOption =
| CommandBooleanOption
| CommandChannelOption
| CommandIntegerOption
| CommandNumberOption
| CommandStringOption
| CommandUserOption
| CommandRoleOption
| CommandSubcommandOption
| CommandSubcommandGroupOption
type CommandExecutor<Global extends boolean> = If<Global, User, GuildMember>
type CommandOptionBase<Type extends ApplicationCommandOptionType> = {
type: Type
description: string
required?: boolean
}
type CommandBooleanOption = CommandOptionBase<ApplicationCommandOptionType.Boolean>
type CommandChannelOption = CommandOptionBase<ApplicationCommandOptionType.Channel> & {
types: APIApplicationCommandChannelOption['channel_types']
}
interface CommandOptionChoice<ValueType = number | string> {
name: string
value: ValueType
}
type CommandOptionWithAutocompleteOrChoicesWrapper<
Base extends CommandOptionBase<ApplicationCommandOptionType>,
ChoiceType extends CommandOptionChoice,
> =
| (Base & {
autocomplete: true
choices?: never
})
| (Base & {
autocomplete?: false
choices?: ChoiceType[] | readonly ChoiceType[]
})
type CommandIntegerOption = CommandOptionWithAutocompleteOrChoicesWrapper<
CommandOptionBase<ApplicationCommandOptionType.Integer>,
CommandOptionChoice<number>
> & {
min?: number
max?: number
}
type CommandNumberOption = CommandOptionWithAutocompleteOrChoicesWrapper<
CommandOptionBase<ApplicationCommandOptionType.Number>,
CommandOptionChoice<number>
> & {
min?: number
max?: number
}
type CommandStringOption = CommandOptionWithAutocompleteOrChoicesWrapper<
CommandOptionBase<ApplicationCommandOptionType.String>,
CommandOptionChoice<string>
> & {
minLength?: number
maxLength?: number
}
type CommandUserOption = CommandOptionBase<ApplicationCommandOptionType.User>
type CommandRoleOption = CommandOptionBase<ApplicationCommandOptionType.Role>
type SubcommandLikeApplicationCommandOptionType =
| ApplicationCommandOptionType.Subcommand
| ApplicationCommandOptionType.SubcommandGroup
interface CommandSubcommandLikeOption<
Type extends SubcommandLikeApplicationCommandOptionType = SubcommandLikeApplicationCommandOptionType,
> extends CommandOptionBase<Type> {
options: CommandOptionsOptions
required?: never
}
type CommandSubcommandOption = CommandSubcommandLikeOption<ApplicationCommandOptionType.Subcommand>
type CommandSubcommandGroupOption = CommandSubcommandLikeOption<ApplicationCommandOptionType.SubcommandGroup>
export type CommandRequirements = Filter & {
mode?: 'all' | 'any'
adminOnly?: boolean
permissions?: bigint
defaultCondition?: 'fail' | 'pass'
memberRequirementsForUsers?: 'pass' | 'fail'
}

View File

@@ -17,6 +17,7 @@ export default class CommandError extends Error {
export enum CommandErrorType { export enum CommandErrorType {
Generic, Generic,
MissingArgument, MissingArgument,
InvalidArgument,
InvalidUser, InvalidUser,
InvalidChannel, InvalidChannel,
InvalidDuration, InvalidDuration,
@@ -25,6 +26,7 @@ export enum CommandErrorType {
const ErrorTitleMap: Record<CommandErrorType, string> = { const ErrorTitleMap: Record<CommandErrorType, string> = {
[CommandErrorType.Generic]: 'An exception was thrown', [CommandErrorType.Generic]: 'An exception was thrown',
[CommandErrorType.MissingArgument]: 'Missing argument', [CommandErrorType.MissingArgument]: 'Missing argument',
[CommandErrorType.InvalidArgument]: 'Invalid argument',
[CommandErrorType.InvalidUser]: 'Invalid user', [CommandErrorType.InvalidUser]: 'Invalid user',
[CommandErrorType.InvalidChannel]: 'Invalid channel', [CommandErrorType.InvalidChannel]: 'Invalid channel',
[CommandErrorType.InvalidDuration]: 'Invalid duration', [CommandErrorType.InvalidDuration]: 'Invalid duration',

View File

@@ -0,0 +1,34 @@
import { inspect } from 'util'
import { runInThisContext } from 'vm'
import { ApplicationCommandOptionType } from 'discord.js'
import { AdminCommand } from '$/classes/Command'
import { createSuccessEmbed } from '$/utils/discord/embeds'
export default new AdminCommand({
name: 'eval',
description: 'Make the bot less sentient by evaluating code',
options: {
code: {
description: 'The code to evaluate',
type: ApplicationCommandOptionType.String,
required: true,
},
['show-hidden']: {
description: 'Show hidden properties',
type: ApplicationCommandOptionType.Boolean,
required: false,
},
},
async execute(_, trigger, { code, 'show-hidden': showHidden }) {
await trigger.reply({
ephemeral: true,
embeds: [
createSuccessEmbed('Evaluate', `\`\`\`js\n${code}\`\`\``).addFields({
name: 'Result',
value: `\`\`\`js\n${inspect(runInThisContext(code), { depth: 1, showHidden, getters: true, numericSeparator: true, showProxy: true })}\`\`\``,
}),
],
})
},
})

View File

@@ -0,0 +1,21 @@
import { ApplicationCommandOptionType } from 'discord.js'
import { AdminCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError'
export default new AdminCommand({
name: 'exception-test',
description: 'Makes the bot intentionally hate you by throwing an exception',
options: {
type: {
description: 'The type of exception to throw',
type: ApplicationCommandOptionType.String,
required: true,
choices: Object.keys(CommandErrorType).map(k => ({ name: k, value: k })),
},
},
async execute(_, __, { type }) {
if (type === 'Process') throw new Error('Intentional process exception')
throw new CommandError(CommandErrorType[type as keyof typeof CommandErrorType], 'Intentional bot design') // ;)
},
})

View File

@@ -0,0 +1,17 @@
import { AdminCommand } from '$/classes/Command'
import { join, dirname } from 'path'
import type { Config } from 'config.schema'
export default new AdminCommand({
name: 'reload',
description: 'Reload configuration',
async execute(context, trigger) {
context.config = ((await import(join(dirname(Bun.main), '..', 'config.js'))) as { default: Config }).default
await trigger.reply({
content: 'Reloaded configuration',
ephemeral: true,
})
},
})

View File

@@ -0,0 +1,93 @@
import { ApplicationCommandOptionType, Routes } from 'discord.js'
import { AdminCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { createSuccessEmbed } from '$/utils/discord/embeds'
const SubcommandOptions = {
where: {
description: 'Where to register the commands',
type: ApplicationCommandOptionType.String,
choices: [
{ name: 'globally', value: 'global' },
{ name: 'this server', value: 'server' },
],
required: true,
},
} as const
export default new AdminCommand({
name: 'slash-commands',
description: 'Register or delete slash commands',
options: {
register: {
description: 'Register slash commands',
type: ApplicationCommandOptionType.Subcommand,
options: SubcommandOptions,
},
delete: {
description: 'Delete slash commands',
type: ApplicationCommandOptionType.Subcommand,
options: SubcommandOptions,
},
},
allowMessageCommand: true,
async execute(context, trigger, { delete: deleteOption, register }) {
const action = register ? 'register' : 'delete'
const { where } = (deleteOption ?? register)!
if (!trigger.inGuild())
throw new CommandError(CommandErrorType.Generic, 'This command can only be used in a server.')
const { global: globalCommands, guild: guildCommands } = Object.groupBy(
Object.values(context.discord.commands),
cmd => (cmd.global ? 'global' : 'guild'),
)
const {
client,
client: { rest },
} = trigger
let response: string | undefined
switch (action) {
case 'register':
if (where === 'global') {
response = 'Registered global slash commands'
await rest.put(Routes.applicationCommands(client.application.id), {
body: globalCommands?.map(c => c.json),
})
} else {
response = 'Registered slash commands on this server'
await rest.put(Routes.applicationGuildCommands(client.application.id, trigger.guildId), {
body: guildCommands?.map(c => c.json),
})
}
break
case 'delete':
if (where === 'global') {
response = 'Deleted global slash commands'
await rest.put(Routes.applicationCommands(client.application.id), {
body: [],
})
} else {
response = 'Deleted slash commands on this server'
await rest.put(Routes.applicationGuildCommands(client.application.id, trigger.guildId), {
body: [],
})
}
break
}
await trigger.reply({ embeds: [createSuccessEmbed(response!)] })
},
})

View File

@@ -0,0 +1,24 @@
import { AdminCommand } from '$/classes/Command'
export default new AdminCommand({
name: 'stop',
description: "You don't want to run this unless the bot starts to go insane, and like, you really need to stop it.",
async execute({ api, logger, executor }, trigger) {
api.intentionallyDisconnecting = true
logger.fatal('Stopping bot...')
trigger.reply({
content: 'Stopping... (I will go offline once done)',
ephemeral: true,
})
if (!api.client.disconnected) api.client.disconnect()
logger.warn('Disconnected from API')
trigger.client.destroy()
logger.warn('Disconnected from Discord API')
logger.info(`Bot stopped, requested by ${executor.id}`)
process.exit(0)
},
})

View File

@@ -1,32 +0,0 @@
import { inspect } from 'util'
import { SlashCommandBuilder } from 'discord.js'
import { createSuccessEmbed } from '$/utils/discord/embeds'
import type { Command } from '../types'
export default {
data: new SlashCommandBuilder()
.setName('eval')
.setDescription('Make the bot less sentient by evaluating code')
.addStringOption(option => option.setName('code').setDescription('The code to evaluate').setRequired(true))
.setDMPermission(true)
.toJSON(),
ownerOnly: true,
global: true,
async execute(_, interaction) {
const code = interaction.options.getString('code', true)
await interaction.reply({
ephemeral: true,
embeds: [
createSuccessEmbed('Evaluate', `\`\`\`js\n${code}\`\`\``).addFields({
name: 'Result',
// biome-ignore lint/security/noGlobalEval: Deal with it
value: `\`\`\`js\n${inspect(eval(code), { depth: 1 })}\`\`\``,
}),
],
})
},
} satisfies Command

View File

@@ -1,36 +0,0 @@
import { SlashCommandBuilder } from 'discord.js'
import CommandError, { CommandErrorType } from '$/classes/CommandError'
import type { Command } from '../types'
export default {
data: new SlashCommandBuilder()
.setName('exception-test')
.setDescription('Makes the bot intentionally hate you by throwing an exception')
.addStringOption(option =>
option
.setName('type')
.setDescription('The type of exception to throw')
.setRequired(true)
.addChoices(
Object.keys(CommandErrorType).map(
k =>
({
name: k,
value: k,
}) as const,
),
),
)
.setDMPermission(true)
.toJSON(),
ownerOnly: true,
global: true,
async execute(_, interaction) {
const type = interaction.options.getString('type', true)
if (type === 'Process') throw new Error('Intentional process exception')
throw new CommandError(CommandErrorType[type as keyof typeof CommandErrorType], 'Intentional bot design') // ;)
},
} satisfies Command

View File

@@ -1,35 +0,0 @@
import { SlashCommandBuilder } from 'discord.js'
import type { Command } from '../types'
export default {
data: new SlashCommandBuilder()
.setName('stop')
.setDescription(
"You don't want to run this unless the bot starts to go insane, and like, you really need to stop it.",
)
.setDMPermission(true)
.toJSON(),
ownerOnly: true,
global: true,
async execute({ api, logger }, interaction) {
api.isStopping = true
logger.fatal('Stopping bot...')
await interaction.reply({
content: 'Stopping... (I will go offline once done)',
ephemeral: true,
})
api.client.disconnect()
logger.warn('Disconnected from API')
await interaction.client.destroy()
logger.warn('Disconnected from Discord API')
logger.info(`Bot stopped, requested by ${interaction.user.id}`)
process.exit(0)
},
} satisfies Command

View File

@@ -1,32 +1,37 @@
import { EmbedBuilder } from 'discord.js'
import Command from '$/classes/Command'
import { applyCommonEmbedStyles } from '$/utils/discord/embeds' import { applyCommonEmbedStyles } from '$/utils/discord/embeds'
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js' export default new Command({
name: 'coinflip',
import type { Command } from '../types' description: 'Do a coinflip!',
export default {
data: new SlashCommandBuilder().setName('coinflip').setDescription('Do a coinflip!').setDMPermission(true).toJSON(),
global: true, global: true,
requirements: {
async execute(_, interaction) { defaultCondition: 'pass',
},
allowMessageCommand: true,
async execute(_, trigger) {
const result = Math.random() < 0.5 ? ('heads' as const) : ('tails' as const) const result = Math.random() < 0.5 ? ('heads' as const) : ('tails' as const)
const embed = applyCommonEmbedStyles(new EmbedBuilder().setTitle('Flipping... 🪙'), true, false, false) const embed = applyCommonEmbedStyles(new EmbedBuilder().setTitle('Flipping... 🪙'), false, false, true)
await interaction.reply({ const reply = await trigger
.reply({
embeds: [embed.toJSON()], embeds: [embed.toJSON()],
}) })
.then(it => it.fetch())
embed.setTitle(`The coin landed on... **${result.toUpperCase()}**! ${EmojiMap[result]}`) embed.setTitle(`The coin landed on... **${result.toUpperCase()}**! ${EmojiMap[result]}`)
setTimeout( setTimeout(
() => () =>
interaction.editReply({ reply.edit({
embeds: [embed.toJSON()], embeds: [embed.toJSON()],
}), }),
1500, 1500,
) )
}, },
} satisfies Command })
const EmojiMap: Record<'heads' | 'tails', string> = { const EmojiMap: Record<'heads' | 'tails', string> = {
heads: '🤯', heads: '🤯',

View File

@@ -1,44 +1,46 @@
import { SlashCommandBuilder, type TextBasedChannel } from 'discord.js' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { ApplicationCommandOptionType, Message } from 'discord.js'
import { ModerationCommand } from '../../classes/Command'
import { config } from '$/context' export default new ModerationCommand({
import type { Command } from '../types' name: 'reply',
description: 'Send a message as the bot',
export default { options: {
data: new SlashCommandBuilder() message: {
.setName('reply') description: 'The message to send',
.setDescription('Send a message as the bot') required: true,
.addStringOption(option => option.setName('message').setDescription('The message to send').setRequired(true)) type: ApplicationCommandOptionType.String,
.addStringOption(option =>
option
.setName('reference')
.setDescription('The message ID to reply to (use `latest` to reply to the latest message)')
.setRequired(false),
)
.toJSON(),
memberRequirements: {
roles: config.moderation?.roles ?? [],
}, },
reference: {
description: 'The message ID to reply to (use `latest` to reply to the latest message)',
required: false,
type: ApplicationCommandOptionType.String,
},
},
allowMessageCommand: false,
async execute({ logger, executor }, trigger, { reference: ref, message: msg }) {
if (trigger instanceof Message) return
global: false, const channel = await trigger.guild!.channels.fetch(trigger.channelId)
if (!channel?.isTextBased())
async execute({ logger }, interaction) { throw new CommandError(
const msg = interaction.options.getString('message', true) CommandErrorType.InvalidArgument,
const ref = interaction.options.getString('reference') 'This command can only be used in or on text channels',
)
const channel = (await interaction.guild!.channels.fetch(interaction.channelId)) as TextBasedChannel const refMsg = ref?.startsWith('latest')
const refMsg = ref?.startsWith('latest') ? (await channel.messages.fetch({ limit: 1 })).at(0)?.id : ref ? await channel.messages.fetch({ limit: 1 }).then(it => it.first())
: ref
await channel.send({ await channel.send({
content: msg, content: msg,
reply: refMsg ? { messageReference: refMsg, failIfNotExists: true } : undefined, reply: refMsg ? { messageReference: refMsg, failIfNotExists: true } : undefined,
}) })
logger.info(`User ${interaction.user.tag} made the bot say: ${msg}`) logger.info(`User ${executor.user.tag} made the bot say: ${msg}`)
await interaction.reply({ await trigger.reply({
content: 'OK!', content: 'OK!',
ephemeral: true, ephemeral: true,
}) })
}, },
} satisfies Command })

View File

@@ -1,58 +1,58 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import type { Command } from '../types'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { config } from '$/context'
import { createModerationActionEmbed } from '$/utils/discord/embeds' import { createModerationActionEmbed } from '$/utils/discord/embeds'
import { sendModerationReplyAndLogs } from '$/utils/discord/moderation' import { sendModerationReplyAndLogs } from '$/utils/discord/moderation'
import { parseDuration } from '$/utils/duration' import { parseDuration } from '$/utils/duration'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'ban',
.setName('ban') description: 'Ban a user',
.setDescription('Ban a user') options: {
.addUserOption(option => option.setName('user').setRequired(true).setDescription('The user to ban')) user: {
.addStringOption(option => option.setName('reason').setDescription('The reason for banning the user')) description: 'The user to ban',
.addStringOption(option => required: true,
option.setName('dmd').setDescription('Duration to delete messages (must be from 0 to 7 days)'), type: ModerationCommand.OptionType.User,
)
.toJSON(),
memberRequirements: {
roles: config.moderation?.roles ?? [],
}, },
reason: {
description: 'The reason for banning the user',
required: false,
type: ModerationCommand.OptionType.String,
},
dmd: {
description: 'Duration to delete messages (must be from 0 to 7 days)',
required: false,
type: ModerationCommand.OptionType.String,
},
},
async execute({ logger, executor }, interaction, { user, reason, dmd }) {
const guild = await interaction.client.guilds.fetch(interaction.guildId)
const member = await guild.members.fetch(user).catch(() => {})
const moderator = await guild.members.fetch(executor.user)
global: false, if (member) {
if (!member.bannable)
async execute({ logger }, interaction) { throw new CommandError(CommandErrorType.Generic, 'This user cannot be banned by the bot.')
const user = interaction.options.getUser('user', true)
const reason = interaction.options.getString('reason') ?? 'No reason provided'
const dmd = interaction.options.getString('dmd')
const member = await interaction.guild!.members.fetch(user.id)
const moderator = await interaction.guild!.members.fetch(interaction.user.id)
if (member.bannable) throw new CommandError(CommandErrorType.Generic, 'This user cannot be banned by the bot.')
if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0) if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidUser, CommandErrorType.InvalidUser,
'You cannot ban a user with a role equal to or higher than yours.', 'You cannot ban a user with a role equal to or higher than yours.',
) )
}
const dms = Math.floor(dmd ? parseDuration(dmd) : 0 / 1000) const dms = Math.floor(dmd ? parseDuration(dmd) : 0 / 1000)
await interaction.guild!.members.ban(user, { await interaction.guild!.members.ban(user, {
reason: `Banned by moderator ${interaction.user.tag} (${interaction.user.id}): ${reason}`, reason: `Banned by moderator ${executor.user.tag} (${executor.id}): ${reason}`,
deleteMessageSeconds: dms, deleteMessageSeconds: dms,
}) })
await sendModerationReplyAndLogs( await sendModerationReplyAndLogs(
interaction, interaction,
createModerationActionEmbed('Banned', user, interaction.user, reason), createModerationActionEmbed('Banned', user, executor.user, reason),
) )
logger.info( logger.info(
`${interaction.user.tag} (${interaction.user.id}) banned ${user.tag} (${user.id}) because ${reason}, deleting their messages sent in the previous ${dms}s`, `${executor.user.tag} (${executor.id}) banned ${user.tag} (${user.id}) because ${reason}, deleting their messages sent in the previous ${dms}s`,
) )
}, },
} satisfies Command })

View File

@@ -1,31 +1,24 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import type { Command } from '../types'
import { config } from '$/context'
import { createSuccessEmbed } from '$/utils/discord/embeds' import { createSuccessEmbed } from '$/utils/discord/embeds'
import { cureNickname } from '$/utils/discord/moderation' import { cureNickname } from '$/utils/discord/moderation'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'cure',
.setName('cure') description: "Cure a member's nickname",
.setDescription("Cure a member's nickname") options: {
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to cure')) member: {
.toJSON(), description: 'The member to cure',
required: true,
memberRequirements: { type: ModerationCommand.OptionType.User,
roles: config.moderation?.roles ?? [],
}, },
},
global: false, async execute(_, interaction, { member: user }) {
const guild = await interaction.client.guilds.fetch(interaction.guildId)
async execute(_, interaction) { const member = await guild.members.fetch(user)
const user = interaction.options.getUser('member', true)
const member = await interaction.guild!.members.fetch(user.id)
await cureNickname(member) await cureNickname(member)
await interaction.reply({ await interaction.reply({
embeds: [createSuccessEmbed(null, `Cured nickname for ${member.toString()}`)], embeds: [createSuccessEmbed(null, `Cured nickname for ${member.toString()}`)],
ephemeral: true, ephemeral: true,
}) })
}, },
} satisfies Command })

View File

@@ -1,44 +1,47 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { applyRolePreset } from '$/utils/discord/rolePresets'
import type { Command } from '../types'
import { config } from '$/context'
import { createModerationActionEmbed } from '$/utils/discord/embeds' import { createModerationActionEmbed } from '$/utils/discord/embeds'
import { sendModerationReplyAndLogs } from '$/utils/discord/moderation' import { sendModerationReplyAndLogs } from '$/utils/discord/moderation'
import { applyRolePreset, removeRolePreset } from '$/utils/discord/rolePresets'
import { parseDuration } from '$/utils/duration' import { parseDuration } from '$/utils/duration'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'mute',
.setName('mute') description: 'Mute a member',
.setDescription('Mute a member') options: {
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to mute')) member: {
.addStringOption(option => option.setName('reason').setDescription('The reason for muting the member')) description: 'The member to mute',
.addStringOption(option => option.setName('duration').setDescription('The duration of the mute')) required: true,
.toJSON(), type: ModerationCommand.OptionType.User,
memberRequirements: {
roles: config.moderation?.roles ?? [],
}, },
reason: {
description: 'The reason for muting the member',
required: false,
type: ModerationCommand.OptionType.String,
},
duration: {
description: 'The duration of the mute',
required: false,
type: ModerationCommand.OptionType.String,
},
},
async execute(
{ logger, executor },
interaction,
{ member: user, reason = 'No reason provided', duration: durationInput },
) {
const guild = await interaction.client.guilds.fetch(interaction.guildId)
const member = await guild.members.fetch(user.id)
const moderator = await guild.members.fetch(executor.id)
const duration = durationInput ? parseDuration(durationInput) : Infinity
global: false, if (Number.isInteger(duration) && duration! < 1)
async execute({ logger }, interaction, { userIsOwner }) {
const user = interaction.options.getUser('member', true)
const reason = interaction.options.getString('reason') ?? 'No reason provided'
const duration = interaction.options.getString('duration')
const durationMs = duration ? parseDuration(duration) : null
if (Number.isInteger(durationMs) && durationMs! < 1)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidDuration, CommandErrorType.InvalidDuration,
'The duration must be at least 1 millisecond long.', 'The duration must be at least 1 millisecond long.',
) )
const expires = durationMs ? Date.now() + durationMs : null const expires = Math.max(duration, Date.now() + duration)
const moderator = await interaction.guild!.members.fetch(interaction.user.id)
const member = await interaction.guild!.members.fetch(user.id)
if (!member) if (!member)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidUser, CommandErrorType.InvalidUser,
@@ -48,20 +51,25 @@ export default {
if (!member.manageable) if (!member.manageable)
throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.') throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.')
if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0 && !userIsOwner) if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidUser, CommandErrorType.InvalidUser,
'You cannot mute a user with a role equal to or higher than yours.', 'You cannot mute a user with a role equal to or higher than yours.',
) )
await applyRolePreset(member, 'mute', durationMs ? Date.now() + durationMs : null) await applyRolePreset(member, 'mute', expires)
await sendModerationReplyAndLogs( await sendModerationReplyAndLogs(
interaction, interaction,
createModerationActionEmbed('Muted', user, interaction.user, reason, durationMs), createModerationActionEmbed('Muted', user, executor.user, reason, duration),
) )
if (duration)
setTimeout(() => {
removeRolePreset(member, 'mute')
}, duration)
logger.info( logger.info(
`Moderator ${interaction.user.tag} (${interaction.user.id}) muted ${user.tag} (${user.id}) until ${expires} because ${reason}`, `Moderator ${executor.user.tag} (${executor.user.id}) muted ${user.tag} (${user.id}) until ${expires} because ${reason}`,
) )
}, },
} satisfies Command })

View File

@@ -1,42 +1,37 @@
import { EmbedBuilder, GuildChannel, SlashCommandBuilder } from 'discord.js' import { EmbedBuilder } from 'discord.js'
import { ModerationCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { config } from '$/context'
import { applyCommonEmbedStyles } from '$/utils/discord/embeds' import { applyCommonEmbedStyles } from '$/utils/discord/embeds'
import type { Command } from '../types' export default new ModerationCommand({
name: 'purge',
export default { description: 'Purge messages from a channel',
data: new SlashCommandBuilder() options: {
.setName('purge') amount: {
.setDescription('Purge messages from a channel') description: 'The amount of messages to remove',
.addIntegerOption(option => required: false,
option.setName('amount').setDescription('The amount of messages to remove').setMaxValue(100).setMinValue(1), type: ModerationCommand.OptionType.Integer,
) min: 1,
.addUserOption(option => max: 100,
option.setName('user').setDescription('The user to remove messages from (needs `until`)'),
)
.addStringOption(option =>
option.setName('until').setDescription('The message ID to remove messages until (overrides `amount`)'),
)
.toJSON(),
memberRequirements: {
roles: config.moderation?.roles ?? [],
}, },
user: {
global: false, description: 'The user to remove messages from (needs `until`)',
required: false,
async execute({ logger }, interaction) { type: ModerationCommand.OptionType.User,
const amount = interaction.options.getInteger('amount') },
const user = interaction.options.getUser('user') until: {
const until = interaction.options.getString('until') description: 'The message ID to remove messages until (overrides `amount`)',
required: false,
type: ModerationCommand.OptionType.String,
},
},
async execute({ logger, executor }, interaction, { amount, user, until }) {
if (!amount && !until) if (!amount && !until)
throw new CommandError(CommandErrorType.MissingArgument, 'Either `amount` or `until` must be provided.') throw new CommandError(CommandErrorType.MissingArgument, 'Either `amount` or `until` must be provided.')
const channel = interaction.channel! const channel = interaction.channel!
if (!(channel.isTextBased() && channel instanceof GuildChannel)) if (!channel.isTextBased())
throw new CommandError(CommandErrorType.InvalidChannel, 'The supplied channel is not a text channel.') throw new CommandError(CommandErrorType.InvalidChannel, 'The supplied channel is not a text channel.')
const embed = applyCommonEmbedStyles( const embed = applyCommonEmbedStyles(
@@ -59,8 +54,9 @@ export default {
await channel.bulkDelete(messages, true) await channel.bulkDelete(messages, true)
logger.info( logger.info(
`Moderator ${interaction.user.tag} (${interaction.user.id}) purged ${messages.size} messages in #${channel.name} (${channel.id})`, `Moderator ${executor.user.tag} (${executor.id}) purged ${messages.size} messages in #${channel.name} (${channel.id})`,
) )
await reply.edit({ await reply.edit({
embeds: [ embeds: [
embed.setTitle('Purged messages').setDescription(null).addFields({ embed.setTitle('Purged messages').setDescription(null).addFields({
@@ -70,4 +66,4 @@ export default {
], ],
}) })
}, },
} satisfies Command })

View File

@@ -1,49 +1,48 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { sendPresetReplyAndLogs } from '$/utils/discord/moderation' import { sendPresetReplyAndLogs } from '$/utils/discord/moderation'
import { applyRolePreset, removeRolePreset } from '$/utils/discord/rolePresets' import { applyRolePreset, removeRolePreset } from '$/utils/discord/rolePresets'
import { parseDuration } from '$/utils/duration' import { parseDuration } from '$/utils/duration'
import type { Command } from '../types'
export default { const SubcommandOptions = {
data: new SlashCommandBuilder() member: {
.setName('role-preset') description: 'The member to manage',
.setDescription('Manage role presets for a member') required: true,
.addStringOption(option => type: ModerationCommand.OptionType.User,
option
.setName('action')
.setRequired(true)
.setDescription('The action to perform')
.addChoices([
{ name: 'apply', value: 'apply' },
{ name: 'remove', value: 'remove' },
]),
)
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to manage'))
.addStringOption(option =>
option.setName('preset').setRequired(true).setDescription('The preset to apply or remove'),
)
.addStringOption(option =>
option.setName('duration').setDescription('The duration to apply the preset for (only for apply action)'),
)
.toJSON(),
memberRequirements: {
roles: ['955220417969262612', '973886585294704640'],
}, },
preset: {
description: 'The preset to apply or remove',
required: true,
type: ModerationCommand.OptionType.String,
},
duration: {
description: 'The duration to apply the preset for (only for apply action)',
required: false,
type: ModerationCommand.OptionType.String,
},
} as const
global: false, export default new ModerationCommand({
name: 'role-preset',
description: 'Manage role presets for a member',
options: {
apply: {
description: 'Apply a role preset to a member',
type: ModerationCommand.OptionType.Subcommand,
options: SubcommandOptions,
},
remove: {
description: 'Remove a role preset from a member',
type: ModerationCommand.OptionType.Subcommand,
options: SubcommandOptions,
},
},
async execute({ logger, executor }, trigger, { apply, remove }) {
let expires: number | undefined
const { member: user, duration: durationInput, preset } = (apply ?? remove)!
const moderator = await trigger.guild!.members.fetch(executor.user.id)
const member = await trigger.guild!.members.fetch(user.id)
async execute({ logger }, interaction, { userIsOwner }) {
const action = interaction.options.getString('action', true) as 'apply' | 'remove'
const user = interaction.options.getUser('member', true)
const preset = interaction.options.getString('preset', true)
const duration = interaction.options.getString('duration')
let expires: number | null | undefined = undefined
const moderator = await interaction.guild!.members.fetch(interaction.user.id)
const member = await interaction.guild!.members.fetch(user.id)
if (!member) if (!member)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidUser, CommandErrorType.InvalidUser,
@@ -53,32 +52,37 @@ export default {
if (!member.manageable) if (!member.manageable)
throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.') throw new CommandError(CommandErrorType.Generic, 'This user cannot be managed by the bot.')
if (action === 'apply') { if (apply) {
const durationMs = duration ? parseDuration(duration) : null const duration = durationInput ? parseDuration(durationInput) : Infinity
if (Number.isInteger(durationMs) && durationMs! < 1) if (Number.isInteger(duration) && duration! < 1)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidDuration, CommandErrorType.InvalidDuration,
'The duration must be at least 1 millisecond long.', 'The duration must be at least 1 millisecond long.',
) )
if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0 && !userIsOwner) if (moderator.roles.highest.comparePositionTo(member.roles.highest) <= 0)
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidUser, CommandErrorType.InvalidUser,
'You cannot apply a role preset to a user with a role equal to or higher than yours.', 'You cannot apply a role preset to a user with a role equal to or higher than yours.',
) )
expires = durationMs ? Date.now() + durationMs : null expires = Math.max(duration, Date.now() + duration)
await applyRolePreset(member, preset, expires) await applyRolePreset(member, preset, expires)
logger.info( logger.info(
`Moderator ${interaction.user.tag} (${interaction.user.id}) applied role preset ${preset} to ${user.id} until ${expires}`, `Moderator ${executor.user.tag} (${executor.user.id}) applied role preset ${preset} to ${user.id} until ${expires}`,
) )
} else if (action === 'remove') { } else if (remove) {
await removeRolePreset(member, preset) await removeRolePreset(member, preset)
logger.info( logger.info(
`Moderator ${interaction.user.tag} (${interaction.user.id}) removed role preset ${preset} from ${user.id}`, `Moderator ${executor.user.tag} (${executor.user.id}) removed role preset ${preset} from ${user.id}`,
) )
} }
await sendPresetReplyAndLogs(action, interaction, user, preset, expires) if (expires)
setTimeout(() => {
removeRolePreset(member, preset)
}, expires)
await sendPresetReplyAndLogs(apply ? 'apply' : 'remove', trigger, executor, user, preset, expires)
}, },
} satisfies Command })

View File

@@ -1,42 +1,34 @@
import { createSuccessEmbed } from '$/utils/discord/embeds' import { createSuccessEmbed } from '$/utils/discord/embeds'
import { durationToString, parseDuration } from '$/utils/duration' import { durationToString, parseDuration } from '$/utils/duration'
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { config } from '$/context' import { ChannelType } from 'discord.js'
import type { Command } from '../types'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'slowmode',
.setName('slowmode') description: 'Set a slowmode for a channel',
.setDescription('Set a slowmode for the current channel') options: {
.addStringOption(option => option.setName('duration').setDescription('The duration to set').setRequired(true)) duration: {
.addStringOption(option => description: 'The duration to set',
option required: true,
.setName('channel') type: ModerationCommand.OptionType.String,
.setDescription('The channel to set the slowmode on (defaults to current channel)')
.setRequired(false),
)
.toJSON(),
memberRequirements: {
roles: config.moderation?.roles ?? [],
}, },
channel: {
description: 'The channel to set the slowmode on (defaults to current channel)',
required: false,
type: ModerationCommand.OptionType.Channel,
types: [ChannelType.GuildText],
},
},
async execute({ logger, executor }, interaction, { duration: durationInput, channel: channelInput }) {
const channel = channelInput ?? (await interaction.guild!.channels.fetch(interaction.channelId))
const duration = parseDuration(durationInput)
global: false, if (!channel?.isTextBased() || channel.isDMBased())
async execute({ logger }, interaction) {
const durationStr = interaction.options.getString('duration', true)
const id = interaction.options.getChannel('channel')?.id ?? interaction.channelId
const duration = parseDuration(durationStr)
const channel = await interaction.guild!.channels.fetch(id)
if (!channel?.isTextBased())
throw new CommandError( throw new CommandError(
CommandErrorType.InvalidChannel, CommandErrorType.InvalidChannel,
'The supplied channel is not a text channel or does not exist.', 'The supplied channel is not a text channel.',
) )
if (Number.isNaN(duration)) throw new CommandError(CommandErrorType.InvalidDuration, 'Invalid duration.') if (Number.isNaN(duration)) throw new CommandError(CommandErrorType.InvalidDuration, 'Invalid duration.')
@@ -46,10 +38,7 @@ export default {
'Duration out of range, must be between 0s and 6h.', 'Duration out of range, must be between 0s and 6h.',
) )
logger.info(`Setting slowmode to ${duration}ms on ${channel.id}`) await channel.setRateLimitPerUser(duration / 1000, `Set by ${executor.user.tag} (${executor.id})`)
await channel.setRateLimitPerUser(duration / 1000, `Set by ${interaction.user.tag} (${interaction.user.id})`)
await interaction.reply({ await interaction.reply({
embeds: [ embeds: [
createSuccessEmbed( createSuccessEmbed(
@@ -59,7 +48,7 @@ export default {
}) })
logger.info( logger.info(
`${interaction.user.tag} (${interaction.user.id}) set the slowmode on ${channel.name} (${channel.id}) to ${duration}ms`, `${executor.user.tag} (${executor.id}) set the slowmode on ${channel.name} (${channel.id}) to ${duration}ms`,
) )
}, },
} satisfies Command })

View File

@@ -1,33 +1,21 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import type { Command } from '../types'
import { config } from '$/context'
import { createModerationActionEmbed } from '$/utils/discord/embeds' import { createModerationActionEmbed } from '$/utils/discord/embeds'
import { sendModerationReplyAndLogs } from '$/utils/discord/moderation' import { sendModerationReplyAndLogs } from '$/utils/discord/moderation'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'unban',
.setName('unban') description: 'Unban a user',
.setDescription('Unban a user') options: {
.addUserOption(option => option.setName('user').setRequired(true).setDescription('The user to unban')) user: {
.toJSON(), description: 'The user to unban',
required: true,
memberRequirements: { type: ModerationCommand.OptionType.User,
roles: config.moderation?.roles ?? [],
}, },
global: false,
async execute({ logger }, interaction) {
const user = interaction.options.getUser('user', true)
await interaction.guild!.members.unban(
user,
`Unbanned by moderator ${interaction.user.tag} (${interaction.user.id})`,
)
await sendModerationReplyAndLogs(interaction, createModerationActionEmbed('Unbanned', user, interaction.user))
logger.info(`${interaction.user.tag} (${interaction.user.id}) unbanned ${user.tag} (${user.id})`)
}, },
} satisfies Command async execute({ logger, executor }, interaction, { user }) {
await interaction.guild!.members.unban(user, `Unbanned by moderator ${executor.user.tag} (${executor.id})`)
await sendModerationReplyAndLogs(interaction, createModerationActionEmbed('Unbanned', user, executor.user))
logger.info(`${executor.user.tag} (${executor.id}) unbanned ${user.tag} (${user.id})`)
},
})

View File

@@ -1,29 +1,22 @@
import { SlashCommandBuilder } from 'discord.js' import { ModerationCommand } from '$/classes/Command'
import CommandError, { CommandErrorType } from '$/classes/CommandError' import CommandError, { CommandErrorType } from '$/classes/CommandError'
import { config } from '$/context'
import { appliedPresets } from '$/database/schemas' import { appliedPresets } from '$/database/schemas'
import { createModerationActionEmbed } from '$/utils/discord/embeds' import { createModerationActionEmbed } from '$/utils/discord/embeds'
import { sendModerationReplyAndLogs } from '$/utils/discord/moderation' import { sendModerationReplyAndLogs } from '$/utils/discord/moderation'
import { removeRolePreset } from '$/utils/discord/rolePresets' import { removeRolePreset } from '$/utils/discord/rolePresets'
import { and, eq } from 'drizzle-orm' import { and, eq } from 'drizzle-orm'
import type { Command } from '../types'
export default { export default new ModerationCommand({
data: new SlashCommandBuilder() name: 'unmute',
.setName('unmute') description: 'Unmute a member',
.setDescription('Unmute a member') options: {
.addUserOption(option => option.setName('member').setRequired(true).setDescription('The member to unmute')) member: {
.toJSON(), description: 'The member to unmute',
required: true,
memberRequirements: { type: ModerationCommand.OptionType.User,
roles: config.moderation?.roles ?? [],
}, },
},
global: false, async execute({ logger, database, executor }, interaction, { member: user }) {
async execute({ logger, database }, interaction) {
const user = interaction.options.getUser('member', true)
const member = await interaction.guild!.members.fetch(user.id) const member = await interaction.guild!.members.fetch(user.id)
if (!member) if (!member)
throw new CommandError( throw new CommandError(
@@ -39,8 +32,8 @@ export default {
throw new CommandError(CommandErrorType.Generic, 'This user is not muted.') throw new CommandError(CommandErrorType.Generic, 'This user is not muted.')
await removeRolePreset(member, 'mute') await removeRolePreset(member, 'mute')
await sendModerationReplyAndLogs(interaction, createModerationActionEmbed('Unmuted', user, interaction.user)) await sendModerationReplyAndLogs(interaction, createModerationActionEmbed('Unmuted', user, executor.user))
logger.info(`Moderator ${interaction.user.tag} (${interaction.user.id}) unmuted ${user.tag} (${user.id})`) logger.info(`Moderator ${executor.user.tag} (${executor.id}) unmuted ${user.tag} (${user.id})`)
}, },
} satisfies Command })

View File

@@ -1,56 +0,0 @@
import type { SlashCommandBuilder } from '@discordjs/builders'
import type { ChatInputCommandInteraction } from 'discord.js'
// Temporary system
export type Command = {
data: ReturnType<SlashCommandBuilder['toJSON']>
// The function has to return void or Promise<void>
// because TS may complain about some code paths not returning a value
/**
* The function to execute when this command is triggered
* @param interaction The interaction that triggered this command
*/
execute: (
context: typeof import('../context'),
interaction: ChatInputCommandInteraction,
info: Info,
) => Promise<void> | void
memberRequirements?: {
/**
* The mode to use when checking for requirements.
* - `all` means that the user needs meet all requirements specified.
* - `any` means that the user needs to meet any of the requirements specified.
*
* @default "all"
*/
mode?: 'all' | 'any'
/**
* The permissions required to use this command (in BitFields).
*
* - **0n** means that everyone can use this command.
* - **-1n** means that only bot owners can use this command.
* @default -1n
*/
permissions?: bigint
/**
* The roles required to use this command.
* By default, this is set to `[]`.
*/
roles?: string[]
}
/**
* Whether this command can only be used by bot owners.
* @default false
*/
ownerOnly?: boolean
/**
* Whether to register this command as a global slash command.
* This is set to `false` and commands will be registered in allowed guilds only by default.
* @default false
*/
global?: boolean
}
export interface Info {
userIsOwner: boolean
}

View File

@@ -4,12 +4,6 @@ export const MessageScanLabeledResponseReactions = {
delete: '❌', delete: '❌',
} as const } as const
export const MessageScanHumanizedMode = {
ocr: 'image recognition',
nlp: 'text analysis',
match: 'pattern matching',
} as const
export const DefaultEmbedColor = '#4E98F0' export const DefaultEmbedColor = '#4E98F0'
export const ReVancedLogoURL = export const ReVancedLogoURL =
'https://media.discordapp.net/attachments/1095487869923119144/1115436493050224660/revanced-logo.png' 'https://media.discordapp.net/attachments/1095487869923119144/1115436493050224660/revanced-logo.png'

View File

@@ -1,22 +1,24 @@
import { Database } from 'bun:sqlite' import { Database } from 'bun:sqlite'
import { existsSync, readFileSync, readdirSync } from 'fs'
import { join } from 'path'
import { Client as APIClient } from '@revanced/bot-api' import { Client as APIClient } from '@revanced/bot-api'
import { createLogger } from '@revanced/bot-shared' import { createLogger } from '@revanced/bot-shared'
import { ActivityType, Client as DiscordClient, Partials } from 'discord.js' import { Client as DiscordClient, type Message, Partials } from 'discord.js'
import { drizzle } from 'drizzle-orm/bun-sqlite' import { drizzle } from 'drizzle-orm/bun-sqlite'
// Export config first, as commands require them // Export some things first, as commands require them
import config from '../config.js' import config from '../config.js'
export { config } export { config }
import * as commands from './commands'
import * as schemas from './database/schemas'
import type { Command } from './commands/types'
export const logger = createLogger({ export const logger = createLogger({
level: config.logLevel === 'none' ? Number.MAX_SAFE_INTEGER : config.logLevel, level: config.logLevel === 'none' ? Number.MAX_SAFE_INTEGER : config.logLevel,
}) })
import * as commands from './commands'
import * as schemas from './database/schemas'
import type { default as Command, CommandOptionsOptions } from './classes/Command'
export const api = { export const api = {
client: new APIClient({ client: new APIClient({
api: { api: {
@@ -25,11 +27,37 @@ export const api = {
}, },
}, },
}), }),
isStopping: false, intentionallyDisconnecting: false,
disconnectCount: 0, disconnectCount: 0,
} }
const db = new Database(process.env['DATABASE_PATH']) const DatabasePath = process.env['DATABASE_PATH']
const DatabaseSchemaDir = join(import.meta.dir, '..', '.drizzle')
let dbSchemaFileName: string | undefined
if (DatabasePath && !existsSync(DatabasePath)) {
logger.warn('Database file not found, trying to create from schema...')
try {
const file = readdirSync(DatabaseSchemaDir, { withFileTypes: true })
.filter(file => file.isFile() && file.name.endsWith('.sql'))
.sort()
.at(-1)
if (!file) throw new Error('No schema file found')
dbSchemaFileName = file.name
logger.debug(`Using schema file: ${dbSchemaFileName}`)
} catch (e) {
logger.fatal('Could not create database from schema, check if the schema file exists and is accessible')
logger.fatal(e)
process.exit(1)
}
}
const db = new Database(DatabasePath, { readwrite: true, create: true })
if (dbSchemaFileName) db.run(readFileSync(join(DatabaseSchemaDir, dbSchemaFileName)).toString())
export const database = drizzle(db, { export const database = drizzle(db, {
schema: schemas, schema: schemas,
@@ -52,17 +80,24 @@ export const discord = {
repliedUser: true, repliedUser: true,
}, },
partials: [Partials.Message, Partials.Reaction], partials: [Partials.Message, Partials.Reaction],
presence: {
activities: [
{
type: ActivityType.Watching,
name: 'cat videos',
},
],
},
}), }),
commands: Object.fromEntries(Object.values<Command>(commands).map(cmd => [cmd.data.name, cmd])) as Record< commands: Object.fromEntries(Object.values(commands).map(cmd => [cmd.name, cmd])) as Record<
string, string,
Command Command<boolean, CommandOptionsOptions | undefined, boolean>
>,
stickyMessages: {} as Record<
string,
Record<
string,
{
forceSendTimerActive?: boolean
timeoutMs: number
forceSendMs?: number
send: (forced?: boolean) => Promise<void>
currentMessage?: Message<true>
interval?: NodeJS.Timeout
forceSendInterval?: NodeJS.Timeout
}
>
>, >,
} as const } as const

View File

@@ -2,7 +2,7 @@ import { on, withContext } from '$utils/api/events'
import { DisconnectReason, HumanizedDisconnectReason } from '@revanced/bot-shared' import { DisconnectReason, HumanizedDisconnectReason } from '@revanced/bot-shared'
withContext(on, 'disconnect', ({ api, config, logger }, reason, msg) => { withContext(on, 'disconnect', ({ api, config, logger }, reason, msg) => {
if (reason === DisconnectReason.PlannedDisconnect && api.isStopping) return if (reason === DisconnectReason.PlannedDisconnect && api.intentionallyDisconnecting) return
const ws = api.client.ws const ws = api.client.ws
if (!ws.disconnected) ws.disconnect() if (!ws.disconnected) ws.disconnect()
@@ -16,7 +16,7 @@ withContext(on, 'disconnect', ({ api, config, logger }, reason, msg) => {
) )
if (api.disconnectCount >= (config.api.disconnectLimit ?? 3)) { if (api.disconnectCount >= (config.api.disconnectLimit ?? 3)) {
console.error('Disconnected from bot API too many times') logger.fatal('Disconnected from bot API too many times')
// We don't want the process hanging // We don't want the process hanging
process.exit(1) process.exit(1)
} }

View File

@@ -1,3 +1,7 @@
import { on, withContext } from '$utils/api/events' import { on, withContext } from '$utils/api/events'
withContext(on, 'ready', ({ logger }) => void logger.info('Connected to the bot API')) withContext(on, 'ready', ({ api, logger }) => {
// Reset disconnect count, so it doesn't meet the threshold for an accidental disconnect
api.disconnectCount = 0
logger.info('Connected to the bot API')
})

View File

@@ -1,76 +1,22 @@
import CommandError from '$/classes/CommandError' import CommandError from '$/classes/CommandError'
import { createErrorEmbed, createStackTraceEmbed } from '$utils/discord/embeds' import { createStackTraceEmbed } from '$utils/discord/embeds'
import { on, withContext } from '$utils/discord/events' import { on, withContext } from '$utils/discord/events'
withContext(on, 'interactionCreate', async (context, interaction) => { withContext(on, 'interactionCreate', async (context, interaction) => {
if (!interaction.isChatInputCommand()) return if (!interaction.isChatInputCommand()) return
const { logger, discord, config } = context const { logger, discord } = context
const command = discord.commands[interaction.commandName] const command = discord.commands[interaction.commandName]
logger.debug(`Command ${interaction.commandName} being invoked by ${interaction.user.tag}`) logger.debug(`Command ${interaction.commandName} being invoked by ${interaction.user.tag}`)
if (!command) return void logger.error(`Command ${interaction.commandName} not implemented but registered!!!`) if (!command)
return void logger.error(`Interaction command ${interaction.commandName} not implemented but registered!!!`)
const isOwner = config.owners.includes(interaction.user.id)
/**
* Owner check
*/
if (command.ownerOnly && !isOwner)
return void (await interaction.reply({
embeds: [createErrorEmbed('Massive skill issue', 'This command can only be used by the bot owners.')],
ephemeral: true,
}))
/**
* Sanity check
*/
if (!command.global && !interaction.inGuild()) {
logger.error(`Command ${interaction.commandName} cannot be run in DMs, but was registered as global`)
await interaction.reply({
embeds: [createErrorEmbed('Cannot run that here', 'This command can only be used in a server.')],
ephemeral: true,
})
return
}
/**
* Permission checks
*/
if (interaction.inGuild()) {
// Bot owners get bypass
if (command.memberRequirements && !isOwner) {
const { permissions = 0n, roles = [], mode } = command.memberRequirements
const member = await interaction.guild!.members.fetch(interaction.user.id)
const [missingPermissions, missingRoles] = [
// This command is an owner-only command (the user is not an owner, checked above)
permissions <= 0n ||
// or the user doesn't have the required permissions
(permissions > 0n && !interaction.memberPermissions.has(permissions)),
// If not:
!roles.some(x => member.roles.cache.has(x)),
]
if ((mode === 'any' && missingPermissions && missingRoles) || missingPermissions || missingRoles)
return void interaction.reply({
embeds: [
createErrorEmbed(
'Missing roles or permissions',
"You don't have the required roles or permissions to use this command.",
),
],
ephemeral: true,
})
}
}
try { try {
logger.debug(`Command ${interaction.commandName} being executed`) logger.debug(`Command ${interaction.commandName} being executed`)
await command.execute(context, interaction, { userIsOwner: isOwner }) await command.onInteraction(context, interaction)
} catch (err) { } catch (err) {
if (!(err instanceof CommandError))
logger.error(`Error while executing command ${interaction.commandName}:`, err) logger.error(`Error while executing command ${interaction.commandName}:`, err)
await interaction[interaction.replied ? 'followUp' : 'reply']({ await interaction[interaction.replied ? 'followUp' : 'reply']({
embeds: [err instanceof CommandError ? err.toEmbed() : createStackTraceEmbed(err)], embeds: [err instanceof CommandError ? err.toEmbed() : createStackTraceEmbed(err)],

View File

@@ -0,0 +1,64 @@
import { type CommandArguments, CommandSpecialArgumentType } from '$/classes/Command'
import CommandError from '$/classes/CommandError'
import { createStackTraceEmbed } from '$utils/discord/embeds'
import { on, withContext } from '$utils/discord/events'
withContext(on, 'messageCreate', async (context, msg) => {
const { logger, discord, config } = context
if (msg.author.bot) return
const regex = new RegExp(
`^(?:${config.prefix ? `${escapeRegexSpecials(config.prefix)}|` : ''}${msg.client.user.toString()}\\s*)([a-zA-Z-_]+)(?:\\s+)?(.+)?`,
)
const matches = msg.content.match(regex)
if (!matches) return
const [, commandName, argsString] = matches
if (!commandName) return
const command = discord.commands[commandName]
logger.debug(`Command ${commandName} being invoked by ${msg.author.id}`)
if (!command) return void logger.debug(`Message command ${commandName} not implemented`)
const argsRegex: RegExp = /[^\s"]+|"([^"]*)"/g
const args: CommandArguments = []
let match: RegExpExecArray | null
// biome-ignore lint/suspicious/noAssignInExpressions: nuh uh
while ((match = argsRegex.exec(argsString ?? '')) !== null) {
const arg = match[1] ? match[1] : match[0]
const mentionMatch = arg.match(/<(@(?:!|&)?|#)(.+?)>/)
if (mentionMatch) {
const [, prefix, id] = mentionMatch
if (!id || !prefix) {
args.push('')
continue
}
args.push({
type: CommandSpecialArgumentType[prefix[1] === '&' ? 'Role' : prefix[0] === '#' ? 'Channel' : 'User'],
id,
})
} else args.push(arg)
}
try {
logger.debug(`Command ${commandName} being executed`)
await command.onMessage(context, msg, args)
} catch (err) {
if (!(err instanceof CommandError)) logger.error(`Error while executing command ${commandName}:`, err)
await msg.reply({ embeds: [err instanceof CommandError ? err.toEmbed() : createStackTraceEmbed(err)] })
}
})
const escapeRegexSpecials = (str: string): string => {
let escapedStr = ''
for (const char of str) {
if (['.', '+', '*', '?', '$', '(', ')', '[', ']', '{', '}', '|', '\\'].includes(char)) escapedStr += `\\${char}`
else escapedStr += char
}
return escapedStr
}

View File

@@ -1,6 +1,6 @@
import { MessageScanLabeledResponseReactions } from '$/constants' import { MessageScanLabeledResponseReactions } from '$/constants'
import { responses } from '$/database/schemas' import { responses } from '$/database/schemas'
import { getResponseFromText, shouldScanMessage } from '$/utils/discord/messageScan' import { getResponseFromText, messageMatchesFilter } from '$/utils/discord/messageScan'
import { createMessageScanResponseEmbed } from '$utils/discord/embeds' import { createMessageScanResponseEmbed } from '$utils/discord/embeds'
import { on, withContext } from '$utils/discord/events' import { on, withContext } from '$utils/discord/events'
@@ -13,25 +13,34 @@ withContext(on, 'messageCreate', async (context, msg) => {
} = context } = context
if (!config || !config.responses) return if (!config || !config.responses) return
if (msg.author.bot && !config.scanBots) return
if (!msg.inGuild() && !config.scanOutsideGuilds) return
if (msg.inGuild() && msg.member?.partial) await msg.member.fetch()
const filteredResponses = config.responses.filter(x => shouldScanMessage(msg, x.filterOverride ?? config.filter)) const filteredResponses = config.responses.filter(x => messageMatchesFilter(msg, x.filterOverride ?? config.filter))
if (!filteredResponses.length) return if (!filteredResponses.length) return
if (msg.content.length) { if (msg.content.length) {
try { try {
logger.debug(`Classifying message ${msg.id}`) logger.debug(`Classifying message ${msg.id}`)
const { response, label } = await getResponseFromText(msg.content, filteredResponses, context) const { response, label, respondToReply } = await getResponseFromText(
msg.content,
filteredResponses,
context,
)
if (response) { if (response) {
logger.debug('Response found') logger.debug('Response found')
const reply = await msg.reply({ const toReply = respondToReply ? (msg.reference?.messageId ? await msg.fetchReference() : msg) : msg
embeds: [createMessageScanResponseEmbed(response, label ? 'nlp' : 'match')], const reply = await toReply.reply({
...response,
embeds: response.embeds?.map(createMessageScanResponseEmbed),
}) })
if (label) if (label) {
db.insert(responses).values({ await db.insert(responses).values({
replyId: reply.id, replyId: reply.id,
channelId: reply.channel.id, channelId: reply.channel.id,
guildId: reply.guild!.id, guildId: reply.guild!.id,
@@ -40,7 +49,6 @@ withContext(on, 'messageCreate', async (context, msg) => {
content: msg.content, content: msg.content,
}) })
if (label) {
for (const reaction of Object.values(MessageScanLabeledResponseReactions)) { for (const reaction of Object.values(MessageScanLabeledResponseReactions)) {
await reply.react(reaction) await reply.react(reaction)
} }
@@ -51,11 +59,22 @@ withContext(on, 'messageCreate', async (context, msg) => {
} }
} }
if (msg.attachments.size > 0) { if (msg.attachments.size > 0 && config.attachments?.scanAttachments) {
logger.debug(`Classifying message attachments for ${msg.id}`) logger.debug(`Classifying message attachments for ${msg.id}`)
for (const attachment of msg.attachments.values()) { for (const attachment of msg.attachments.values()) {
if (attachment.contentType && !config.allowedAttachmentMimeTypes.includes(attachment.contentType)) continue if (
config.attachments.allowedMimeTypes &&
!config.attachments.allowedMimeTypes.includes(attachment.contentType!)
) {
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${attachment.contentType}`)
continue
}
if (attachment.contentType?.startsWith('text/') && attachment.size > (config.attachments.maxTextFileSize ?? 512 * 1000)) {
logger.debug(`Attachment ${attachment.url} is too large be to scanned, size is ${attachment.size}`)
continue
}
try { try {
const { text: content } = await api.client.parseImage(attachment.url) const { text: content } = await api.client.parseImage(attachment.url)
@@ -64,7 +83,8 @@ withContext(on, 'messageCreate', async (context, msg) => {
if (response) { if (response) {
logger.debug(`Response found for attachment: ${attachment.url}`) logger.debug(`Response found for attachment: ${attachment.url}`)
await msg.reply({ await msg.reply({
embeds: [createMessageScanResponseEmbed(response, 'ocr')], ...response,
embeds: response.embeds?.map(createMessageScanResponseEmbed),
}) })
break break

View File

@@ -0,0 +1,30 @@
import { on, withContext } from '$utils/discord/events'
withContext(on, 'messageCreate', async ({ discord, logger }, msg) => {
if (!msg.inGuild()) return
if (msg.author.id === msg.client.user.id) return
const store = discord.stickyMessages[msg.guildId]?.[msg.channelId]
if (!store) return
if (!store.interval) store.interval = setTimeout(store.send, store.timeoutMs) as NodeJS.Timeout
else {
store.interval.refresh()
if (!store.forceSendTimerActive && store.forceSendMs) {
logger.debug(`Channel ${msg.channelId} in guild ${msg.guildId} is active, starting force send timer`)
store.forceSendTimerActive = true
if (!store.forceSendInterval)
store.forceSendInterval = setTimeout(
() =>
store.send(true).then(() => {
store.forceSendTimerActive = false
}),
store.forceSendMs,
) as NodeJS.Timeout
else store.forceSendInterval.refresh()
}
}
})

View File

@@ -13,12 +13,14 @@ import {
import type { ConfigMessageScanResponseLabelConfig } from '$/../config.schema' import type { ConfigMessageScanResponseLabelConfig } from '$/../config.schema'
import { responses } from '$/database/schemas' import { responses } from '$/database/schemas'
import { handleUserResponseCorrection } from '$/utils/discord/messageScan' import { handleUserResponseCorrection } from '$/utils/discord/messageScan'
import { isAdmin } from '$/utils/discord/permissions'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
const PossibleReactions = Object.values(Reactions) as string[] const PossibleReactions = Object.values(Reactions) as string[]
withContext(on, 'messageReactionAdd', async (context, rct, user) => { withContext(on, 'messageReactionAdd', async (context, rct, user) => {
if (user.bot) return if (user.bot) return
await rct.users.remove(user.id)
const { database: db, logger, config } = context const { database: db, logger, config } = context
const { messageScan: msConfig } = config const { messageScan: msConfig } = config
@@ -32,12 +34,9 @@ withContext(on, 'messageReactionAdd', async (context, rct, user) => {
if (reactionMessage.author.id !== reaction.client.user!.id) return if (reactionMessage.author.id !== reaction.client.user!.id) return
if (!PossibleReactions.includes(reaction.emoji.name!)) return if (!PossibleReactions.includes(reaction.emoji.name!)) return
if (!config.owners.includes(user.id)) { if (!isAdmin(reactionMessage.member || reactionMessage.author)) {
// User is in guild, and config has member requirements // User is in guild, and config has member requirements
if ( if (reactionMessage.inGuild() && msConfig.humanCorrections.allow) {
reactionMessage.inGuild() &&
(msConfig.humanCorrections.allow?.members || msConfig.humanCorrections.allow?.users)
) {
const { const {
allow: { users: allowedUsers, members: allowedMembers }, allow: { users: allowedUsers, members: allowedMembers },
} = msConfig.humanCorrections } = msConfig.humanCorrections
@@ -46,22 +45,26 @@ withContext(on, 'messageReactionAdd', async (context, rct, user) => {
const member = await reactionMessage.guild.members.fetch(user.id) const member = await reactionMessage.guild.members.fetch(user.id)
const { permissions, roles } = allowedMembers const { permissions, roles } = allowedMembers
if (!(member.permissions.has(permissions ?? 0n) || roles?.some(role => member.roles.cache.has(role)))) if (
!(
(permissions ? member.permissions.has(permissions) : false) ||
roles?.some(role => member.roles.cache.has(role))
)
)
return return
} else if (allowedUsers) { } else if (!allowedUsers?.includes(user.id)) return
if (!allowedUsers.includes(user.id)) return } else
} else {
return void logger.warn( return void logger.warn(
'No member or user requirements set for human corrections, all requests will be ignored', 'No member or user requirements set for human corrections, all requests will be ignored',
) )
} }
}
}
// Sanity check // Sanity check
const response = await db.query.responses.findFirst({ where: eq(responses.replyId, rct.message.id) }) const response = await db.query.responses.findFirst({ where: eq(responses.replyId, rct.message.id) })
if (!response || response.correctedById) return if (!response || response.correctedById) return
logger.debug(`User ${user.id} is trying to correct the response ${rct.message.id}`)
const handleCorrection = (label: string) => const handleCorrection = (label: string) =>
handleUserResponseCorrection(context, response, reactionMessage, label, user) handleUserResponseCorrection(context, response, reactionMessage, label, user)

View File

@@ -1,15 +1,67 @@
import { database, logger } from '$/context' import { database, logger } from '$/context'
import { appliedPresets } from '$/database/schemas' import { appliedPresets } from '$/database/schemas'
import { applyCommonEmbedStyles } from '$/utils/discord/embeds'
import { on, withContext } from '$/utils/discord/events'
import { removeRolePreset } from '$/utils/discord/rolePresets' import { removeRolePreset } from '$/utils/discord/rolePresets'
import type { Client } from 'discord.js'
import { lt } from 'drizzle-orm' import { lt } from 'drizzle-orm'
import { on, withContext } from 'src/utils/discord/events'
export default withContext(on, 'ready', ({ config, logger }, client) => { import type { Client } from 'discord.js'
export default withContext(on, 'ready', async ({ config, discord, logger }, client) => {
logger.info(`Connected to Discord API, logged in as ${client.user.displayName} (@${client.user.tag})!`) logger.info(`Connected to Discord API, logged in as ${client.user.displayName} (@${client.user.tag})!`)
logger.info( logger.info(`Bot is in ${client.guilds.cache.size} guilds`)
`Bot is in ${client.guilds.cache.size} guilds, if this is not expected, please run the /leave-unknowns command`,
if (config.stickyMessages)
for (const [guildId, channels] of Object.entries(config.stickyMessages)) {
const guild = await client.guilds.fetch(guildId)
discord.stickyMessages[guildId] = {}
for (const [channelId, { message, timeout, forceSendTimeout }] of Object.entries(channels)) {
const channel = await guild.channels.fetch(channelId)
if (!channel?.isTextBased()) return
const send = async (forced = false) => {
try {
const msg = await channel.send({
...message,
embeds: message.embeds?.map(it => applyCommonEmbedStyles(it, true, true, true)),
})
const store = discord.stickyMessages[guildId]![channelId]
if (!store) return
await store.currentMessage?.delete().catch()
store.currentMessage = msg
if (!forced) {
clearTimeout(store.forceSendInterval)
logger.debug(
`Timeout ended for sticky message in channel ${channelId} in guild ${guildId}, channel is inactive`,
) )
} else {
clearTimeout(store.interval)
logger.debug(
`Forced send timeout for sticky message in channel ${channelId} in guild ${guildId} ended, channel is too active`,
)
}
logger.debug(`Sent sticky message to channel ${channelId} in guild ${guildId}`)
} catch (e) {
logger.error(
`Error while sending sticky message to channel ${channelId} in guild ${guildId}:`,
e,
)
}
}
discord.stickyMessages[guildId]![channelId] = {
forceSendMs: forceSendTimeout,
timeoutMs: timeout,
send,
forceSendTimerActive: false,
}
}
}
if (config.rolePresets) { if (config.rolePresets) {
removeExpiredPresets(client) removeExpiredPresets(client)

View File

@@ -10,5 +10,10 @@ if (missingEnvs.length) {
process.exit(1) process.exit(1)
} }
// Handle uncaught exceptions
process.on('uncaughtException', error => console.error('Uncaught exception:', error))
process.on('unhandledRejection', reason => console.error('Unhandled rejection:', reason))
api.client.connect() api.client.connect()
discord.client.login() discord.client.login()

View File

@@ -1,19 +0,0 @@
import type { Command } from '$commands/types'
import { listAllFilesRecursive } from '$utils/fs'
export const loadCommands = async (dir: string) => {
const commandsMap: Record<string, Command> = {}
const files = listAllFilesRecursive(dir)
const commands = await Promise.all(
files.map(async file => {
const command = await import(file)
return command.default
}),
)
for (const command of commands) {
if (command) commandsMap[command.data.name] = command
}
return commandsMap
}

View File

@@ -1,5 +1,5 @@
import { DefaultEmbedColor, MessageScanHumanizedMode, ReVancedLogoURL } from '$/constants' import { DefaultEmbedColor, ReVancedLogoURL } from '$/constants'
import { EmbedBuilder, type EmbedField, type User } from 'discord.js' import { type APIEmbed, EmbedBuilder, type EmbedField, type JSONEncodable, type User } from 'discord.js'
import type { ConfigMessageScanResponseMessage } from '../../../config.schema' import type { ConfigMessageScanResponseMessage } from '../../../config.schema'
export const createErrorEmbed = (title: string | null, description?: string) => export const createErrorEmbed = (title: string | null, description?: string) =>
@@ -24,21 +24,8 @@ export const createSuccessEmbed = (title: string | null, description?: string) =
) )
export const createMessageScanResponseEmbed = ( export const createMessageScanResponseEmbed = (
response: ConfigMessageScanResponseMessage, response: NonNullable<ConfigMessageScanResponseMessage['embeds']>[number],
mode: 'ocr' | 'nlp' | 'match', ) => applyCommonEmbedStyles(response, true, true, true)
) => {
const embed = new EmbedBuilder().setTitle(response.title ?? null)
if (response.description) embed.setDescription(response.description)
if (response.fields) embed.addFields(response.fields)
embed.setFooter({
text: `ReVanced • Done via ${MessageScanHumanizedMode[mode]}`,
iconURL: ReVancedLogoURL,
})
return applyCommonEmbedStyles(embed, true, true, true)
}
export const createModerationActionEmbed = ( export const createModerationActionEmbed = (
action: string, action: string,
@@ -74,19 +61,23 @@ export const applyReferenceToModerationActionEmbed = (embed: EmbedBuilder, refer
} }
export const applyCommonEmbedStyles = ( export const applyCommonEmbedStyles = (
embed: EmbedBuilder, embed: EmbedBuilder | JSONEncodable<APIEmbed> | APIEmbed,
setThumbnail = false, setThumbnail = false,
setFooter = false, setFooter = false,
setColor = false, setColor = false,
) => { ) => {
// biome-ignore lint/style/noParameterAssign: While this is confusing, it is fine for this purpose
if ('toJSON' in embed) embed = embed.toJSON()
const builder = new EmbedBuilder(embed)
if (setFooter) if (setFooter)
embed.setFooter({ builder.setFooter({
text: 'ReVanced', text: 'ReVanced',
iconURL: ReVancedLogoURL, iconURL: ReVancedLogoURL,
}) })
if (setColor) embed.setColor(DefaultEmbedColor) if (setColor) builder.setColor(DefaultEmbedColor)
if (setThumbnail) embed.setThumbnail(ReVancedLogoURL) if (setThumbnail) builder.setThumbnail(ReVancedLogoURL)
return embed return builder
} }

View File

@@ -1,10 +1,5 @@
import { type Response, responses } from '$/database/schemas' import { type Response, responses } from '$/database/schemas'
import type { import type { Config, ConfigMessageScanResponse, ConfigMessageScanResponseLabelConfig } from 'config.schema'
Config,
ConfigMessageScanResponse,
ConfigMessageScanResponseLabelConfig,
ConfigMessageScanResponseMessage,
} from 'config.schema'
import type { Message, PartialUser, User } from 'discord.js' import type { Message, PartialUser, User } from 'discord.js'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { createMessageScanResponseEmbed } from './embeds' import { createMessageScanResponseEmbed } from './embeds'
@@ -15,9 +10,15 @@ export const getResponseFromText = async (
// Just to be safe that we will never use data from the context parameter // Just to be safe that we will never use data from the context parameter
{ api, logger }: Omit<typeof import('src/context'), 'config'>, { api, logger }: Omit<typeof import('src/context'), 'config'>,
ocrMode = false, ocrMode = false,
) => { ): Promise<
let label: string | undefined Omit<ConfigMessageScanResponse, 'triggers'> & { label?: string; triggers?: ConfigMessageScanResponse['triggers'] }
let response: ConfigMessageScanResponseMessage | undefined | null > => {
type ResponseConfig = Awaited<ReturnType<typeof getResponseFromText>>
let responseConfig: Omit<ResponseConfig, 'triggers'> & { triggers?: ResponseConfig['triggers'] } = {
triggers: undefined,
response: null,
}
const firstLabelIndexes: number[] = [] const firstLabelIndexes: number[] = []
// Test if all regexes before a label trigger is matched // Test if all regexes before a label trigger is matched
@@ -25,29 +26,27 @@ export const getResponseFromText = async (
const trigger = responses[i]! const trigger = responses[i]!
// Filter override check is not neccessary here, we are already passing responses that match the filter // Filter override check is not neccessary here, we are already passing responses that match the filter
// from the messageCreate handler // from the messageCreate handler, see line 17 of messageCreate handler
const { const {
triggers: { text: textTriggers, image: imageTriggers }, triggers: { text: textTriggers, image: imageTriggers },
response: resp,
} = trigger } = trigger
if (response) break
if (ocrMode) { if (ocrMode) {
if (imageTriggers) if (imageTriggers)
for (const regex of imageTriggers) for (const regex of imageTriggers)
if (regex.test(content)) { if (regex.test(content)) {
logger.debug(`Message matched regex (OCR mode): ${regex.source}`) logger.debug(`Message matched regex (OCR mode): ${regex.source}`)
response = resp responseConfig = trigger
break break
} }
} else } else
for (let j = 0; j < textTriggers!.length; j++) { for (let j = 0; j < textTriggers!.length; j++) {
const trigger = textTriggers![j]! const regex = textTriggers![j]!
if (trigger instanceof RegExp) { if (regex instanceof RegExp) {
if (trigger.test(content)) { if (regex.test(content)) {
logger.debug(`Message matched regex (before mode): ${trigger.source}`) logger.debug(`Message matched regex (before mode): ${regex.source}`)
response = resp responseConfig = trigger
break break
} }
} else { } else {
@@ -58,52 +57,51 @@ export const getResponseFromText = async (
} }
// If none of the regexes match, we can search for labels immediately // If none of the regexes match, we can search for labels immediately
if (!response && !ocrMode) { if (!responseConfig.triggers && !ocrMode) {
logger.debug('No match from before regexes, doing NLP') logger.debug('No match from before regexes, doing NLP')
const scan = await api.client.parseText(content) const scan = await api.client.parseText(content)
if (scan.labels.length) { if (scan.labels.length) {
const matchedLabel = scan.labels[0]! const matchedLabel = scan.labels[0]!
logger.debug(`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`) logger.debug(`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`)
let triggerConfig: ConfigMessageScanResponseLabelConfig | undefined let trigger: ConfigMessageScanResponseLabelConfig | undefined
const labelConfig = responses.find(x => { const response = responses.find(x => {
const config = x.triggers.text!.find( const config = x.triggers.text!.find(
(x): x is ConfigMessageScanResponseLabelConfig => 'label' in x && x.label === matchedLabel.name, (x): x is ConfigMessageScanResponseLabelConfig => 'label' in x && x.label === matchedLabel.name,
) )
if (config) triggerConfig = config if (config) trigger = config
return config return config
}) })
if (!labelConfig) {
logger.warn(`No label config found for label ${matchedLabel.name}`)
return { response: null, label: undefined }
}
if (matchedLabel.confidence >= triggerConfig!.threshold) {
logger.debug('Label confidence is enough')
label = matchedLabel.name
response = labelConfig.response
}
}
}
// If we still don't have a label, we can match all regexes after the initial label trigger
if (!response) { if (!response) {
logger.warn(`No response config found for label ${matchedLabel.name}`)
// This returns the default value set in line 17, which means no response matched
return responseConfig
}
if (matchedLabel.confidence >= trigger!.threshold) {
logger.debug('Label confidence is enough')
responseConfig = { ...responseConfig, ...response, label: trigger!.label }
}
}
}
// If we still don't have a response config, we can match all regexes after the initial label trigger
if (!responseConfig.triggers && ocrMode) {
logger.debug('No match from NLP, doing after regexes') logger.debug('No match from NLP, doing after regexes')
for (let i = 0; i < responses.length; i++) { for (let i = 0; i < responses.length; i++) {
const { const {
triggers: { text: textTriggers }, triggers: { text: textTriggers },
response: resp,
} = responses[i]! } = responses[i]!
const firstLabelIndex = firstLabelIndexes[i] ?? -1 const firstLabelIndex = firstLabelIndexes[i] ?? -1
for (let i = firstLabelIndex + 1; i < textTriggers!.length; i++) { for (let j = firstLabelIndex + 1; j < textTriggers!.length; j++) {
const trigger = textTriggers![i]! const trigger = textTriggers![j]!
if (trigger instanceof RegExp) { if (trigger instanceof RegExp) {
if (trigger.test(content)) { if (trigger.test(content)) {
logger.debug(`Message matched regex (after mode): ${trigger.source}`) logger.debug(`Message matched regex (after mode): ${trigger.source}`)
response = resp responseConfig = responses[i]!
break break
} }
} }
@@ -111,30 +109,32 @@ export const getResponseFromText = async (
} }
} }
return { return responseConfig
response,
label,
}
} }
export const shouldScanMessage = ( export const messageMatchesFilter = (message: Message, filter: NonNullable<Config['messageScan']>['filter']) => {
message: Message,
filter: NonNullable<Config['messageScan']>['filter'],
): message is Message<true> => {
if (message.author.bot) return false
if (!message.guild) return false
if (!filter) return true if (!filter) return true
const filters = [ const memberRoles = new Set(message.member?.roles.cache.keys())
filter.users?.includes(message.author.id), const { blacklist, whitelist } = filter
message.member?.roles.cache.some(x => filter.roles?.includes(x.id)),
filter.channels?.includes(message.channel.id),
]
if (filter.whitelist && filters.every(x => !x)) return false // If matches only blacklist, will return false
if (!filter.whitelist && filters.some(x => x)) return false // If matches whitelist but also matches blacklist, will return false
// If matches only whitelist, will return true
return true // If matches neither, will return true
return (
(whitelist
? whitelist.channels?.includes(message.channelId) ||
whitelist.roles?.some(role => memberRoles.has(role)) ||
whitelist.users?.includes(message.author.id)
: true) &&
!(
blacklist &&
(blacklist.channels?.includes(message.channelId) ||
blacklist.roles?.some(role => memberRoles.has(role)) ||
blacklist.users?.includes(message.author.id))
)
)
} }
export const handleUserResponseCorrection = async ( export const handleUserResponseCorrection = async (
@@ -158,8 +158,10 @@ export const handleUserResponseCorrection = async (
correctedById: user.id, correctedById: user.id,
}) })
.where(eq(responses.replyId, response.replyId)) .where(eq(responses.replyId, response.replyId))
await reply.edit({ await reply.edit({
embeds: [createMessageScanResponseEmbed(correctLabelResponse.response, 'nlp')], ...correctLabelResponse.response,
embeds: correctLabelResponse.response.embeds?.map(createMessageScanResponseEmbed),
}) })
} }

View File

@@ -1,6 +1,6 @@
import { config, logger } from '$/context' import { config, logger } from '$/context'
import decancer from 'decancer' import decancer from 'decancer'
import type { ChatInputCommandInteraction, EmbedBuilder, Guild, GuildMember, User } from 'discord.js' import type { ChatInputCommandInteraction, EmbedBuilder, Guild, GuildMember, Message, User } from 'discord.js'
import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from './embeds' import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from './embeds'
const PresetLogAction = { const PresetLogAction = {
@@ -10,19 +10,23 @@ const PresetLogAction = {
export const sendPresetReplyAndLogs = ( export const sendPresetReplyAndLogs = (
action: keyof typeof PresetLogAction, action: keyof typeof PresetLogAction,
interaction: ChatInputCommandInteraction, interaction: ChatInputCommandInteraction | Message,
executor: GuildMember,
user: User, user: User,
preset: string, preset: string,
expires?: number | null, expires?: number | null,
) => ) =>
sendModerationReplyAndLogs( sendModerationReplyAndLogs(
interaction, interaction,
createModerationActionEmbed(PresetLogAction[action], user, interaction.user, undefined, expires, [ createModerationActionEmbed(PresetLogAction[action], user, executor.user, undefined, expires, [
[{ name: 'Preset', value: preset, inline: true }], [{ name: 'Preset', value: preset, inline: true }],
]), ]),
) )
export const sendModerationReplyAndLogs = async (interaction: ChatInputCommandInteraction, embed: EmbedBuilder) => { export const sendModerationReplyAndLogs = async (
interaction: ChatInputCommandInteraction | Message,
embed: EmbedBuilder,
) => {
const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch()) const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch())
const logChannel = await getLogChannel(interaction.guild!) const logChannel = await getLogChannel(interaction.guild!)
await logChannel?.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] }) await logChannel?.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] })
@@ -34,7 +38,7 @@ export const getLogChannel = async (guild: Guild) => {
try { try {
const channel = await guild.channels.fetch(logConfig.thread ?? logConfig.channel) const channel = await guild.channels.fetch(logConfig.thread ?? logConfig.channel)
if (!channel || !channel.isTextBased()) if (!channel?.isTextBased())
return void logger.warn('The moderation log channel does not exist, skipping logging') return void logger.warn('The moderation log channel does not exist, skipping logging')
return channel return channel
@@ -46,13 +50,13 @@ export const getLogChannel = async (guild: Guild) => {
} }
export const cureNickname = async (member: GuildMember) => { export const cureNickname = async (member: GuildMember) => {
if (!member.manageable) throw new Error('Member is not manageable') if (!member.manageable) return
const name = member.displayName const name = member.displayName
let cured = decancer(name) let cured = decancer(name)
.toString() .toString()
.replace(/[^a-zA-Z0-9]/g, '') .replace(new RegExp(config.moderation?.cure?.removeCharactersRegex ?? '[^a-zA-Z0-9 \\-_]', 'g'), '')
if (cured.length < 3 || !/^[a-zA-Z]/.test(cured)) if (cured.length < (config?.moderation?.cure?.minimumNameLength ?? 3))
cured = cured =
member.user.username.length >= 3 member.user.username.length >= 3
? member.user.username ? member.user.username

View File

@@ -0,0 +1,14 @@
import { GuildMember, type User } from 'discord.js'
import { config } from '../../context'
export const isAdmin = (userOrMember: User | GuildMember) => {
return (
config.admin?.users?.includes(userOrMember.id) ||
(userOrMember instanceof GuildMember && isMemberAdmin(userOrMember))
)
}
export const isMemberAdmin = (member: GuildMember) => {
const roles = new Set(member.roles.cache.keys())
return Boolean(config?.admin?.roles?.[member.guild.id]?.some(role => roles.has(role)))
}

View File

@@ -6,9 +6,9 @@ import { and, eq } from 'drizzle-orm'
// TODO: Fix this type // TODO: Fix this type
type PresetKey = string type PresetKey = string
export const applyRolePreset = async (member: GuildMember, presetName: PresetKey, untilMs: number | null) => { export const applyRolePreset = async (member: GuildMember, presetName: PresetKey, untilMs: number) => {
const afterInsert = await applyRolesUsingPreset(presetName, member, true) const afterInsert = await applyRolesUsingPreset(presetName, member, true)
const until = untilMs ? Math.ceil(untilMs / 1000) : null const until = untilMs === Infinity ? null : Math.ceil(untilMs / 1000)
await database await database
.insert(appliedPresets) .insert(appliedPresets)

View File

@@ -7,17 +7,27 @@ export const listAllFilesRecursive = (dir: string): string[] =>
.filter(x => x.isFile()) .filter(x => x.isFile())
.map(x => join(x.parentPath, x.name).replaceAll(pathSep, posixPathSep)) .map(x => join(x.parentPath, x.name).replaceAll(pathSep, posixPathSep))
export const generateCommandsIndex = (dirPath: string) => generateIndexes(dirPath, x => !x.endsWith('types.ts')) export const generateCommandsIndex = (dirPath: string) =>
generateIndexes(dirPath, (x, i) => `export { default as C${i} } from './${x}'`)
export const generateEventsIndex = (dirPath: string) => generateIndexes(dirPath) export const generateEventsIndex = (dirPath: string) => generateIndexes(dirPath)
const generateIndexes = async (dirPath: string, pathFilter?: (path: string) => boolean) => { const generateIndexes = async (
dirPath: string,
customMap?: (path: string, index: number) => string,
pathFilter?: (path: string) => boolean,
) => {
const files = listAllFilesRecursive(dirPath) const files = listAllFilesRecursive(dirPath)
.filter(x => (x.endsWith('.ts') && !x.endsWith('index.ts') && pathFilter ? pathFilter(x) : true)) .filter(x => x.endsWith('.ts') && !x.endsWith('index.ts') && (pathFilter ? pathFilter(x) : true))
.map(x => relative(dirPath, x).replaceAll(pathSep, posixPathSep)) .map(x => relative(dirPath, x).replaceAll(pathSep, posixPathSep))
writeFileSync( writeFileSync(
join(dirPath, 'index.ts'), join(dirPath, 'index.ts'),
`// AUTO-GENERATED BY A SCRIPT, DON'T TOUCH\n\n${files.map(c => `import './${c.split('.').at(-2)}'`).join('\n')}`, `// AUTO-GENERATED BY A SCRIPT, DON'T TOUCH\n\n${files
.map((c, i) => {
const path = c.split('.').at(-2)!
return customMap ? customMap(path, i) : `import './${path}'`
})
.join('\n')}`,
) )
} }

View File

@@ -9,6 +9,7 @@
"composite": false, "composite": false,
"exactOptionalPropertyTypes": false, "exactOptionalPropertyTypes": false,
"esModuleInterop": true, "esModuleInterop": true,
"allowJs": true,
"paths": { "paths": {
"$/*": ["./src/*"], "$/*": ["./src/*"],
"$constants": ["./src/constants"], "$constants": ["./src/constants"],

BIN
bun.lockb

Binary file not shown.

View File

@@ -41,11 +41,11 @@
"@tsconfig/strictest": "^2.0.5", "@tsconfig/strictest": "^2.0.5",
"@types/bun": "^1.1.6", "@types/bun": "^1.1.6",
"conventional-changelog-conventionalcommits": "^7.0.2", "conventional-changelog-conventionalcommits": "^7.0.2",
"lefthook": "^1.7.4", "lefthook": "^1.7.5",
"portainer-service-webhook": "https://github.com/newarifrh/portainer-service-webhook#v1", "portainer-service-webhook": "https://github.com/newarifrh/portainer-service-webhook#v1",
"semantic-release": "^24.0.0", "semantic-release": "^24.0.0",
"turbo": "^2.0.9", "turbo": "^2.0.9",
"typescript": "^5.5.3" "typescript": "^5.5.4"
}, },
"trustedDependencies": [ "trustedDependencies": [
"@biomejs/biome", "@biomejs/biome",
@@ -54,6 +54,8 @@
"lefthook" "lefthook"
], ],
"patchedDependencies": { "patchedDependencies": {
"@semantic-release/npm@12.0.1": "patches/@semantic-release%2Fnpm@12.0.1.patch" "@semantic-release/npm@12.0.1": "patches/@semantic-release%2Fnpm@12.0.1.patch",
"drizzle-kit@0.22.8": "patches/drizzle-kit@0.22.8.patch",
"decancer@3.2.3": "patches/decancer@3.2.3.patch"
} }
} }

View File

@@ -52,17 +52,20 @@ export default class Client {
// But if we add anything similar, this will cause another race condition // But if we add anything similar, this will cause another race condition
// To fix this, we can try adding a instanced function that would return the currentSequence // To fix this, we can try adding a instanced function that would return the currentSequence
// and it would be updated every time a "heartbeat ack" packet is received // and it would be updated every time a "heartbeat ack" packet is received
return Promise.race([ const packet = await Promise.race([
this.#awaiter.await(ServerOperation.ParsedText, this.ws.currentSequence), this.#awaiter.await(ServerOperation.ParsedText, this.ws.currentSequence),
this.#awaiter.await(ServerOperation.ParseTextFailed, this.ws.timeout + 5000), this.#awaiter.await(ServerOperation.ParseTextFailed, this.ws.currentSequence, this.ws.timeout + 5000),
]) ])
.then(pkt => { .then(pkt => {
if (pkt.op === ServerOperation.ParsedText) return pkt.d if (pkt.op === ServerOperation.ParsedText) return pkt.d
throw new Error('Failed to parse text, the API encountered an error') return null
}) })
.catch(() => { .catch(() => {
throw new Error('Failed to parse text, the API did not respond in time') throw new Error('Failed to parse text, the API did not respond in time')
}) })
if (!packet) throw new Error('Failed to parse text, the API encountered an error')
return packet
} }
/** /**
@@ -82,17 +85,20 @@ export default class Client {
// See line 50 // See line 50
return Promise.race([ const packet = await Promise.race([
this.#awaiter.await(ServerOperation.ParsedImage, this.ws.currentSequence), this.#awaiter.await(ServerOperation.ParsedImage, this.ws.currentSequence),
this.#awaiter.await(ServerOperation.ParseImageFailed, this.ws.timeout + 5000), this.#awaiter.await(ServerOperation.ParseImageFailed, this.ws.currentSequence, this.ws.timeout + 5000),
]) ])
.then(pkt => { .then(pkt => {
if (pkt.op === ServerOperation.ParsedImage) return pkt.d if (pkt.op === ServerOperation.ParsedImage) return pkt.d
throw new Error('Failed to parse image, the API encountered an error') return null
}) })
.catch(() => { .catch(() => {
throw new Error('Failed to parse image, the API did not respond in time') throw new Error('Failed to parse image, the API did not respond in time')
}) })
if (!packet) throw new Error('Failed to parse image, the API encountered an error')
return packet
} }
async trainMessage(text: string, label: string) { async trainMessage(text: string, label: string) {
@@ -107,17 +113,20 @@ export default class Client {
}) })
// See line 50 // See line 50
return Promise.race([ const packet = await Promise.race([
this.#awaiter.await(ServerOperation.TrainedMessage, this.ws.currentSequence), this.#awaiter.await(ServerOperation.TrainedMessage, this.ws.currentSequence),
this.#awaiter.await(ServerOperation.TrainMessageFailed, this.ws.timeout + 5000), this.#awaiter.await(ServerOperation.TrainMessageFailed, this.ws.currentSequence, this.ws.timeout + 5000),
]) ])
.then(pkt => { .then(pkt => {
if (pkt.op === ServerOperation.TrainedMessage) return pkt.d if (pkt.op === ServerOperation.TrainedMessage) return pkt.d
throw new Error('Failed to train message, the API encountered an error') return null
}) })
.catch(() => { .catch(() => {
throw new Error('Failed to train message, the API did not respond in time') throw new Error('Failed to train message, the API did not respond in time')
}) })
if (!packet) throw new Error('Failed to train message, the API encountered an error')
return packet
} }
/** /**
@@ -163,13 +172,17 @@ export default class Client {
/** /**
* Disconnects the client from the API * Disconnects the client from the API
*/ */
disconnect() { disconnect(force?: boolean) {
this.ws.disconnect() this.ws.disconnect(force)
} }
#throwIfNotReady() { #throwIfNotReady() {
if (!this.isReady()) throw new Error('Client is not ready') if (!this.isReady()) throw new Error('Client is not ready')
} }
get disconnected() {
return this.ws.disconnected
}
} }
export class ClientWebSocketPacketAwaiter { export class ClientWebSocketPacketAwaiter {

View File

@@ -49,31 +49,39 @@ export class ClientWebSocketManager {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
if (!this.ready) { if (!this.ready) {
this.#socket?.close(DisconnectReason.TooSlow) this.#socket?.close(DisconnectReason.TooSlow)
throw new Error('WebSocket connection was not readied in time') this._handleDisconnect(DisconnectReason.TooSlow, 'WebSocket connection was not readied in time')
} }
}, this.timeout) }, this.timeout)
this.#socket.on('open', () => { const closeBeforeReadyHandler = (code: number, reason: Buffer) => {
this._handleDisconnect(code, reason.toString())
cleanup()
}
const readyHandler = () => {
this.disconnected = false this.disconnected = false
clearTimeout(timeout) cleanup()
this.#listen() this.#listen()
rs() rs()
}) }
this.#socket.on('error', err => { const socket = this.#socket
const cleanup = () => {
socket.off('open', readyHandler)
socket.off('close', closeBeforeReadyHandler)
clearTimeout(timeout) clearTimeout(timeout)
throw err }
})
this.#socket.on('close', (code, reason) => { this.#socket.on('open', readyHandler)
clearTimeout(timeout) this.#socket.on('close', closeBeforeReadyHandler)
this._handleDisconnect(code, reason.toString())
throw new Error('WebSocket connection closed before ready')
})
} catch (e) { } catch (e) {
rj(e) rj(e)
} }
}).finally(() => { })
.then(() => {
this.#socket.on('close', (code, reason) => this._handleDisconnect(code, reason.toString()))
})
.finally(() => {
this.connecting = false this.connecting = false
}) })
} }
@@ -119,15 +127,15 @@ export class ClientWebSocketManager {
this.currentSequence++ this.currentSequence++
this.#socket.send(serializePacket(packet), err => { this.#socket.send(serializePacket(packet), err => {
throw err if (err) throw err
}) })
} }
/** /**
* Disconnects from the WebSocket API * Disconnects from the WebSocket API
*/ */
disconnect() { disconnect(force?: boolean) {
this.#throwIfDisconnected('Cannot disconnect when already disconnected from the server') if (!force) this.#throwIfDisconnected('Cannot disconnect when already disconnected from the server')
this._handleDisconnect(DisconnectReason.PlannedDisconnect) this._handleDisconnect(DisconnectReason.PlannedDisconnect)
} }

View File

@@ -1,9 +1,9 @@
import { import {
url,
type AnySchema, type AnySchema,
type NullSchema, type NullSchema,
type ObjectSchema, type ObjectSchema,
type Output, type Output,
type BooleanSchema,
array, array,
enum_, enum_,
null_, null_,
@@ -11,6 +11,8 @@ import {
parse, parse,
special, special,
string, string,
boolean,
url,
// merge // merge
} from 'valibot' } from 'valibot'
import DisconnectReason from '../constants/DisconnectReason' import DisconnectReason from '../constants/DisconnectReason'
@@ -26,8 +28,7 @@ export const PacketSchema = special<Packet>(input => {
'op' in input && 'op' in input &&
typeof input.op === 'number' && typeof input.op === 'number' &&
input.op in Operation && input.op in Operation &&
'd' in input && 'd' in input
typeof input.d === 'object'
) { ) {
if (input.op in ServerOperation && !('s' in input && typeof input.s === 'number')) return false if (input.op in ServerOperation && !('s' in input && typeof input.s === 'number')) return false
@@ -62,7 +63,7 @@ export const PacketDataSchemas = {
[ServerOperation.Disconnect]: object({ [ServerOperation.Disconnect]: object({
reason: enum_(DisconnectReason), reason: enum_(DisconnectReason),
}), }),
[ServerOperation.TrainedMessage]: null_(), [ServerOperation.TrainedMessage]: boolean(),
[ServerOperation.TrainMessageFailed]: null_(), [ServerOperation.TrainMessageFailed]: null_(),
[ClientOperation.ParseText]: object({ [ClientOperation.ParseText]: object({
@@ -78,7 +79,7 @@ export const PacketDataSchemas = {
} as const satisfies Record< } as const satisfies Record<
Operation, Operation,
// biome-ignore lint/suspicious/noExplicitAny: This is a schema, it's not possible to type it // biome-ignore lint/suspicious/noExplicitAny: This is a schema, it's not possible to type it
ObjectSchema<any> | AnySchema | NullSchema ObjectSchema<any> | AnySchema | NullSchema | BooleanSchema
> >
export type Packet<TOp extends Operation = Operation> = TOp extends ServerOperation export type Packet<TOp extends Operation = Operation> = TOp extends ServerOperation

View File

@@ -1,8 +1,14 @@
diff --git a/node_modules/@semantic-release/npm/.bun-tag-3853154e196b7721 b/.bun-tag-3853154e196b7721
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/@semantic-release/npm/.bun-tag-550461f23a8ec245 b/.bun-tag-550461f23a8ec245
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/node_modules/@semantic-release/npm/.bun-tag-c9c8130945517add b/.bun-tag-c9c8130945517add diff --git a/node_modules/@semantic-release/npm/.bun-tag-c9c8130945517add b/.bun-tag-c9c8130945517add
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/lib/prepare.js b/lib/prepare.js diff --git a/lib/prepare.js b/lib/prepare.js
index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360e031c7b5 100644 index 3e76bec44cf595a1b4141728336bed904d4d518d..4b25ca64879bbee2a600f2b23b738c86136ad9c6 100644
--- a/lib/prepare.js --- a/lib/prepare.js
+++ b/lib/prepare.js +++ b/lib/prepare.js
@@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
@@ -14,7 +20,7 @@ index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360
export default async function ( export default async function (
npmrc, npmrc,
@@ -11,19 +12,12 @@ export default async function ( @@ -11,19 +12,13 @@ export default async function (
logger.log("Write version %s to package.json in %s", version, basePath); logger.log("Write version %s to package.json in %s", version, basePath);
@@ -36,10 +42,11 @@ index 3e76bec44cf595a1b4141728336bed904d4d518d..c6baf4e8de9bdf7536f9ad2e9eb9c360
- await versionResult; - await versionResult;
+ await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, detectIndent(pkgJsonRaw).indent)) + await writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, detectIndent(pkgJsonRaw).indent))
+ await execa("bun", ["install"]);
if (tarballDir) { if (tarballDir) {
logger.log("Creating npm package version %s", version); logger.log("Creating npm package version %s", version);
@@ -38,7 +32,7 @@ export default async function ( @@ -38,7 +33,7 @@ export default async function (
// Only move the tarball if we need to // Only move the tarball if we need to
// Fixes: https://github.com/semantic-release/npm/issues/169 // Fixes: https://github.com/semantic-release/npm/issues/169
if (tarballSource !== tarballDestination) { if (tarballSource !== tarballDestination) {

View File

@@ -0,0 +1,13 @@
diff --git a/src/lib.js b/src/lib.js
index de45d7dbe82975b09eff3742d0718accae2107fc..0575daa03dfabdd5c96928458ff4270cb8f7188a 100644
--- a/src/lib.js
+++ b/src/lib.js
@@ -42,7 +42,7 @@ function isMusl() {
}
function getBinding(name) {
- const path = join(__dirname, '..', `decancer.${name}.node`)
+ const path = join(import.meta.dir, '..', `decancer.${name}.node`)
return require(existsSync(path) ? path : `@vierofernando/decancer-${name}`)
}

View File

@@ -0,0 +1,21 @@
diff --git a/bin.cjs b/bin.cjs
index 142ed9c20f28dc1080bebfb52325fa308c6cb771..9d3bea0787f6c05df11567c6821bc85743286340 100644
--- a/bin.cjs
+++ b/bin.cjs
@@ -22053,7 +22053,7 @@ var init_sqliteImports = __esm({
const { unregister } = await safeRegister();
for (let i2 = 0; i2 < imports.length; i2++) {
const it = imports[i2];
- const i0 = require(`${it}`);
+ const i0 = await import(`${it}`);
const prepared = prepareFromExports3(i0);
tables.push(...prepared.tables);
}
@@ -129572,6 +129572,7 @@ var generateCommand = new Command("generate").option("--dialect <dialect>", "Dat
} else {
assertUnreachable(dialect7);
}
+ process.exit(0);
});
var migrateCommand = new Command("migrate").option(
"--config <config>",

View File

@@ -26,12 +26,12 @@ const Options = {
'@semantic-release/npm', '@semantic-release/npm',
{ {
npmPublish: false, npmPublish: false,
} },
], ],
[ [
'@semantic-release/git', '@semantic-release/git',
{ {
assets: ['CHANGELOG.md', 'package.json'], assets: ['CHANGELOG.md', 'package.json', '../../bun.lockb'],
}, },
], ],
[ [