Compare commits

...

112 Commits
4.0.0 ... 4.1.5

Author SHA1 Message Date
Kwoth
b009438e0e Fixed .stock command, closes #356 2022-05-11 12:36:50 +02:00
Kwoth
4b5d27d963 Fixed an issue which caused onmessage handlers to not block further execution properly, causing execution of both custom reactions and commands, ref #357 2022-05-11 11:51:01 +02:00
Kwoth
91ee0d121c DM send by the bot when being the target of a .give command will now once again contain amount. 2022-05-07 09:32:27 +02:00
Kwoth
a8767f1136 .clubdesc will now have a nicer response 2022-05-07 08:28:23 +02:00
Kwoth
44478e0f47 Upped version to 4.1.4 2022-05-06 19:15:29 +02:00
Kwoth
c73c2da6a4 fixed .yun, closes #332 2022-05-06 04:31:44 +02:00
Kwoth
5ed005211e Added bank information to .economy 2022-05-05 23:14:58 +02:00
Kwoth
d80cbb4647 Refactors. Cleanup. Refactored responses source gen a little. Parametrized localized strings are now generic. Refactored .cash bank interaction. Updated changelog. Reverted clubapps/ban/unban to use .ToString method and configured it to not have right to left unicode. Added extension methods to SocketMessageComponent akin to ones on the IMessageChannel (RespondConfirm, etc...) 2022-05-05 22:59:07 +02:00
Kwoth
9a96ef76ba Reverted smarttext to newtonsoft to relax the allowed json rules. Upped version to 4.1.3 2022-05-05 05:00:47 +02:00
Kwoth
5b5bc278ff - Reaction roles rewritten completely. They now support multiple exclusivity groups per message and level requirements. However they can only be added one by one
- Bot now support much higher XP values for global and server levels
2022-05-05 04:47:31 +02:00
Kwoth
5cb95cf94d Fixed an issue with embed array not building if the color is invalid or unspecified 2022-05-01 19:20:48 +02:00
Kwoth
f132aa2624 - Added a simple bank system. Users can deposit, withdraw and check the balance of their currency in the bank.
- Users can't check other user's bank balances.
- Added a button on a .$ command which, when clicked, sends you a message with your bank balance that only you can see.
- Updated pagination, it now uses buttons instead of reactions
- using .h <command group> (atm only .bank is a proper group) will list commands with their descriptions in that group
2022-04-29 07:22:49 +02:00
Kwoth
3b6b3bcf07 Added missing postgresql and mysql stondel migrations 2022-04-28 06:46:35 +02:00
Kwoth
78d97db224 .modules commanad now has a medusa module description 2022-04-28 06:20:46 +02:00
Kwoth
35ddd150ba Pagination is now using buttons instead of reactions 2022-04-28 06:10:06 +02:00
Kwoth
39ae070c9d Added .stondel which makes the bot delete stream online messages after the stream goes offline 2022-04-28 01:23:08 +02:00
Kwoth
24a9a02cc3 .give will send dms again 2022-04-27 00:36:27 +02:00
Kwoth
0f68abcac9 Fixed .deletexp command 2022-04-26 14:15:58 +02:00
Kwoth
908c61633d Added bash script prereq installer reference 2022-04-26 02:47:37 +02:00
Kwoth
054fc30672 Added prerequisites to linux release installation guide 2022-04-26 02:45:54 +02:00
Kwoth
11ffdd84a3 Updated changelog 2022-04-26 02:34:52 +02:00
Kwoth
5d2d74b92a Full support for embed arrays in .greet/.bye, .say and other commands which use embeds
- Website to create them is live at eb2.nadeko.bot (it will soon be replacing eb.nadeko.bot)
- Embed arrays don't have a plainText property (it's renamed to 'content')
- Embed arrays use color hex values instead of an integer
- Old embed format will still work
- There shouldn't be any breaking changes
2022-04-26 02:33:19 +02:00
Kwoth
18400dc53a Fixed a nullref message when the bot is loading medusae. Added support for multiple embeds in features which support custom embeds in the form of
{plainText:text-here, embeds: [embedObject, embedObject, embedObject]}
2022-04-17 09:58:30 +02:00
Kwoth
29d94640af Fix some build warnings 2022-04-16 19:00:28 +02:00
Kwoth
f6a53b96c7 Upped version to 4.1.2 2022-04-16 17:27:17 +02:00
Kwoth
1aa95a5dd0 Don't load uwu 2022-04-16 17:25:52 +02:00
Kwoth
fcfeb152c9 Updated changelog 2022-04-16 17:07:23 +02:00
Kwoth
0b64df95ef Fixed an issue with publish not having required dlls 2022-04-16 17:04:10 +02:00
Kwoth
7512f4a1e0 docs: fixed a link to nadeko discord server 2022-04-16 16:35:20 +02:00
Kwoth
789c453863 Updated CHANGELOG.md 2022-04-16 16:25:24 +02:00
Kwoth
615e1dd044 Merge branch 'v4-dev' into 'v4'
Medusa System Added 

Read about the medusa system [here](https://nadekobot.readthedocs.io/en/latest/medusa/creating-a-medusa/)

See merge request Kwoth/nadekobot!239
2022-04-16 12:44:46 +00:00
Kwoth
7cb15f5278 Medusa System Added
Read about the medusa system [here](https://nadekobot.readthedocs.io/en/latest/medusa/creating-a-medusa/)
2022-04-16 12:44:46 +00:00
Kwoth
3a516ab32a Merge branch 'v4' of https://gitlab.com/kwoth/nadekobot into v4 2022-04-13 18:15:06 +02:00
Kwoth
6159d84988 Fixed an issue with expression settings toggle, closes #353 2022-04-13 18:14:58 +02:00
Kwoth
cbe617bc59 Merge branch 'v4' into 'v4'
fix docker volume

See merge request Kwoth/nadekobot!246
2022-04-12 22:05:40 +00:00
Kwoth
e4b98a0c07 Merge branch 'v4-db' into 'v4'
WIP db provider support for Mysql and Postgres

See merge request Kwoth/nadekobot!245
2022-04-11 10:41:26 +00:00
Kwoth
e23233ee06 WIP db provider support for Mysql and Postgres 2022-04-11 10:41:26 +00:00
Ricordeau Raphael
8b1efa9b4f fix docker volume 2022-04-10 11:54:49 +00:00
Kwoth
8e1ec2ed9e Merge branch 'v4' of https://gitlab.com/kwoth/nadekobot into v4 2022-03-27 05:10:32 +02:00
Kwoth
0e555a4ed7 Update responses.uk-UA.json (POEditor.com) 2022-03-26 16:16:23 +00:00
Kwoth
a6a1d8cb6f Update responses.es-ES.json (POEditor.com) 2022-03-26 16:16:22 +00:00
Kwoth
6595c3a43d Update responses.ru-RU.json (POEditor.com) 2022-03-26 16:16:21 +00:00
Kwoth
77bb5724d8 Update responses.pt-BR.json (POEditor.com) 2022-03-26 16:16:20 +00:00
Kwoth
48ea595136 Update responses.pl-PL.json (POEditor.com) 2022-03-26 16:16:19 +00:00
Kwoth
1068457584 Update responses.it-IT.json (POEditor.com) 2022-03-26 16:16:18 +00:00
Kwoth
eca7973266 Update responses.id-ID.json (POEditor.com) 2022-03-26 16:16:17 +00:00
Kwoth
46ba2df0b8 Update responses.de-DE.json (POEditor.com) 2022-03-26 16:16:17 +00:00
Kwoth
a8f9a6e439 Update responses.fr-FR.json (POEditor.com) 2022-03-26 16:16:16 +00:00
Kwoth
cca5b5d41b Update responses.nl-NL.json (POEditor.com) 2022-03-26 16:16:15 +00:00
Kwoth
95bde7021a small wallet change 2022-03-25 01:08:22 +01:00
Kwoth
9bf9e06dbf Gateway errors should no longer be logged on selfhost version 2022-03-24 20:09:27 +01:00
Kwoth
c1bfb19a1d Fixed 'Features' output in .sinfo 2022-03-24 20:02:11 +01:00
Kwoth
ad0a9ceb9d Disabled linq2db query cache to see if it helps with out of memory issues 2022-03-24 02:46:27 +01:00
Kwoth
75f0574cc8 Small fix for a reminder query 2022-03-24 01:51:49 +01:00
Kwoth
c3fda25a93 Removed some classes used for debugging 2022-03-24 01:41:30 +01:00
Kwoth
7991659f74 Removed api error logging in stock getsymbolasync, closes #344 2022-03-24 01:40:29 +01:00
Kwoth
77195843d0 Fixed .feed related commands and updated an example to a working one. Updated packages 2022-03-22 11:49:32 +01:00
Kwoth
bed3347cd9 Upped version to 4.0.6, Updated changelog 2022-03-21 20:05:34 +01:00
Kwoth
0c74a9874e Merge branch 'v4' of https://gitlab.com/kwoth/nadekobot into v4 2022-03-21 20:05:18 +01:00
Kwoth
b079492d38 Fixed voice presence logging, closes #338 2022-03-21 19:58:36 +01:00
Kwoth
28720ebcea Fixed .clubaccept, .clubban, .clubkick and .clubunban, closes #341 2022-03-21 19:32:31 +01:00
Kwoth
3d456e5c14 Merge branch 'hokutochen-v4-patch-80574' into 'v4'
updating guide for twitch client secret

See merge request Kwoth/nadekobot!235
2022-03-21 15:30:21 +00:00
Hokuto Chen
83017e25ab updating guide for twitch client secret 2022-03-21 15:30:20 +00:00
Kwoth
72b3565e2e Merge branch 'hokutochen-v4-patch-55285' into 'v4'
Updated tmux guide

See merge request Kwoth/nadekobot!243
2022-03-21 15:29:55 +00:00
Kwoth
59cea6ee38 Version upped to 4.0.5, updated changelog 2022-03-21 16:00:05 +01:00
Kwoth
cbcfa77a34 Updated discord.net to 3.4.1 2022-03-21 15:57:42 +01:00
Kwoth
cfb202cc95 A few more improvements and cleanup for the wallet 2022-03-21 15:55:57 +01:00
Kwoth
b7d1fd1b47 Many IDisposable fixes. GlobalNadeko won't have file watchers for creds. Wallet simplified 2022-03-21 15:33:18 +01:00
Kwoth
4cf3bdb53a Removed unneeded httpclient for searchimages and some minor cleanup 2022-03-21 13:33:43 +01:00
Kwoth
aab5bc9744 Added long delay during graceful coordinator restart, and some logging 2022-03-21 13:25:48 +01:00
Kwoth
1f14c9066e Added several missing using statements 2022-03-21 13:20:26 +01:00
Kwoth
9ade3c9537 Added some custom settings to GlobalNadeko configuration 2022-03-21 13:02:55 +01:00
Kwoth
1dc393d2b1 - Moved update loop to a separate method.
- Added optimize flag to GlobalNadeko configuration.
- Added some packages which will be needed soon
2022-03-21 12:01:07 +01:00
Kwoth
798b66db9b Fixed playing rotate nullref 2022-03-21 11:07:42 +01:00
Kwoth
57e65e5515 Coordinator fix 2022-03-21 01:52:32 +01:00
Kwoth
86e728b753 Converted many raw sql queries to their linq2db equivalents 2022-03-20 19:31:04 +01:00
Kwoth
fd032d3e91 Fixed addbulkasync method 2022-03-20 12:19:12 +01:00
Kwoth
eab16865cd Added a missing savechanges 2022-03-20 12:07:06 +01:00
Hokuto Chen
fc4cbf5276 Updated tmux guide 2022-03-20 01:01:32 +00:00
Kwoth
a34a86bbfa Rewrote several raw queries to linqtodb 2022-03-20 01:14:24 +01:00
Kwoth
a016b3546f Added some missing tables to the context, no functional change 2022-03-20 01:09:22 +01:00
Kwoth
e09435da37 Moved some ICurrencyService methods to extensions to simplify use 2022-03-19 21:19:22 +01:00
Kwoth
416f3d604c Added a missing using 2022-03-19 17:01:03 +01:00
Kwoth
d9f371f994 Added logging to coordinator, downgraded framework to net5, downgraded grpc packages to 2.41 2022-03-19 11:19:08 +01:00
Kwoth
c1c22d0477 Merge branch 'v4' of https://gitlab.com/kwoth/nadekobot into v4 2022-03-18 23:50:55 +01:00
Kwoth
339f13d31a Updated gitlab-ci and docker-guide 2022-03-18 23:49:35 +01:00
Kwoth
4190f07d9c Merge branch 'eDavid-v4-patch-48565' into 'v4'
python3 folder is up to 3.9 now

See merge request Kwoth/nadekobot!241
2022-03-18 18:14:04 +00:00
David
a9bdf36c53 python3 folder is up to 3.9 now 2022-03-18 17:31:27 +00:00
Kwoth
1913cd4309 Merge branch 'hokutochen-v4-patch-69894' into 'v4'
wremoved "and when queue is empty" from .qap description.

See merge request Kwoth/nadekobot!238
2022-03-15 09:30:51 +00:00
Hokuto Chen
624439f684 wremoved "and when queue is empty" from .qap description. 2022-03-15 09:30:51 +00:00
Kwoth
3e14dc22cb Merge branch 'hokutochen-v4-patch-80313' into 'v4'
incorrect bot version (we are on 4.0.4)?

See merge request Kwoth/nadekobot!236
2022-03-15 00:00:17 +00:00
Kwoth
f4178aeacd Merge branch 'ban=patch' into 'v4'
Fix ban dms sending to mod. Closes #340

Closes #340

See merge request Kwoth/nadekobot!237
2022-03-14 23:59:55 +00:00
Alan Beatty
aaf3c9cfe9 Fix ban dms sending to mod. Closes #340 2022-03-14 11:44:15 -05:00
Kwoth
f6ee012b15 Removed some references to .cr commands from command strings 2022-03-13 06:57:22 +01:00
Hokuto Chen
363ef42923 incorrect bot version (we are on 4.0.4)? 2022-03-12 20:55:03 +00:00
Kwoth
cc522ef872 Updated CHANGELOG.md 2022-03-04 04:55:39 +01:00
Kwoth
0e192ee7f0 Removed some lingering strings calling Expressions 'CustomReactions'. Id which shows up when you add a new expr is now correct. 2022-03-04 04:51:02 +01:00
Kwoth
ddd0592b30 Changed the intent error message to say 'all intents' 2022-03-04 02:23:48 +01:00
Kwoth
c4efe2965b Upped version to 4.0.3 2022-03-04 01:28:14 +01:00
Kwoth
a90b5a62f3 Fixed number spam in the console 2022-03-04 01:26:46 +01:00
Kwoth
50a4497532 Update CHANGELOG.md 2022-03-03 23:32:05 +00:00
Kwoth
c3d6183d73 Update CHANGELOG.md 2022-03-03 23:30:44 +00:00
Kwoth
864a8fd7b6 Updated CHANGELOG.md 2022-03-03 23:22:12 +01:00
Kwoth
e7db631151 Fixed .rero issue which was introduced in 4.0 2022-03-03 22:32:28 +01:00
Kwoth
c7b312196e Added upgrade instructions to the docs 2022-03-03 10:15:17 +01:00
Kwoth
8cd7a50720 Updated guide to reflect the new changes to the installer 2022-03-03 03:09:59 +01:00
Kwoth
f82c4c7019 - Updated Changelog.md
- Fixed intent error message condition
2022-03-03 02:45:18 +01:00
Kwoth
03367c5ec4 Added usePrivilegedIntents to creds.yml as an option for users who don't have them enabled
Added an explanation on how to enable intents if the bot doesn't have them and fails to login
2022-03-03 02:31:32 +01:00
Kwoth
01cc6e52d5 Enabled client log everywhere which could hopefully print login errors 2022-03-03 01:42:22 +01:00
Kwoth
c903bc9003 Added one more log line to pinpoint the login hang 2022-03-03 01:33:05 +01:00
Kwoth
323699d103 Small change to Bot.cs to pinpoint the logging in hang issue 2022-03-03 01:13:49 +01:00
Kwoth
63ced029ab Updated CHANGELOG.md 2022-03-02 06:54:17 +01:00
275 changed files with 58278 additions and 2395 deletions

View File

@@ -1,6 +1,8 @@
# Ignore all files
*
# Use Nadeko.Medusa project
!src/Nadeko.Medusa/**
# Use NadekoBot project
!src/NadekoBot/**
# Use NadekoBot.Coordinator project

6
.gitignore vendored
View File

@@ -1,5 +1,11 @@
#Manually added files
# medusa stuff
!src/NadekoBot/data/medusae/medusa.yml
src/NadekoBot/data/medusae/**
# other
command_errors*.txt
output/
src/NadekoBot/output

View File

@@ -97,29 +97,29 @@ upload-windows-updater-release:
- aws --endpoint-url $AWS_SERVICE_URL s3api put-object --bucket "$AWS_BUCKET_NAME" --key "dl/bot/$INSTALLER_FILE_NAME" --acl public-read --body "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
- aws --endpoint-url $AWS_SERVICE_URL s3api put-object --bucket "$AWS_BUCKET_NAME" --key "dl/bot/releases-v3.json" --acl public-read --body "releases-v3.json"
# docker-build:
# # Use the official docker image.
# image: docker:latest
# stage: build
# services:
# - docker:dind
# before_script:
# - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# # Default branch leaves tag empty (= latest tag)
# # All other branches are tagged with the escaped branch name (commit ref slug)
# script:
# - |
# if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
# tag=""
# echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
# else
# tag=":$CI_COMMIT_REF_SLUG"
# echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
# fi
# - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
# - docker push "$CI_REGISTRY_IMAGE${tag}"
# # Run this job in a branch where a Dockerfile exists
# rules:
# - if: $CI_COMMIT_BRANCH
# exists:
# - Dockerfile
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag=""
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
- docker push "$CI_REGISTRY_IMAGE${tag}"
# Run this job in a branch where a Dockerfile exists
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile

View File

@@ -1,10 +1,144 @@
# Changelog
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
#todo .trans fix
## Unreleased
## [4.1.5] - 11.05.2022
### Changed
- `.clubdesc <msg>` will now have a nicer response
### Fixed
- `.give` DM will once again show an amount
- Fixed an issue with filters not working and with custom reactions no longer being able to override commands.
- Fixed `.stock` command
## [4.1.4] - 06-05-2022
### Fixed
- Fixed `.yun`
## [4.1.3] - 06.05.2022
### Added
- Added support for embed arrays in commands such as .say, .greet, .bye, etc...
- Website to create them is live at eb.nadeko.bot (old one is moved to oldeb.nadeko.bot)
- Embed arrays don't have a plainText property (it's renamed to 'content')
- Embed arrays use color hex values instead of an integer
- Old embed format will still work
- There shouldn't be any breaking changes
- Added `.stondel` command which, when toggled, will make the bot delete online stream messages on the server when the stream goes offline
- Added a simple bank system.
- Users can deposit, withdraw and check the balance of their currency in the bank.
- Users can't check other user's bank balances.
- Added a button on a .$ command which, when clicked, sends you a message with your bank balance that only you can see.
- Added `.h <command group>`
- Using this command will list all commands in the specified group
- Atm only .bank is a proper group (`.h bank`)
- Added "Bank Accounts" entry to `.economy`
### Changed
- Reaction roles rewritten completely
- Supports multiple exclusivity groups per message
- Supports level requirements
- However they can only be added one by one
- Use the following commands for more information
- `.h .reroa`
- `.h .reroli`
- `.h .rerot`
- `.h .rerorm`
- `.h .rerodela`
- Pagination is now using buttons instead of reactions
- Bot will now support much higher XP values for global and server levels
- [dev] Small change and generation perf improvement for the localized response strings
### Fixed
- Fixed `.deletexp` command
- `.give` command should send DMs again
- `.modules` command now has a medusa module description
## [4.1.2] - 16.04.2022
### Fixed
- Fixed an issue with missing `.dll` files in release versions
## [4.1.0] - 16.04.2022
### Added
- NadekoBot now supports mysql, postgresql and sqlite
- To change the db nadeko will use, simply change the `db type` in `creds.yml`
- There is no migration code right now, which means that if you want to switch to another system you'll either have to manually export/import your database or start fresh
- Medusa system
- A massive new feature which allows developers to create custom modules/plugins/cogs
- They can be load/unloaded/updated at runtime without restarting the bot
### Changed
- Minor club rework
- Clubs names are now case sensitive (owo and OwO can be 2 different clubs)
- Removed discriminators
- Current discriminators which are greater than 1 are appended to clubnames to avoid duplicates, you can rename your club with `.clubrename` to remove it
- Most of the clubs with #1 discriminator no longer have it (For example MyClub#1 will now just be MyClub)
- [dev] A lot of refactoring and slight functionality changes within Nadeko's behavior system and command handler which were required in order to support the medusa system
### Removed
- Removed `.clublevelreq` command as it doesn't serve much purpose
## [4.0.6] - 21.03.2022
### Fixed
- Fixed voice presence logging
- Fixed .clubaccept, .clubban, .clubkick and .clubunban commands
## [4.0.5] - 21.03.2022
### Fixed
- Fixed several bugs in the currency code
- Fixed some potential memory leaks
- Fixed some response strings
## [4.0.4] - 04.03.2022
### Fixed
- Fixed the `id` which shows up when you add a new Expression
- Fixed some strings which were still referring to "CustomReaction(s)" instead of "Expression(s)"
## [4.0.3] - 04.03.2022
### Fixed
- Console should no longer spam numbers when `.antispam` is enabled
## [4.0.2] - 03.03.2022
### Fixed
- Fixed `.rero` not working due to a bug introduced in 4.0
## [4.0.1] - 03.03.2022
### Added
- Added `usePrivilegedIntents` to creds.yml if you don't have or don't want (?) to use them
- Added a human-readable, detailed error message if logging in fails due to missing privileged intents
## [4.0.0] - 02.03.2022
### Added
- Added `.deleteemptyservers` command
- Added `.curtr <id>` which lets you see full information about one of your own transactions with the specified id
@@ -43,6 +177,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- [dev] Moved FilterWordsChannelId to a separate table
### Fixed
- Fixed twitch stream notifications (rewrote it to use the new api)
- Fixed an extra whitespace in usage part of command help if the command has no arguments
- Possible small fix for `.prune` ratelimiting
- `.gvc` should now properly trigger when a user is already in a gvc and changes his activity
@@ -462,4 +597,4 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
### Removed
- Removed admin requirement on `.scrm` as it didn't make sense
- Some Music commands are removed because of the complexity they bring in with little value (if you *really* want them back, you can open an issue and specify your *good* reason)
- Some Music commands are removed because of the complexity they bring in with little value (if you *really* want them back, you can open an issue and specify your *good* reason)

View File

@@ -1,6 +1,7 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
COPY src/Nadeko.Medusa/*.csproj src/Nadeko.Medusa/
COPY src/NadekoBot/*.csproj src/NadekoBot/
COPY src/NadekoBot.Coordinator/*.csproj src/NadekoBot.Coordinator/
COPY src/NadekoBot.Generators/*.csproj src/NadekoBot.Generators/
@@ -25,7 +26,7 @@ RUN set -xe; \
useradd -m nadeko; \
apt-get update; \
apt-get install -y libopus0 libsodium23 libsqlite3-0 curl ffmpeg python3 python3-pip sudo; \
update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1; \
update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1; \
echo 'Defaults>nadeko env_keep+="ASPNETCORE_* DOTNET_* NadekoBot_* shard_id total_shards TZ"' > /etc/sudoers.d/nadeko; \
pip3 install --upgrade youtube-dl; \
apt-get remove -y python3-pip; \
@@ -37,6 +38,6 @@ COPY docker-entrypoint.sh /usr/local/sbin
ENV shard_id=0
ENV total_shards=1
VOLUME [ "app/data" ]
VOLUME [ "/app/data" ]
ENTRYPOINT [ "/usr/local/sbin/docker-entrypoint.sh" ]
CMD dotnet NadekoBot.dll "$shard_id" "$total_shards"
CMD dotnet NadekoBot.dll "$shard_id" "$total_shards"

View File

@@ -11,6 +11,7 @@ ProjectSection(SolutionItems) = preProject
LICENSE.md = LICENSE.md
README.md = README.md
.gitlab-ci.yml = .gitlab-ci.yml
Dockerfile = Dockerfile
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NadekoBot", "src\NadekoBot\NadekoBot.csproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}"
@@ -27,6 +28,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NadekoBot.Generators", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NadekoBot.VotesApi", "src\NadekoBot.VotesApi\NadekoBot.VotesApi.csproj", "{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nadeko.Medusa", "src\Nadeko.Medusa\Nadeko.Medusa.csproj", "{E685977E-31A4-46F4-A5D7-4E3E39E82E43}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -70,6 +73,12 @@ Global
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}.Release|Any CPU.Build.0 = Release|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.GlobalNadeko|Any CPU.ActiveCfg = Debug|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E685977E-31A4-46F4-A5D7-4E3E39E82E43}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -82,6 +91,7 @@ Global
{AE9B7F8C-81D7-4401-83A3-643B38258374} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
{3BC3BDF8-1A0B-45EB-AB2B-C0891D4D37B8} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
{E685977E-31A4-46F4-A5D7-4E3E39E82E43} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5F3F555C-855F-4BE8-B526-D062D3E8ACA4}

View File

@@ -1,18 +1,25 @@
# Setting up NadekoBot with Docker
# DO NOT USE YET - WORK IN PROGRESS
# WORK IN PROGRESS
### Docker Compose
### Installation
1. Create a `/srv/nadeko` folder
- `mkdir -p /srv/nadeko`
2. Create a `docker-compose.yml`
- nano `docker-compose.yml`
- copy the following contents into it:
##### docker-compose.yml
```yml
version: "3.7"
services:
nadeko:
image: registry.gitlab.com/veovis/nadekobot:v3-docker
image: registry.gitlab.com/kwoth/nadekobot:latest
depends_on:
- redis
environment:
TZ: Europe/Paris
#NadekoBot_RedisOptions: redis,name=nadeko
NadekoBot_RedisOptions: redis,name=nadeko
#NadekoBot_ShardRunCommand: dotnet
#NadekoBot_ShardRunArguments: /app/NadekoBot.dll {0} {1}
volumes:
@@ -27,6 +34,12 @@ services:
volumes:
- /srv/nadeko/redis-data:/data
```
3. Save your file and run docker compose
- `docker-compose up`
4. Edit creds in `/srv/nadeko/conf/creds.yml`
5. Run it again with
- `docker-compose up`
### Updating
- `cd /srv/nadeko`
- `docker-compose pull`

View File

@@ -26,14 +26,24 @@ It is recommended that you use **Ubuntu 20.04**, as there have been nearly no pr
## Linux From Source
Open Terminal (if you're on an installation with a window manager) and navigate to the location where you want to install the bot (for example `cd ~`)
##### Migration from v3 -> v4
Follow the following few steps only if you're migrating from v3. If not, skip to installation instructions.
Use the new installer script: `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
> - Install prerequisites (type `1` and press `enter`)
> - Download (type `2` and press `enter`)
> - Run (type `3` and press `enter`)
> - Done
##### Installation Instructions
1. Download and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/master/linuxAIO.sh && bash linuxAIO.sh`
Open Terminal (if you're on an installation with a window manager) and navigate to the location where you want to install the bot (for example `cd ~`)
1. Download and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
2. Install prerequisites (type `1` and press enter)
3. Download the bot (type `2` and press enter)
4. Exit the installer (type `5` and press enter)
4. Exit the installer (type `6` and press enter)
5. Copy the creds.yml template `cp nadekobot/output/creds_example.yml nadekobot/output/creds.yml`
6. Open `nadekobot/output/creds.yml` with your favorite text editor. We will use nano here
- `nano nadekobot/output/creds.yml`
@@ -42,12 +52,13 @@ Open Terminal (if you're on an installation with a window manager) and navigate
- `CTRL` + `X`
- `Y`
- `Enter`
8. Run the bot (type `3` and press enter)
8. Run the installer script again `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
9. Run the bot (type `3` and press enter)
##### Source Update Instructions
1. ⚠ Stop the bot ⚠
2. Update and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/master/linuxAIO.sh && bash linuxAIO.sh`
2. Update and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
3. Update the bot (type `2` and press enter)
4. Run the bot (type `3` and press enter)
5. 🎉
@@ -81,6 +92,19 @@ Open Terminal (if you're on an installation with a window manager) and navigate
##### Release Update Instructions
###### Prerequisites
1. Nadeko requires redis to function
- ubuntu installation command: `sudo apt-get install redis-server`
2. Playing music requires `ffmpeg`, `libopus`, `libsodium` and `youtube-dl` (which in turn requires python3)
- ubuntu installation command: `sudo apt-get install ffmpeg libopus0 opus-tools libopus-dev libsodium-dev -y`
3. Make sure your python is version 3+ with `python --version`
- if it's not, you can install python 3 and make it the default with: `sudo apt-get install python3.8 python-is-python3`
*You can use nadeko bash script [prerequisites installer](https://gitlab.com/Kwoth/nadeko-bash-installer/-/blob/v4/n-prereq.sh) as a reference*
###### Installation
1. Stop the bot
2. Download the latest release from <https://gitlab.com/Kwoth/nadekobot/-/releases>
- Look for the file called "x.x.x-linux-x64-build.tar" (where `X.X.X` is a version, for example 3.0.4) and download it
@@ -130,10 +154,16 @@ While there are two run modes built into the installer, these options only run N
Using `tmux` is the simplest method, and is therefore recommended for most users.
1. Start a tmux session:
- `tmux`
**Before proceeding, make sure your bot is not running by either running `.die` in your Discord server or exiting the process with `Ctrl+C`.**
If you are presented with the installer main menu, exit it by choosing Option `8`.
1. Create a new session: `tmux new -s nadeko`
The above command will create a new session named **nadeko** *(you can replace “nadeko” with anything you prefer, it's your session name)*.
2. Navigate to the project's root directory
- Project root directory location example: `/home/user/nadekobot/`
- Project root directory location example: `cd /home/user/nadekobot/`
3. Enter the `output` directory:
- `cd output`
4. Run the bot using:
@@ -141,8 +171,10 @@ Using `tmux` is the simplest method, and is therefore recommended for most users
5. Detatch the tmux session:
- Press `Ctrl` + `B`
- Then press `D`
Now check your Discord server, the bot should be online. Nadeko should now be running in the background of your system.
To re-open the tmux session to either update, restart, or whatever, execute `tmux a -t nadeko`. *(Make sure to replace "nadeko" with your session name. If you didn't change it, leave it as it.)*
Nadeko should now be running in the background of your system. To re-open the tmux session to either update, restart, or whatever, execute `tmux a`.
### Systemd

View File

@@ -31,7 +31,7 @@ sudo ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
##### Installation Instructions
1. Download and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/master/linuxAIO.sh && bash linuxAIO.sh`
1. Download and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
2. Install prerequisites (type `1` and press enter)
3. Download the bot (type `2` and press enter)
4. Exit the installer in order to set up your `creds.yml`
@@ -49,7 +49,7 @@ sudo ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
##### Update Instructions
1. ⚠ Stop the bot
2. Update and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/master/linuxAIO.sh && bash linuxAIO.sh`
2. Update and run the **new** installer script `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
3. Update the bot (type `2` and press enter)
4. Run the bot (type `3` and press enter)
5. 🎉

View File

@@ -24,7 +24,7 @@ This part is completely optional, **however it's necessary for music and a few o
- Open up `creds.yml` and look for `GoogleAPIKey`, paste your API key after the `:`.
- It should look like this:
```yml
GoogleApiKey: AIzaSyDSci1sdlWQOWNVj1vlXxxxxxbk0oWMEzM
GoogleApiKey: 'AIzaSyDSci1sdlWQOWNVj1vlXxxxxxbk0oWMEzM'
```
- **MashapeKey**
- Required for Hearthstone cards.
@@ -40,16 +40,19 @@ This part is completely optional, **however it's necessary for music and a few o
- For Patreon creators only.
- **PatreonCampaignId**
- For Patreon creators only. Id of your campaign.
- **TwitchClientId**
- **TwitchClientId and TwitchClientSecret**
- Mandatory for following twitch streams with `.twitch` (or `.stadd` with twitch link)
- Go to [apps page](https://dev.twitch.tv/console/apps/create) on twitch and register your application.
- Go to [apps page](https://dev.twitch.tv/console) on twitch and register your application.
- You need 2FA enabled on twitch in order to create an application
- You can set `http://localhost` as the OAuth Redirect URL (and press Add button)
- Select `Chat Bot` from the Category dropdown
- Once created, clicking on your application will show a new Client ID field
- Copy it to your creds.yml as shown below
- Once created, `click Manage`
- Click `New Secret` and select `OK` in the popup
**Note: You will need to generate a new Client Secret everytime you exit the page**
- Copy both to your creds.yml as shown below
```yml
TwitchClientId: "516tr61tr1qweqwe86trg3g"
twitchClientId: 516tr61tr1qweqwe86trg3g
twitchClientSecret: 16tr61tr1q86tweqwe
```
- **LocationIqApiKey**
- Optional. Used only for the `.time` command. https://locationiq.com api key (register and you will receive the token in the email).
@@ -94,7 +97,7 @@ RestartCommand:
```yml
# DO NOT CHANGE
version: 1
version: 4
# Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/
token: 'MTE5Nzc3MDIxMzE5NTc3NjEw.VlhNCw.BuqJFyzdIUAK1PRf1eK1Cu89Jew'
# List of Ids of the users who have bot owner permissions
@@ -153,6 +156,13 @@ timezoneDbApiKey:
coinmarketcapApiKey:
# Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api
osuApiKey: 4c8c8fdffdsfdsfsdfsfa33f3f3140a7d93320d6
# Optional Trovo client id.
# You should use this if Trovo stream notifications stopped working or you're getting ratelimit errors.
trovoClientId:
# Obtain by creating an application at https://dev.twitch.tv/console/apps
twitchClientId: jf2w6kkyrlzfl6mp1b4k25h4jr6b2o
# Obtain by creating an application at https://dev.twitch.tv/console/apps
twitchClientSecret: 16tr61tr1q86tweqwe
# Command and args which will be used to restart the bot.
# Only used if bot is executed directly (NOT through the coordinator)
# placeholders:

View File

@@ -0,0 +1,251 @@
# Creating A Medusa
## Theory
### Introduction
Medusa system allows you to write independent medusae (known as "modules", "cogs" or "plugins" in other software) which you can then load, unload and update at will without restarting the bot.
The system itself borrows some design from the current way Nadeko's Modules are written but mostly from never-released `Ayu.Commands` system which was designed to be used for a full Nadeko v3 rewrite.
The medusa base classes used for development are open source [here](https://gitlab.com/Kwoth/nadekobot/-/tree/v4/src/Nadeko.Medusa) in case you need reference, as there is no generated documentation at the moment.
### Term list
#### Medusa
- The project itself which compiles to a single `.dll` (and some optional auxiliary files), it can contain multiple [Sneks](#snek), [Services](#service), and [ParamParsers](#param-parser)
#### Snek
- A class which will be added as a single Module to NadekoBot on load. It also acts as a [lifecycle handler](snek-lifecycle.md) and as a singleton service with the support for initialize and cleanup.
- It can contain a Snek (called SubSnek) but only 1 level of nesting is supported (you can only have a snek contain a subsnek, but a subsnek can't contain any other sneks)
- Sneks can have their own prefix
- For example if you set this to 'test' then a command called 'cmd' will have to be invoked by using `.test cmd` instead of `.cmd`
#### Snek Command
- Acts as a normal command
- Has context injected as a first argument which controls where the command can be executed
- `AnyContext` the command can be executed in both DMs and Servers
- `GuildContext` the command can only be executed in Servers
- `DmContext` the command can only be executed in DMs
- Support the usual features such as default values, leftover, params, etc.
- It also supports dependency injection via `[inject]` attribute. These dependencies must come after the context and before any input parameters
- Supports `ValueTask`, `Task`, `Task<T>` and `void` return types
#### Param Parser
- Allows custom parsing of command arguments into your own types.
- Overriding existing parsers (for example for IGuildUser, etc...) can cause issues.
#### Service
- Usually not needed.
- They are marked with a `[svc]` attribute, and offer a way to inject dependencies to different parts of your medusa.
- Transient and Singleton lifetimes are supported.
### Localization
Response and command strings can be kept in one of three different places based on whether you plan to allow support for localization
option 1) `res.yml` and `cmds.yml`
If you don't plan on having your app localized, but you just *may* in the future, you should keep your strings in the `res.yml` and `cmds.yml` file the root folder of your project, and they will be automatically copied to the output whenever you build your medusa.
##### Example project folder structure:
- uwu/
- uwu.csproj
- uwu.cs
- res.yml
- cmds.yml
##### Example output folder structure:
- medusae/uwu/
- uwu.dll
- res.yml
- cmds.yml
option 2) `strings` folder
If you plan on having your app localized (or want to allow your consumers to easily add languages themselves), you should keep your response strings in the `strings/res/en-us.yml` and your command strings in `strings/cmds/en-us.yml` file. This will be your base file, and from there you can make support for additional languages, for example `strings/res/ru-ru.yml` and `strings/cmds/ru-ru.yml`
##### Example project folder structure:
- uwu/
- uwu.csproj
- uwu.cs
- strings/
- res/
- en-us.yml
- ru-ru.yml
- cmds/
- en-us.yml
- ru-ru.yml
##### Example output folder structure:
- medusae/uwu/
- uwu.dll
- strings/
- res/
- en-us.yml
- ru-ru.yml
- cmds/
- en-us.yml
- ru-ru.yml
option 3) In the code
If you don't want any auxiliary files, and you don't want to bother making new .yml files to keep your strings in, you can specify the command strings directly in the `[cmd]` attribute itself, and use non-localized methods for message sending in your commands.
If you update your response strings .yml file(s) while the medusa is loaded and running, running `.stringsreload` will reload the responses without the need to reload the medusa or restart the bot.
#### Config
- Medusa config is kept in `medusae/medusa.yml` file
- At the moment this config only keeps track of which medusae are currently loaded (they will also be always loaded at startup)
- If a medusa is causing issues and you're unable to unload it, you can remove it from the `loaded:` list in this config file and restart the bot. It won't be loaded next time the bot is started up
#### Unloadability issues
To make sure your medusa can be properly unloaded/reloaded you must:
- Make sure that none of your types and objects are referenced by the Bot or Bot's services after the DisposeAsync is called on your Snek instances.
- Make sure that all of your commands execute quickly and don't have any long running tasks, as they will hold a reference to a type from your assembly
- If you are still having issues, you can always run `.meunload` followed by a bot restart, or if you want to find what is causing the medusa unloadability issues, you can check the [microsoft's assembly unloadability debugging guide](https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability)
## Practice
This section will guide you through how to create a simple custom medusa. You can find the entirety of this code hosted [here](https://gitlab.com/nadeko/example_medusa)
#### Prerequisite
- [.net6 sdk](https://dotnet.microsoft.com/en-us/download) installed
- Optional: use [vscode](https://code.visualstudio.com/download) to write code
#### Guide
- Open your favorite terminal and navigate to a folder where you will keep your project .
- Create a new folder
- `mkdir example_medusa`
- Create a new .net class library
- `dotnet new classlib`
- Open the current folder with your favorite editor/IDE. In this case we'll use VsCode
- `code .`
- Remove the `Class1.cs` file
- Replace the contents of the `.csproj` file with the following contents
```xml
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!-- Reduces some boilerplate in your .cs files -->
<ImplicitUsings>enable</ImplicitUsings>
<!-- Use latest .net features -->
<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<!-- tell .net that this library will be used as a plugin -->
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<!-- Base medusa package. You MUST reference this in order to have a working medusa -->
<!-- Also, this package comes from MyGet, which requires you to have a NuGet.Config file next to your .csproj -->
<PackageReference Include="Nadeko.Medusa" Version="1.0.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- Note: If you want to use NadekoBot services etc... You will have to manually clone
the gitlab.com/kwoth/nadekobot repo locally and reference the NadekoBot.csproj because there is no NadekoBot package atm.
It is strongly recommended that you checkout a specific tag which matches your version of nadeko,
as there could be breaking changes even between minor versions of NadekoBot.
For example if you're running NadekoBot 4.1.0 locally for which you want to create a medusa for,
you should do "git checkout 4.1.0" in your NadekoBot solution and then reference the NadekoBot.csproj
-->
</ItemGroup>
<!-- Copy shortcut and full strings to output (if they exist) -->
<ItemGroup>
<None Update="res.yml;cmds.yml;strings/**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
```
- Create a `MySnek.cs` file and add the following contents
```cs
using Nadeko.Snake;
using NadekoBot;
using Discord;
public sealed class MySnek : Snek
{
[cmd]
public async Task Hello(AnyContext ctx)
{
await ctx.Channel.SendMessageAsync($"Hello everyone!");
}
[cmd]
public async Task Hello(AnyContext ctx, IUser target)
{
await ctx.ConfirmLocalizedAsync("hello", target);
}
}
```
- Create `res.yml` and `cmds.yml` files with the following contents
`res.yml`
```yml
medusa.description: "This is my medusa's description"
hello: "Hello {0}, from res.yml!"
```
`cmds.yml`
```yml
hello:
desc: "This is a basic hello command"
args:
- ""
- "@Someone"
```
- Add `NuGet.Config` file which will let you use the base Nadeko.Medusa package. This file should always look like this and you shouldn't change it
```xml
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="nadeko.bot" value="https://www.myget.org/F/nadeko/api/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
```
### Build it
- Build your Medusa into a dll that Nadeko can load. In your terminal, type:
- `dotnet publish -o bin/medusae/example_medusa /p:DebugType=embedded`
- Done. You can now try it out in action.
### Try it out
- Copy the `bin/medusae/example_medusa` folder into your NadekoBot's `data/medusae/` folder. (Nadeko version 4.1.0+)
- Load it with `.meload example_medusa`
- In the channel your bot can see, run the following commands to try it out
- `.hello` and
- `.hello @<someone>`
- Check its information with
- `.meinfo example_medusa`
- Unload it
- `.meunload example_medusa`
- Congrats! You've just made your first medusa!

View File

@@ -0,0 +1,31 @@
## Getting Started
### What is the Medusa system?
- It is a dynamic module/plugin/cog system for NadekoBot introduced in **NadekoBot 4.1.0**
- Allows developers to add custom functionality to Nadeko without modifying the original code
- Allows for those custom features to be updated during bot runtime (if properly written), without the need for bot restart.
- They are added to `data/medusae` folder and are loaded, unloaded and handled through discord commands.
- `.meload` Loads the specified medusa (see `.h .meload`)
- `.meunload` Unloads the specified medusa (see `.h .meunload`)
- `.meinfo` Checks medusae information (see `.h .meinfo`)
- `.melist` Lists the available medusae (see `.h .melist`)
### How to make one?
Medusae are written in [C#](https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/) programming language, so you will need at least low-intermediate knowledge of it in order to make a useful Medusa.
Follow the [creating a medusa guide](creating-a-medusa.md)
### Where to get medusae other people made?
*It is EXTREMELY, and I repeat **EXTREMELY** dangerous to run medusae of strangers or people you don't FULLY trust.* ⚠
*It can not only lead to your bot being stolen, but it also puts your entire computer and personal files in jeopardy.*
**It is strongly recommended to run only the medusae you yourself wrote, and only on a hosted VPS or dedicated server which ONLY hosts your bot, to minimize the potential damage caused by bad actors.**
No easy way at the moment, except asking in the `#dev-and-modding` chat in [#NadekoLog server](https://discord.nadeko.bot)

View File

@@ -0,0 +1,19 @@
# Snek Lifecycle
*You can override several methods to hook into command handler's lifecycle.
These methods start with `Exec*`*
- `ExecOnMessageAsync` runs first right after any message was received
- `ExecInputTransformAsync` runs after ExecOnMessageAsync and allows you to transform the message content before the bot looks for the matching command
- `ExecPreCommandAsync` runs after a command was found but not executed, allowing you to potentially prevent command execution
- `ExecPostCommandAsync` runs if the command was successfully executed
- `ExecOnNoCommandAsync` runs instead of ExecPostCommandAsync if no command was found for a message
*Besides that, sneks have 2 methods with which you can initialize and cleanup your snek*
- `InitializeAsync` Runs when the medusa which contains this snek is being loaded
- `DisposeAsync` Runs when the medusa which contains this snek is being unloaded

View File

@@ -28,6 +28,8 @@ Some features have their own specific placeholders which are noted in that featu
- `%server.id%` - Server ID
- `%server.name%` - Server name
- `%server.members%` - Member count
- `%server.boosters%` - Number of users boosting the server
- `%server.boost_level%` - Server Boost level
- `%server.time%` - Server time (requires `.timezone` to be set)
### Channel placeholders
@@ -79,11 +81,9 @@ Some features have their own specific placeholders which are noted in that featu
### Music placeholders
!!! Note
These placeholders will only work in rotating playing statuses.
- `%music.queued%` - Amount of songs currently queued
- `%music.playing%` - Current song name
- `%music.queued%` - Number of songs currently queued
- `%music.playing%` - Current song name (random playing song if bot is playing on multiple servers)
- `%music.servers%` - Number of servers currently listening to music
### Miscellaneous placeholders

View File

@@ -86,7 +86,10 @@ nav:
- Custom Reactions: custom-reactions.md
- Placeholders: placeholders.md
- Config: config-guide.md
- Bot Config: bce-guide.md
- Medusa System:
- medusa/getting-started.md
- medusa/creating-a-medusa.md
- medusa/snek-lifecycle.md
- Contribution Guide: contribution-guide.md
- Donate: donate.md
- License: license.md

View File

@@ -0,0 +1,10 @@
namespace Nadeko.Snake;
/// <summary>
/// Overridden to implement custom checks which commands have to pass in order to be executed.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public abstract class FilterAttribute : Attribute
{
public abstract ValueTask<bool> CheckAsync(AnyContext ctx);
}

View File

@@ -0,0 +1,37 @@
namespace Nadeko.Snake;
/// <summary>
/// Marks a method as a snek command
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class cmdAttribute : Attribute
{
/// <summary>
/// Command description. Avoid using, as cmds.yml is preferred
/// </summary>
public string? desc { get; set; }
/// <summary>
/// Command args examples. Avoid using, as cmds.yml is preferred
/// </summary>
public string[]? args { get; set; }
/// <summary>
/// Command aliases
/// </summary>
public string[] Aliases { get; }
public cmdAttribute()
{
desc = null;
args = null;
Aliases = Array.Empty<string>();
}
public cmdAttribute(params string[] aliases)
{
Aliases = aliases;
desc = null;
args = null;
}
}

View File

@@ -0,0 +1,10 @@
namespace Nadeko.Snake;
/// <summary>
/// Marks services in command arguments for injection.
/// The injected services must come after the context and before any input parameters.
/// </summary>
public class injectAttribute : Attribute
{
}

View File

@@ -0,0 +1,10 @@
namespace Nadeko.Snake;
/// <summary>
/// Marks the parameter to take
/// </summary>
[AttributeUsage(AttributeTargets.Parameter)]
public class leftoverAttribute : Attribute
{
}

View File

@@ -0,0 +1,20 @@
namespace Nadeko.Snake;
/// <summary>
/// Sets the priority of a command in case there are multiple commands with the same name but different parameters.
/// Higher value means higher priority.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class prioAttribute : Attribute
{
public int Priority { get; }
/// <summary>
/// Snek command priority
/// </summary>
/// <param name="priority">Priority value. The higher the value, the higher the priority</param>
public prioAttribute(int priority)
{
Priority = priority;
}
}

View File

@@ -0,0 +1,23 @@
namespace Nadeko.Snake;
/// <summary>
/// Marks the class as a service which can be used within the same Medusa
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class svcAttribute : Attribute
{
public Lifetime Lifetime { get; }
public svcAttribute(Lifetime lifetime)
{
Lifetime = lifetime;
}
}
/// <summary>
/// Lifetime for <see cref="svcAttribute"/>
/// </summary>
public enum Lifetime
{
Singleton,
Transient
}

View File

@@ -0,0 +1,47 @@
using Discord;
using NadekoBot;
namespace Nadeko.Snake;
/// <summary>
/// Commands which take this class as a first parameter can be executed in both DMs and Servers
/// </summary>
public abstract class AnyContext
{
/// <summary>
/// Channel from the which the command is invoked
/// </summary>
public abstract IMessageChannel Channel { get; }
/// <summary>
/// Message which triggered the command
/// </summary>
public abstract IUserMessage Message { get; }
/// <summary>
/// The user who invoked the command
/// </summary>
public abstract IUser User { get; }
/// <summary>
/// Provides access to strings used by this medusa
/// </summary>
public abstract IMedusaStrings Strings { get; }
/// <summary>
/// Gets a formatted localized string using a key and arguments which should be formatted in
/// </summary>
/// <param name="key">The key of the string as specified in localization files</param>
/// <param name="args">Arguments (if any) to format in</param>
/// <returns>A formatted localized string</returns>
public abstract string GetText(string key, object[]? args = null);
/// <summary>
/// Creates a context-aware <see cref="IEmbedBuilder"/> instance
/// (future feature for guild-based embed colors)
/// Any code dealing with embeds should use it for future-proofness
/// instead of manually creating embedbuilder instances
/// </summary>
/// <returns>A context-aware <see cref="IEmbedBuilder"/> instance </returns>
public abstract IEmbedBuilder Embed();
}

View File

@@ -0,0 +1,11 @@
using Discord;
namespace Nadeko.Snake;
/// <summary>
/// Commands which take this type as the first parameter can only be executed in DMs
/// </summary>
public abstract class DmContext : AnyContext
{
public abstract override IDMChannel Channel { get; }
}

View File

@@ -0,0 +1,12 @@
using Discord;
namespace Nadeko.Snake;
/// <summary>
/// Commands which take this type as a first parameter can only be executed in a server
/// </summary>
public abstract class GuildContext : AnyContext
{
public abstract override ITextChannel Channel { get; }
public abstract IGuild Guild { get; }
}

View File

@@ -0,0 +1,8 @@
namespace NadekoBot;
public enum EmbedColor
{
Ok,
Pending,
Error
}

View File

@@ -0,0 +1,14 @@
namespace NadekoBot;
public static class EmbedBuilderExtensions
{
public static IEmbedBuilder WithOkColor(this IEmbedBuilder eb)
=> eb.WithColor(EmbedColor.Ok);
public static IEmbedBuilder WithPendingColor(this IEmbedBuilder eb)
=> eb.WithColor(EmbedColor.Pending);
public static IEmbedBuilder WithErrorColor(this IEmbedBuilder eb)
=> eb.WithColor(EmbedColor.Error);
}

View File

@@ -0,0 +1,66 @@
using Discord;
using Nadeko.Snake;
namespace NadekoBot;
public static class MedusaExtensions
{
public static Task<IUserMessage> EmbedAsync(this IMessageChannel ch, IEmbedBuilder embed, string msg = "")
=> ch.SendMessageAsync(msg,
embed: embed.Build(),
options: new()
{
RetryMode = RetryMode.AlwaysRetry
});
// unlocalized
public static Task<IUserMessage> SendConfirmAsync(this IMessageChannel ch, AnyContext ctx, string msg)
=> ch.EmbedAsync(ctx.Embed().WithOkColor().WithDescription(msg));
public static Task<IUserMessage> SendPendingAsync(this IMessageChannel ch, AnyContext ctx, string msg)
=> ch.EmbedAsync(ctx.Embed().WithPendingColor().WithDescription(msg));
public static Task<IUserMessage> SendErrorAsync(this IMessageChannel ch, AnyContext ctx, string msg)
=> ch.EmbedAsync(ctx.Embed().WithErrorColor().WithDescription(msg));
// unlocalized
public static Task<IUserMessage> SendConfirmAsync(this AnyContext ctx, string msg)
=> ctx.Channel.SendConfirmAsync(ctx, msg);
public static Task<IUserMessage> SendPendingAsync(this AnyContext ctx, string msg)
=> ctx.Channel.SendPendingAsync(ctx, msg);
public static Task<IUserMessage> SendErrorAsync(this AnyContext ctx, string msg)
=> ctx.Channel.SendErrorAsync(ctx, msg);
// localized
public static Task ConfirmAsync(this AnyContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("✅"));
public static Task ErrorAsync(this AnyContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("❌"));
public static Task WarningAsync(this AnyContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("⚠️"));
public static Task WaitAsync(this AnyContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("🤔"));
public static Task<IUserMessage> ErrorLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendErrorAsync(ctx.GetText(key));
public static Task<IUserMessage> PendingLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendPendingAsync(ctx.GetText(key, args));
public static Task<IUserMessage> ConfirmLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendConfirmAsync(ctx.GetText(key, args));
public static Task<IUserMessage> ReplyErrorLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendErrorAsync($"{Format.Bold(ctx.User.ToString())} {ctx.GetText(key)}");
public static Task<IUserMessage> ReplyPendingLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendPendingAsync($"{Format.Bold(ctx.User.ToString())} {ctx.GetText(key)}");
public static Task<IUserMessage> ReplyConfirmLocalizedAsync(this AnyContext ctx, string key, params object[]? args)
=> ctx.SendConfirmAsync($"{Format.Bold(ctx.User.ToString())} {ctx.GetText(key)}");
}

View File

@@ -1,23 +1,17 @@
#nullable disable
using Discord;
namespace NadekoBot;
public interface IEmbedBuilder
{
IEmbedBuilder WithDescription(string desc);
IEmbedBuilder WithDescription(string? desc);
IEmbedBuilder WithTitle(string title);
IEmbedBuilder AddField(string title, object value, bool isInline = false);
IEmbedBuilder WithFooter(string text, string iconUrl = null);
IEmbedBuilder WithAuthor(string name, string iconUrl = null, string url = null);
IEmbedBuilder WithFooter(string text, string? iconUrl = null);
IEmbedBuilder WithAuthor(string name, string? iconUrl = null, string? url = null);
IEmbedBuilder WithColor(EmbedColor color);
Embed Build();
IEmbedBuilder WithUrl(string url);
IEmbedBuilder WithImageUrl(string url);
IEmbedBuilder WithThumbnailUrl(string url);
}
public enum EmbedColor
{
Ok,
Pending,
Error
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<RootNamespace>Nadeko.Snake</RootNamespace>
<Authors>The NadekoBot Team</Authors>
<Version>1.0.2</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Discord.Net.Core" Version="3.5.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,16 @@
namespace Nadeko.Snake;
/// <summary>
/// Overridden to implement parsers for custom types
/// </summary>
/// <typeparam name="T">Type into which to parse the input</typeparam>
public abstract class ParamParser<T>
{
/// <summary>
/// Overridden to implement parsing logic
/// </summary>
/// <param name="ctx">Context</param>
/// <param name="input">Input to parse</param>
/// <returns>A <see cref="ParseResult{T}"/> with successful or failed status</returns>
public abstract ValueTask<ParseResult<T>> TryParseAsync(AnyContext ctx, string input);
}

View File

@@ -0,0 +1,48 @@
namespace Nadeko.Snake;
public readonly struct ParseResult<T>
{
/// <summary>
/// Whether the parsing was successful
/// </summary>
public bool IsSuccess { get; private init; }
/// <summary>
/// Parsed value. It should only have value if <see cref="IsSuccess"/> is set to true
/// </summary>
public T? Data { get; private init; }
/// <summary>
/// Instantiate a **successful** parse result
/// </summary>
/// <param name="data">Parsed value</param>
public ParseResult(T data)
{
Data = data;
IsSuccess = true;
}
/// <summary>
/// Create a new <see cref="ParseResult{T}"/> with IsSuccess = false
/// </summary>
/// <returns>A new <see cref="ParseResult{T}"/></returns>
public static ParseResult<T> Fail()
=> new ParseResult<T>
{
IsSuccess = false,
Data = default,
};
/// <summary>
/// Create a new <see cref="ParseResult{T}"/> with IsSuccess = true
/// </summary>
/// <param name="obj">Value of the parsed object</param>
/// <returns>A new <see cref="ParseResult{T}"/></returns>
public static ParseResult<T> Success(T obj)
=> new ParseResult<T>
{
IsSuccess = true,
Data = obj,
};
}

View File

@@ -0,0 +1 @@
This is the library which is the base of any medusa.

143
src/Nadeko.Medusa/Snek.cs Normal file
View File

@@ -0,0 +1,143 @@
using Discord;
namespace Nadeko.Snake;
/// <summary>
/// The base class which will be loaded as a module into NadekoBot
/// Any user-defined snek has to inherit from this class.
/// Sneks get instantiated ONLY ONCE during the loading,
/// and any snek commands will be executed on the same instance.
/// </summary>
public abstract class Snek : IAsyncDisposable
{
/// <summary>
/// Name of the snek. Defaults to the lowercase class name
/// </summary>
public virtual string Name
=> GetType().Name.ToLowerInvariant();
/// <summary>
/// The prefix required before the command name. For example
/// if you set this to 'test' then a command called 'cmd' will have to be invoked by using
/// '.test cmd' instead of `.cmd`
/// </summary>
public virtual string Prefix
=> string.Empty;
/// <summary>
/// Executed once this snek has been instantiated and before any command is executed.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing completion</returns>
public virtual ValueTask InitializeAsync()
=> default;
/// <summary>
/// Override to cleanup any resources or references which might hold this snek in memory
/// </summary>
/// <returns></returns>
public virtual ValueTask DisposeAsync()
=> default;
/// <summary>
/// This method is called right after the message was received by the bot.
/// You can use this method to make the bot conditionally ignore some messages and prevent further processing.
/// <para>Execution order:</para>
/// <para>
/// *<see cref="ExecOnMessageAsync"/>* →
/// <see cref="ExecInputTransformAsync"/> →
/// <see cref="ExecPreCommandAsync"/> →
/// <see cref="ExecPostCommandAsync"/> OR <see cref="ExecOnNoCommandAsync"/>
/// </para>
/// </summary>
/// <param name="guild">Guild in which the message was sent</param>
/// <param name="msg">Message received by the bot</param>
/// <returns>A <see cref="ValueTask"/> representing whether the message should be ignored and not processed further</returns>
public virtual ValueTask<bool> ExecOnMessageAsync(IGuild? guild, IUserMessage msg)
=> default;
/// <summary>
/// Override this method to modify input before the bot searches for any commands matching the input
/// Executed after <see cref="ExecOnMessageAsync"/>
/// This is useful if you want to reinterpret the message under some conditions
/// <para>Execution order:</para>
/// <para>
/// <see cref="ExecOnMessageAsync"/> →
/// *<see cref="ExecInputTransformAsync"/>* →
/// <see cref="ExecPreCommandAsync"/> →
/// <see cref="ExecPostCommandAsync"/> OR <see cref="ExecOnNoCommandAsync"/>
/// </para>
/// </summary>
/// <param name="guild">Guild in which the message was sent</param>
/// <param name="channel">Channel in which the message was sent</param>
/// <param name="user">User who sent the message</param>
/// <param name="input">Content of the message</param>
/// <returns>A <see cref="ValueTask"/> representing new, potentially modified content</returns>
public virtual ValueTask<string?> ExecInputTransformAsync(
IGuild? guild,
IMessageChannel channel,
IUser user,
string input
)
=> default;
/// <summary>
/// This method is called after the command was found but not executed,
/// and can be used to prevent the command's execution.
/// The command information doesn't have to be from this snek as this method
/// will be called when *any* command from any module or snek was found.
/// You can choose to prevent the execution of the command by returning "true" value.
/// <para>Execution order:</para>
/// <para>
/// <see cref="ExecOnMessageAsync"/> →
/// <see cref="ExecInputTransformAsync"/> →
/// *<see cref="ExecPreCommandAsync"/>* →
/// <see cref="ExecPostCommandAsync"/> OR <see cref="ExecOnNoCommandAsync"/>
/// </para>
/// </summary>
/// <param name="context">Command context</param>
/// <param name="moduleName">Name of the snek or module from which the command originates</param>
/// <param name="commandName">Name of the command which is about to be executed</param>
/// <returns>A <see cref="ValueTask"/> representing whether the execution should be blocked</returns>
public virtual ValueTask<bool> ExecPreCommandAsync(
AnyContext context,
string moduleName,
string commandName
)
=> default;
/// <summary>
/// This method is called after the command was succesfully executed.
/// If this method was called, then <see cref="ExecOnNoCommandAsync"/> will not be executed
/// <para>Execution order:</para>
/// <para>
/// <see cref="ExecOnMessageAsync"/> →
/// <see cref="ExecInputTransformAsync"/> →
/// <see cref="ExecPreCommandAsync"/> →
/// *<see cref="ExecPostCommandAsync"/>* OR <see cref="ExecOnNoCommandAsync"/>
/// </para>
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing completion</returns>
public virtual ValueTask ExecPostCommandAsync(AnyContext ctx, string moduleName, string commandName)
=> default;
/// <summary>
/// This method is called if no command was found for the input.
/// Useful if you want to have games or features which take arbitrary input
/// but ignore any messages which were blocked or caused a command execution
/// If this method was called, then <see cref="ExecPostCommandAsync"/> will not be executed
/// <para>Execution order:</para>
/// <para>
/// <see cref="ExecOnMessageAsync"/> →
/// <see cref="ExecInputTransformAsync"/> →
/// <see cref="ExecPreCommandAsync"/> →
/// <see cref="ExecPostCommandAsync"/> OR *<see cref="ExecOnNoCommandAsync"/>*
/// </para>
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing completion</returns>
public virtual ValueTask ExecOnNoCommandAsync(IGuild? guild, IUserMessage msg)
=> default;
}
public readonly struct ExecResponse
{
}

View File

@@ -0,0 +1,24 @@
using YamlDotNet.Serialization;
namespace Nadeko.Snake;
public readonly struct CommandStrings
{
public CommandStrings(string? desc, string[]? args)
{
Desc = desc;
Args = args;
}
[YamlMember(Alias = "desc")]
public string? Desc { get; init; }
[YamlMember(Alias = "args")]
public string[]? Args { get; init; }
public void Deconstruct(out string? desc, out string[]? args)
{
desc = Desc;
args = Args;
}
}

View File

@@ -0,0 +1,15 @@
using System.Globalization;
namespace Nadeko.Snake;
/// <summary>
/// Defines methods to retrieve and reload medusa strings
/// </summary>
public interface IMedusaStrings
{
// string GetText(string key, ulong? guildId = null, params object[] data);
string? GetText(string key, CultureInfo locale, params object[] data);
void Reload();
CommandStrings GetCommandStrings(string commandName, CultureInfo cultureInfo);
string? GetDescription(CultureInfo? locale);
}

View File

@@ -0,0 +1,28 @@
namespace Nadeko.Snake;
/// <summary>
/// Implemented by classes which provide localized strings in their own ways
/// </summary>
public interface IMedusaStringsProvider
{
/// <summary>
/// Gets localized string
/// </summary>
/// <param name="localeName">Language name</param>
/// <param name="key">String key</param>
/// <returns>Localized string</returns>
string? GetText(string localeName, string key);
/// <summary>
/// Reloads string cache
/// </summary>
void Reload();
// /// <summary>
// /// Gets command arg examples and description
// /// </summary>
// /// <param name="localeName">Language name</param>
// /// <param name="commandName">Command name</param>
// CommandStrings GetCommandStrings(string localeName, string commandName);
CommandStrings? GetCommandStrings(string localeName, string commandName);
}

View File

@@ -0,0 +1,40 @@
namespace Nadeko.Snake;
public class LocalMedusaStringsProvider : IMedusaStringsProvider
{
private readonly StringsLoader _source;
private IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> _responseStrings;
private IReadOnlyDictionary<string, IReadOnlyDictionary<string, CommandStrings>> _commandStrings;
public LocalMedusaStringsProvider(StringsLoader source)
{
_source = source;
_responseStrings = _source.GetResponseStrings();
_commandStrings = _source.GetCommandStrings();
}
public void Reload()
{
_responseStrings = _source.GetResponseStrings();
_commandStrings = _source.GetCommandStrings();
}
public string? GetText(string localeName, string key)
{
if (_responseStrings.TryGetValue(localeName.ToLowerInvariant(), out var langStrings)
&& langStrings.TryGetValue(key.ToLowerInvariant(), out var text))
return text;
return null;
}
public CommandStrings? GetCommandStrings(string localeName, string commandName)
{
if (_commandStrings.TryGetValue(localeName.ToLowerInvariant(), out var langStrings)
&& langStrings.TryGetValue(commandName.ToLowerInvariant(), out var strings))
return strings;
return null;
}
}

View File

@@ -0,0 +1,79 @@
using System.Globalization;
using Serilog;
namespace Nadeko.Snake;
public class MedusaStrings : IMedusaStrings
{
/// <summary>
/// Used as failsafe in case response key doesn't exist in the selected or default language.
/// </summary>
private readonly CultureInfo _usCultureInfo = new("en-US");
private readonly IMedusaStringsProvider _stringsProvider;
public MedusaStrings(IMedusaStringsProvider stringsProvider)
{
_stringsProvider = stringsProvider;
}
private string? GetString(string key, CultureInfo cultureInfo)
=> _stringsProvider.GetText(cultureInfo.Name, key);
public string? GetText(string key, CultureInfo cultureInfo)
=> GetString(key, cultureInfo)
?? GetString(key, _usCultureInfo);
public string? GetText(string key, CultureInfo cultureInfo, params object[] data)
{
var text = GetText(key, cultureInfo);
if (string.IsNullOrWhiteSpace(text))
return null;
try
{
return string.Format(text, data);
}
catch (FormatException)
{
Log.Warning(" Key '{Key}' is not properly formatted in '{LanguageName}' response strings",
key,
cultureInfo.Name);
return $"⚠️ Response string key '{key}' is not properly formatted. Please report this.\n\n{text}";
}
}
public CommandStrings GetCommandStrings(string commandName, CultureInfo cultureInfo)
{
var cmdStrings = _stringsProvider.GetCommandStrings(cultureInfo.Name, commandName);
if (cmdStrings is null)
{
if (cultureInfo.Name == _usCultureInfo.Name)
{
Log.Warning("'{CommandName}' doesn't exist in 'en-US' command strings for one of the medusae",
commandName);
return new(null, null);
}
Log.Information("Missing '{CommandName}' command strings for the '{LocaleName}' locale",
commandName,
cultureInfo.Name);
return GetCommandStrings(commandName, _usCultureInfo);
}
return cmdStrings.Value;
}
public string? GetDescription(CultureInfo? locale = null)
=> GetText("medusa.description", locale ?? _usCultureInfo);
public static MedusaStrings CreateDefault(string basePath)
=> new MedusaStrings(new LocalMedusaStringsProvider(new(basePath)));
public void Reload()
=> _stringsProvider.Reload();
}

View File

@@ -0,0 +1,137 @@
using System.Diagnostics.CodeAnalysis;
using Serilog;
using YamlDotNet.Serialization;
namespace Nadeko.Snake;
/// <summary>
/// Loads strings from the shortcut or localizable path
/// </summary>
public class StringsLoader
{
private readonly string _localizableResponsesPath;
private readonly string _shortcutResponsesFile;
private readonly string _localizableCommandsPath;
private readonly string _shortcutCommandsFile;
public StringsLoader(string basePath)
{
_localizableResponsesPath = Path.Join(basePath, "strings/res");
_shortcutResponsesFile = Path.Join(basePath, "res.yml");
_localizableCommandsPath = Path.Join(basePath, "strings/cmds");
_shortcutCommandsFile = Path.Join(basePath, "cmds.yml");
}
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, CommandStrings>> GetCommandStrings()
{
var outputDict = new Dictionary<string, IReadOnlyDictionary<string, CommandStrings>>();
if (File.Exists(_shortcutCommandsFile))
{
if (TryLoadCommandsFromFile(_shortcutCommandsFile, out var dict, out _))
{
outputDict["en-us"] = dict;
}
return outputDict;
}
if (Directory.Exists(_localizableCommandsPath))
{
foreach (var cmdsFile in Directory.EnumerateFiles(_localizableCommandsPath))
{
if (TryLoadCommandsFromFile(cmdsFile, out var dict, out var locale) && locale is not null)
{
outputDict[locale.ToLowerInvariant()] = dict;
}
}
}
return outputDict;
}
private static readonly IDeserializer _deserializer = new DeserializerBuilder().Build();
private static bool TryLoadCommandsFromFile(string file,
[NotNullWhen(true)] out IReadOnlyDictionary<string, CommandStrings>? strings,
out string? localeName)
{
try
{
var text = File.ReadAllText(file);
strings = _deserializer.Deserialize<Dictionary<string, CommandStrings>?>(text)
?? new();
localeName = GetLocaleName(file);
return true;
}
catch (Exception ex)
{
Log.Error(ex, "Error loading {FileName} command strings: {ErrorMessage}", file, ex.Message);
}
strings = null;
localeName = null;
return false;
}
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> GetResponseStrings()
{
var outputDict = new Dictionary<string, IReadOnlyDictionary<string, string>>();
// try to load a shortcut file
if (File.Exists(_shortcutResponsesFile))
{
if (TryLoadResponsesFromFile(_shortcutResponsesFile, out var dict, out _))
{
outputDict["en-us"] = dict;
}
return outputDict;
}
if (!Directory.Exists(_localizableResponsesPath))
return outputDict;
// if shortcut file doesn't exist, try to load localizable files
foreach (var file in Directory.GetFiles(_localizableResponsesPath))
{
if (TryLoadResponsesFromFile(file, out var strings, out var localeName) && localeName is not null)
{
outputDict[localeName.ToLowerInvariant()] = strings;
}
}
return outputDict;
}
private static bool TryLoadResponsesFromFile(string file,
[NotNullWhen(true)] out IReadOnlyDictionary<string, string>? strings,
out string? localeName)
{
try
{
strings = _deserializer.Deserialize<Dictionary<string, string>?>(File.ReadAllText(file));
if (strings is null)
{
localeName = null;
return false;
}
localeName = GetLocaleName(file).ToLowerInvariant();
return true;
}
catch (Exception ex)
{
Log.Error(ex, "Error loading {FileName} response strings: {ErrorMessage}", file, ex.Message);
strings = null;
localeName = null;
return false;
}
}
private static string GetLocaleName(string fileName)
=> Path.GetFileNameWithoutExtension(fileName);
}

View File

@@ -0,0 +1,2 @@
dotnet pack -o bin/Release/packed
dotnet nuget push bin/Release/packed/ --api-key $env:nadeko_myget_api_key --source https://www.myget.org/F/nadeko/api/v2/package

View File

@@ -15,6 +15,9 @@ namespace NadekoBot.Services
.MinimumLevel.Override("System", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File("coord.log", LogEventLevel.Information,
rollOnFileSizeLimit: true,
fileSizeLimitBytes: 10_000_000)
.WriteTo.Console(LogEventLevel.Information,
theme: GetTheme(),
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] | #{LogSource} | {Message:lj}{NewLine}{Exception}")

View File

@@ -9,9 +9,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.42.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.44.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

View File

@@ -114,14 +114,6 @@ namespace NadekoBot.Coordinator
StartShard(shardId);
break;
}
if (status.Process is null or {HasExited: true})
{
Log.Warning("Shard {ShardId} is starting (process)...", shardId);
hadAction = true;
StartShard(shardId);
break;
}
if (DateTime.UtcNow - status.LastUpdate >
TimeSpan.FromSeconds(_config.UnresponsiveSec))
@@ -139,6 +131,24 @@ namespace NadekoBot.Coordinator
StartShard(shardId);
break;
}
try
{
if (status.Process is null or { HasExited: true })
{
Log.Warning("Shard {ShardId} is starting (process)...", shardId);
hadAction = true;
StartShard(shardId);
break;
}
}
catch (InvalidOperationException)
{
Log.Warning("Process for shard {ShardId} is bugged... ", shardId);
hadAction = true;
StartShard(shardId);
break;
}
}
}
@@ -161,17 +171,13 @@ namespace NadekoBot.Coordinator
var status = _shardStatuses[shardId];
try
{
if (status.Process is { HasExited: false } p)
{
try
{
p.Kill(true);
}
catch
{
}
}
status.Process?.Kill(true);
}
catch
{
}
try
{
status.Process?.Dispose();
}
catch
@@ -280,10 +286,10 @@ namespace NadekoBot.Coordinator
for (var shardId = 0; shardId < _shardStatuses.Length; shardId++)
{
var status = _shardStatuses[shardId];
if (status.Process is { } p)
if (status.Process is Process p)
{
p.Kill();
p.Dispose();
try{p.Kill();} catch {}
try{p.Dispose();} catch {}
_shardStatuses[shardId] = status with
{
Process = null,

View File

@@ -97,13 +97,12 @@ public class CmdAttribute : System.Attribute
var name = $"{model.Namespace}.{string.Join(".", model.ClassHierarchy)}.g.cs";
try
{
Debug.WriteLine($"Writing {name}");
var source = GetSourceText(model);
ctx.AddSource(name, SourceText.From(source, Encoding.UTF8));
}
catch (Exception ex)
{
Debug.WriteLine($"Error writing source file {name}\n" + ex);
Console.WriteLine($"Error writing source file {name}\n" + ex);
}
}
}

View File

@@ -62,6 +62,7 @@ namespace NadekoBot.Generators
sw.WriteLine("{");
sw.Indent++;
var typedParamStrings = new List<string>(10);
foreach (var field in fields)
{
var matches = Regex.Matches(field.Value, @"{(?<num>\d)[}:]");
@@ -71,20 +72,30 @@ namespace NadekoBot.Generators
max = Math.Max(max, int.Parse(match.Groups["num"].Value) + 1);
}
List<string> typedParamStrings = new List<string>();
var paramStrings = string.Empty;
typedParamStrings.Clear();
var typeParams = new string[max];
var passedParamString = string.Empty;
for (var i = 0; i < max; i++)
{
typedParamStrings.Add($"object p{i}");
paramStrings += $", p{i}";
typedParamStrings.Add($"in T{i} p{i}");
passedParamString += $", p{i}";
typeParams[i] = $"T{i}";
}
var sig = string.Empty;
if(max > 0)
var typeParamStr = string.Empty;
if (max > 0)
{
sig = $"({string.Join(", ", typedParamStrings)})";
sw.WriteLine($"public static LocStr {field.Name}{sig} => new LocStr(\"{field.Name}\"{paramStrings});");
typeParamStr = $"<{string.Join(", ", typeParams)}>";
}
sw.WriteLine("public static LocStr {0}{1}{2} => new LocStr(\"{3}\"{4});",
field.Name,
typeParamStr,
sig,
field.Name,
passedParamString);
}
sw.Indent--;

View File

@@ -41,10 +41,7 @@ public sealed class Bot
_credsProvider = new BotCredsProvider(totalShards);
_creds = _credsProvider.GetCreds();
_db = new(_creds);
if (shardId == 0)
_db.Setup();
_db = new(_credsProvider);
var messageCacheSize =
#if GLOBAL_NADEKO
@@ -52,6 +49,9 @@ public sealed class Bot
#else
50;
#endif
if(!_creds.UsePrivilegedIntents)
Log.Warning("You are not using privileged intents. Some features will not work properly");
Client = new(new()
{
@@ -63,21 +63,22 @@ public sealed class Bot
AlwaysDownloadUsers = false,
AlwaysResolveStickers = false,
AlwaysDownloadDefaultStickers = false,
GatewayIntents = GatewayIntents.All,
LogGatewayIntentWarnings = false
GatewayIntents = _creds.UsePrivilegedIntents
? GatewayIntents.All
: GatewayIntents.AllUnprivileged,
LogGatewayIntentWarnings = false,
FormatUsersInBidirectionalUnicode = false,
});
_commandService = new(new()
{
CaseSensitiveCommands = false,
DefaultRunMode = RunMode.Sync
DefaultRunMode = RunMode.Sync,
});
// _interactionService = new(Client.Rest);
#if GLOBAL_NADEKO || DEBUG
Client.Log += Client_Log;
#endif
}
@@ -116,8 +117,6 @@ public sealed class Bot
// admin
#if GLOBAL_NADEKO
svcs.AddSingleton<ILogCommandService, DummyLogCommandService>();
#else
svcs.AddSingleton<ILogCommandService, LogCommandService>();
#endif
svcs.AddHttpClient();
@@ -149,12 +148,14 @@ public sealed class Bot
typeof(INService),
// behaviours
typeof(IEarlyBehavior),
typeof(ILateBlocker),
typeof(IExecOnMessage),
typeof(IInputTransformer),
typeof(ILateExecutor))
typeof(IExecPreCommand),
typeof(IExecPostCommand),
typeof(IExecNoCommand))
.WithoutAttribute<DontAddToIocContainerAttribute>()
#if GLOBAL_NADEKO
.WithoutAttribute<NoPublicBotAttribute>()
.WithoutAttribute<NoPublicBotAttribute>()
#endif
)
.AsSelfWithInterfaces()
@@ -162,8 +163,7 @@ public sealed class Bot
//initialize Services
Services = svcs.BuildServiceProvider();
var exec = Services.GetRequiredService<IBehaviourExecutor>();
exec.Initialize();
Services.GetRequiredService<IBehaviorHandler>().Initialize();
if (Client.ShardId == 0)
ApplyConfigMigrations();
@@ -216,30 +216,28 @@ public sealed class Bot
private async Task LoginAsync(string token)
{
var clientReady = new TaskCompletionSource<bool>();
var clientReady = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
Task SetClientReady()
async Task SetClientReady()
{
_ = Task.Run(async () =>
clientReady.TrySetResult(true);
try
{
clientReady.TrySetResult(true);
try
{
foreach (var chan in await Client.GetDMChannelsAsync())
await chan.CloseAsync();
}
catch
{
// ignored
}
});
return Task.CompletedTask;
foreach (var chan in await Client.GetDMChannelsAsync())
await chan.CloseAsync();
}
catch
{
// ignored
}
}
//connect
Log.Information("Shard {ShardId} logging in ...", Client.ShardId);
try
{
Client.Ready += SetClientReady;
await Client.LoginAsync(TokenType.Bot, token);
await Client.StartAsync();
}
@@ -253,11 +251,10 @@ public sealed class Bot
LoginErrorHandler.Handle(ex);
Helpers.ReadErrorAndExit(4);
}
Client.Ready += SetClientReady;
await clientReady.Task;
await clientReady.Task.ConfigureAwait(false);
Client.Ready -= SetClientReady;
Client.JoinedGuild += Client_JoinedGuild;
Client.LeftGuild += Client_LeftGuild;
@@ -288,6 +285,9 @@ public sealed class Bot
public async Task RunAsync()
{
if (ShardId == 0)
await _db.SetupAsync();
var sw = Stopwatch.StartNew();
await LoginAsync(_creds.Token);
@@ -341,11 +341,40 @@ public sealed class Bot
private Task Client_Log(LogMessage arg)
{
if (arg.Message?.Contains("unknown dispatch", StringComparison.InvariantCultureIgnoreCase) ?? false)
return Task.CompletedTask;
if (arg.Exception is { InnerException: WebSocketClosedException { CloseCode: 4014 } })
{
Log.Error(@"
Login failed.
*** Please enable privileged intents ***
Certain Nadeko features require Discord's privileged gateway intents.
These include greeting and goodbye messages, as well as creating the Owner message channels for DM forwarding.
How to enable privileged intents:
1. Head over to the Discord Developer Portal https://discord.com/developers/applications/
2. Select your Application.
3. Click on `Bot` in the left side navigation panel, and scroll down to the intents section.
4. Enable all intents.
5. Restart your bot.
Read this only if your bot is in 100 or more servers:
You'll need to apply to use the intents with Discord, but for small selfhosts, all that is required is enabling the intents in the developer portal.
Yes, this is a new thing from Discord, as of October 2020. No, there's nothing we can do about it. Yes, we're aware it worked before.
While waiting for your bot to be accepted, you can change the 'usePrivilegedIntents' inside your creds.yml to 'false', although this will break many of the nadeko's features");
return Task.CompletedTask;
}
#if GLOBAL_NADEKO || DEBUG
if (arg.Exception is not null)
Log.Warning(arg.Exception, "{ErrorSource} | {ErrorMessage}", arg.Source, arg.Message);
else
Log.Warning("{ErrorSource} | {ErrorMessage}", arg.Source, arg.Message);
#endif
return Task.CompletedTask;
}

View File

@@ -14,12 +14,15 @@ public sealed class Creds : IBotCredentials
[Comment(@"List of Ids of the users who have bot owner permissions
**DO NOT ADD PEOPLE YOU DON'T TRUST**")]
public ICollection<ulong> OwnerIds { get; set; }
[Comment("Keep this on 'true' unless you're sure your bot shouldn't use privileged intents or you're waiting to be accepted")]
public bool UsePrivilegedIntents { get; set; }
[Comment(@"The number of shards that the bot will running on.
Leave at 1 if you don't know what you're doing.")]
public int TotalShards { get; set; }
[Comment(
[Comment(
@"Login to https://console.cloud.google.com, create a new project, go to APIs & Services -> Library -> YouTube Data API and enable it.
Then, go to APIs and Services -> Credentials and click Create credentials -> API key.
Used only for Youtube Data Api (at the moment).")]
@@ -96,8 +99,9 @@ Windows default
public Creds()
{
Version = 3;
Version = 5;
Token = string.Empty;
UsePrivilegedIntents = true;
OwnerIds = new List<ulong>();
TotalShards = 1;
GoogleApiKey = string.Empty;
@@ -120,10 +124,15 @@ Windows default
public class DbOptions
{
[Comment(@"Database type. Only sqlite supported atm")]
[Comment(@"Database type. ""sqlite"", ""mysql"" and ""postgresql"" are supported.
Default is ""sqlite""")]
public string Type { get; set; }
[Comment(@"Connection string. Will default to ""Data Source=data/NadekoBot.db""")]
[Comment(@"Database connection string.
You MUST change this if you're not using ""sqlite"" type.
Default is ""Data Source=data/NadekoBot.db""
Example for mysql: ""Server=localhost;Port=3306;Uid=root;Pwd=my_super_secret_mysql_password;Database=nadeko""
Example for postgresql: ""Server=localhost;Port=5432;User Id=postgres;Password=my_super_secret_postgres_password;Database=nadeko;""")]
public string ConnectionString { get; set; }
}

View File

@@ -6,6 +6,7 @@ public interface IBotCredentials
string Token { get; }
string GoogleApiKey { get; }
ICollection<ulong> OwnerIds { get; }
bool UsePrivilegedIntents { get; }
string RapidApiKey { get; }
Creds.DbOptions Db { get; }

View File

@@ -0,0 +1,27 @@
namespace NadekoBot;
public sealed class NadekoActionInteraction : NadekoOwnInteraction
{
private readonly NadekoInteractionData _data;
private readonly Func<SocketMessageComponent, Task> _action;
public NadekoActionInteraction(
DiscordSocketClient client,
ulong authorId,
NadekoInteractionData data,
Func<SocketMessageComponent, Task> action
)
: base(client, authorId)
{
_data = data;
_action = action;
}
public override string Name
=> _data.CustomId;
public override IEmote Emote
=> _data.Emote;
public override Task ExecuteOnActionAsync(SocketMessageComponent smc)
=> _action(smc);
}

View File

@@ -0,0 +1,77 @@
namespace NadekoBot;
public abstract class NadekoInteraction
{
// improvements:
// - state in OnAction
// - configurable delay
// -
public abstract string Name { get; }
public abstract IEmote Emote { get; }
protected readonly DiscordSocketClient _client;
protected readonly TaskCompletionSource<bool> _interactionCompletedSource;
protected IUserMessage message = null!;
protected NadekoInteraction(DiscordSocketClient client)
{
_client = client;
_interactionCompletedSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
}
public async Task RunAsync(IUserMessage msg)
{
message = msg;
_client.InteractionCreated += OnInteraction;
await Task.WhenAny(Task.Delay(10_000), _interactionCompletedSource.Task);
_client.InteractionCreated -= OnInteraction;
await msg.ModifyAsync(m => m.Components = new ComponentBuilder().Build());
}
protected abstract ValueTask<bool> Validate(SocketMessageComponent smc);
private async Task OnInteraction(SocketInteraction arg)
{
if (arg is not SocketMessageComponent smc)
return;
if (smc.Message.Id != message.Id)
return;
if (smc.Data.CustomId != Name)
return;
if (!await Validate(smc))
{
await smc.DeferAsync();
return;
}
_ = Task.Run(async () =>
{
await ExecuteOnActionAsync(smc);
// this should only be a thing on single-response buttons
_interactionCompletedSource.TrySetResult(true);
if (!smc.HasResponded)
{
await smc.DeferAsync();
}
});
}
public MessageComponent CreateComponent()
{
var comp = new ComponentBuilder()
.WithButton(new ButtonBuilder(style: ButtonStyle.Secondary, emote: Emote, customId: Name));
return comp.Build();
}
public abstract Task ExecuteOnActionAsync(SocketMessageComponent smc);
}

View File

@@ -0,0 +1,41 @@
namespace NadekoBot;
/// <summary>
/// Builder class for NadekoInteractions
/// </summary>
public class NadekoInteractionBuilder
{
private NadekoInteractionData? iData;
private Func<SocketMessageComponent, Task>? action;
// private bool isOwn;
public NadekoInteractionBuilder WithData<T>(in T data)
where T : NadekoInteractionData
{
iData = data;
return this;
}
// public NadekoOwnInteractionBuiler WithIsOwn(bool isOwn = true)
// {
// this.isOwn = isOwn;
// return this;
// }
public NadekoInteractionBuilder WithAction(in Func<SocketMessageComponent, Task> fn)
{
this.action = fn;
return this;
}
public NadekoActionInteraction Build(DiscordSocketClient client, ulong userId)
{
if (iData is null)
throw new InvalidOperationException("You have to specify the data before building the interaction");
if (action is null)
throw new InvalidOperationException("You have to specify the action before building the interaction");
return new(client, userId, iData, action);
}
}

View File

@@ -0,0 +1,8 @@
namespace NadekoBot;
/// <summary>
/// Represents essential interacation data
/// </summary>
/// <param name="Emote">Emote which will show on a button</param>
/// <param name="CustomId">Custom interaction id</param>
public record NadekoInteractionData(IEmote Emote, string CustomId);

View File

@@ -0,0 +1,15 @@
namespace NadekoBot;
/// <summary>
/// Interaction which only the author can use
/// </summary>
public abstract class NadekoOwnInteraction : NadekoInteraction
{
protected readonly ulong _authorId;
protected NadekoOwnInteraction(DiscordSocketClient client, ulong authorId) : base(client)
=> _authorId = authorId;
protected override ValueTask<bool> Validate(SocketMessageComponent smc)
=> new(smc.User.Id == _authorId);
}

View File

@@ -0,0 +1,16 @@
#nullable disable
using LinqToDB;
using System.Linq.Expressions;
namespace NadekoBot.Common;
public static class Linq2DbExpressions
{
[ExpressionMethod(nameof(GuildOnShardExpression))]
public static bool GuildOnShard(ulong guildId, int totalShards, int shardId)
=> throw new NotSupportedException();
private static Expression<Func<ulong, int, int, bool>> GuildOnShardExpression()
=> (guildId, totalShards, shardId)
=> guildId / 4194304 % (ulong)totalShards == (ulong)shardId;
}

View File

@@ -0,0 +1,76 @@
#nullable enable
[DontAddToIocContainer]
public sealed class BehaviorAdapter : ICustomBehavior
{
private readonly WeakReference<Snek> _snekWr;
private readonly IMedusaStrings _strings;
private readonly IServiceProvider _services;
private readonly string _name;
// unused
public int Priority
=> 0;
public BehaviorAdapter(WeakReference<Snek> snekWr, IMedusaStrings strings, IServiceProvider services)
{
_snekWr = snekWr;
_strings = strings;
_services = services;
_name = snekWr.TryGetTarget(out var snek)
? $"snek/{snek.GetType().Name}"
: "unknown";
}
public async Task<bool> ExecPreCommandAsync(ICommandContext context, string moduleName, CommandInfo command)
{
if (!_snekWr.TryGetTarget(out var snek))
return false;
return await snek.ExecPreCommandAsync(ContextAdapterFactory.CreateNew(context, _strings, _services),
moduleName,
command.Name);
}
public async Task<bool> ExecOnMessageAsync(IGuild? guild, IUserMessage msg)
{
if (!_snekWr.TryGetTarget(out var snek))
return false;
return await snek.ExecOnMessageAsync(guild, msg);
}
public async Task<string?> TransformInput(
IGuild guild,
IMessageChannel channel,
IUser user,
string input)
{
if (!_snekWr.TryGetTarget(out var snek))
return null;
return await snek.ExecInputTransformAsync(guild, channel, user, input);
}
public async Task ExecOnNoCommandAsync(IGuild? guild, IUserMessage msg)
{
if (!_snekWr.TryGetTarget(out var snek))
return;
await snek.ExecOnNoCommandAsync(guild, msg);
}
public async ValueTask ExecPostCommandAsync(ICommandContext context, string moduleName, string commandName)
{
if (!_snekWr.TryGetTarget(out var snek))
return;
await snek.ExecPostCommandAsync(ContextAdapterFactory.CreateNew(context, _strings, _services),
moduleName,
commandName);
}
public override string ToString()
=> _name;
}

View File

@@ -0,0 +1,7 @@
internal class ContextAdapterFactory
{
public static AnyContext CreateNew(ICommandContext context, IMedusaStrings strings, IServiceProvider services)
=> context.Guild is null
? new DmContextAdapter(context, strings, services)
: new GuildContextAdapter(context, strings, services);
}

View File

@@ -0,0 +1,48 @@
using Microsoft.Extensions.DependencyInjection;
public sealed class DmContextAdapter : DmContext
{
public override IMedusaStrings Strings { get; }
public override IDMChannel Channel { get; }
public override IUserMessage Message { get; }
public override IUser User
=> Message.Author;
private readonly IServiceProvider _services;
private readonly Lazy<IEmbedBuilderService> _ebs;
private readonly Lazy<IBotStrings> _botStrings;
private readonly Lazy<ILocalization> _localization;
public DmContextAdapter(ICommandContext ctx, IMedusaStrings strings, IServiceProvider services)
{
if (ctx is not { Channel: IDMChannel ch })
{
throw new ArgumentException("Can't use non-dm context to create DmContextAdapter", nameof(ctx));
}
Strings = strings;
_services = services;
Channel = ch;
Message = ctx.Message;
_ebs = new(_services.GetRequiredService<IEmbedBuilderService>());
_botStrings = new(_services.GetRequiredService<IBotStrings>);
_localization = new(_services.GetRequiredService<ILocalization>());
}
public override IEmbedBuilder Embed()
=> _ebs.Value.Create();
public override string GetText(string key, object[]? args = null)
{
var cultureInfo = _localization.Value.GetCultureInfo(default(ulong?));
var output = Strings.GetText(key, cultureInfo, args ?? Array.Empty<object>());
if (!string.IsNullOrWhiteSpace(output))
return output;
return _botStrings.Value.GetText(key, cultureInfo, args);
}
}

View File

@@ -0,0 +1,51 @@
using Microsoft.Extensions.DependencyInjection;
public sealed class GuildContextAdapter : GuildContext
{
private readonly IServiceProvider _services;
private readonly ICommandContext _ctx;
private readonly Lazy<IEmbedBuilderService> _ebs;
private readonly Lazy<IBotStrings> _botStrings;
private readonly Lazy<ILocalization> _localization;
public override IMedusaStrings Strings { get; }
public override IGuild Guild { get; }
public override ITextChannel Channel { get; }
public override IUserMessage Message
=> _ctx.Message;
public override IGuildUser User { get; }
public override IEmbedBuilder Embed()
=> _ebs.Value.Create();
public GuildContextAdapter(ICommandContext ctx, IMedusaStrings strings, IServiceProvider services)
{
if (ctx.Guild is not IGuild guild || ctx.Channel is not ITextChannel channel)
{
throw new ArgumentException("Can't use non-guild context to create GuildContextAdapter", nameof(ctx));
}
Strings = strings;
User = (IGuildUser)ctx.User;
_services = services;
_ebs = new(_services.GetRequiredService<IEmbedBuilderService>());
_botStrings = new(_services.GetRequiredService<IBotStrings>);
_localization = new(_services.GetRequiredService<ILocalization>());
(_ctx, Guild, Channel) = (ctx, guild, channel);
}
public override string GetText(string key, object[]? args = null)
{
args ??= Array.Empty<object>();
var cultureInfo = _localization.Value.GetCultureInfo(_ctx.Guild.Id);
var output = Strings.GetText(key, cultureInfo, args);
if (!string.IsNullOrWhiteSpace(output))
return output;
return _botStrings.Value.GetText(key, cultureInfo, args);
}
}

View File

@@ -0,0 +1,32 @@
public sealed class ParamParserAdapter<T> : TypeReader
{
private readonly ParamParser<T> _parser;
private readonly IMedusaStrings _strings;
private readonly IServiceProvider _services;
public ParamParserAdapter(ParamParser<T> parser,
IMedusaStrings strings,
IServiceProvider services)
{
_parser = parser;
_strings = strings;
_services = services;
}
public override async Task<Discord.Commands.TypeReaderResult> ReadAsync(
ICommandContext context,
string input,
IServiceProvider services)
{
var medusaContext = ContextAdapterFactory.CreateNew(context,
_strings,
_services);
var result = await _parser.TryParseAsync(medusaContext, input);
if(result.IsSuccess)
return Discord.Commands.TypeReaderResult.FromSuccess(result.Data);
return Discord.Commands.TypeReaderResult.FromError(CommandError.Unsuccessful, "Invalid input");
}
}

View File

@@ -0,0 +1,27 @@
namespace Nadeko.Medusa;
/// <summary>
/// Enum specifying in which context the command can be executed
/// </summary>
public enum CommandContextType
{
/// <summary>
/// Command can only be executed in a guild
/// </summary>
Guild,
/// <summary>
/// Command can only be executed in DMs
/// </summary>
Dm,
/// <summary>
/// Command can be executed anywhere
/// </summary>
Any,
/// <summary>
/// Command can be executed anywhere, and it doesn't require context to be passed to it
/// </summary>
Unspecified
}

View File

@@ -0,0 +1,8 @@
namespace Nadeko.Medusa;
public interface IMedusaConfigService
{
IReadOnlyCollection<string> GetLoadedMedusae();
void AddLoadedMedusa(string name);
void RemoveLoadedMedusa(string name);
}

View File

@@ -0,0 +1,20 @@
#nullable enable
using Cloneable;
using NadekoBot.Common.Yml;
namespace Nadeko.Medusa;
[Cloneable]
public sealed partial class MedusaConfig : ICloneable<MedusaConfig>
{
[Comment(@"DO NOT CHANGE")]
public int Version { get; set; } = 1;
[Comment("List of medusae automatically loaded at startup")]
public List<string>? Loaded { get; set; }
public MedusaConfig()
{
Loaded = new();
}
}

View File

@@ -0,0 +1,49 @@
using NadekoBot.Common.Configs;
namespace Nadeko.Medusa;
public sealed class MedusaConfigService : ConfigServiceBase<MedusaConfig>, IMedusaConfigService
{
private const string FILE_PATH = "data/medusae/medusa.yml";
private static readonly TypedKey<MedusaConfig> _changeKey = new("config.medusa.updated");
public override string Name
=> "medusa";
public MedusaConfigService(
IConfigSeria serializer,
IPubSub pubSub)
: base(FILE_PATH, serializer, pubSub, _changeKey)
{
}
public IReadOnlyCollection<string> GetLoadedMedusae()
=> Data.Loaded?.ToList() ?? new List<string>();
public void AddLoadedMedusa(string name)
{
name = name.Trim().ToLowerInvariant();
ModifyConfig(conf =>
{
if (conf.Loaded is null)
conf.Loaded = new();
if(!conf.Loaded.Contains(name))
conf.Loaded.Add(name);
});
}
public void RemoveLoadedMedusa(string name)
{
name = name.Trim().ToLowerInvariant();
ModifyConfig(conf =>
{
if (conf.Loaded is null)
conf.Loaded = new();
conf.Loaded.Remove(name);
});
}
}

View File

@@ -0,0 +1,23 @@
using System.Globalization;
namespace Nadeko.Medusa;
public interface IMedusaLoaderService
{
Task<MedusaLoadResult> LoadMedusaAsync(string medusaName);
Task<MedusaUnloadResult> UnloadMedusaAsync(string medusaName);
string GetCommandDescription(string medusaName, string commandName, CultureInfo culture);
string[] GetCommandExampleArgs(string medusaName, string commandName, CultureInfo culture);
Task ReloadStrings();
IReadOnlyCollection<string> GetAllMedusae();
IReadOnlyCollection<MedusaStats> GetLoadedMedusae(CultureInfo? cultureInfo = null);
}
public sealed record MedusaStats(string Name,
string? Description,
IReadOnlyCollection<SnekStats> Sneks);
public sealed record SnekStats(string Name,
IReadOnlyCollection<SnekCommandStats> Commands);
public sealed record SnekCommandStats(string Name);

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.Loader;
namespace Nadeko.Medusa;
public sealed class MedusaAssemblyLoadContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver _depResolver;
public MedusaAssemblyLoadContext(string pluginPath) : base(isCollectible: true)
{
_depResolver = new(pluginPath);
}
protected override Assembly? Load(AssemblyName assemblyName)
{
var assemblyPath = _depResolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}
return null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
var libraryPath = _depResolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}
return IntPtr.Zero;
}
}

View File

@@ -0,0 +1,877 @@
using Discord.Commands.Builders;
using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Common.ModuleBehaviors;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Nadeko.Medusa;
// ReSharper disable RedundantAssignment
public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor, INService
{
private readonly CommandService _cmdService;
private readonly IServiceProvider _botServices;
private readonly IBehaviorHandler _behHandler;
private readonly IPubSub _pubSub;
private readonly IMedusaConfigService _medusaConfig;
private readonly ConcurrentDictionary<string, ResolvedMedusa> _resolved = new();
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
private readonly TypedKey<string> _loadKey = new("medusa:load");
private readonly TypedKey<string> _unloadKey = new("medusa:unload");
private readonly TypedKey<bool> _stringsReload = new("medusa:reload_strings");
private const string BASE_DIR = "data/medusae";
public MedusaLoaderService(CommandService cmdService,
IServiceProvider botServices,
IBehaviorHandler behHandler,
IPubSub pubSub,
IMedusaConfigService medusaConfig)
{
_cmdService = cmdService;
_botServices = botServices;
_behHandler = behHandler;
_pubSub = pubSub;
_medusaConfig = medusaConfig;
// has to be done this way to support this feature on sharded bots
_pubSub.Sub(_loadKey, async name => await InternalLoadAsync(name));
_pubSub.Sub(_unloadKey, async name => await InternalUnloadAsync(name));
_pubSub.Sub(_stringsReload, async _ => await ReloadStringsInternal());
}
public IReadOnlyCollection<string> GetAllMedusae()
{
if (!Directory.Exists(BASE_DIR))
return Array.Empty<string>();
return Directory.GetDirectories(BASE_DIR)
.Select(x => Path.GetRelativePath(BASE_DIR, x))
.ToArray();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public IReadOnlyCollection<MedusaStats> GetLoadedMedusae(CultureInfo? culture)
{
var toReturn = new List<MedusaStats>(_resolved.Count);
foreach (var (name, resolvedData) in _resolved)
{
var sneks = new List<SnekStats>(resolvedData.SnekInfos.Count);
foreach (var snekInfos in resolvedData.SnekInfos.Concat(resolvedData.SnekInfos.SelectMany(x => x.Subsneks)))
{
var commands = new List<SnekCommandStats>();
foreach (var command in snekInfos.Commands)
{
commands.Add(new SnekCommandStats(command.Aliases.First()));
}
sneks.Add(new SnekStats(snekInfos.Name, commands));
}
toReturn.Add(new MedusaStats(name, resolvedData.Strings.GetDescription(culture), sneks));
}
return toReturn;
}
public async Task OnReadyAsync()
{
foreach (var name in _medusaConfig.GetLoadedMedusae())
{
var result = await InternalLoadAsync(name);
if(result != MedusaLoadResult.Success)
Log.Warning("Unable to load '{MedusaName}' medusa", name);
else
Log.Warning("Loaded medusa '{MedusaName}'", name);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<MedusaLoadResult> LoadMedusaAsync(string medusaName)
{
// try loading on this shard first to see if it works
var res = await InternalLoadAsync(medusaName);
if (res == MedusaLoadResult.Success)
{
// if it does publish it so that other shards can load the medusa too
// this method will be ran twice on this shard but it doesn't matter as
// the second attempt will be ignored
await _pubSub.Pub(_loadKey, medusaName);
}
return res;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public async Task<MedusaUnloadResult> UnloadMedusaAsync(string medusaName)
{
var res = await InternalUnloadAsync(medusaName);
if (res == MedusaUnloadResult.Success)
{
await _pubSub.Pub(_unloadKey, medusaName);
}
return res;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public string[] GetCommandExampleArgs(string medusaName, string commandName, CultureInfo culture)
{
if (!_resolved.TryGetValue(medusaName, out var data))
return Array.Empty<string>();
return data.Strings.GetCommandStrings(commandName, culture).Args
?? data.SnekInfos
.SelectMany(x => x.Commands)
.FirstOrDefault(x => x.Aliases.Any(alias
=> alias.Equals(commandName, StringComparison.InvariantCultureIgnoreCase)))
?.OptionalStrings
.Args
?? new[] { string.Empty };
}
public Task ReloadStrings()
=> _pubSub.Pub(_stringsReload, true);
[MethodImpl(MethodImplOptions.NoInlining)]
private void ReloadStringsSync()
{
foreach (var resolved in _resolved.Values)
{
resolved.Strings.Reload();
}
}
private async Task ReloadStringsInternal()
{
await _lock.WaitAsync();
try
{
ReloadStringsSync();
}
finally
{
_lock.Release();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public string GetCommandDescription(string medusaName, string commandName, CultureInfo culture)
{
if (!_resolved.TryGetValue(medusaName, out var data))
return string.Empty;
return data.Strings.GetCommandStrings(commandName, culture).Desc
?? data.SnekInfos
.SelectMany(x => x.Commands)
.FirstOrDefault(x => x.Aliases.Any(alias
=> alias.Equals(commandName, StringComparison.InvariantCultureIgnoreCase)))
?.OptionalStrings
.Desc
?? string.Empty;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private async ValueTask<MedusaLoadResult> InternalLoadAsync(string name)
{
if (_resolved.ContainsKey(name))
return MedusaLoadResult.AlreadyLoaded;
var safeName = Uri.EscapeDataString(name);
name = name.ToLowerInvariant();
await _lock.WaitAsync();
try
{
if (LoadAssemblyInternal(safeName,
out var ctx,
out var snekData,
out var services,
out var strings,
out var typeReaders))
{
var moduleInfos = new List<ModuleInfo>();
LoadTypeReadersInternal(typeReaders);
foreach (var point in snekData)
{
try
{
// initialize snek and subsneks
await point.Instance.InitializeAsync();
foreach (var sub in point.Subsneks)
{
await sub.Instance.InitializeAsync();
}
var module = await LoadModuleInternalAsync(name, point, strings, services);
moduleInfos.Add(module);
}
catch (Exception ex)
{
Log.Warning(ex,
"Error loading snek {SnekName}",
point.Name);
}
}
var execs = GetExecsInternal(snekData, strings, services);
await _behHandler.AddRangeAsync(execs);
_resolved[name] = new(LoadContext: ctx,
ModuleInfos: moduleInfos.ToImmutableArray(),
SnekInfos: snekData.ToImmutableArray(),
strings,
typeReaders,
execs)
{
Services = services
};
services = null;
_medusaConfig.AddLoadedMedusa(safeName);
return MedusaLoadResult.Success;
}
return MedusaLoadResult.Empty;
}
catch (Exception ex) when (ex is FileNotFoundException or BadImageFormatException)
{
return MedusaLoadResult.NotFound;
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred loading a medusa");
return MedusaLoadResult.UnknownError;
}
finally
{
_lock.Release();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private IReadOnlyCollection<ICustomBehavior> GetExecsInternal(IReadOnlyCollection<SnekInfo> snekData, IMedusaStrings strings, IServiceProvider services)
{
var behs = new List<ICustomBehavior>();
foreach (var snek in snekData)
{
behs.Add(new BehaviorAdapter(new(snek.Instance), strings, services));
foreach (var sub in snek.Subsneks)
{
behs.Add(new BehaviorAdapter(new(sub.Instance), strings, services));
}
}
return behs;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void LoadTypeReadersInternal(Dictionary<Type, TypeReader> typeReaders)
{
var notAddedTypeReaders = new List<Type>();
foreach (var (type, typeReader) in typeReaders)
{
// if type reader for this type already exists, it will not be replaced
if (_cmdService.TypeReaders.Contains(type))
{
notAddedTypeReaders.Add(type);
continue;
}
_cmdService.AddTypeReader(type, typeReader);
}
// remove the ones that were not added
// to prevent them from being unloaded later
// as they didn't come from this medusa
foreach (var toRemove in notAddedTypeReaders)
{
typeReaders.Remove(toRemove);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private bool LoadAssemblyInternal(
string safeName,
[NotNullWhen(true)] out WeakReference<MedusaAssemblyLoadContext>? ctxWr,
[NotNullWhen(true)] out IReadOnlyCollection<SnekInfo>? snekData,
out IServiceProvider services,
out IMedusaStrings strings,
out Dictionary<Type, TypeReader> typeReaders)
{
ctxWr = null;
snekData = null;
var path = $"{BASE_DIR}/{safeName}/{safeName}.dll";
strings = MedusaStrings.CreateDefault($"{BASE_DIR}/{safeName}");
var ctx = new MedusaAssemblyLoadContext(Path.GetDirectoryName(path)!);
var a = ctx.LoadFromAssemblyPath(Path.GetFullPath(path));
var sis = LoadSneksFromAssembly(a, out services);
typeReaders = LoadTypeReadersFromAssembly(a, strings, services);
if (sis.Count == 0)
{
return false;
}
ctxWr = new(ctx);
snekData = sis;
return true;
}
private static readonly Type _paramParserType = typeof(ParamParser<>);
[MethodImpl(MethodImplOptions.NoInlining)]
private Dictionary<Type, TypeReader> LoadTypeReadersFromAssembly(
Assembly assembly,
IMedusaStrings strings,
IServiceProvider services)
{
var paramParsers = assembly.GetExportedTypes()
.Where(x => x.IsClass
&& !x.IsAbstract
&& x.BaseType is not null
&& x.BaseType.IsGenericType
&& x.BaseType.GetGenericTypeDefinition() == _paramParserType);
var typeReaders = new Dictionary<Type, TypeReader>();
foreach (var parserType in paramParsers)
{
var parserObj = ActivatorUtilities.CreateInstance(services, parserType);
var targetType = parserType.BaseType!.GetGenericArguments()[0];
var typeReaderInstance = (TypeReader)Activator.CreateInstance(
typeof(ParamParserAdapter<>).MakeGenericType(targetType),
args: new[] { parserObj, strings, services })!;
typeReaders.Add(targetType, typeReaderInstance);
}
return typeReaders;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private async Task<ModuleInfo> LoadModuleInternalAsync(string medusaName, SnekInfo snekInfo, IMedusaStrings strings, IServiceProvider services)
{
var module = await _cmdService.CreateModuleAsync(snekInfo.Instance.Prefix,
CreateModuleFactory(medusaName, snekInfo, strings, services));
return module;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private Action<ModuleBuilder> CreateModuleFactory(
string medusaName,
SnekInfo snekInfo,
IMedusaStrings strings,
IServiceProvider medusaServices)
=> mb =>
{
var m = mb.WithName(snekInfo.Name);
foreach (var cmd in snekInfo.Commands)
{
m.AddCommand(cmd.Aliases.First(),
CreateCallback(cmd.ContextType,
new(snekInfo),
new(cmd),
new(medusaServices),
strings),
CreateCommandFactory(medusaName, cmd));
}
foreach (var subInfo in snekInfo.Subsneks)
m.AddModule(subInfo.Instance.Prefix, CreateModuleFactory(medusaName, subInfo, strings, medusaServices));
};
private static readonly RequireContextAttribute _reqGuild = new RequireContextAttribute(ContextType.Guild);
private static readonly RequireContextAttribute _reqDm = new RequireContextAttribute(ContextType.DM);
private Action<CommandBuilder> CreateCommandFactory(string medusaName, SnekCommandData cmd)
=> (cb) =>
{
cb.AddAliases(cmd.Aliases.Skip(1).ToArray());
if (cmd.ContextType == CommandContextType.Guild)
cb.AddPrecondition(_reqGuild);
else if (cmd.ContextType == CommandContextType.Dm)
cb.AddPrecondition(_reqDm);
cb.WithPriority(cmd.Priority);
// using summary to save method name
// method name is used to retrieve desc/usages
cb.WithRemarks($"medusa///{medusaName}");
cb.WithSummary(cmd.MethodInfo.Name.ToLowerInvariant());
foreach (var param in cmd.Parameters)
{
cb.AddParameter(param.Name, param.Type, CreateParamFactory(param));
}
};
private Action<ParameterBuilder> CreateParamFactory(ParamData paramData)
=> (pb) =>
{
pb.WithIsMultiple(paramData.IsParams)
.WithIsOptional(paramData.IsOptional)
.WithIsRemainder(paramData.IsLeftover);
};
[MethodImpl(MethodImplOptions.NoInlining)]
private Func<ICommandContext, object[], IServiceProvider, CommandInfo, Task> CreateCallback(
CommandContextType contextType,
WeakReference<SnekInfo> snekDataWr,
WeakReference<SnekCommandData> snekCommandDataWr,
WeakReference<IServiceProvider> medusaServicesWr,
IMedusaStrings strings)
=> async (context, parameters, svcs, _) =>
{
if (!snekCommandDataWr.TryGetTarget(out var cmdData)
|| !snekDataWr.TryGetTarget(out var snekData)
|| !medusaServicesWr.TryGetTarget(out var medusaServices))
{
Log.Warning("Attempted to run an unloaded snek's command");
return;
}
var paramObjs = ParamObjs(contextType, cmdData, parameters, context, svcs, medusaServices, strings);
try
{
var methodInfo = cmdData.MethodInfo;
if (methodInfo.ReturnType == typeof(Task)
|| (methodInfo.ReturnType.IsGenericType
&& methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)))
{
await (Task)methodInfo.Invoke(snekData.Instance, paramObjs)!;
}
else if (methodInfo.ReturnType == typeof(ValueTask))
{
await ((ValueTask)methodInfo.Invoke(snekData.Instance, paramObjs)!).AsTask();
}
else // if (methodInfo.ReturnType == typeof(void))
{
methodInfo.Invoke(snekData.Instance, paramObjs);
}
}
finally
{
paramObjs = null;
cmdData = null;
snekData = null;
medusaServices = null;
}
};
[MethodImpl(MethodImplOptions.NoInlining)]
private static object[] ParamObjs(
CommandContextType contextType,
SnekCommandData cmdData,
object[] parameters,
ICommandContext context,
IServiceProvider svcs,
IServiceProvider svcProvider,
IMedusaStrings strings)
{
var extraParams = contextType == CommandContextType.Unspecified ? 0 : 1;
extraParams += cmdData.InjectedParams.Count;
var paramObjs = new object[parameters.Length + extraParams];
var startAt = 0;
if (contextType != CommandContextType.Unspecified)
{
paramObjs[0] = ContextAdapterFactory.CreateNew(context, strings, svcs);
startAt = 1;
}
for (var i = 0; i < cmdData.InjectedParams.Count; i++)
{
var svc = svcProvider.GetService(cmdData.InjectedParams[i]);
if (svc is null)
{
throw new ArgumentException($"Cannot inject a service of type {cmdData.InjectedParams[i]}");
}
paramObjs[i + startAt] = svc;
svc = null;
}
startAt += cmdData.InjectedParams.Count;
for (var i = 0; i < parameters.Length; i++)
paramObjs[startAt + i] = parameters[i];
return paramObjs;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private async Task<MedusaUnloadResult> InternalUnloadAsync(string name)
{
name = name.ToLowerInvariant();
if (!_resolved.Remove(name, out var lsi))
return MedusaUnloadResult.NotLoaded;
await _lock.WaitAsync();
try
{
UnloadTypeReaders(lsi.TypeReaders);
foreach (var mi in lsi.ModuleInfos)
{
await _cmdService.RemoveModuleAsync(mi);
}
await _behHandler.RemoveRangeAsync(lsi.Execs);
await DisposeSnekInstances(lsi);
var lc = lsi.LoadContext;
// removing this line will prevent assembly from being unloaded quickly
// as this local variable will be held for a long time potentially
// due to how async works
lsi.Services = null!;
lsi = null;
_medusaConfig.RemoveLoadedMedusa(name);
return UnloadInternal(lc)
? MedusaUnloadResult.Success
: MedusaUnloadResult.PossiblyUnable;
}
finally
{
_lock.Release();
}
}
private void UnloadTypeReaders(Dictionary<Type, TypeReader> valueTypeReaders)
{
foreach (var tr in valueTypeReaders)
{
_cmdService.TryRemoveTypeReader(tr.Key, false, out _);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private async Task DisposeSnekInstances(ResolvedMedusa medusa)
{
foreach (var si in medusa.SnekInfos)
{
try
{
await si.Instance.DisposeAsync();
foreach (var sub in si.Subsneks)
{
await sub.Instance.DisposeAsync();
}
}
catch (Exception ex)
{
Log.Warning(ex,
"Failed cleanup of Snek {SnekName}. This medusa might not unload correctly",
si.Instance.Name);
}
}
// medusae = null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private bool UnloadInternal(WeakReference<MedusaAssemblyLoadContext> lsi)
{
UnloadContext(lsi);
GcCleanup();
return !lsi.TryGetTarget(out _);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void UnloadContext(WeakReference<MedusaAssemblyLoadContext> lsiLoadContext)
{
if(lsiLoadContext.TryGetTarget(out var ctx))
ctx.Unload();
}
private void GcCleanup()
{
// cleanup
for (var i = 0; i < 10; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.WaitForFullGCComplete();
GC.Collect();
}
}
private static readonly Type _snekType = typeof(Snek);
[MethodImpl(MethodImplOptions.NoInlining)]
private IServiceProvider LoadMedusaServicesInternal(Assembly a)
=> new ServiceCollection()
.Scan(x => x.FromAssemblies(a)
.AddClasses(static x => x.WithAttribute<svcAttribute>(x => x.Lifetime == Lifetime.Transient))
.AsSelfWithInterfaces()
.WithTransientLifetime()
.AddClasses(static x => x.WithAttribute<svcAttribute>(x => x.Lifetime == Lifetime.Singleton))
.AsSelfWithInterfaces()
.WithSingletonLifetime())
.BuildServiceProvider();
[MethodImpl(MethodImplOptions.NoInlining)]
public IReadOnlyCollection<SnekInfo> LoadSneksFromAssembly(Assembly a, out IServiceProvider services)
{
var medusaServices = LoadMedusaServicesInternal(a);
services = new MedusaServiceProvider(_botServices, medusaServices);
// find all types in teh assembly
var types = a.GetExportedTypes();
// snek is always a public non abstract class
var classes = types.Where(static x => x.IsClass
&& (x.IsNestedPublic || x.IsPublic)
&& !x.IsAbstract
&& x.BaseType == _snekType
&& (x.DeclaringType is null || x.DeclaringType.IsAssignableTo(_snekType)))
.ToList();
var topModules = new Dictionary<Type, SnekInfo>();
foreach (var cl in classes)
{
if (cl.DeclaringType is not null)
continue;
// get module data, and add it to the topModules dictionary
var module = GetModuleData(cl, services);
topModules.Add(cl, module);
}
foreach (var c in classes)
{
if (c.DeclaringType is not Type dt)
continue;
// if there is no top level module which this module is a child of
// just print a warning and skip it
if (!topModules.TryGetValue(dt, out var parentData))
{
Log.Warning("Can't load submodule {SubName} because parent module {Name} does not exist",
c.Name,
dt.Name);
continue;
}
GetModuleData(c, services, parentData);
}
return topModules.Values.ToArray();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private SnekInfo GetModuleData(Type type, IServiceProvider services, SnekInfo? parentData = null)
{
var filters = type.GetCustomAttributes<FilterAttribute>(true)
.ToArray();
var instance = (Snek)ActivatorUtilities.CreateInstance(services, type);
var module = new SnekInfo(instance.Name,
parentData,
instance,
GetCommands(instance, type),
filters);
if (parentData is not null)
parentData.Subsneks.Add(module);
return module;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private IReadOnlyCollection<SnekCommandData> GetCommands(Snek instance, Type type)
{
var methodInfos = type
.GetMethods(BindingFlags.Instance
| BindingFlags.DeclaredOnly
| BindingFlags.Public)
.Where(static x =>
{
if(x.GetCustomAttribute<cmdAttribute>(true) is null)
return false;
if (x.ReturnType.IsGenericType)
{
var genericType = x.ReturnType.GetGenericTypeDefinition();
if (genericType == typeof(Task<>))
return true;
// if (genericType == typeof(ValueTask<>))
// return true;
Log.Warning("Method {MethodName} has an invalid return type: {ReturnType}",
x.Name,
x.ReturnType);
return false;
}
var succ = x.ReturnType == typeof(Task)
|| x.ReturnType == typeof(ValueTask)
|| x.ReturnType == typeof(void);
if (!succ)
{
Log.Warning("Method {MethodName} has an invalid return type: {ReturnType}",
x.Name,
x.ReturnType);
}
return succ;
});
var cmds = new List<SnekCommandData>();
foreach (var method in methodInfos)
{
var filters = method.GetCustomAttributes<FilterAttribute>().ToArray();
var prio = method.GetCustomAttribute<prioAttribute>()?.Priority ?? 0;
var paramInfos = method.GetParameters();
var cmdParams = new List<ParamData>();
var diParams = new List<Type>();
var cmdContext = CommandContextType.Unspecified;
var canInject = false;
for (var paramCounter = 0; paramCounter < paramInfos.Length; paramCounter++)
{
var pi = paramInfos[paramCounter];
var paramName = pi.Name ?? "unnamed";
var isContext = paramCounter == 0 && pi.ParameterType.IsAssignableTo(typeof(AnyContext));
var leftoverAttribute = pi.GetCustomAttribute<leftoverAttribute>(true);
var hasDefaultValue = pi.HasDefaultValue;
var isLeftover = leftoverAttribute != null;
var isParams = pi.GetCustomAttribute<ParamArrayAttribute>() is not null;
var paramType = pi.ParameterType;
var isInjected = pi.GetCustomAttribute<injectAttribute>(true) is not null;
if (isContext)
{
if (hasDefaultValue || leftoverAttribute != null || isParams)
throw new ArgumentException("IContext parameter cannot be optional, leftover, constant or params. " + GetErrorPath(method, pi));
if (paramCounter != 0)
throw new ArgumentException($"IContext parameter has to be first. {GetErrorPath(method, pi)}");
canInject = true;
if (paramType.IsAssignableTo(typeof(GuildContext)))
cmdContext = CommandContextType.Guild;
else if (paramType.IsAssignableTo(typeof(DmContext)))
cmdContext = CommandContextType.Dm;
else
cmdContext = CommandContextType.Any;
continue;
}
if (isInjected)
{
if (!canInject && paramCounter != 0)
throw new ArgumentException($"Parameters marked as [Injected] have to come after IContext");
canInject = true;
diParams.Add(paramType);
continue;
}
canInject = false;
if (isParams)
{
if (hasDefaultValue)
throw new NotSupportedException("Params can't have const values at the moment. "
+ GetErrorPath(method, pi));
// if it's params, it means it's an array, and i only need a parser for the actual type,
// as the parser will run on each array element, it can't be null
paramType = paramType.GetElementType()!;
}
// leftover can only be the last parameter.
if (isLeftover && paramCounter != paramInfos.Length - 1)
{
var path = GetErrorPath(method, pi);
Log.Error("Only one parameter can be marked [Leftover] and it has to be the last one. {Path} ",
path);
throw new ArgumentException("Leftover attribute error.");
}
cmdParams.Add(new ParamData(paramType, paramName, hasDefaultValue, isLeftover, isParams));
}
var cmdAttribute = method.GetCustomAttribute<cmdAttribute>()!;
var aliases = cmdAttribute.Aliases;
if (aliases.Length == 0)
aliases = new[] { method.Name.ToLowerInvariant() };
cmds.Add(new(
aliases,
method,
instance,
filters,
cmdContext,
diParams,
cmdParams,
new(cmdAttribute.desc, cmdAttribute.args),
prio
));
}
return cmds;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private string GetErrorPath(MethodInfo m, System.Reflection.ParameterInfo pi)
=> $@"Module: {m.DeclaringType?.Name}
Command: {m.Name}
ParamName: {pi.Name}
ParamType: {pi.ParameterType.Name}";
}
public enum MedusaLoadResult
{
Success,
NotFound,
AlreadyLoaded,
Empty,
UnknownError,
}
public enum MedusaUnloadResult
{
Success,
NotLoaded,
PossiblyUnable,
NotFound,
}

View File

@@ -0,0 +1,24 @@
using System.Runtime.CompilerServices;
namespace Nadeko.Medusa;
public class MedusaServiceProvider : IServiceProvider
{
private readonly IServiceProvider _nadekoServices;
private readonly IServiceProvider _medusaServices;
public MedusaServiceProvider(IServiceProvider nadekoServices, IServiceProvider medusaServices)
{
_nadekoServices = nadekoServices;
_medusaServices = medusaServices;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public object? GetService(Type serviceType)
{
if (!serviceType.Assembly.IsCollectible)
return _nadekoServices.GetService(serviceType);
return _medusaServices.GetService(serviceType);
}
}

View File

@@ -0,0 +1,9 @@
namespace Nadeko.Medusa;
public sealed record ParamData(
Type Type,
string Name,
bool IsOptional,
bool IsLeftover,
bool IsParams
);

View File

@@ -0,0 +1,14 @@
using System.Collections.Immutable;
namespace Nadeko.Medusa;
public sealed record ResolvedMedusa(
WeakReference<MedusaAssemblyLoadContext> LoadContext,
IImmutableList<ModuleInfo> ModuleInfos,
IImmutableList<SnekInfo> SnekInfos,
IMedusaStrings Strings,
Dictionary<Type, TypeReader> TypeReaders,
IReadOnlyCollection<ICustomBehavior> Execs)
{
public IServiceProvider Services { get; set; } = null!;
}

View File

@@ -0,0 +1,41 @@
using Microsoft.VisualBasic;
using System.Reflection;
using CommandStrings = Nadeko.Snake.CommandStrings;
namespace Nadeko.Medusa;
public sealed class SnekCommandData
{
public SnekCommandData(
IReadOnlyCollection<string> aliases,
MethodInfo methodInfo,
Snek module,
FilterAttribute[] filters,
CommandContextType contextType,
IReadOnlyList<Type> injectedParams,
IReadOnlyList<ParamData> parameters,
CommandStrings strings,
int priority)
{
Aliases = aliases;
MethodInfo = methodInfo;
Module = module;
Filters = filters;
ContextType = contextType;
InjectedParams = injectedParams;
Parameters = parameters;
Priority = priority;
OptionalStrings = strings;
}
public CommandStrings OptionalStrings { get; set; }
public IReadOnlyCollection<string> Aliases { get; }
public MethodInfo MethodInfo { get; set; }
public Snek Module { get; set; }
public FilterAttribute[] Filters { get; set; }
public CommandContextType ContextType { get; }
public IReadOnlyList<Type> InjectedParams { get; }
public IReadOnlyList<ParamData> Parameters { get; }
public int Priority { get; }
}

View File

@@ -0,0 +1,11 @@
namespace Nadeko.Medusa;
public sealed record SnekInfo(
string Name,
SnekInfo? Parent,
Snek Instance,
IReadOnlyCollection<SnekCommandData> Commands,
IReadOnlyCollection<FilterAttribute> Filters)
{
public List<SnekInfo> Subsneks { get; set; } = new();
}

View File

@@ -1,10 +0,0 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// Implemented by modules which block execution before anything is executed
/// </summary>
public interface IEarlyBehavior
{
int Priority { get; }
Task<bool> RunBehavior(IGuild guild, IUserMessage msg);
}

View File

@@ -0,0 +1,19 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// Executed if no command was found for this message
/// </summary>
public interface IExecNoCommand
{
/// <summary>
/// Executed at the end of the lifecycle if no command was found
/// <see cref="IExecOnMessage"/> →
/// <see cref="IInputTransformer"/> →
/// <see cref="IExecPreCommand"/> →
/// [<see cref="IExecPostCommand"/> | *<see cref="IExecNoCommand"/>*]
/// </summary>
/// <param name="guild"></param>
/// <param name="msg"></param>
/// <returns>A task representing completion</returns>
Task ExecOnNoCommandAsync(IGuild guild, IUserMessage msg);
}

View File

@@ -0,0 +1,21 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// Implemented by modules to handle non-bot messages received
/// </summary>
public interface IExecOnMessage
{
int Priority { get; }
/// <summary>
/// Ran after a non-bot message was received
/// *<see cref="IExecOnMessage"/>* →
/// <see cref="IInputTransformer"/> →
/// <see cref="IExecPreCommand"/> →
/// [<see cref="IExecPostCommand"/> | <see cref="IExecNoCommand"/>]
/// </summary>
/// <param name="guild">Guild where the message was sent</param>
/// <param name="msg">The message that was received</param>
/// <returns>Whether further processing of this message should be blocked</returns>
Task<bool> ExecOnMessageAsync(IGuild guild, IUserMessage msg);
}

View File

@@ -0,0 +1,22 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// This interface's method is executed after the command successfully finished execution.
/// ***There is no support for this method in NadekoBot services.***
/// It is only meant to be used in medusa system
/// </summary>
public interface IExecPostCommand
{
/// <summary>
/// Executed after a command was successfully executed
/// <see cref="IExecOnMessage"/> →
/// <see cref="IInputTransformer"/> →
/// <see cref="IExecPreCommand"/> →
/// [*<see cref="IExecPostCommand"/>* | <see cref="IExecNoCommand"/>]
/// </summary>
/// <param name="ctx">Command context</param>
/// <param name="moduleName">Module name</param>
/// <param name="commandName">Command name</param>
/// <returns>A task representing completion</returns>
ValueTask ExecPostCommandAsync(ICommandContext ctx, string moduleName, string commandName);
}

View File

@@ -0,0 +1,25 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// This interface's method is executed after a command was found but before it was executed.
/// Able to block further processing of a command
/// </summary>
public interface IExecPreCommand
{
public int Priority { get; }
/// <summary>
/// <para>
/// Ran after a command was found but before execution.
/// </para>
/// <see cref="IExecOnMessage"/> →
/// <see cref="IInputTransformer"/> →
/// *<see cref="IExecPreCommand"/>* →
/// [<see cref="IExecPostCommand"/> | <see cref="IExecNoCommand"/>]
/// </summary>
/// <param name="context">Command context</param>
/// <param name="moduleName">Name of the module</param>
/// <param name="command">Command info</param>
/// <returns>Whether further processing of the command is blocked</returns>
Task<bool> ExecPreCommandAsync(ICommandContext context, string moduleName, CommandInfo command);
}

View File

@@ -1,8 +1,23 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// Implemented by services which may transform input before a command is searched for
/// </summary>
public interface IInputTransformer
{
Task<string> TransformInput(
/// <summary>
/// Ran after a non-bot message was received
/// <see cref="IExecOnMessage"/> ->
/// *<see cref="IInputTransformer"/>* ->
/// <see cref="IExecPreCommand"/> ->
/// [<see cref="IExecPostCommand"/> OR <see cref="IExecNoCommand"/>]
/// </summary>
/// <param name="guild">Guild</param>
/// <param name="channel">Channel in which the message was sent</param>
/// <param name="user">User who sent the message</param>
/// <param name="input">Content of the message</param>
/// <returns>New input, if any, otherwise null</returns>
Task<string?> TransformInput(
IGuild guild,
IMessageChannel channel,
IUser user,

View File

@@ -1,8 +0,0 @@
namespace NadekoBot.Common.ModuleBehaviors;
public interface ILateBlocker
{
public int Priority { get; }
Task<bool> TryBlockLate(ICommandContext context, string moduleName, CommandInfo command);
}

View File

@@ -1,9 +0,0 @@
namespace NadekoBot.Common.ModuleBehaviors;
/// <summary>
/// Last thing to be executed, won't stop further executions
/// </summary>
public interface ILateExecutor
{
Task LateExecute(IGuild guild, IUserMessage msg);
}

View File

@@ -1,5 +1,6 @@
#nullable disable
using System.Globalization;
using MessageType = NadekoBot.Extensions.MessageType;
// ReSharper disable InconsistentNaming
@@ -29,20 +30,15 @@ public abstract class NadekoModule : ModuleBase
protected string GetText(in LocStr data)
=> Strings.GetText(data, Culture);
public Task<IUserMessage> SendErrorAsync(string error)
=> ctx.Channel.SendErrorAsync(_eb, error);
public Task<IUserMessage> SendErrorAsync(
string title,
string error,
string url = null,
string footer = null)
string footer = null,
NadekoInteraction inter = null)
=> ctx.Channel.SendErrorAsync(_eb, title, error, url, footer);
public Task<IUserMessage> SendConfirmAsync(string text)
=> ctx.Channel.SendConfirmAsync(_eb, text);
public Task<IUserMessage> SendConfirmAsync(
string title,
string text,
@@ -50,25 +46,33 @@ public abstract class NadekoModule : ModuleBase
string footer = null)
=> ctx.Channel.SendConfirmAsync(_eb, title, text, url, footer);
public Task<IUserMessage> SendPendingAsync(string text)
=> ctx.Channel.SendPendingAsync(_eb, text);
//
public Task<IUserMessage> SendErrorAsync(string text, NadekoInteraction inter = null)
=> ctx.Channel.SendAsync(_eb, text, MessageType.Error, inter);
public Task<IUserMessage> SendConfirmAsync(string text, NadekoInteraction inter = null)
=> ctx.Channel.SendAsync(_eb, text, MessageType.Ok, inter);
public Task<IUserMessage> SendPendingAsync(string text, NadekoInteraction inter = null)
=> ctx.Channel.SendAsync(_eb, text, MessageType.Pending, inter);
public Task<IUserMessage> ErrorLocalizedAsync(LocStr str)
=> SendErrorAsync(GetText(str));
// localized normal
public Task<IUserMessage> ErrorLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendErrorAsync(GetText(str), inter);
public Task<IUserMessage> PendingLocalizedAsync(LocStr str)
=> SendPendingAsync(GetText(str));
public Task<IUserMessage> PendingLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendPendingAsync(GetText(str), inter);
public Task<IUserMessage> ConfirmLocalizedAsync(LocStr str)
=> SendConfirmAsync(GetText(str));
public Task<IUserMessage> ConfirmLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendConfirmAsync(GetText(str), inter);
public Task<IUserMessage> ReplyErrorLocalizedAsync(LocStr str)
// localized replies
public Task<IUserMessage> ReplyErrorLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendErrorAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}");
public Task<IUserMessage> ReplyPendingLocalizedAsync(LocStr str)
public Task<IUserMessage> ReplyPendingLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendPendingAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}");
public Task<IUserMessage> ReplyConfirmLocalizedAsync(LocStr str)
public Task<IUserMessage> ReplyConfirmLocalizedAsync(LocStr str, NadekoInteraction inter = null)
=> SendConfirmAsync($"{Format.Bold(ctx.User.ToString())} {GetText(str)}");
public async Task<bool> PromptUserConfirmAsync(IEmbedBuilder embed)

View File

@@ -18,4 +18,13 @@ public sealed class NoPublicBotAttribute : PreconditionAttribute
return Task.FromResult(PreconditionResult.FromSuccess());
#endif
}
}
/// <summary>
/// Classed marked with this attribute will not be added to the service provider
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class DontAddToIocContainerAttribute : Attribute
{
}

View File

@@ -34,61 +34,59 @@ public class Replacer
public SmartText Replace(SmartText data)
=> data switch
{
SmartEmbedText embedData => Replace(embedData),
SmartEmbedText embedData => Replace(embedData) with
{
PlainText = Replace(embedData.PlainText),
Color = embedData.Color
},
SmartPlainText plain => Replace(plain),
SmartEmbedTextArray arr => Replace(arr),
_ => throw new ArgumentOutOfRangeException(nameof(data), "Unsupported argument type")
};
public SmartPlainText Replace(SmartPlainText plainText)
=> Replace(plainText.Text);
public SmartEmbedText Replace(SmartEmbedText embedData)
{
var newEmbedData = new SmartEmbedText
private SmartEmbedTextArray Replace(SmartEmbedTextArray embedArr)
=> new()
{
Embeds = embedArr.Embeds.Map(e => Replace(e) with
{
Color = e.Color
}),
Content = Replace(embedArr.Content)
};
private SmartPlainText Replace(SmartPlainText plain)
=> Replace(plain.Text);
private T Replace<T>(T embedData) where T: SmartEmbedTextBase, new()
{
var newEmbedData = new T
{
PlainText = Replace(embedData.PlainText),
Description = Replace(embedData.Description),
Title = Replace(embedData.Title),
Thumbnail = Replace(embedData.Thumbnail),
Image = Replace(embedData.Image),
Url = Replace(embedData.Url)
};
if (embedData.Author is not null)
{
newEmbedData.Author = new()
{
Name = Replace(embedData.Author.Name),
IconUrl = Replace(embedData.Author.IconUrl)
};
}
if (embedData.Fields is not null)
{
var fields = new List<SmartTextEmbedField>();
foreach (var f in embedData.Fields)
{
var newF = new SmartTextEmbedField
Url = Replace(embedData.Url),
Author = embedData.Author is null
? null
: new()
{
Name = Replace(f.Name),
Value = Replace(f.Value),
Inline = f.Inline
};
fields.Add(newF);
}
newEmbedData.Fields = fields.ToArray();
}
if (embedData.Footer is not null)
{
newEmbedData.Footer = new()
Name = Replace(embedData.Author.Name),
IconUrl = Replace(embedData.Author.IconUrl)
},
Fields = embedData.Fields?.Map(f => new SmartTextEmbedField
{
Text = Replace(embedData.Footer.Text),
IconUrl = Replace(embedData.Footer.IconUrl)
};
}
newEmbedData.Color = embedData.Color;
Name = Replace(f.Name),
Value = Replace(f.Value),
Inline = f.Inline
}),
Footer = embedData.Footer is null
? null
: new()
{
Text = Replace(embedData.Footer.Text),
IconUrl = Replace(embedData.Footer.IconUrl)
}
};
return newEmbedData;
}

View File

@@ -1,20 +1,67 @@
#nullable disable
using SixLabors.ImageSharp.PixelFormats;
#nullable disable
namespace NadekoBot;
public sealed record SmartEmbedText : SmartText
public sealed record SmartEmbedArrayElementText : SmartEmbedTextBase
{
public string PlainText { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Url { get; set; }
public string Thumbnail { get; set; }
public string Image { get; set; }
public string Color { get; init; } = string.Empty;
public SmartTextEmbedAuthor Author { get; set; }
public SmartTextEmbedFooter Footer { get; set; }
public SmartTextEmbedField[] Fields { get; set; }
public SmartEmbedArrayElementText() : base()
{
}
public SmartEmbedArrayElementText(IEmbed eb) : base(eb)
{
}
public uint Color { get; set; } = 7458112;
protected override EmbedBuilder GetEmbedInternal()
{
var embed = base.GetEmbedInternal();
if (Rgba32.TryParseHex(Color, out var color))
return embed.WithColor(color.ToDiscordColor());
return embed;
}
}
public sealed record SmartEmbedText : SmartEmbedTextBase
{
public string PlainText { get; init; }
public uint Color { get; init; } = 7458112;
public SmartEmbedText()
{
}
private SmartEmbedText(IEmbed eb, string plainText = null)
: base(eb)
=> (PlainText, Color) = (plainText, eb.Color?.RawValue ?? 0);
public static SmartEmbedText FromEmbed(IEmbed eb, string plainText = null)
=> new(eb, plainText);
protected override EmbedBuilder GetEmbedInternal()
{
var embed = base.GetEmbedInternal();
return embed.WithColor(Color);
}
}
public abstract record SmartEmbedTextBase : SmartText
{
public string Title { get; init; }
public string Description { get; init; }
public string Url { get; init; }
public string Thumbnail { get; init; }
public string Image { get; init; }
public SmartTextEmbedAuthor Author { get; init; }
public SmartTextEmbedFooter Footer { get; init; }
public SmartTextEmbedField[] Fields { get; init; }
public bool IsValid
=> !string.IsNullOrWhiteSpace(Title)
@@ -26,36 +73,37 @@ public sealed record SmartEmbedText : SmartText
&& (!string.IsNullOrWhiteSpace(Footer.Text) || !string.IsNullOrWhiteSpace(Footer.IconUrl)))
|| Fields is { Length: > 0 };
public static SmartEmbedText FromEmbed(IEmbed eb, string plainText = null)
protected SmartEmbedTextBase()
{
var set = new SmartEmbedText
{
PlainText = plainText,
Title = eb.Title,
Description = eb.Description,
Url = eb.Url,
Thumbnail = eb.Thumbnail?.Url,
Image = eb.Image?.Url,
Author = eb.Author is { } ea
? new()
{
Name = ea.Name,
Url = ea.Url,
IconUrl = ea.IconUrl
}
: null,
Footer = eb.Footer is { } ef
? new()
{
Text = ef.Text,
IconUrl = ef.IconUrl
}
: null
};
}
protected SmartEmbedTextBase(IEmbed eb)
{
Title = eb.Title;
Description = eb.Description;
Url = eb.Url;
Thumbnail = eb.Thumbnail?.Url;
Image = eb.Image?.Url;
Author = eb.Author is { } ea
? new()
{
Name = ea.Name,
Url = ea.Url,
IconUrl = ea.IconUrl
}
: null;
Footer = eb.Footer is { } ef
? new()
{
Text = ef.Text,
IconUrl = ef.IconUrl
}
: null;
if (eb.Fields.Length > 0)
{
set.Fields = eb.Fields.Select(field
Fields = eb.Fields.Select(field
=> new SmartTextEmbedField
{
Inline = field.Inline,
@@ -64,14 +112,14 @@ public sealed record SmartEmbedText : SmartText
})
.ToArray();
}
set.Color = eb.Color?.RawValue ?? 0;
return set;
}
public EmbedBuilder GetEmbed()
=> GetEmbedInternal();
protected virtual EmbedBuilder GetEmbedInternal()
{
var embed = new EmbedBuilder().WithColor(Color);
var embed = new EmbedBuilder();
if (!string.IsNullOrWhiteSpace(Title))
embed.WithTitle(Title);

View File

@@ -0,0 +1,28 @@
#nullable disable
namespace NadekoBot;
public sealed record SmartEmbedTextArray : SmartText
{
public string Content { get; set; }
public SmartEmbedArrayElementText[] Embeds { get; set; }
public bool IsValid
=> Embeds?.All(x => x.IsValid) ?? false;
public EmbedBuilder[] GetEmbedBuilders()
{
if (Embeds is null)
return Array.Empty<EmbedBuilder>();
return Embeds.Map(em => em.GetEmbed());
}
public void NormalizeFields()
{
if (Embeds is null)
return;
foreach(var eb in Embeds)
eb.NormalizeFields();
}
}

View File

@@ -1,5 +1,6 @@
#nullable disable
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace NadekoBot;
@@ -11,6 +12,9 @@ public abstract record SmartText
public bool IsPlainText
=> this is SmartPlainText;
public bool IsEmbedArray
=> this is SmartEmbedTextArray;
public static SmartText operator +(SmartText text, string input)
=> text switch
{
@@ -19,6 +23,10 @@ public abstract record SmartText
PlainText = set.PlainText + input
},
SmartPlainText spt => new SmartPlainText(spt.Text + input),
SmartEmbedTextArray arr => arr with
{
Content = arr.Content + input
},
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
@@ -30,27 +38,46 @@ public abstract record SmartText
PlainText = input + set.PlainText
},
SmartPlainText spt => new SmartPlainText(input + spt.Text),
SmartEmbedTextArray arr => arr with
{
Content = input + arr.Content
},
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
[CanBeNull]
public static SmartText CreateFrom(string input)
{
if (string.IsNullOrWhiteSpace(input) || !input.TrimStart().StartsWith("{"))
if (string.IsNullOrWhiteSpace(input))
return new SmartPlainText(input);
try
{
var smartEmbedText = JsonConvert.DeserializeObject<SmartEmbedText>(input);
var doc = JObject.Parse(input);
var root = doc.Root;
if (root.Type == JTokenType.Object)
{
if (((JObject)root).TryGetValue("embeds", out _))
{
var arr = root.ToObject<SmartEmbedTextArray>();
if (smartEmbedText is null)
throw new FormatException();
if (arr is null)
return new SmartPlainText(input);
smartEmbedText.NormalizeFields();
arr!.NormalizeFields();
return arr;
}
if (!smartEmbedText.IsValid)
return new SmartPlainText(input);
var obj = root.ToObject<SmartEmbedText>();
return smartEmbedText;
if (obj is null)
return new SmartPlainText(input);
obj.NormalizeFields();
return obj;
}
return new SmartPlainText(input);
}
catch
{

View File

@@ -1,95 +0,0 @@
#nullable disable
namespace NadekoBot.Common;
public sealed class ReactionEventWrapper : IDisposable
{
public event Action<SocketReaction> OnReactionAdded = delegate { };
public event Action<SocketReaction> OnReactionRemoved = delegate { };
public event Action OnReactionsCleared = delegate { };
public IUserMessage Message { get; }
private readonly DiscordSocketClient _client;
private bool disposing;
public ReactionEventWrapper(DiscordSocketClient client, IUserMessage msg)
{
Message = msg ?? throw new ArgumentNullException(nameof(msg));
_client = client;
_client.ReactionAdded += Discord_ReactionAdded;
_client.ReactionRemoved += Discord_ReactionRemoved;
_client.ReactionsCleared += Discord_ReactionsCleared;
}
public void Dispose()
{
if (disposing)
return;
disposing = true;
UnsubAll();
}
private Task Discord_ReactionsCleared(Cacheable<IUserMessage, ulong> msg, Cacheable<IMessageChannel, ulong> channel)
{
Task.Run(() =>
{
try
{
if (msg.Id == Message.Id)
OnReactionsCleared?.Invoke();
}
catch { }
});
return Task.CompletedTask;
}
private Task Discord_ReactionRemoved(
Cacheable<IUserMessage, ulong> msg,
Cacheable<IMessageChannel, ulong> cacheable,
SocketReaction reaction)
{
Task.Run(() =>
{
try
{
if (msg.Id == Message.Id)
OnReactionRemoved?.Invoke(reaction);
}
catch { }
});
return Task.CompletedTask;
}
private Task Discord_ReactionAdded(
Cacheable<IUserMessage, ulong> msg,
Cacheable<IMessageChannel, ulong> cacheable,
SocketReaction reaction)
{
Task.Run(() =>
{
try
{
if (msg.Id == Message.Id)
OnReactionAdded?.Invoke(reaction);
}
catch
{
}
});
return Task.CompletedTask;
}
public void UnsubAll()
{
_client.ReactionAdded -= Discord_ReactionAdded;
_client.ReactionRemoved -= Discord_ReactionRemoved;
_client.ReactionsCleared -= Discord_ReactionsCleared;
OnReactionAdded = null;
OnReactionRemoved = null;
OnReactionsCleared = null;
}
}

View File

@@ -1,4 +1,5 @@
#nullable disable
namespace NadekoBot.Common.TypeReaders;
[MeansImplicitUse(ImplicitUseTargetFlags.Default | ImplicitUseTargetFlags.WithInheritors)]

View File

@@ -12,7 +12,7 @@ public static class ClubExtensions
.ThenInclude(x => x.User)
.Include(x => x.Bans)
.ThenInclude(x => x.User)
.Include(x => x.Users)
.Include(x => x.Members)
.AsQueryable();
public static ClubInfo GetByOwner(this DbSet<ClubInfo> clubs, ulong userId)
@@ -20,24 +20,14 @@ public static class ClubExtensions
public static ClubInfo GetByOwnerOrAdmin(this DbSet<ClubInfo> clubs, ulong userId)
=> Include(clubs)
.FirstOrDefault(c => c.Owner.UserId == userId || c.Users.Any(u => u.UserId == userId && u.IsClubAdmin));
.FirstOrDefault(c => c.Owner.UserId == userId || c.Members.Any(u => u.UserId == userId && u.IsClubAdmin));
public static ClubInfo GetByMember(this DbSet<ClubInfo> clubs, ulong userId)
=> Include(clubs).FirstOrDefault(c => c.Users.Any(u => u.UserId == userId));
=> Include(clubs).FirstOrDefault(c => c.Members.Any(u => u.UserId == userId));
public static ClubInfo GetByName(this DbSet<ClubInfo> clubs, string name, int discrim)
public static ClubInfo GetByName(this DbSet<ClubInfo> clubs, string name)
=> Include(clubs)
.FirstOrDefault(c => EF.Functions.Collate(c.Name, "NOCASE") == EF.Functions.Collate(name, "NOCASE")
&& c.Discrim == discrim);
public static int GetNextDiscrim(this DbSet<ClubInfo> clubs, string name)
=> Include(clubs)
.Where(x =>
EF.Functions.Collate(x.Name, "NOCASE") == EF.Functions.Collate(name, "NOCASE"))
.Select(x => x.Discrim)
.DefaultIfEmpty()
.Max()
+ 1;
.FirstOrDefault(c => c.Name == name);
public static List<ClubInfo> GetClubLeaderboardPage(this DbSet<ClubInfo> clubs, int page)
=> clubs.AsNoTracking().OrderByDescending(x => x.Xp).Skip(page * 9).Take(9).ToList();

View File

@@ -9,6 +9,11 @@ namespace NadekoBot.Db;
public static class DiscordUserExtensions
{
public static Task<DiscordUser> GetByUserIdAsync(
this IQueryable<DiscordUser> set,
ulong userId)
=> set.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId);
public static void EnsureUserCreated(
this NadekoContext ctx,
ulong userId,
@@ -37,20 +42,49 @@ public static class DiscordUserExtensions
UserId = userId
});
public static Task EnsureUserCreatedAsync(
this NadekoContext ctx,
ulong userId)
=> ctx.DiscordUser
.ToLinqToDBTable()
.InsertOrUpdateAsync(
() => new()
{
UserId = userId,
Username = "Unknown",
Discriminator = "????",
AvatarId = string.Empty,
TotalXp = 0,
CurrencyAmount = 0
},
old => new()
{
},
() => new()
{
UserId = userId
});
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown
public static DiscordUser GetOrCreateUser(
this NadekoContext ctx,
ulong userId,
string username,
string discrim,
string avatarId)
string avatarId,
Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>> includes = null)
{
ctx.EnsureUserCreated(userId, username, discrim, avatarId);
return ctx.DiscordUser.Include(x => x.Club).First(u => u.UserId == userId);
IQueryable<DiscordUser> queryable = ctx.DiscordUser;
if (includes is not null)
queryable = includes(queryable);
return queryable.First(u => u.UserId == userId);
}
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original)
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId);
public static DiscordUser GetOrCreateUser(this NadekoContext ctx, IUser original, Func<IQueryable<DiscordUser>, IQueryable<DiscordUser>> includes = null)
=> ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId, includes);
public static int GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id)
=> users.AsQueryable()
@@ -84,77 +118,6 @@ public static class DiscordUserExtensions
item.CurrencyAmount = 0;
}
public static bool TryUpdateCurrencyState(
this NadekoContext ctx,
ulong userId,
string name,
string discrim,
string avatarId,
long amount,
bool allowNegative = false)
{
if (amount == 0)
return true;
// if remove - try to remove if he has more or equal than the amount
// and return number of rows > 0 (was there a change)
if (amount < 0 && !allowNegative)
{
var rows = ctx.Database.ExecuteSqlInterpolated($@"
UPDATE DiscordUser
SET CurrencyAmount=CurrencyAmount+{amount}
WHERE UserId={userId} AND CurrencyAmount>={-amount};");
return rows > 0;
}
// if remove and negative is allowed, just remove without any condition
if (amount < 0 && allowNegative)
{
var rows = ctx.Database.ExecuteSqlInterpolated($@"
UPDATE DiscordUser
SET CurrencyAmount=CurrencyAmount+{amount}
WHERE UserId={userId};");
return rows > 0;
}
// if add - create a new user with default values if it doesn't exist
// if it exists, sum current amount with the new one, if it doesn't
// he just has the new amount
var updatedUserData = !string.IsNullOrWhiteSpace(name);
name ??= "Unknown";
discrim ??= "????";
avatarId ??= "";
// just update the amount, there is no new user data
if (!updatedUserData)
{
ctx.Database.ExecuteSqlInterpolated($@"
UPDATE OR IGNORE DiscordUser
SET CurrencyAmount=CurrencyAmount+{amount}
WHERE UserId={userId};
INSERT OR IGNORE INTO DiscordUser (UserId, Username, Discriminator, AvatarId, CurrencyAmount, TotalXp)
VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount}, 0);
");
}
else
{
ctx.Database.ExecuteSqlInterpolated($@"
UPDATE OR IGNORE DiscordUser
SET CurrencyAmount=CurrencyAmount+{amount},
Username={name},
Discriminator={discrim},
AvatarId={avatarId}
WHERE UserId={userId};
INSERT OR IGNORE INTO DiscordUser (UserId, Username, Discriminator, AvatarId, CurrencyAmount, TotalXp)
VALUES ({userId}, {name}, {discrim}, {avatarId}, {amount}, 0);
");
}
return true;
}
public static decimal GetTotalCurrency(this DbSet<DiscordUser> users)
=> users.Sum((Func<DiscordUser, decimal>)(x => x.CurrencyAmount));

View File

@@ -50,9 +50,7 @@ public static class GuildConfigExtensions
.Include(gc => gc.StreamRole)
.Include(gc => gc.XpSettings)
.ThenInclude(x => x.ExclusionList)
.Include(gc => gc.DelMsgOnCmdChannels)
.Include(gc => gc.ReactionRoleMessages)
.ThenInclude(x => x.ReactionRoles);
.Include(gc => gc.DelMsgOnCmdChannels);
public static IEnumerable<GuildConfig> GetAllGuildConfigs(
this DbSet<GuildConfig> configs,
@@ -100,6 +98,26 @@ public static class GuildConfigExtensions
}
return config;
// ctx.GuildConfigs
// .ToLinqToDBTable()
// .InsertOrUpdate(() => new()
// {
// GuildId = guildId,
// Permissions = Permissionv2.GetDefaultPermlist,
// WarningsInitialized = true,
// WarnPunishments = DefaultWarnPunishments
// },
// _ => new(),
// () => new()
// {
// GuildId = guildId
// });
//
// if(includes is null)
// return ctx.GuildConfigs
// .ToLinqToDBTable()
// .First(x => x.GuildId == guildId);
}
public static LogSetting LogSettingsFor(this NadekoContext ctx, ulong guildId)

View File

@@ -1,4 +1,6 @@
#nullable disable
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models;
using NadekoBot.Services.Database;
@@ -75,11 +77,22 @@ public static class WaifuExtensions
.Select(x => x.Waifu.UserId)
.FirstOrDefault();
public static WaifuInfoStats GetWaifuInfo(this NadekoContext ctx, ulong userId)
public static async Task<WaifuInfoStats> GetWaifuInfoAsync(this NadekoContext ctx, ulong userId)
{
ctx.Database.ExecuteSqlInterpolated($@"
INSERT OR IGNORE INTO WaifuInfo (AffinityId, ClaimerId, Price, WaifuId)
VALUES ({null}, {null}, {1}, (SELECT Id FROM DiscordUser WHERE UserId={userId}));");
await ctx.WaifuInfo
.ToLinqToDBTable()
.InsertOrUpdateAsync(() => new()
{
AffinityId = null,
ClaimerId = null,
Price = 1,
WaifuId = ctx.DiscordUser.Where(x => x.UserId == userId).Select(x => x.Id).First()
},
_ => new(),
() => new()
{
WaifuId = ctx.DiscordUser.Where(x => x.UserId == userId).Select(x => x.Id).First()
});
var toReturn = ctx.WaifuInfo.AsQueryable()
.Where(w => w.WaifuId

View File

@@ -0,0 +1,9 @@
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Db.Models;
public class BankUser : DbEntity
{
public ulong UserId { get; set; }
public long Balance { get; set; }
}

View File

@@ -8,24 +8,19 @@ public class ClubInfo : DbEntity
{
[MaxLength(20)]
public string Name { get; set; }
public int Discrim { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; } = string.Empty;
public int MinimumLevelReq { get; set; } = 5;
public int Xp { get; set; } = 0;
public int OwnerId { get; set; }
public int? OwnerId { get; set; }
public DiscordUser Owner { get; set; }
public List<DiscordUser> Users { get; set; } = new();
public List<DiscordUser> Members { get; set; } = new();
public List<ClubApplicants> Applicants { get; set; } = new();
public List<ClubBans> Bans { get; set; } = new();
public string Description { get; set; }
public override string ToString()
=> Name + "#" + Discrim;
=> Name;
}
public class ClubApplicants

View File

@@ -10,10 +10,11 @@ public class DiscordUser : DbEntity
public string Discriminator { get; set; }
public string AvatarId { get; set; }
public int? ClubId { get; set; }
public ClubInfo Club { get; set; }
public bool IsClubAdmin { get; set; }
public int TotalXp { get; set; }
public long TotalXp { get; set; }
public DateTime LastLevelUp { get; set; } = DateTime.UtcNow;
public DateTime LastXpGain { get; set; } = DateTime.MinValue;
public XpNotificationLocation NotifyOnLevelUp { get; set; }

View File

@@ -90,8 +90,8 @@ public class GuildConfig : DbEntity
public XpSettings XpSettings { get; set; }
public List<FeedSub> FeedSubs { get; set; } = new();
public IndexedCollection<ReactionRoleMessage> ReactionRoleMessages { get; set; } = new();
public bool NotifyStreamOffline { get; set; }
public bool DeleteStreamOnlineMessage { get; set; }
public List<GroupName> SelfAssignableRoleGroupNames { get; set; }
public int WarnExpireHours { get; set; }
public WarnExpireAction WarnExpireAction { get; set; } = WarnExpireAction.Clear;

View File

@@ -1,22 +1,18 @@
#nullable disable
using System.ComponentModel.DataAnnotations;
namespace NadekoBot.Services.Database.Models;
public class ReactionRoleMessage : DbEntity, IIndexed
public class ReactionRoleV2 : DbEntity
{
public int Index { get; set; }
public int GuildConfigId { get; set; }
public GuildConfig GuildConfig { get; set; }
public ulong GuildId { get; set; }
public ulong ChannelId { get; set; }
public ulong MessageId { get; set; }
public List<ReactionRole> ReactionRoles { get; set; }
public bool Exclusive { get; set; }
}
public class ReactionRole : DbEntity
{
public string EmoteName { get; set; }
[MaxLength(100)]
public string Emote { get; set; }
public ulong RoleId { get; set; }
public int Group { get; set; }
public int LevelReq { get; set; }
}

View File

@@ -5,8 +5,8 @@ public class UserXpStats : DbEntity
{
public ulong UserId { get; set; }
public ulong GuildId { get; set; }
public int Xp { get; set; }
public int AwardedXp { get; set; }
public long Xp { get; set; }
public long AwardedXp { get; set; }
public XpNotificationLocation NotifyOnLevelUp { get; set; }
public DateTime LastLevelUp { get; set; } = DateTime.UtcNow;
}

View File

@@ -3,6 +3,7 @@ namespace NadekoBot.Services.Database.Models;
public class WaifuItem : DbEntity
{
public WaifuInfo WaifuInfo { get; set; }
public int? WaifuInfoId { get; set; }
public string ItemEmoji { get; set; }
public string Name { get; set; }

View File

@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db.Models;
namespace NadekoBot.Services.Database;
public sealed class MysqlContext : NadekoContext
{
private readonly string _connStr;
private readonly string _version;
protected override string CurrencyTransactionOtherIdDefaultValue
=> "NULL";
protected override string DiscordUserLastXpGainDefaultValue
=> "(UTC_TIMESTAMP - INTERVAL 1 year)";
protected override string LastLevelUpDefaultValue
=> "(UTC_TIMESTAMP)";
public MysqlContext(string connStr = "Server=localhost", string version = "8.0")
{
_connStr = connStr;
_version = version;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder
.UseLowerCaseNamingConvention()
.UseMySql(_connStr, ServerVersion.Parse(_version));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// mysql is case insensitive by default
// we can set binary collation to change that
modelBuilder.Entity<ClubInfo>()
.Property(x => x.Name)
.UseCollation("utf8mb4_bin");
}
}

View File

@@ -1,5 +1,4 @@
#nullable disable
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Logging;
@@ -10,23 +9,7 @@ using NadekoBot.Services.Database.Models;
namespace NadekoBot.Services.Database;
public class NadekoContextFactory : IDesignTimeDbContextFactory<NadekoContext>
{
public NadekoContext CreateDbContext(string[] args)
{
LogSetup.SetupLogger(-2);
var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
var creds = new BotCredsProvider().GetCreds();
var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString);
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);
optionsBuilder.UseSqlite(builder.ToString());
var ctx = new NadekoContext(optionsBuilder.Options);
ctx.Database.SetCommandTimeout(60);
return ctx;
}
}
public class NadekoContext : DbContext
public abstract class NadekoContext : DbContext
{
public DbSet<GuildConfig> GuildConfigs { get; set; }
@@ -37,9 +20,13 @@ public class NadekoContext : DbContext
public DbSet<NadekoExpression> Expressions { get; set; }
public DbSet<CurrencyTransaction> CurrencyTransactions { get; set; }
public DbSet<WaifuUpdate> WaifuUpdates { get; set; }
public DbSet<WaifuItem> WaifuItem { get; set; }
public DbSet<Warning> Warnings { get; set; }
public DbSet<UserXpStats> UserXpStats { get; set; }
public DbSet<ClubInfo> Clubs { get; set; }
public DbSet<ClubBans> ClubBans { get; set; }
public DbSet<ClubApplicants> ClubApplicants { get; set; }
//logging
public DbSet<LogSetting> LogSettings { get; set; }
@@ -64,12 +51,19 @@ public class NadekoContext : DbContext
public DbSet<AutoTranslateUser> AutoTranslateUsers { get; set; }
public DbSet<Permissionv2> Permissions { get; set; }
public DbSet<BankUser> BankUsers { get; set; }
public DbSet<ReactionRoleV2> ReactionRoles { get; set; }
public NadekoContext(DbContextOptions<NadekoContext> options)
: base(options)
{
}
#region Mandatory Provider-Specific Values
protected abstract string CurrencyTransactionOtherIdDefaultValue { get; }
protected abstract string DiscordUserLastXpGainDefaultValue { get; }
protected abstract string LastLevelUpDefaultValue { get; }
#endregion
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region QUOTES
@@ -163,10 +157,10 @@ public class NadekoContext : DbContext
.HasDefaultValue(XpNotificationLocation.None);
du.Property(x => x.LastXpGain)
.HasDefaultValueSql("datetime('now', '-1 years')");
.HasDefaultValueSql(DiscordUserLastXpGainDefaultValue);
du.Property(x => x.LastLevelUp)
.HasDefaultValueSql("datetime('now')");
.HasDefaultValueSql(LastLevelUpDefaultValue);
du.Property(x => x.TotalXp)
.HasDefaultValue(0);
@@ -175,7 +169,10 @@ public class NadekoContext : DbContext
.HasDefaultValue(0);
du.HasAlternateKey(w => w.UserId);
du.HasOne(x => x.Club).WithMany(x => x.Users).IsRequired(false);
du.HasOne(x => x.Club)
.WithMany(x => x.Members)
.IsRequired(false)
.OnDelete(DeleteBehavior.NoAction);
du.HasIndex(x => x.TotalXp);
du.HasIndex(x => x.CurrencyAmount);
@@ -214,7 +211,7 @@ public class NadekoContext : DbContext
.IsUnique();
xps.Property(x => x.LastLevelUp)
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 307, DateTimeKind.Local));
.HasDefaultValueSql(LastLevelUpDefaultValue);
xps.HasIndex(x => x.UserId);
xps.HasIndex(x => x.GuildId);
@@ -244,13 +241,14 @@ public class NadekoContext : DbContext
#region Club
var ci = modelBuilder.Entity<ClubInfo>();
ci.HasOne(x => x.Owner).WithOne().HasForeignKey<ClubInfo>(x => x.OwnerId);
ci.HasOne(x => x.Owner)
.WithOne()
.HasForeignKey<ClubInfo>(x => x.OwnerId)
.OnDelete(DeleteBehavior.SetNull);
ci.HasAlternateKey(x => new
{
x.Name,
x.Discrim
x.Name
});
#endregion
@@ -264,9 +262,13 @@ public class NadekoContext : DbContext
t.UserId
});
modelBuilder.Entity<ClubApplicants>().HasOne(pt => pt.User).WithMany();
modelBuilder.Entity<ClubApplicants>()
.HasOne(pt => pt.User)
.WithMany();
modelBuilder.Entity<ClubApplicants>().HasOne(pt => pt.Club).WithMany(x => x.Applicants);
modelBuilder.Entity<ClubApplicants>()
.HasOne(pt => pt.Club)
.WithMany(x => x.Applicants);
modelBuilder.Entity<ClubBans>()
.HasKey(t => new
@@ -275,9 +277,13 @@ public class NadekoContext : DbContext
t.UserId
});
modelBuilder.Entity<ClubBans>().HasOne(pt => pt.User).WithMany();
modelBuilder.Entity<ClubBans>()
.HasOne(pt => pt.User)
.WithMany();
modelBuilder.Entity<ClubBans>().HasOne(pt => pt.Club).WithMany(x => x.Bans);
modelBuilder.Entity<ClubBans>()
.HasOne(pt => pt.Club)
.WithMany(x => x.Bans);
#endregion
@@ -295,7 +301,7 @@ public class NadekoContext : DbContext
.IsUnique(false);
e.Property(x => x.OtherId)
.HasDefaultValueSql("NULL");
.HasDefaultValueSql(CurrencyTransactionOtherIdDefaultValue);
e.Property(x => x.Type)
.IsRequired();
@@ -357,11 +363,18 @@ public class NadekoContext : DbContext
#region Reaction roles
modelBuilder.Entity<ReactionRoleMessage>(rrm => rrm
.HasMany(x => x.ReactionRoles)
.WithOne()
.OnDelete(DeleteBehavior.Cascade));
modelBuilder.Entity<ReactionRoleV2>(rr2 =>
{
rr2.HasIndex(x => x.GuildId)
.IsUnique(false);
rr2.HasIndex(x => new
{
x.MessageId,
x.Emote
}).IsUnique();
});
#endregion
#region LogSettings
@@ -400,6 +413,13 @@ public class NadekoContext : DbContext
x.ChannelId,
x.UserId
}));
#region BANK
modelBuilder.Entity<BankUser>(bu => bu.HasIndex(x => x.UserId).IsUnique());
#endregion
}
#if DEBUG

Some files were not shown because too many files have changed in this diff Show More