mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ab0fd44b46 | ||
|
b61f499f91 | ||
|
53d365db3a | ||
|
140c4f7fd6 | ||
|
5627a3b172 | ||
|
4795fa98a0 | ||
|
93453ba522 | ||
|
c6a9108474 | ||
|
c3ba805acf | ||
|
c0ce22a6b7 | ||
|
22183501fe | ||
|
2fbdab3235 | ||
|
804d3f79fd | ||
|
fb119cca4c | ||
|
31af5ea8c2 | ||
|
e1776d6093 | ||
|
33dd4bbf0e | ||
|
af343ac1f0 | ||
|
065807c180 | ||
|
9cd24feccc | ||
|
a2d1506915 | ||
|
54a32a5770 | ||
|
5b9abeb0b2 | ||
|
accfb2d1ac | ||
|
71d383c4db | ||
|
197ee9f5ff | ||
|
d51d159962 | ||
|
89b0eabd41 | ||
|
8d932d546a | ||
|
9ea3460e3d | ||
|
7bd4db60a8 | ||
|
42e1f35df2 | ||
|
179784da3e | ||
|
9ed0c870d1 | ||
|
77e288ee54 | ||
|
58adaa9110 | ||
|
d3a73945e7 | ||
|
caca407abd | ||
|
4fd7b2d8cd | ||
|
eaea6e3c54 | ||
|
0bb68c7723 |
@@ -1,8 +1,6 @@
|
|||||||
# Ignore all files
|
# Ignore all files
|
||||||
*
|
*
|
||||||
|
|
||||||
# Use Nadeko.Medusa project
|
|
||||||
!src/Nadeko.Medusa/**
|
|
||||||
# Use NadekoBot project
|
# Use NadekoBot project
|
||||||
!src/NadekoBot/**
|
!src/NadekoBot/**
|
||||||
# Use NadekoBot.Coordinator project
|
# Use NadekoBot.Coordinator project
|
||||||
|
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,11 +1,5 @@
|
|||||||
#Manually added files
|
#Manually added files
|
||||||
|
|
||||||
# medusa stuff
|
|
||||||
!src/NadekoBot/data/medusae/medusa.yml
|
|
||||||
src/NadekoBot/data/medusae/**
|
|
||||||
|
|
||||||
# other
|
|
||||||
|
|
||||||
command_errors*.txt
|
command_errors*.txt
|
||||||
output/
|
output/
|
||||||
src/NadekoBot/output
|
src/NadekoBot/output
|
||||||
@@ -14,7 +8,7 @@ src/NadekoBot/creds.yml
|
|||||||
src/NadekoBot/Command Errors*.txt
|
src/NadekoBot/Command Errors*.txt
|
||||||
|
|
||||||
src/NadekoBot/creds.yml
|
src/NadekoBot/creds.yml
|
||||||
# credentials file before and after v3
|
# credentials file before and after migrations
|
||||||
src/NadekoBot/credentials.json
|
src/NadekoBot/credentials.json
|
||||||
src/NadekoBot/old_credentials.json
|
src/NadekoBot/old_credentials.json
|
||||||
src/NadekoBot/credentials.json.bak
|
src/NadekoBot/credentials.json.bak
|
||||||
@@ -262,7 +256,7 @@ PublishScripts/
|
|||||||
!**/packages/build/
|
!**/packages/build/
|
||||||
# Uncomment if necessary however generally it will be regenerated when needed
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
#!**/packages/repositories.config
|
#!**/packages/repositories.config
|
||||||
# NuGet v4's project.json files produces more ignoreable files
|
# NuGet v3's project.json files produces more ignoreable files
|
||||||
*.nuget.props
|
*.nuget.props
|
||||||
*.nuget.targets
|
*.nuget.targets
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
image: mcr.microsoft.com/dotnet/sdk:6.0
|
image: mcr.microsoft.com/dotnet/sdk:5.0
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
@@ -22,8 +22,8 @@ variables:
|
|||||||
build:
|
build:
|
||||||
stage: build
|
stage: build
|
||||||
script:
|
script:
|
||||||
- "dotnet publish -c Release -r linux-x64 --self-contained -o $LINUX_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
|
- "dotnet publish -c Release -r linux-x64 -o $LINUX_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
|
||||||
- "dotnet publish -c Release -r win7-x64 --self-contained -o $WIN_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
|
- "dotnet publish -c Release -r win7-x64 -o $WIN_X64_OUTPUT_DIR src/NadekoBot/NadekoBot.csproj"
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- "$LINUX_X64_OUTPUT_DIR/"
|
- "$LINUX_X64_OUTPUT_DIR/"
|
||||||
@@ -50,7 +50,7 @@ release:
|
|||||||
- if: $CI_COMMIT_TAG
|
- if: $CI_COMMIT_TAG
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
release-cli create --name "NadekoBot v$CI_COMMIT_TAG" --description "## [Changelog](https://gitlab.com/Kwoth/nadekobot/-/blob/v4/CHANGELOG.md#$(echo "$CI_COMMIT_TAG" | sed "s/\.//g")-$(date +%d%m%Y))" --tag-name $CI_COMMIT_TAG \
|
release-cli create --name "NadekoBot v$CI_COMMIT_TAG" --description "## [Changelog](https://gitlab.com/Kwoth/nadekobot/-/blob/v3/CHANGELOG.md#$(echo "$CI_COMMIT_TAG" | sed "s/\.//g")-$(date +%d%m%Y))" --tag-name $CI_COMMIT_TAG \
|
||||||
--assets-link "{\"name\":\"${LINUX_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_X64_RELEASE}\"}" \
|
--assets-link "{\"name\":\"${LINUX_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${LINUX_X64_RELEASE}\"}" \
|
||||||
--assets-link "{\"name\":\"${WIN_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WIN_X64_RELEASE}\"}"
|
--assets-link "{\"name\":\"${WIN_X64_RELEASE}\",\"url\":\"${PACKAGE_REGISTRY_URL}/${WIN_X64_RELEASE}\"}"
|
||||||
|
|
||||||
@@ -65,18 +65,18 @@ test:
|
|||||||
publish-windows:
|
publish-windows:
|
||||||
stage: publish-windows
|
stage: publish-windows
|
||||||
rules:
|
rules:
|
||||||
- if: "$CI_COMMIT_TAG"
|
- if: '$CI_COMMIT_TAG'
|
||||||
image: scottyhardy/docker-wine
|
image: scottyhardy/docker-wine
|
||||||
before_script:
|
before_script:
|
||||||
- choco install dotnet-6.0-runtime --version=6.0.4 -y
|
- choco install dotnet-5.0-runtime -y
|
||||||
- choco install dotnet-6.0-sdk --version=6.0.202 -y
|
- choco install dotnet-5.0-sdk -y
|
||||||
- choco install innosetup -y
|
- choco install innosetup -y
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
|
- "$INSTALLER_OUTPUT_DIR/$INSTALLER_FILE_NAME"
|
||||||
script:
|
script:
|
||||||
- dotnet clean
|
- dotnet clean
|
||||||
- dotnet restore -f --no-cache -v n
|
- dotnet restore
|
||||||
- dotnet publish -c Release --runtime win7-x64 /p:Version=$CI_COMMIT_TAG src/NadekoBot
|
- dotnet publish -c Release --runtime win7-x64 /p:Version=$CI_COMMIT_TAG src/NadekoBot
|
||||||
- $env:NADEKOBOT_INSTALL_VERSION = $CI_COMMIT_TAG
|
- $env:NADEKOBOT_INSTALL_VERSION = $CI_COMMIT_TAG
|
||||||
- iscc.exe "/O+" ".\exe_builder.iss"
|
- iscc.exe "/O+" ".\exe_builder.iss"
|
||||||
@@ -86,7 +86,7 @@ publish-windows:
|
|||||||
upload-windows-updater-release:
|
upload-windows-updater-release:
|
||||||
stage: upload-windows-updater-release
|
stage: upload-windows-updater-release
|
||||||
rules:
|
rules:
|
||||||
- if: "$CI_COMMIT_TAG"
|
- if: '$CI_COMMIT_TAG'
|
||||||
image:
|
image:
|
||||||
name: amazon/aws-cli
|
name: amazon/aws-cli
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
|
201
CHANGELOG.md
201
CHANGELOG.md
@@ -1,212 +1,15 @@
|
|||||||
|
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
|
Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o
|
||||||
|
|
||||||
## Unreleased
|
## [3.0.13] - 14.04.2021
|
||||||
|
|
||||||
## [4.1.6] - 14.05.2022
|
|
||||||
|
|
||||||
### Fixed
|
|
||||||
|
|
||||||
- Fixed windows release and updated packages
|
|
||||||
|
|
||||||
## [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
|
|
||||||
- Added trovo.live support for stream notifications (`.stadd`)
|
|
||||||
- Added unclaimed waifu decay functionality
|
|
||||||
- Added 3 new settings to `data/gambling.yml` to control it:
|
|
||||||
- waifu.decay.percent - How much % to subtract from unclaimed waifu
|
|
||||||
- waifu.decay.hourInterval - How often to decay the price
|
|
||||||
- waifu.decay.minPrice - Unclaimed waifus with price lower than the one specified here will not be affected by the decay
|
|
||||||
- Added `currency.transactionsLifetime` to `data/gambling.yml` Any transaction older than the number of days specified will be automatically deleted
|
|
||||||
- Added `.stock` command to check stock prices and charts
|
|
||||||
- Re-added `.qap / .queueautoplay`
|
|
||||||
|
|
||||||
### Changed
|
|
||||||
- CustomReactions module (and customreactions db table) has been renamed to Expressions.
|
|
||||||
- This was done to remove confusion about how it relates to discord Reactions (it doesn't, it was created and named before discord reactions existed)
|
|
||||||
- Expression command now start with ex/expr and end with the name of the action or setting.
|
|
||||||
- For example `.exd` (`.dcr`) is expression delete, `.exa` (`.acr`)
|
|
||||||
- Permissions (`.lp`) be automatically updated with "ACTUALEXPRESSIONS", "EXPRESSIONS" instead of "ACTUALCUSTOMREACTIONS" and "CUSTOMREACTIONS"
|
|
||||||
- Permissions for `.ecr` (now `.exe`), `.scr` (now `.exs`), `.dcr` (now `.exd`), `.acr` (now `.exa`), `.lcr` (now `.exl`) will be automatically updated
|
|
||||||
- If you have custom permissions for other CustomReaction commands
|
|
||||||
- Some of the old aliases like `.acr` `.dcr` `.lcr` and a few others have been kept
|
|
||||||
- Currency output format improvement (will use guild locale now for some commands)
|
|
||||||
- `.crypto` will now also show CoinMarketCap rank
|
|
||||||
- Waifus can now be claimed for much higher prices (int -> long)
|
|
||||||
- Several strings and commands related to music have been changed
|
|
||||||
- Changed `.ms / .movesong` to `.tm / .trackmove` but kept old aliases
|
|
||||||
- Changed ~~song~~ -> `track` throughout music module strings
|
|
||||||
- Improved .curtrs (It will now have a lot more useful data in the database, show Tx ids, and be partially localized)
|
|
||||||
- [dev] Reason renamed to Note
|
|
||||||
- [dev] Added Type, Extra, OtherId fields to the database
|
|
||||||
- [dev] CommandStrings will now use methodname as the key, and **not** the command name (first entry in aliases.yml)
|
|
||||||
- In other words aliases.yml and commands.en-US.yml will use the same keys (once again)
|
|
||||||
- [dev] Reorganized module and submodule folders
|
|
||||||
- [dev] Permissionv2 db table renamed to Permissions
|
|
||||||
- [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
|
|
||||||
- `.gvc` should now properly detect multiple activities
|
|
||||||
- Fixed reference to non-existent command in bot.yml
|
|
||||||
- Comment indentation in .yml files should now make more sense
|
|
||||||
- Fixed `.warn` punishments not being applied properly when using weighted warnings
|
|
||||||
- Fixed embed color when disabling `.antialt`
|
|
||||||
|
|
||||||
### Removed
|
|
||||||
- Removed `.bce` - use `.config` or `.config bot` specifically for bot config
|
|
||||||
- Removed obsolete placeholders: %users% %servers% %userfull% %username% %userdiscrim% %useravatar% %id% %uid% %chname% %cid% %sid% %members% %server_time% %shardid% %time% %mention%
|
|
||||||
- Removed some obsolete commands and strings
|
|
||||||
- Removed code which migrated 2.x to v3 credentials, settings, etc...
|
|
||||||
|
|
||||||
## [3.0.13] - 14.01.2022
|
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fixed `.greetdm` causing ratelimits during raids
|
- Fixed `.greetdm` causing ratelimits during raids
|
||||||
- Fixed `.gelbooru`
|
- Fixed `.gelbooru`
|
||||||
|
|
||||||
## [3.0.12] - 06.01.2022
|
## [3.0.12] - 06.01.2021
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- `.smch` Fixed
|
- `.smch` Fixed
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
|
||||||
WORKDIR /source
|
WORKDIR /source
|
||||||
|
|
||||||
COPY src/Nadeko.Medusa/*.csproj src/Nadeko.Medusa/
|
|
||||||
COPY src/NadekoBot/*.csproj src/NadekoBot/
|
COPY src/NadekoBot/*.csproj src/NadekoBot/
|
||||||
COPY src/NadekoBot.Coordinator/*.csproj src/NadekoBot.Coordinator/
|
COPY src/NadekoBot.Coordinator/*.csproj src/NadekoBot.Coordinator/
|
||||||
COPY src/NadekoBot.Generators/*.csproj src/NadekoBot.Generators/
|
COPY src/NadekoBot.Generators/*.csproj src/NadekoBot.Generators/
|
||||||
@@ -19,14 +18,14 @@ RUN set -xe; \
|
|||||||
chmod +x /app/NadekoBot
|
chmod +x /app/NadekoBot
|
||||||
|
|
||||||
# final stage/image
|
# final stage/image
|
||||||
FROM mcr.microsoft.com/dotnet/runtime:6.0
|
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
RUN set -xe; \
|
RUN set -xe; \
|
||||||
useradd -m nadeko; \
|
useradd -m nadeko; \
|
||||||
apt-get update; \
|
apt-get update; \
|
||||||
apt-get install -y libopus0 libsodium23 libsqlite3-0 curl ffmpeg python3 python3-pip sudo; \
|
apt-get install -y libopus0 libsodium23 libsqlite3-0 curl ffmpeg python3 python3-pip sudo; \
|
||||||
update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1; \
|
update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1; \
|
||||||
echo 'Defaults>nadeko env_keep+="ASPNETCORE_* DOTNET_* NadekoBot_* shard_id total_shards TZ"' > /etc/sudoers.d/nadeko; \
|
echo 'Defaults>nadeko env_keep+="ASPNETCORE_* DOTNET_* NadekoBot_* shard_id total_shards TZ"' > /etc/sudoers.d/nadeko; \
|
||||||
pip3 install --upgrade youtube-dl; \
|
pip3 install --upgrade youtube-dl; \
|
||||||
apt-get remove -y python3-pip; \
|
apt-get remove -y python3-pip; \
|
||||||
@@ -38,6 +37,6 @@ COPY docker-entrypoint.sh /usr/local/sbin
|
|||||||
ENV shard_id=0
|
ENV shard_id=0
|
||||||
ENV total_shards=1
|
ENV total_shards=1
|
||||||
|
|
||||||
VOLUME [ "/app/data" ]
|
VOLUME [ "app/data" ]
|
||||||
ENTRYPOINT [ "/usr/local/sbin/docker-entrypoint.sh" ]
|
ENTRYPOINT [ "/usr/local/sbin/docker-entrypoint.sh" ]
|
||||||
CMD dotnet NadekoBot.dll "$shard_id" "$total_shards"
|
CMD dotnet NadekoBot.dll "$shard_id" "$total_shards"
|
@@ -11,7 +11,6 @@ ProjectSection(SolutionItems) = preProject
|
|||||||
LICENSE.md = LICENSE.md
|
LICENSE.md = LICENSE.md
|
||||||
README.md = README.md
|
README.md = README.md
|
||||||
.gitlab-ci.yml = .gitlab-ci.yml
|
.gitlab-ci.yml = .gitlab-ci.yml
|
||||||
Dockerfile = Dockerfile
|
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NadekoBot", "src\NadekoBot\NadekoBot.csproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NadekoBot", "src\NadekoBot\NadekoBot.csproj", "{45EC1473-C678-4857-A544-07DFE0D0B478}"
|
||||||
@@ -28,8 +27,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NadekoBot.Generators", "src
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NadekoBot.VotesApi", "src\NadekoBot.VotesApi\NadekoBot.VotesApi.csproj", "{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NadekoBot.VotesApi", "src\NadekoBot.VotesApi\NadekoBot.VotesApi.csproj", "{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nadeko.Medusa", "src\Nadeko.Medusa\Nadeko.Medusa.csproj", "{E685977E-31A4-46F4-A5D7-4E3E39E82E43}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -73,12 +70,6 @@ Global
|
|||||||
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}.GlobalNadeko|Any CPU.Build.0 = Debug|Any CPU
|
{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.ActiveCfg = Release|Any CPU
|
||||||
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -91,7 +82,6 @@ Global
|
|||||||
{AE9B7F8C-81D7-4401-83A3-643B38258374} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
|
{AE9B7F8C-81D7-4401-83A3-643B38258374} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
|
||||||
{3BC3BDF8-1A0B-45EB-AB2B-C0891D4D37B8} = {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}
|
{3BC82CFE-BEE7-451F-986B-17EDD1570C4F} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
|
||||||
{E685977E-31A4-46F4-A5D7-4E3E39E82E43} = {04929013-5BAB-42B0-B9B2-8F2BB8F16AF2}
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {5F3F555C-855F-4BE8-B526-D062D3E8ACA4}
|
SolutionGuid = {5F3F555C-855F-4BE8-B526-D062D3E8ACA4}
|
||||||
|
8
NuGet.Config
Normal file
8
NuGet.Config
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<configuration>
|
||||||
|
<packageSources>
|
||||||
|
<add key="Discord.Net" value="https://www.myget.org/F/discord-net/api/v3/index.json" />
|
||||||
|
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
|
<add key="Kwoth-myget" value="https://www.myget.org/F/kwoth/api/v3/index.json" />
|
||||||
|
</packageSources>
|
||||||
|
</configuration>
|
@@ -1,5 +1,5 @@
|
|||||||
[](https://discord.gg/nadekobot)
|
[](https://discord.gg/nadekobot)
|
||||||
[](http://nadekobot.readthedocs.io/en/v4/?badge=v4)
|
[](http://nadekobot.readthedocs.io/en/v3/?badge=v3)
|
||||||
[](https://top.gg/bot/116275390695079945)
|
[](https://top.gg/bot/116275390695079945)
|
||||||
|
|
||||||
|
|
||||||
@@ -10,5 +10,6 @@
|
|||||||
[](https://nadeko.bot/commands)
|
[](https://nadeko.bot/commands)
|
||||||
|
|
||||||
### Useful links
|
### Useful links
|
||||||
- [Self hosting Guides and Docs](https://nadekobot.readthedocs.io/en/v4)
|
- ❗ [2.x to v3 migration guide](https://nadekobot.readthedocs.io/en/v3/guides/migration-guide/)
|
||||||
|
- [Self hosting Guides and Docs](https://nadekobot.readthedocs.io/en/v3)
|
||||||
- [Discord support server](https://discord.nadeko.bot)
|
- [Discord support server](https://discord.nadeko.bot)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
# How to contribute
|
# How to contribute
|
||||||
|
|
||||||
1. Make Merge Requests to the [**v4 branch**](https://gitlab.com/Kwoth/nadekobot/tree/v4)
|
1. Make Merge Requests to the [**v3 branch**](https://gitlab.com/Kwoth/nadekobot/tree/v3)
|
||||||
2. Keep a single Merge Request to a single feature
|
2. Keep a single Merge Request to a single feature
|
||||||
3. Fill out the MR template
|
3. Fill out the MR template
|
||||||
|
|
||||||
|
@@ -13,11 +13,11 @@ This document aims to guide you through the process of creating a Discord accoun
|
|||||||
- Click on the `Add a Bot` button and confirm that you do want to add a bot to this app.
|
- Click on the `Add a Bot` button and confirm that you do want to add a bot to this app.
|
||||||
- **Optional:** Add bot's avatar and description.
|
- **Optional:** Add bot's avatar and description.
|
||||||
- Copy your Token to `creds.yml` as shown above.
|
- Copy your Token to `creds.yml` as shown above.
|
||||||
- Scroll down to the **`Privileged Gateway Intents`** section
|
- Scroll down to the `Privileged Gateway Intents` section
|
||||||
- **Enable the following:**
|
- Enabled the following:
|
||||||
- **PRESENCE INTENT**
|
- PRESENCE INTENT
|
||||||
- **SERVER MEMBERS INTENT**
|
- SERVER MEMBERS INTENT
|
||||||
- **MESSAGE CONTENT INTENT**
|
- MESSAGE CONTENT INTENT
|
||||||
|
|
||||||
These are required for a number of features to function properly, and all should be on.
|
These are required for a number of features to function properly, and all should be on.
|
||||||
|
|
||||||
|
@@ -1,25 +1,20 @@
|
|||||||
# Setting up NadekoBot with Docker
|
# Setting up NadekoBot with Docker
|
||||||
|
|
||||||
# WORK IN PROGRESS
|
# DO NOT USE YET - WORK IN PROGRESS
|
||||||
|
|
||||||
### Installation
|
Upgrade from 2.x to v3 does not work because the file is mount readonly
|
||||||
|
|
||||||
1. Create a `/srv/nadeko` folder
|
### Docker Compose
|
||||||
- `mkdir -p /srv/nadeko`
|
|
||||||
2. Create a `docker-compose.yml`
|
|
||||||
- nano `docker-compose.yml`
|
|
||||||
- copy the following contents into it:
|
|
||||||
##### docker-compose.yml
|
|
||||||
```yml
|
```yml
|
||||||
version: "3.7"
|
version: "3.7"
|
||||||
services:
|
services:
|
||||||
nadeko:
|
nadeko:
|
||||||
image: registry.gitlab.com/kwoth/nadekobot:latest
|
image: registry.gitlab.com/veovis/nadekobot:v3-docker
|
||||||
depends_on:
|
depends_on:
|
||||||
- redis
|
- redis
|
||||||
environment:
|
environment:
|
||||||
TZ: Europe/Paris
|
TZ: Europe/Paris
|
||||||
NadekoBot_RedisOptions: redis,name=nadeko
|
#NadekoBot_RedisOptions: redis,name=nadeko
|
||||||
#NadekoBot_ShardRunCommand: dotnet
|
#NadekoBot_ShardRunCommand: dotnet
|
||||||
#NadekoBot_ShardRunArguments: /app/NadekoBot.dll {0} {1}
|
#NadekoBot_ShardRunArguments: /app/NadekoBot.dll {0} {1}
|
||||||
volumes:
|
volumes:
|
||||||
@@ -34,12 +29,6 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- /srv/nadeko/redis-data:/data
|
- /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
|
### Updating
|
||||||
- `cd /srv/nadeko`
|
- `cd /srv/nadeko`
|
||||||
- `docker-compose pull`
|
- `docker-compose pull`
|
||||||
|
@@ -1,15 +1,8 @@
|
|||||||
# Setting up NadekoBot on Linux
|
## Migration from 2.x
|
||||||
|
|
||||||
| Table of Contents |
|
##### ⚠ If you're already hosting NadekoBot, _You **MUST** update to latest version of 2.x and **run your bot at least once**_ before switching over to v3.
|
||||||
| :-------------------------------------------------- |
|
|
||||||
| [Linux From Source] |
|
#### [Linux migration instructions](../migration-guide/#linux)
|
||||||
| [Source Update Instructions] |
|
|
||||||
| [Linux Release] |
|
|
||||||
| [Release Update Instructions] |
|
|
||||||
| [Tmux (Preferred Method)] |
|
|
||||||
| [Systemd] |
|
|
||||||
| [Systemd + Script] |
|
|
||||||
| [Setting up Nadeko on a VPS (Digital Ocean)] |
|
|
||||||
|
|
||||||
#### Operating System Compatibility
|
#### Operating System Compatibility
|
||||||
|
|
||||||
@@ -26,24 +19,14 @@ It is recommended that you use **Ubuntu 20.04**, as there have been nearly no pr
|
|||||||
|
|
||||||
## Linux From Source
|
## Linux From Source
|
||||||
|
|
||||||
##### Migration from v3 -> v4
|
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 ~`)
|
||||||
|
|
||||||
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
|
##### Installation Instructions
|
||||||
|
|
||||||
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/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)
|
2. Install prerequisites (type `1` and press enter)
|
||||||
3. Download the bot (type `2` and press enter)
|
3. Download the bot (type `2` and press enter)
|
||||||
4. Exit the installer (type `6` and press enter)
|
4. Exit the installer (type `5` and press enter)
|
||||||
5. Copy the creds.yml template `cp nadekobot/output/creds_example.yml nadekobot/output/creds.yml`
|
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
|
6. Open `nadekobot/output/creds.yml` with your favorite text editor. We will use nano here
|
||||||
- `nano nadekobot/output/creds.yml`
|
- `nano nadekobot/output/creds.yml`
|
||||||
@@ -52,20 +35,19 @@ Open Terminal (if you're on an installation with a window manager) and navigate
|
|||||||
- `CTRL` + `X`
|
- `CTRL` + `X`
|
||||||
- `Y`
|
- `Y`
|
||||||
- `Enter`
|
- `Enter`
|
||||||
8. Run the installer script again `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/v4/linuxAIO.sh && bash linuxAIO.sh`
|
8. Run the bot (type `3` and press enter)
|
||||||
9. Run the bot (type `3` and press enter)
|
|
||||||
|
|
||||||
##### Source Update Instructions
|
##### Update Instructions
|
||||||
|
|
||||||
1. ⚠ Stop the bot ⚠
|
1. ⚠ Stop the bot
|
||||||
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`
|
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`
|
||||||
3. Update the bot (type `2` and press enter)
|
3. Update the bot (type `2` and press enter)
|
||||||
4. Run the bot (type `3` and press enter)
|
4. Run the bot (type `3` and press enter)
|
||||||
5. 🎉
|
5. 🎉
|
||||||
|
|
||||||
## Linux Release
|
## Linux Release
|
||||||
|
|
||||||
**⚠ IF YOU ARE FOLLOWING THE GUIDE ABOVE, IGNORE THIS SECTION ⚠**
|
⚠ IF YOU ARE FOLLOWING THE GUIDE ABOVE, IGNORE THIS SECTION ⚠
|
||||||
|
|
||||||
##### Installation Instructions
|
##### Installation Instructions
|
||||||
|
|
||||||
@@ -90,20 +72,7 @@ Open Terminal (if you're on an installation with a window manager) and navigate
|
|||||||
9. Run the bot
|
9. Run the bot
|
||||||
- `./NadekoBot`
|
- `./NadekoBot`
|
||||||
|
|
||||||
##### Release Update Instructions
|
##### 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
|
1. Stop the bot
|
||||||
2. Download the latest release from <https://gitlab.com/Kwoth/nadekobot/-/releases>
|
2. Download the latest release from <https://gitlab.com/Kwoth/nadekobot/-/releases>
|
||||||
@@ -150,20 +119,14 @@ cd nadekobot && chmod +x NadekoBot
|
|||||||
|
|
||||||
While there are two run modes built into the installer, these options only run Nadeko within the current session. Below are 3 methods of running Nadeko as a background process.
|
While there are two run modes built into the installer, these options only run Nadeko within the current session. Below are 3 methods of running Nadeko as a background process.
|
||||||
|
|
||||||
### Tmux Method (Preferred)
|
### Tmux (Preferred Method)
|
||||||
|
|
||||||
Using `tmux` is the simplest method, and is therefore recommended for most users.
|
Using `tmux` is the simplest method, and is therefore recommended for most users.
|
||||||
|
|
||||||
**Before proceeding, make sure your bot is not running by either running `.die` in your Discord server or exiting the process with `Ctrl+C`.**
|
1. Start a tmux session:
|
||||||
|
- `tmux`
|
||||||
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
|
2. Navigate to the project's root directory
|
||||||
- Project root directory location example: `cd /home/user/nadekobot/`
|
- Project root directory location example: `/home/user/nadekobot/`
|
||||||
3. Enter the `output` directory:
|
3. Enter the `output` directory:
|
||||||
- `cd output`
|
- `cd output`
|
||||||
4. Run the bot using:
|
4. Run the bot using:
|
||||||
@@ -171,10 +134,8 @@ The above command will create a new session named **nadeko** *(you can replace
|
|||||||
5. Detatch the tmux session:
|
5. Detatch the tmux session:
|
||||||
- Press `Ctrl` + `B`
|
- Press `Ctrl` + `B`
|
||||||
- Then press `D`
|
- 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
|
### Systemd
|
||||||
|
|
||||||
@@ -329,12 +290,3 @@ If you are running your droplet for the first time, it will most likely ask you
|
|||||||
**Save the new password somewhere safe.**
|
**Save the new password somewhere safe.**
|
||||||
|
|
||||||
After that, your droplet should be ready for use. [Follow the guide from the beginning](#linux-from-source) to set Nadeko up on your newly created VPS.
|
After that, your droplet should be ready for use. [Follow the guide from the beginning](#linux-from-source) to set Nadeko up on your newly created VPS.
|
||||||
|
|
||||||
[Linux From Source]: #linux-from-source
|
|
||||||
[Source Update Instructions]: #source-update-instructions
|
|
||||||
[Linux Release]: #linux-release
|
|
||||||
[Release Update Instructions]: #release-update-instructions
|
|
||||||
[Tmux (Preferred Method)]: #tmux-preferred-method
|
|
||||||
[Systemd]: #systemd
|
|
||||||
[Systemd + Script]: #systemd-script
|
|
||||||
[Setting up Nadeko on a VPS (Digital Ocean)]: #setting-up-nadeko-on-a-linux-vps-digital-ocean-droplet
|
|
||||||
|
66
docs/guides/migration-guide.md
Normal file
66
docs/guides/migration-guide.md
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# Migration instructions (2.x to v3)
|
||||||
|
|
||||||
|
## Windows
|
||||||
|
|
||||||
|
1. Run your NadekoBot Updater first, and **make sure your bot is updated to at least 2.46.5**
|
||||||
|
- **Run your 2.46.5 Bot** and make sure it works, and then **stop it**
|
||||||
|
- Close your old NadekoBot Updater
|
||||||
|
2. Get the new NadekoBot v3 Updater [here](https://dl.nadeko.bot/v3)
|
||||||
|
3. Click on the + icon to add a new bot
|
||||||
|
4. Next to the path, click on the folder icon and select the folder where your 2.46.5 bot is
|
||||||
|
- ℹ In case you're not sure where it's located, you can open your old updater and see it
|
||||||
|
5. If you've selected the correct path, you should have an **Update** button available, click it
|
||||||
|
6. You're done; you can now run your bot, and you can uninstall your old updater if you no longer have 2.x bots
|
||||||
|
7. 🎉
|
||||||
|
|
||||||
|
## Linux
|
||||||
|
|
||||||
|
1. In order to migrate a bot hosted on **Linux**, first update your current version to the latest 2.x version using the 2.x installer, run the bot, and make sure it works. Then:
|
||||||
|
- Run the **old** installer with `cd ~ && wget -N https://github.com/Kwoth/NadekoBot-BashScript/raw/1.9/linuxAIO.sh && bash linuxAIO.sh`
|
||||||
|
- Run option **1** again
|
||||||
|
- You **MUST** Run the bot now to ensure database is ready for migration
|
||||||
|
- Type `.stats` and ensure the version is `2.46.5` or later
|
||||||
|
- Stop the bot
|
||||||
|
2. Make sure your bot's folder is called `NadekoBot`
|
||||||
|
- Run `cd ~ && ls`
|
||||||
|
- Confirm there is a folder called NadekoBot (not nadekobot, in all lowercase)
|
||||||
|
3. Migrate your bot's data using the new installer:
|
||||||
|
- Run the **new** installer `cd ~ && wget -N https://gitlab.com/Kwoth/nadeko-bash-installer/-/raw/master/linuxAIO.sh && bash linuxAIO.sh`
|
||||||
|
- The installer should notify you that your data is ready for migration in a message above the menu.
|
||||||
|
- Install prerequisites (type `1` and press enter), and make sure it is successful
|
||||||
|
- Download NadekoBot v3 (type `2` and press enter)
|
||||||
|
- Run the bot (type `3` and press enter)
|
||||||
|
4. Make sure your permissions, custom reactions, credentials, and other data is preserved
|
||||||
|
- `.stats` to ensure owner id (credentials) is correct
|
||||||
|
- `.lcr` to see custom reactions
|
||||||
|
- `.lp` to list permissions
|
||||||
|
5. 🎉 Enjoy. If you want to learn how to update the bot, click [here](../linux-guide/#update-instructions)
|
||||||
|
|
||||||
|
## Manual
|
||||||
|
|
||||||
|
⚠ NOT RECOMMENDED
|
||||||
|
⚠ NadekoBot v3 requires [.net 5](https://dotnet.microsoft.com/download/dotnet/5.0)
|
||||||
|
|
||||||
|
1. In order to migrate a bot hosted **on Linux or from source on Windows**
|
||||||
|
- First update your current version to the latest 2.x version using the 2.x installer
|
||||||
|
- Then you **must** run the bot to prepare the database for the migration, and make sure the bot works prior to upgrade.
|
||||||
|
Then:
|
||||||
|
2. Rename your old nadeko bot folder to `nadekobot_2x`
|
||||||
|
- `mv NadekoBot nadekobot_2x`
|
||||||
|
3. Build the new version and move old data to the output folder
|
||||||
|
1. Clone the v3 branch to a separate folder
|
||||||
|
- `git clone https://gitlab.com/kwoth/nadekobot -b v3 --depth 1`
|
||||||
|
2. Build the bot
|
||||||
|
- `dotnet publish -c Release -o output/ src/NadekoBot/`
|
||||||
|
3. Copy old data
|
||||||
|
- ⚠ Be sure you copy the correct command for your system!
|
||||||
|
- **Windows:** `cp -r -fo nadekobot_2x/src/NadekoBot/data nadekobot/src/NadekoBot/data`
|
||||||
|
- **Linux:** `cp -rf nadekobot_2x/src/NadekoBot/data nadekobot/src/NadekoBot/data`
|
||||||
|
4. Copy the database
|
||||||
|
- `cp nadekobot_2x/src/NadekoBot/bin/Release/netcoreapp2.1/data/NadekoBot.db nadekobot/output/data`
|
||||||
|
5. Copy your credentials
|
||||||
|
- `cp nadekobot_2x/src/NadekoBot/credentials.json nadekobot/output/`
|
||||||
|
4. Run the bot
|
||||||
|
- `cd nadekobot/output`
|
||||||
|
- `dotnet NadekoBot.dll`
|
||||||
|
5. That's it. Just make sure that when you're updating the bot, you're properly backing up your old data.
|
@@ -12,7 +12,7 @@ Open Terminal (if you don't know how to, click on the magnifying glass on the to
|
|||||||
- `brew install wget`
|
- `brew install wget`
|
||||||
|
|
||||||
###### Dotnet
|
###### Dotnet
|
||||||
- Download [.net6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0)
|
- Download [.net5 SDK](https://dotnet.microsoft.com/download/dotnet/5.0)
|
||||||
- Open the `.pkg` file you've downloaded and install it.
|
- Open the `.pkg` file you've downloaded and install it.
|
||||||
- Run this command in Terminal. There might be output. If there is, disregard it. (copy-paste the entire block)
|
- Run this command in Terminal. There might be output. If there is, disregard it. (copy-paste the entire block)
|
||||||
```bash
|
```bash
|
||||||
@@ -31,7 +31,7 @@ sudo ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
|
|||||||
|
|
||||||
##### Installation Instructions
|
##### Installation Instructions
|
||||||
|
|
||||||
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`
|
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`
|
||||||
2. Install prerequisites (type `1` and press enter)
|
2. Install prerequisites (type `1` and press enter)
|
||||||
3. Download the bot (type `2` and press enter)
|
3. Download the bot (type `2` and press enter)
|
||||||
4. Exit the installer in order to set up your `creds.yml`
|
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
|
##### Update Instructions
|
||||||
|
|
||||||
1. ⚠ Stop the bot
|
1. ⚠ Stop the bot
|
||||||
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`
|
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`
|
||||||
3. Update the bot (type `2` and press enter)
|
3. Update the bot (type `2` and press enter)
|
||||||
4. Run the bot (type `3` and press enter)
|
4. Run the bot (type `3` and press enter)
|
||||||
5. 🎉
|
5. 🎉
|
||||||
|
@@ -1,3 +1,9 @@
|
|||||||
|
## Migration from 2.x
|
||||||
|
|
||||||
|
⚠ If you're already hosting NadekoBot, You **MUST** update to latest version of 2.x and **run your bot at least once** before switching over to v3.
|
||||||
|
|
||||||
|
#### [Windows migration instructions](../migration-guide#windows)
|
||||||
|
|
||||||
## Setting Up NadekoBot on Windows With the Updater
|
## Setting Up NadekoBot on Windows With the Updater
|
||||||
|
|
||||||
| Table of Contents|
|
| Table of Contents|
|
||||||
@@ -42,8 +48,6 @@
|
|||||||
|
|
||||||
- Either click on **`RUN`** button in the updater or run the bot via its desktop shortcut.
|
- Either click on **`RUN`** button in the updater or run the bot via its desktop shortcut.
|
||||||
|
|
||||||
### If you get a "No owner channels created..." message. Please follow the creds guide again [**HERE**](../../creds-guide).
|
|
||||||
|
|
||||||
#### Updating Nadeko
|
#### Updating Nadeko
|
||||||
|
|
||||||
- Make sure Nadeko is closed and not running
|
- Make sure Nadeko is closed and not running
|
||||||
@@ -70,7 +74,7 @@ You can still install them manually:
|
|||||||
##### Prerequisites
|
##### Prerequisites
|
||||||
|
|
||||||
**Install these before proceeding or your bot will not work!**
|
**Install these before proceeding or your bot will not work!**
|
||||||
- [.net 6](https://dotnet.microsoft.com/download/dotnet/6.0) - needed to compile and run the bot
|
- [.net 5](https://dotnet.microsoft.com/download/dotnet/5.0) - needed to compile and run the bot
|
||||||
- [git](https://git-scm.com/downloads) - needed to clone the repository (you can also download the zip manually and extract it, but this guide assumes you're using git)
|
- [git](https://git-scm.com/downloads) - needed to clone the repository (you can also download the zip manually and extract it, but this guide assumes you're using git)
|
||||||
- [redis](https://github.com/MicrosoftArchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.msi) - to cache things needed by some features and persist through restarts
|
- [redis](https://github.com/MicrosoftArchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.msi) - to cache things needed by some features and persist through restarts
|
||||||
|
|
||||||
@@ -78,7 +82,7 @@ You can still install them manually:
|
|||||||
|
|
||||||
Open PowerShell (press windows button on your keyboard and type powershell, it should show up; alternatively, right click the start menu and select Windows PowerShell), and navigate to the location where you want to install the bot (for example `cd ~/Desktop/`)
|
Open PowerShell (press windows button on your keyboard and type powershell, it should show up; alternatively, right click the start menu and select Windows PowerShell), and navigate to the location where you want to install the bot (for example `cd ~/Desktop/`)
|
||||||
|
|
||||||
1. `git clone https://gitlab.com/kwoth/nadekobot -b v4 --depth 1`
|
1. `git clone https://gitlab.com/kwoth/nadekobot -b v3 --depth 1`
|
||||||
2. `cd nadekobot`
|
2. `cd nadekobot`
|
||||||
3. `dotnet publish -c Release -o output/ src/NadekoBot/`
|
3. `dotnet publish -c Release -o output/ src/NadekoBot/`
|
||||||
4. `cd output`
|
4. `cd output`
|
||||||
@@ -122,7 +126,7 @@ In order to use music commands, you need ffmpeg and youtube-dl installed.
|
|||||||
- [ffmpeg-32bit] | [ffmpeg-64bit] - Download the **appropriate version** for your system (32 bit if you're running a 32 bit OS, or 64 if you're running a 64bit OS). Unzip it, and move `ffmpeg.exe` to a path that's in your PATH environment variable. If you don't know what that is, just move the `ffmpeg.exe` file to `NadekoBot/output`.
|
- [ffmpeg-32bit] | [ffmpeg-64bit] - Download the **appropriate version** for your system (32 bit if you're running a 32 bit OS, or 64 if you're running a 64bit OS). Unzip it, and move `ffmpeg.exe` to a path that's in your PATH environment variable. If you don't know what that is, just move the `ffmpeg.exe` file to `NadekoBot/output`.
|
||||||
- [youtube-dl] - Click to download the file, then move `youtube-dl.exe` to a path that's in your PATH environment variable. If you don't know what that is, just move the `youtube-dl.exe` file to `NadekoBot/system`.
|
- [youtube-dl] - Click to download the file, then move `youtube-dl.exe` to a path that's in your PATH environment variable. If you don't know what that is, just move the `youtube-dl.exe` file to `NadekoBot/system`.
|
||||||
|
|
||||||
[Updater]: https://dl.nadeko.bot/
|
[Updater]: https://dl.nadeko.bot/v3
|
||||||
[Notepad++]: https://notepad-plus-plus.org/
|
[Notepad++]: https://notepad-plus-plus.org/
|
||||||
[.net]: https://dotnet.microsoft.com/download/dotnet/5.0
|
[.net]: https://dotnet.microsoft.com/download/dotnet/5.0
|
||||||
[Redis]: https://github.com/MicrosoftArchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.msi
|
[Redis]: https://github.com/MicrosoftArchive/redis/releases/download/win-3.0.504/Redis-x64-3.0.504.msi
|
||||||
|
@@ -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 `:`.
|
- Open up `creds.yml` and look for `GoogleAPIKey`, paste your API key after the `:`.
|
||||||
- It should look like this:
|
- It should look like this:
|
||||||
```yml
|
```yml
|
||||||
GoogleApiKey: 'AIzaSyDSci1sdlWQOWNVj1vlXxxxxxbk0oWMEzM'
|
GoogleApiKey: AIzaSyDSci1sdlWQOWNVj1vlXxxxxxbk0oWMEzM
|
||||||
```
|
```
|
||||||
- **MashapeKey**
|
- **MashapeKey**
|
||||||
- Required for Hearthstone cards.
|
- Required for Hearthstone cards.
|
||||||
@@ -40,19 +40,16 @@ This part is completely optional, **however it's necessary for music and a few o
|
|||||||
- For Patreon creators only.
|
- For Patreon creators only.
|
||||||
- **PatreonCampaignId**
|
- **PatreonCampaignId**
|
||||||
- For Patreon creators only. Id of your campaign.
|
- For Patreon creators only. Id of your campaign.
|
||||||
- **TwitchClientId and TwitchClientSecret**
|
- **TwitchClientId**
|
||||||
- Mandatory for following twitch streams with `.twitch` (or `.stadd` with twitch link)
|
- Mandatory for following twitch streams with `.twitch` (or `.stadd` with twitch link)
|
||||||
- Go to [apps page](https://dev.twitch.tv/console) on twitch and register your application.
|
- Go to [apps page](https://dev.twitch.tv/console/apps/create) on twitch and register your application.
|
||||||
- You need 2FA enabled on twitch in order to create an 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)
|
- You can set `http://localhost` as the OAuth Redirect URL (and press Add button)
|
||||||
- Select `Chat Bot` from the Category dropdown
|
- Select `Chat Bot` from the Category dropdown
|
||||||
- Once created, `click Manage`
|
- Once created, clicking on your application will show a new Client ID field
|
||||||
- Click `New Secret` and select `OK` in the popup
|
- Copy it to your creds.yml as shown below
|
||||||
**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
|
```yml
|
||||||
twitchClientId: 516tr61tr1qweqwe86trg3g
|
TwitchClientId: "516tr61tr1qweqwe86trg3g"
|
||||||
twitchClientSecret: 16tr61tr1q86tweqwe
|
|
||||||
```
|
```
|
||||||
- **LocationIqApiKey**
|
- **LocationIqApiKey**
|
||||||
- Optional. Used only for the `.time` command. https://locationiq.com api key (register and you will receive the token in the email).
|
- Optional. Used only for the `.time` command. https://locationiq.com api key (register and you will receive the token in the email).
|
||||||
@@ -78,7 +75,6 @@ For Windows (Updater), add this to your `creds.yml`
|
|||||||
```yml
|
```yml
|
||||||
RestartCommand:
|
RestartCommand:
|
||||||
Cmd: "NadekoBot.exe"
|
Cmd: "NadekoBot.exe"
|
||||||
args: "{0}"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
For Windows (Source), Linux or OSX, add this to your `creds.yml`
|
For Windows (Source), Linux or OSX, add this to your `creds.yml`
|
||||||
@@ -97,7 +93,7 @@ RestartCommand:
|
|||||||
|
|
||||||
```yml
|
```yml
|
||||||
# DO NOT CHANGE
|
# DO NOT CHANGE
|
||||||
version: 4
|
version: 1
|
||||||
# Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/
|
# Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/
|
||||||
token: 'MTE5Nzc3MDIxMzE5NTc3NjEw.VlhNCw.BuqJFyzdIUAK1PRf1eK1Cu89Jew'
|
token: 'MTE5Nzc3MDIxMzE5NTc3NjEw.VlhNCw.BuqJFyzdIUAK1PRf1eK1Cu89Jew'
|
||||||
# List of Ids of the users who have bot owner permissions
|
# List of Ids of the users who have bot owner permissions
|
||||||
@@ -156,13 +152,6 @@ timezoneDbApiKey:
|
|||||||
coinmarketcapApiKey:
|
coinmarketcapApiKey:
|
||||||
# Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api
|
# Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api
|
||||||
osuApiKey: 4c8c8fdffdsfdsfsdfsfa33f3f3140a7d93320d6
|
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.
|
# Command and args which will be used to restart the bot.
|
||||||
# Only used if bot is executed directly (NOT through the coordinator)
|
# Only used if bot is executed directly (NOT through the coordinator)
|
||||||
# placeholders:
|
# placeholders:
|
||||||
@@ -172,8 +161,8 @@ twitchClientSecret: 16tr61tr1q86tweqwe
|
|||||||
# cmd: dotnet
|
# cmd: dotnet
|
||||||
# args: "NadekoBot.dll -- {0}"
|
# args: "NadekoBot.dll -- {0}"
|
||||||
# Windows default
|
# Windows default
|
||||||
# cmd: "NadekoBot.exe"
|
# cmd: NadekoBot.exe
|
||||||
# args: "{0}"
|
# args: {0}
|
||||||
restartCommand:
|
restartCommand:
|
||||||
cmd:
|
cmd:
|
||||||
args:
|
args:
|
||||||
|
@@ -1,251 +0,0 @@
|
|||||||
# 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!
|
|
@@ -1,31 +0,0 @@
|
|||||||
## 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)
|
|
||||||
|
|
@@ -1,19 +0,0 @@
|
|||||||
# 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
|
|
||||||
|
|
@@ -28,8 +28,6 @@ Some features have their own specific placeholders which are noted in that featu
|
|||||||
- `%server.id%` - Server ID
|
- `%server.id%` - Server ID
|
||||||
- `%server.name%` - Server name
|
- `%server.name%` - Server name
|
||||||
- `%server.members%` - Member count
|
- `%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)
|
- `%server.time%` - Server time (requires `.timezone` to be set)
|
||||||
|
|
||||||
### Channel placeholders
|
### Channel placeholders
|
||||||
@@ -81,9 +79,11 @@ Some features have their own specific placeholders which are noted in that featu
|
|||||||
|
|
||||||
### Music placeholders
|
### Music placeholders
|
||||||
|
|
||||||
- `%music.queued%` - Number of songs currently queued
|
!!! Note
|
||||||
- `%music.playing%` - Current song name (random playing song if bot is playing on multiple servers)
|
These placeholders will only work in rotating playing statuses.
|
||||||
- `%music.servers%` - Number of servers currently listening to music
|
|
||||||
|
- `%music.queued%` - Amount of songs currently queued
|
||||||
|
- `%music.playing%` - Current song name
|
||||||
|
|
||||||
### Miscellaneous placeholders
|
### Miscellaneous placeholders
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
#define sysfolder "system"
|
#define sysfolder "system"
|
||||||
#define version GetEnv("NADEKOBOT_INSTALL_VERSION")
|
#define version GetEnv("NADEKOBOT_INSTALL_VERSION")
|
||||||
#define target "win7-x64"
|
#define target "win7-x64"
|
||||||
#define platform "net6.0"
|
#define platform "net5.0"
|
||||||
|
|
||||||
[Setup]
|
[Setup]
|
||||||
AppName = {param:botname|NadekoBot}
|
AppName = {param:botname|NadekoBot}
|
||||||
|
@@ -72,6 +72,7 @@ markdown_extensions:
|
|||||||
nav:
|
nav:
|
||||||
- Home: index.md
|
- Home: index.md
|
||||||
- Guides:
|
- Guides:
|
||||||
|
- ❗ Migration Guide: guides/migration-guide.md
|
||||||
- Windows Guide: guides/windows-guide.md
|
- Windows Guide: guides/windows-guide.md
|
||||||
- Linux Guide: guides/linux-guide.md
|
- Linux Guide: guides/linux-guide.md
|
||||||
- OSX Guide: guides/osx-guide.md
|
- OSX Guide: guides/osx-guide.md
|
||||||
@@ -86,10 +87,7 @@ nav:
|
|||||||
- Custom Reactions: custom-reactions.md
|
- Custom Reactions: custom-reactions.md
|
||||||
- Placeholders: placeholders.md
|
- Placeholders: placeholders.md
|
||||||
- Config: config-guide.md
|
- Config: config-guide.md
|
||||||
- Medusa System:
|
- Bot Config: bce-guide.md
|
||||||
- medusa/getting-started.md
|
|
||||||
- medusa/creating-a-medusa.md
|
|
||||||
- medusa/snek-lifecycle.md
|
|
||||||
- Contribution Guide: contribution-guide.md
|
- Contribution Guide: contribution-guide.md
|
||||||
- Donate: donate.md
|
- Donate: donate.md
|
||||||
- License: license.md
|
- License: license.md
|
||||||
|
@@ -1,10 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
@@ -1,37 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,10 +0,0 @@
|
|||||||
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
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
@@ -1,10 +0,0 @@
|
|||||||
namespace Nadeko.Snake;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Marks the parameter to take
|
|
||||||
/// </summary>
|
|
||||||
[AttributeUsage(AttributeTargets.Parameter)]
|
|
||||||
public class leftoverAttribute : Attribute
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
@@ -1,20 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,23 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@@ -1,47 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
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; }
|
|
||||||
}
|
|
@@ -1,12 +0,0 @@
|
|||||||
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; }
|
|
||||||
}
|
|
@@ -1,8 +0,0 @@
|
|||||||
namespace NadekoBot;
|
|
||||||
|
|
||||||
public enum EmbedColor
|
|
||||||
{
|
|
||||||
Ok,
|
|
||||||
Pending,
|
|
||||||
Error
|
|
||||||
}
|
|
@@ -1,14 +0,0 @@
|
|||||||
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);
|
|
||||||
|
|
||||||
}
|
|
@@ -1,66 +0,0 @@
|
|||||||
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)}");
|
|
||||||
}
|
|
@@ -1,17 +0,0 @@
|
|||||||
using Discord;
|
|
||||||
|
|
||||||
namespace NadekoBot;
|
|
||||||
|
|
||||||
public interface IEmbedBuilder
|
|
||||||
{
|
|
||||||
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 WithColor(EmbedColor color);
|
|
||||||
Embed Build();
|
|
||||||
IEmbedBuilder WithUrl(string url);
|
|
||||||
IEmbedBuilder WithImageUrl(string url);
|
|
||||||
IEmbedBuilder WithThumbnailUrl(string url);
|
|
||||||
}
|
|
@@ -1,21 +0,0 @@
|
|||||||
<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.6.1" />
|
|
||||||
<PackageReference Include="Serilog" Version="2.11.0" />
|
|
||||||
<PackageReference Include="YamlDotNet" Version="11.2.1" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
@@ -1,16 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
@@ -1,48 +0,0 @@
|
|||||||
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,
|
|
||||||
};
|
|
||||||
}
|
|
@@ -1 +0,0 @@
|
|||||||
This is the library which is the base of any medusa.
|
|
@@ -1,143 +0,0 @@
|
|||||||
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
|
|
||||||
{
|
|
||||||
}
|
|
@@ -1,24 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,15 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
@@ -1,28 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
@@ -1,40 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,79 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
@@ -1,137 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
@@ -1,2 +0,0 @@
|
|||||||
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
|
|
@@ -12,7 +12,9 @@ namespace NadekoBot.Coordinator
|
|||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
public CoordStartup(IConfiguration config)
|
public CoordStartup(IConfiguration config)
|
||||||
=> Configuration = config;
|
{
|
||||||
|
Configuration = config;
|
||||||
|
}
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
@@ -15,16 +15,13 @@ namespace NadekoBot.Services
|
|||||||
.MinimumLevel.Override("System", LogEventLevel.Information)
|
.MinimumLevel.Override("System", LogEventLevel.Information)
|
||||||
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.WriteTo.File("coord.log", LogEventLevel.Information,
|
|
||||||
rollOnFileSizeLimit: true,
|
|
||||||
fileSizeLimitBytes: 10_000_000)
|
|
||||||
.WriteTo.Console(LogEventLevel.Information,
|
.WriteTo.Console(LogEventLevel.Information,
|
||||||
theme: GetTheme(),
|
theme: GetTheme(),
|
||||||
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] | #{LogSource} | {Message:lj}{NewLine}{Exception}")
|
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] | #{LogSource} | {Message:lj}{NewLine}{Exception}")
|
||||||
.Enrich.WithProperty("LogSource", source)
|
.Enrich.WithProperty("LogSource", source)
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
Console.OutputEncoding = Encoding.UTF8;
|
System.Console.OutputEncoding = Encoding.UTF8;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConsoleTheme GetTheme()
|
private static ConsoleTheme GetTheme()
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -9,10 +9,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Grpc.AspNetCore" Version="2.45.0" />
|
<PackageReference Include="Grpc.AspNetCore" Version="2.41.0" />
|
||||||
<PackageReference Include="Serilog" Version="2.11.0" />
|
<PackageReference Include="Serilog" Version="2.10.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
|
||||||
<PackageReference Include="YamlDotNet" Version="11.2.1" />
|
<PackageReference Include="YamlDotNet" Version="11.2.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@ using System.Linq;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using YamlDotNet.Serialization;
|
using YamlDotNet.Serialization;
|
||||||
@@ -29,7 +30,7 @@ namespace NadekoBot.Coordinator
|
|||||||
private readonly Random _rng;
|
private readonly Random _rng;
|
||||||
private bool _gracefulImminent;
|
private bool _gracefulImminent;
|
||||||
|
|
||||||
public CoordinatorRunner()
|
public CoordinatorRunner(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_serializer = new();
|
_serializer = new();
|
||||||
_deserializer = new();
|
_deserializer = new();
|
||||||
@@ -90,7 +91,7 @@ namespace NadekoBot.Coordinator
|
|||||||
var shardIds = Enumerable.Range(0, 1) // shard 0 is always first
|
var shardIds = Enumerable.Range(0, 1) // shard 0 is always first
|
||||||
.Append((int)((117523346618318850 >> 22) % _config.TotalShards)) // then nadeko server shard
|
.Append((int)((117523346618318850 >> 22) % _config.TotalShards)) // then nadeko server shard
|
||||||
.Concat(Enumerable.Range(1, _config.TotalShards - 1)
|
.Concat(Enumerable.Range(1, _config.TotalShards - 1)
|
||||||
.OrderBy(_ => _rng.Next())) // then all other shards in a random order
|
.OrderBy(x => _rng.Next())) // then all other shards in a random order
|
||||||
.Distinct()
|
.Distinct()
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
@@ -115,6 +116,14 @@ namespace NadekoBot.Coordinator
|
|||||||
break;
|
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 >
|
if (DateTime.UtcNow - status.LastUpdate >
|
||||||
TimeSpan.FromSeconds(_config.UnresponsiveSec))
|
TimeSpan.FromSeconds(_config.UnresponsiveSec))
|
||||||
{
|
{
|
||||||
@@ -131,24 +140,6 @@ namespace NadekoBot.Coordinator
|
|||||||
StartShard(shardId);
|
StartShard(shardId);
|
||||||
break;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,13 +162,17 @@ namespace NadekoBot.Coordinator
|
|||||||
var status = _shardStatuses[shardId];
|
var status = _shardStatuses[shardId];
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
status.Process?.Kill(true);
|
if (status.Process is { HasExited: false } p)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
p.Kill(true);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
try
|
}
|
||||||
{
|
|
||||||
status.Process?.Dispose();
|
status.Process?.Dispose();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -196,7 +191,8 @@ namespace NadekoBot.Coordinator
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Process StartShardProcess(int shardId)
|
private Process StartShardProcess(int shardId)
|
||||||
=> Process.Start(new ProcessStartInfo()
|
{
|
||||||
|
return Process.Start(new ProcessStartInfo()
|
||||||
{
|
{
|
||||||
FileName = _config.ShardStartCommand,
|
FileName = _config.ShardStartCommand,
|
||||||
Arguments = string.Format(_config.ShardStartArgs,
|
Arguments = string.Format(_config.ShardStartArgs,
|
||||||
@@ -209,6 +205,7 @@ namespace NadekoBot.Coordinator
|
|||||||
// CreateNoWindow = true,
|
// CreateNoWindow = true,
|
||||||
// UseShellExecute = false,
|
// UseShellExecute = false,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public bool Heartbeat(int shardId, int guildCount, ConnState state)
|
public bool Heartbeat(int shardId, int guildCount, ConnState state)
|
||||||
{
|
{
|
||||||
@@ -242,6 +239,7 @@ namespace NadekoBot.Coordinator
|
|||||||
{
|
{
|
||||||
lock (locker)
|
lock (locker)
|
||||||
{
|
{
|
||||||
|
ref var toSave = ref _config;
|
||||||
SaveConfig(new Config(
|
SaveConfig(new Config(
|
||||||
totalShards,
|
totalShards,
|
||||||
_config.RecheckIntervalMs,
|
_config.RecheckIntervalMs,
|
||||||
@@ -288,8 +286,8 @@ namespace NadekoBot.Coordinator
|
|||||||
var status = _shardStatuses[shardId];
|
var status = _shardStatuses[shardId];
|
||||||
if (status.Process is Process p)
|
if (status.Process is Process p)
|
||||||
{
|
{
|
||||||
try{p.Kill();} catch {}
|
p.Kill();
|
||||||
try{p.Dispose();} catch {}
|
p.Dispose();
|
||||||
_shardStatuses[shardId] = status with
|
_shardStatuses[shardId] = status with
|
||||||
{
|
{
|
||||||
Process = null,
|
Process = null,
|
||||||
@@ -316,7 +314,7 @@ namespace NadekoBot.Coordinator
|
|||||||
})
|
})
|
||||||
.ToList()
|
.ToList()
|
||||||
};
|
};
|
||||||
var jsonState = JsonSerializer.Serialize(coordState, new JsonSerializerOptions()
|
var jsonState = JsonSerializer.Serialize(coordState, new ()
|
||||||
{
|
{
|
||||||
WriteIndented = true,
|
WriteIndented = true,
|
||||||
});
|
});
|
||||||
@@ -348,7 +346,7 @@ namespace NadekoBot.Coordinator
|
|||||||
|
|
||||||
if (savedState.StatusObjects.Count != _config.TotalShards)
|
if (savedState.StatusObjects.Count != _config.TotalShards)
|
||||||
{
|
{
|
||||||
Log.Error("Unable to restore old state because shard count doesn't match");
|
Log.Error("Unable to restore old state because shard count doesn't match.");
|
||||||
File.Move(GRACEFUL_STATE_PATH, GRACEFUL_STATE_BACKUP_PATH, overwrite: true);
|
File.Move(GRACEFUL_STATE_PATH, GRACEFUL_STATE_BACKUP_PATH, overwrite: true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -359,7 +357,7 @@ namespace NadekoBot.Coordinator
|
|||||||
{
|
{
|
||||||
var statusObj = savedState.StatusObjects[shardId];
|
var statusObj = savedState.StatusObjects[shardId];
|
||||||
Process p = null;
|
Process p = null;
|
||||||
if (statusObj.Pid is { } pid)
|
if (statusObj.Pid is int pid)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -367,7 +365,7 @@ namespace NadekoBot.Coordinator
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Log.Warning(ex, "Process for shard {ShardId} is not runnning", shardId);
|
Log.Warning(ex, $"Process for shard {shardId} is not runnning.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,7 +441,9 @@ namespace NadekoBot.Coordinator
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string GetConfigText()
|
public string GetConfigText()
|
||||||
=> File.ReadAllText(CONFIG_PATH);
|
{
|
||||||
|
return File.ReadAllText(CONFIG_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
public void SetConfigText(string text)
|
public void SetConfigText(string text)
|
||||||
{
|
{
|
||||||
|
@@ -5,12 +5,14 @@ using Grpc.Core;
|
|||||||
|
|
||||||
namespace NadekoBot.Coordinator
|
namespace NadekoBot.Coordinator
|
||||||
{
|
{
|
||||||
public sealed class CoordinatorService : Coordinator.CoordinatorBase
|
public sealed class CoordinatorService : NadekoBot.Coordinator.Coordinator.CoordinatorBase
|
||||||
{
|
{
|
||||||
private readonly CoordinatorRunner _runner;
|
private readonly CoordinatorRunner _runner;
|
||||||
|
|
||||||
public CoordinatorService(CoordinatorRunner runner)
|
public CoordinatorService(CoordinatorRunner runner)
|
||||||
=> _runner = runner;
|
{
|
||||||
|
_runner = runner;
|
||||||
|
}
|
||||||
|
|
||||||
public override Task<HeartbeatReply> Heartbeat(HeartbeatRequest request, ServerCallContext context)
|
public override Task<HeartbeatReply> Heartbeat(HeartbeatRequest request, ServerCallContext context)
|
||||||
{
|
{
|
||||||
@@ -111,10 +113,11 @@ namespace NadekoBot.Coordinator
|
|||||||
return new DieReply();
|
return new DieReply();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<SetConfigTextReply> SetConfigText(SetConfigTextRequest request, ServerCallContext context)
|
public override async Task<SetConfigTextReply> SetConfigText(SetConfigTextRequest request, ServerCallContext context)
|
||||||
{
|
{
|
||||||
var error = string.Empty;
|
await Task.Yield();
|
||||||
var success = true;
|
string error = string.Empty;
|
||||||
|
bool success = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_runner.SetConfigText(request.ConfigYml);
|
_runner.SetConfigText(request.ConfigYml);
|
||||||
@@ -125,11 +128,11 @@ namespace NadekoBot.Coordinator
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.FromResult<SetConfigTextReply>(new(new()
|
return new(new()
|
||||||
{
|
{
|
||||||
Success = success,
|
Success = success,
|
||||||
Error = error
|
Error = error
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<GetConfigTextReply> GetConfigText(GetConfigTextRequest request, ServerCallContext context)
|
public override Task<GetConfigTextReply> GetConfigText(GetConfigTextRequest request, ServerCallContext context)
|
||||||
|
@@ -1,4 +1,6 @@
|
|||||||
namespace NadekoBot.Coordinator
|
using System;
|
||||||
|
|
||||||
|
namespace NadekoBot.Coordinator
|
||||||
{
|
{
|
||||||
public class JsonStatusObject
|
public class JsonStatusObject
|
||||||
{
|
{
|
||||||
|
@@ -1,254 +0,0 @@
|
|||||||
// Code temporarily yeeted from
|
|
||||||
// https://github.com/mostmand/Cloneable/blob/master/Cloneable/CloneableGenerator.cs
|
|
||||||
// because of NRT issue
|
|
||||||
#nullable enable
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using Microsoft.CodeAnalysis.Text;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Cloneable
|
|
||||||
{
|
|
||||||
[Generator]
|
|
||||||
public class CloneableGenerator : ISourceGenerator
|
|
||||||
{
|
|
||||||
private const string PREVENT_DEEP_COPY_KEY_STRING = "PreventDeepCopy";
|
|
||||||
private const string EXPLICIT_DECLARATION_KEY_STRING = "ExplicitDeclaration";
|
|
||||||
|
|
||||||
private const string CLONEABLE_NAMESPACE = "Cloneable";
|
|
||||||
private const string CLONEABLE_ATTRIBUTE_STRING = "CloneableAttribute";
|
|
||||||
private const string CLONE_ATTRIBUTE_STRING = "CloneAttribute";
|
|
||||||
private const string IGNORE_CLONE_ATTRIBUTE_STRING = "IgnoreCloneAttribute";
|
|
||||||
|
|
||||||
private const string CLONEABLE_ATTRIBUTE_TEXT = @"// <AutoGenerated/>
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace " + CLONEABLE_NAMESPACE + @"
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = false)]
|
|
||||||
public sealed class " + CLONEABLE_ATTRIBUTE_STRING + @" : Attribute
|
|
||||||
{
|
|
||||||
public " + CLONEABLE_ATTRIBUTE_STRING + @"()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool " + EXPLICIT_DECLARATION_KEY_STRING + @" { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
";
|
|
||||||
|
|
||||||
private const string CLONE_PROPERTY_ATTRIBUTE_TEXT = @"// <AutoGenerated/>
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace " + CLONEABLE_NAMESPACE + @"
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
|
|
||||||
public sealed class " + CLONE_ATTRIBUTE_STRING + @" : Attribute
|
|
||||||
{
|
|
||||||
public " + CLONE_ATTRIBUTE_STRING + @"()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool " + PREVENT_DEEP_COPY_KEY_STRING + @" { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
";
|
|
||||||
|
|
||||||
private const string IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT = @"// <AutoGenerated/>
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace " + CLONEABLE_NAMESPACE + @"
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
|
|
||||||
public sealed class " + IGNORE_CLONE_ATTRIBUTE_STRING + @" : Attribute
|
|
||||||
{
|
|
||||||
public " + IGNORE_CLONE_ATTRIBUTE_STRING + @"()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
";
|
|
||||||
|
|
||||||
private INamedTypeSymbol? _cloneableAttribute;
|
|
||||||
private INamedTypeSymbol? _ignoreCloneAttribute;
|
|
||||||
private INamedTypeSymbol? _cloneAttribute;
|
|
||||||
|
|
||||||
public void Initialize(GeneratorInitializationContext context)
|
|
||||||
=> context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
|
||||||
|
|
||||||
public void Execute(GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
InjectCloneableAttributes(context);
|
|
||||||
GenerateCloneMethods(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateCloneMethods(GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Compilation compilation = GetCompilation(context);
|
|
||||||
|
|
||||||
InitAttributes(compilation);
|
|
||||||
|
|
||||||
var classSymbols = GetClassSymbols(compilation, receiver);
|
|
||||||
foreach (var classSymbol in classSymbols)
|
|
||||||
{
|
|
||||||
if (!classSymbol.TryGetAttribute(_cloneableAttribute!, out var attributes))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var attribute = attributes.Single();
|
|
||||||
var isExplicit = (bool?)attribute.NamedArguments.FirstOrDefault(e => e.Key.Equals(EXPLICIT_DECLARATION_KEY_STRING)).Value.Value ?? false;
|
|
||||||
context.AddSource($"{classSymbol.Name}_cloneable.g.cs", SourceText.From(CreateCloneableCode(classSymbol, isExplicit), Encoding.UTF8));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitAttributes(Compilation compilation)
|
|
||||||
{
|
|
||||||
_cloneableAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{CLONEABLE_ATTRIBUTE_STRING}")!;
|
|
||||||
_cloneAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{CLONE_ATTRIBUTE_STRING}")!;
|
|
||||||
_ignoreCloneAttribute = compilation.GetTypeByMetadataName($"{CLONEABLE_NAMESPACE}.{IGNORE_CLONE_ATTRIBUTE_STRING}")!;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Compilation GetCompilation(GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
var options = context.Compilation.SyntaxTrees.First().Options as CSharpParseOptions;
|
|
||||||
|
|
||||||
var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(CLONEABLE_ATTRIBUTE_TEXT, Encoding.UTF8), options)).
|
|
||||||
AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8), options)).
|
|
||||||
AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8), options));
|
|
||||||
return compilation;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string CreateCloneableCode(INamedTypeSymbol classSymbol, bool isExplicit)
|
|
||||||
{
|
|
||||||
string namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
|
||||||
var fieldAssignmentsCode = GenerateFieldAssignmentsCode(classSymbol, isExplicit).ToList();
|
|
||||||
var fieldAssignmentsCodeSafe = fieldAssignmentsCode.Select(x =>
|
|
||||||
{
|
|
||||||
if (x.isCloneable)
|
|
||||||
return x.line + "Safe(referenceChain)";
|
|
||||||
return x.line;
|
|
||||||
});
|
|
||||||
var fieldAssignmentsCodeFast = fieldAssignmentsCode.Select(x =>
|
|
||||||
{
|
|
||||||
if (x.isCloneable)
|
|
||||||
return x.line + "()";
|
|
||||||
return x.line;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $@"using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace {namespaceName}
|
|
||||||
{{
|
|
||||||
{GetAccessModifier(classSymbol)} partial class {classSymbol.Name}
|
|
||||||
{{
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a copy of {classSymbol.Name} with NO circular reference checking. This method should be used if performance matters.
|
|
||||||
///
|
|
||||||
/// <exception cref=""StackOverflowException"">Will occur on any object that has circular references in the hierarchy.</exception>
|
|
||||||
/// </summary>
|
|
||||||
public {classSymbol.Name} Clone()
|
|
||||||
{{
|
|
||||||
return new {classSymbol.Name}
|
|
||||||
{{
|
|
||||||
{string.Join(",\n", fieldAssignmentsCodeFast)}
|
|
||||||
}};
|
|
||||||
}}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a copy of {classSymbol.Name} with circular reference checking. If a circular reference was detected, only a reference of the leaf object is passed instead of cloning it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name=""referenceChain"">Should only be provided if specific objects should not be cloned but passed by reference instead.</param>
|
|
||||||
public {classSymbol.Name} CloneSafe(Stack<object> referenceChain = null)
|
|
||||||
{{
|
|
||||||
if(referenceChain?.Contains(this) == true)
|
|
||||||
return this;
|
|
||||||
referenceChain ??= new Stack<object>();
|
|
||||||
referenceChain.Push(this);
|
|
||||||
var result = new {classSymbol.Name}
|
|
||||||
{{
|
|
||||||
{string.Join($",\n", fieldAssignmentsCodeSafe)}
|
|
||||||
}};
|
|
||||||
referenceChain.Pop();
|
|
||||||
return result;
|
|
||||||
}}
|
|
||||||
}}
|
|
||||||
}}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private IEnumerable<(string line, bool isCloneable)> GenerateFieldAssignmentsCode(INamedTypeSymbol classSymbol, bool isExplicit )
|
|
||||||
{
|
|
||||||
var fieldNames = GetCloneableProperties(classSymbol, isExplicit);
|
|
||||||
|
|
||||||
var fieldAssignments = fieldNames.Select(field => IsFieldCloneable(field, classSymbol))
|
|
||||||
.OrderBy(x => x.isCloneable)
|
|
||||||
.Select(x => (GenerateAssignmentCode(x.item.Name, x.isCloneable), x.isCloneable));
|
|
||||||
return fieldAssignments;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GenerateAssignmentCode(string name, bool isCloneable)
|
|
||||||
{
|
|
||||||
if (isCloneable)
|
|
||||||
{
|
|
||||||
return $@" {name} = this.{name}?.Clone";
|
|
||||||
}
|
|
||||||
|
|
||||||
return $@" {name} = this.{name}";
|
|
||||||
}
|
|
||||||
|
|
||||||
private (IPropertySymbol item, bool isCloneable) IsFieldCloneable(IPropertySymbol x, INamedTypeSymbol classSymbol)
|
|
||||||
{
|
|
||||||
if (SymbolEqualityComparer.Default.Equals(x.Type, classSymbol))
|
|
||||||
{
|
|
||||||
return (x, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!x.Type.TryGetAttribute(_cloneableAttribute!, out var attributes))
|
|
||||||
{
|
|
||||||
return (x, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
var preventDeepCopy = (bool?)attributes.Single().NamedArguments.FirstOrDefault(e => e.Key.Equals(PREVENT_DEEP_COPY_KEY_STRING)).Value.Value ?? false;
|
|
||||||
return (item: x, !preventDeepCopy);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetAccessModifier(INamedTypeSymbol classSymbol)
|
|
||||||
=> classSymbol.DeclaredAccessibility.ToString().ToLowerInvariant();
|
|
||||||
|
|
||||||
private IEnumerable<IPropertySymbol> GetCloneableProperties(ITypeSymbol classSymbol, bool isExplicit)
|
|
||||||
{
|
|
||||||
var targetSymbolMembers = classSymbol.GetMembers().OfType<IPropertySymbol>()
|
|
||||||
.Where(x => x.SetMethod is not null &&
|
|
||||||
x.CanBeReferencedByName);
|
|
||||||
if (isExplicit)
|
|
||||||
{
|
|
||||||
return targetSymbolMembers.Where(x => x.HasAttribute(_cloneAttribute!));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return targetSymbolMembers.Where(x => !x.HasAttribute(_ignoreCloneAttribute!));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<INamedTypeSymbol> GetClassSymbols(Compilation compilation, SyntaxReceiver receiver)
|
|
||||||
=> receiver.CandidateClasses.Select(clazz => GetClassSymbol(compilation, clazz));
|
|
||||||
|
|
||||||
private static INamedTypeSymbol GetClassSymbol(Compilation compilation, ClassDeclarationSyntax clazz)
|
|
||||||
{
|
|
||||||
var model = compilation.GetSemanticModel(clazz.SyntaxTree);
|
|
||||||
var classSymbol = model.GetDeclaredSymbol(clazz)!;
|
|
||||||
return classSymbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void InjectCloneableAttributes(GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
context.AddSource(CLONEABLE_ATTRIBUTE_STRING, SourceText.From(CLONEABLE_ATTRIBUTE_TEXT, Encoding.UTF8));
|
|
||||||
context.AddSource(CLONE_ATTRIBUTE_STRING, SourceText.From(CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8));
|
|
||||||
context.AddSource(IGNORE_CLONE_ATTRIBUTE_STRING, SourceText.From(IGNORE_CLONE_PROPERTY_ATTRIBUTE_TEXT, Encoding.UTF8));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,24 +0,0 @@
|
|||||||
// Code temporarily yeeted from
|
|
||||||
// https://github.com/mostmand/Cloneable/blob/master/Cloneable/CloneableGenerator.cs
|
|
||||||
// because of NRT issue
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace Cloneable
|
|
||||||
{
|
|
||||||
internal static class SymbolExtensions
|
|
||||||
{
|
|
||||||
public static bool TryGetAttribute(this ISymbol symbol, INamedTypeSymbol attributeType,
|
|
||||||
out IEnumerable<AttributeData> attributes)
|
|
||||||
{
|
|
||||||
attributes = symbol.GetAttributes()
|
|
||||||
.Where(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
|
|
||||||
return attributes.Any();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attributeType)
|
|
||||||
=> symbol.GetAttributes()
|
|
||||||
.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,27 +0,0 @@
|
|||||||
// Code temporarily yeeted from
|
|
||||||
// https://github.com/mostmand/Cloneable/blob/master/Cloneable/CloneableGenerator.cs
|
|
||||||
// because of NRT issue
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
|
|
||||||
namespace Cloneable
|
|
||||||
{
|
|
||||||
internal class SyntaxReceiver : ISyntaxReceiver
|
|
||||||
{
|
|
||||||
public IList<ClassDeclarationSyntax> CandidateClasses { get; } = new List<ClassDeclarationSyntax>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called for every syntax node in the compilation, we can inspect the nodes and save any information useful for generation
|
|
||||||
/// </summary>
|
|
||||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
||||||
{
|
|
||||||
// any field with at least one attribute is a candidate for being cloneable
|
|
||||||
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
|
|
||||||
classDeclarationSyntax.AttributeLists.Count > 0)
|
|
||||||
{
|
|
||||||
CandidateClasses.Add(classDeclarationSyntax);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,322 +0,0 @@
|
|||||||
#nullable enable
|
|
||||||
using System;
|
|
||||||
using System.CodeDom.Compiler;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using Microsoft.CodeAnalysis.Text;
|
|
||||||
|
|
||||||
namespace NadekoBot.Generators.Command;
|
|
||||||
|
|
||||||
[Generator]
|
|
||||||
public class CommandAttributesGenerator : IIncrementalGenerator
|
|
||||||
{
|
|
||||||
public const string ATTRIBUTE = @"// <AutoGenerated />
|
|
||||||
|
|
||||||
namespace NadekoBot.Common;
|
|
||||||
|
|
||||||
[System.AttributeUsage(System.AttributeTargets.Method)]
|
|
||||||
public class CmdAttribute : System.Attribute
|
|
||||||
{
|
|
||||||
|
|
||||||
}";
|
|
||||||
|
|
||||||
public class MethodModel
|
|
||||||
{
|
|
||||||
public string? Namespace { get; }
|
|
||||||
public IReadOnlyCollection<string> Classes { get; }
|
|
||||||
public string ReturnType { get; }
|
|
||||||
public string MethodName { get; }
|
|
||||||
public IEnumerable<string> Params { get; }
|
|
||||||
|
|
||||||
public MethodModel(string? ns, IReadOnlyCollection<string> classes, string returnType, string methodName, IEnumerable<string> @params)
|
|
||||||
{
|
|
||||||
Namespace = ns;
|
|
||||||
Classes = classes;
|
|
||||||
ReturnType = returnType;
|
|
||||||
MethodName = methodName;
|
|
||||||
Params = @params;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class FileModel
|
|
||||||
{
|
|
||||||
public string? Namespace { get; }
|
|
||||||
public IReadOnlyCollection<string> ClassHierarchy { get; }
|
|
||||||
public IReadOnlyCollection<MethodModel> Methods { get; }
|
|
||||||
|
|
||||||
public FileModel(string? ns, IReadOnlyCollection<string> classHierarchy, IReadOnlyCollection<MethodModel> methods)
|
|
||||||
{
|
|
||||||
Namespace = ns;
|
|
||||||
ClassHierarchy = classHierarchy;
|
|
||||||
Methods = methods;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
||||||
{
|
|
||||||
// #if DEBUG
|
|
||||||
// SpinWait.SpinUntil(() => Debugger.IsAttached);
|
|
||||||
// #endif
|
|
||||||
context.RegisterPostInitializationOutput(static ctx => ctx.AddSource(
|
|
||||||
"CmdAttribute.g.cs",
|
|
||||||
SourceText.From(ATTRIBUTE, Encoding.UTF8)));
|
|
||||||
|
|
||||||
var methods = context.SyntaxProvider
|
|
||||||
.CreateSyntaxProvider(
|
|
||||||
static (node, _) => node is MethodDeclarationSyntax { AttributeLists.Count: > 0 },
|
|
||||||
static (ctx, cancel) => Transform(ctx, cancel))
|
|
||||||
.Where(static m => m is not null)
|
|
||||||
.Where(static m => m?.ChildTokens().Any(static x => x.IsKind(SyntaxKind.PublicKeyword)) ?? false);
|
|
||||||
|
|
||||||
var compilationMethods = context.CompilationProvider.Combine(methods.Collect());
|
|
||||||
|
|
||||||
context.RegisterSourceOutput(compilationMethods,
|
|
||||||
static (ctx, tuple) => RegisterAction(in ctx, tuple.Left, in tuple.Right));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void RegisterAction(in SourceProductionContext ctx,
|
|
||||||
Compilation comp,
|
|
||||||
in ImmutableArray<MethodDeclarationSyntax?> methods)
|
|
||||||
{
|
|
||||||
if (methods is { IsDefaultOrEmpty: true })
|
|
||||||
return;
|
|
||||||
|
|
||||||
var models = GetModels(comp, methods, ctx.CancellationToken);
|
|
||||||
|
|
||||||
foreach (var model in models)
|
|
||||||
{
|
|
||||||
var name = $"{model.Namespace}.{string.Join(".", model.ClassHierarchy)}.g.cs";
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var source = GetSourceText(model);
|
|
||||||
ctx.AddSource(name, SourceText.From(source, Encoding.UTF8));
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Error writing source file {name}\n" + ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetSourceText(FileModel model)
|
|
||||||
{
|
|
||||||
using var sw = new StringWriter();
|
|
||||||
using var tw = new IndentedTextWriter(sw);
|
|
||||||
|
|
||||||
tw.WriteLine("// <AutoGenerated />");
|
|
||||||
tw.WriteLine("#pragma warning disable CS1066");
|
|
||||||
|
|
||||||
if (model.Namespace is not null)
|
|
||||||
{
|
|
||||||
tw.WriteLine($"namespace {model.Namespace};");
|
|
||||||
tw.WriteLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var className in model.ClassHierarchy)
|
|
||||||
{
|
|
||||||
tw.WriteLine($"public partial class {className}");
|
|
||||||
tw.WriteLine("{");
|
|
||||||
tw.Indent ++;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var method in model.Methods)
|
|
||||||
{
|
|
||||||
tw.WriteLine("[NadekoCommand]");
|
|
||||||
tw.WriteLine("[NadekoDescription]");
|
|
||||||
tw.WriteLine("[Aliases]");
|
|
||||||
tw.WriteLine($"public partial {method.ReturnType} {method.MethodName}({string.Join(", ", method.Params)});");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var _ in model.ClassHierarchy)
|
|
||||||
{
|
|
||||||
tw.Indent --;
|
|
||||||
tw.WriteLine("}");
|
|
||||||
}
|
|
||||||
|
|
||||||
tw.Flush();
|
|
||||||
return sw.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IReadOnlyCollection<FileModel> GetModels(Compilation compilation,
|
|
||||||
in ImmutableArray<MethodDeclarationSyntax?> inputMethods,
|
|
||||||
CancellationToken cancel)
|
|
||||||
{
|
|
||||||
var models = new List<FileModel>();
|
|
||||||
|
|
||||||
var methods = inputMethods
|
|
||||||
.Where(static x => x is not null)
|
|
||||||
.Distinct();
|
|
||||||
|
|
||||||
var methodModels = methods
|
|
||||||
.Select(x => MethodDeclarationToMethodModel(compilation, x!));
|
|
||||||
|
|
||||||
var groups = methodModels
|
|
||||||
.GroupBy(static x => $"{x.Namespace}.{string.Join(".", x.Classes)}");
|
|
||||||
|
|
||||||
foreach (var group in groups)
|
|
||||||
{
|
|
||||||
if (cancel.IsCancellationRequested)
|
|
||||||
return new Collection<FileModel>();
|
|
||||||
|
|
||||||
if (group is null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var elems = group.ToList();
|
|
||||||
if (elems.Count is 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var model = new FileModel(
|
|
||||||
methods: elems,
|
|
||||||
ns: elems[0].Namespace,
|
|
||||||
classHierarchy: elems[0].Classes
|
|
||||||
);
|
|
||||||
|
|
||||||
models.Add(model);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return models;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MethodModel MethodDeclarationToMethodModel(Compilation comp, MethodDeclarationSyntax decl)
|
|
||||||
{
|
|
||||||
// SpinWait.SpinUntil(static () => Debugger.IsAttached);
|
|
||||||
|
|
||||||
var semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
|
|
||||||
var methodModel = new MethodModel(
|
|
||||||
@params: decl.ParameterList.Parameters
|
|
||||||
.Where(p => p.Type is not null)
|
|
||||||
.Select(p =>
|
|
||||||
{
|
|
||||||
var prefix = p.Modifiers.Any(static x => x.IsKind(SyntaxKind.ParamsKeyword))
|
|
||||||
? "params "
|
|
||||||
: string.Empty;
|
|
||||||
|
|
||||||
var type = semanticModel
|
|
||||||
.GetTypeInfo(p.Type!)
|
|
||||||
.Type
|
|
||||||
?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
|
|
||||||
|
|
||||||
|
|
||||||
var name = p.Identifier.Text;
|
|
||||||
|
|
||||||
var suffix = string.Empty;
|
|
||||||
if (p.Default is not null)
|
|
||||||
{
|
|
||||||
if (p.Default.Value is LiteralExpressionSyntax)
|
|
||||||
{
|
|
||||||
suffix = " = " + p.Default.Value;
|
|
||||||
}
|
|
||||||
else if (p.Default.Value is MemberAccessExpressionSyntax maes)
|
|
||||||
{
|
|
||||||
var maesSemModel = comp.GetSemanticModel(maes.SyntaxTree);
|
|
||||||
var sym = maesSemModel.GetSymbolInfo(maes.Name);
|
|
||||||
if (sym.Symbol is null)
|
|
||||||
{
|
|
||||||
suffix = " = " + p.Default.Value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
suffix = " = " + sym.Symbol.ToDisplayString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $"{prefix}{type} {name}{suffix}";
|
|
||||||
})
|
|
||||||
.ToList(),
|
|
||||||
methodName: decl.Identifier.Text,
|
|
||||||
returnType: decl.ReturnType.ToString(),
|
|
||||||
ns: GetNamespace(decl),
|
|
||||||
classes: GetClasses(decl)
|
|
||||||
);
|
|
||||||
|
|
||||||
return methodModel;
|
|
||||||
}
|
|
||||||
|
|
||||||
//https://github.com/andrewlock/NetEscapades.EnumGenerators/blob/main/src/NetEscapades.EnumGenerators/EnumGenerator.cs
|
|
||||||
static string? GetNamespace(MethodDeclarationSyntax declarationSyntax)
|
|
||||||
{
|
|
||||||
// determine the namespace the class is declared in, if any
|
|
||||||
string? nameSpace = null;
|
|
||||||
var parentOfInterest = declarationSyntax.Parent;
|
|
||||||
while (parentOfInterest is not null)
|
|
||||||
{
|
|
||||||
parentOfInterest = parentOfInterest.Parent;
|
|
||||||
|
|
||||||
if (parentOfInterest is BaseNamespaceDeclarationSyntax ns)
|
|
||||||
{
|
|
||||||
nameSpace = ns.Name.ToString();
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (ns.Parent is not NamespaceDeclarationSyntax parent)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ns = parent;
|
|
||||||
nameSpace = $"{ns.Name}.{nameSpace}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return nameSpace;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nameSpace;
|
|
||||||
}
|
|
||||||
|
|
||||||
static IReadOnlyCollection<string> GetClasses(MethodDeclarationSyntax declarationSyntax)
|
|
||||||
{
|
|
||||||
// determine the namespace the class is declared in, if any
|
|
||||||
var classes = new LinkedList<string>();
|
|
||||||
var parentOfInterest = declarationSyntax.Parent;
|
|
||||||
while (parentOfInterest is not null)
|
|
||||||
{
|
|
||||||
if (parentOfInterest is ClassDeclarationSyntax cds)
|
|
||||||
{
|
|
||||||
classes.AddFirst(cds.Identifier.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
parentOfInterest = parentOfInterest.Parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug.WriteLine($"Method {declarationSyntax.Identifier.Text} has {classes.Count} classes");
|
|
||||||
|
|
||||||
return classes;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MethodDeclarationSyntax? Transform(GeneratorSyntaxContext ctx, CancellationToken cancel)
|
|
||||||
{
|
|
||||||
var methodDecl = ctx.Node as MethodDeclarationSyntax;
|
|
||||||
if (methodDecl is null)
|
|
||||||
return default;
|
|
||||||
|
|
||||||
foreach (var attListSyntax in methodDecl.AttributeLists)
|
|
||||||
{
|
|
||||||
foreach (var attSyntax in attListSyntax.Attributes)
|
|
||||||
{
|
|
||||||
if (cancel.IsCancellationRequested)
|
|
||||||
return default;
|
|
||||||
|
|
||||||
var symbol = ctx.SemanticModel.GetSymbolInfo(attSyntax).Symbol;
|
|
||||||
if (symbol is not IMethodSymbol attSymbol)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (attSymbol.ContainingType.ToDisplayString() == "NadekoBot.Common.CmdAttribute")
|
|
||||||
return methodDecl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,32 +1,26 @@
|
|||||||
#nullable enable
|
using System;
|
||||||
using System;
|
|
||||||
using System.CodeDom.Compiler;
|
using System.CodeDom.Compiler;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.Text;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace NadekoBot.Generators
|
namespace NadekoBot.Generators
|
||||||
{
|
{
|
||||||
internal readonly struct TranslationPair
|
internal class TranslationPair
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
public string Name { get; set; }
|
||||||
public string Value { get; }
|
public string Value { get; set; }
|
||||||
|
|
||||||
public TranslationPair(string name, string value)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Generator]
|
[Generator]
|
||||||
public class LocalizedStringsGenerator : ISourceGenerator
|
public class LocalizedStringsGenerator : ISourceGenerator
|
||||||
{
|
{
|
||||||
private const string LOC_STR_SOURCE = @"namespace NadekoBot
|
private const string LocStrSource = @"namespace NadekoBot
|
||||||
{
|
{
|
||||||
public readonly struct LocStr
|
public readonly struct LocStr
|
||||||
{
|
{
|
||||||
@@ -55,14 +49,14 @@ namespace NadekoBot.Generators
|
|||||||
using (var stringWriter = new StringWriter())
|
using (var stringWriter = new StringWriter())
|
||||||
using (var sw = new IndentedTextWriter(stringWriter))
|
using (var sw = new IndentedTextWriter(stringWriter))
|
||||||
{
|
{
|
||||||
sw.WriteLine("namespace NadekoBot;");
|
sw.WriteLine("namespace NadekoBot");
|
||||||
sw.WriteLine();
|
sw.WriteLine("{");
|
||||||
|
sw.Indent++;
|
||||||
|
|
||||||
sw.WriteLine("public static class strs");
|
sw.WriteLine("public static class strs");
|
||||||
sw.WriteLine("{");
|
sw.WriteLine("{");
|
||||||
sw.Indent++;
|
sw.Indent++;
|
||||||
|
|
||||||
var typedParamStrings = new List<string>(10);
|
|
||||||
foreach (var field in fields)
|
foreach (var field in fields)
|
||||||
{
|
{
|
||||||
var matches = Regex.Matches(field.Value, @"{(?<num>\d)[}:]");
|
var matches = Regex.Matches(field.Value, @"{(?<num>\d)[}:]");
|
||||||
@@ -72,70 +66,50 @@ namespace NadekoBot.Generators
|
|||||||
max = Math.Max(max, int.Parse(match.Groups["num"].Value) + 1);
|
max = Math.Max(max, int.Parse(match.Groups["num"].Value) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedParamStrings.Clear();
|
List<string> typedParamStrings = new List<string>();
|
||||||
var typeParams = new string[max];
|
var paramStrings = string.Empty;
|
||||||
var passedParamString = string.Empty;
|
|
||||||
for (var i = 0; i < max; i++)
|
for (var i = 0; i < max; i++)
|
||||||
{
|
{
|
||||||
typedParamStrings.Add($"in T{i} p{i}");
|
typedParamStrings.Add($"object p{i}");
|
||||||
passedParamString += $", p{i}";
|
paramStrings += $", p{i}";
|
||||||
typeParams[i] = $"T{i}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var sig = string.Empty;
|
var sig = string.Empty;
|
||||||
var typeParamStr = string.Empty;
|
|
||||||
if(max > 0)
|
if(max > 0)
|
||||||
{
|
|
||||||
sig = $"({string.Join(", ", typedParamStrings)})";
|
sig = $"({string.Join(", ", typedParamStrings)})";
|
||||||
typeParamStr = $"<{string.Join(", ", typeParams)}>";
|
|
||||||
}
|
sw.WriteLine($"public static LocStr {field.Name}{sig} => new LocStr(\"{field.Name}\"{paramStrings});");
|
||||||
|
|
||||||
sw.WriteLine("public static LocStr {0}{1}{2} => new LocStr(\"{3}\"{4});",
|
|
||||||
field.Name,
|
|
||||||
typeParamStr,
|
|
||||||
sig,
|
|
||||||
field.Name,
|
|
||||||
passedParamString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sw.Indent--;
|
sw.Indent--;
|
||||||
sw.WriteLine("}");
|
sw.WriteLine("}");
|
||||||
|
sw.Indent--;
|
||||||
|
sw.WriteLine("}");
|
||||||
|
|
||||||
|
|
||||||
sw.Flush();
|
sw.Flush();
|
||||||
context.AddSource("strs.g.cs", stringWriter.ToString());
|
context.AddSource("strs.cs", stringWriter.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
context.AddSource("LocStr.g.cs", LOC_STR_SOURCE);
|
context.AddSource("LocStr.cs", LocStrSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<TranslationPair> GetFields(string? dataText)
|
private List<TranslationPair> GetFields(string dataText)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(dataText))
|
if (string.IsNullOrWhiteSpace(dataText))
|
||||||
return new();
|
throw new ArgumentNullException(nameof(dataText));
|
||||||
|
|
||||||
Dictionary<string, string> data;
|
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(dataText);
|
||||||
try
|
|
||||||
{
|
|
||||||
var output = JsonConvert.DeserializeObject<Dictionary<string, string>>(dataText!);
|
|
||||||
if (output is null)
|
|
||||||
return new();
|
|
||||||
|
|
||||||
data = output;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
Debug.WriteLine("Failed parsing responses file.");
|
|
||||||
return new();
|
|
||||||
}
|
|
||||||
|
|
||||||
var list = new List<TranslationPair>();
|
var list = new List<TranslationPair>();
|
||||||
foreach (var entry in data)
|
foreach (var entry in data)
|
||||||
{
|
{
|
||||||
list.Add(new(
|
list.Add(new TranslationPair()
|
||||||
entry.Key,
|
{
|
||||||
entry.Value
|
Name = entry.Key,
|
||||||
));
|
Value = entry.Value
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
@@ -1,15 +1,13 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
<IncludeBuildOutput>false</IncludeBuildOutput>
|
<IncludeBuildOutput>false</IncludeBuildOutput>
|
||||||
<IsRoslynComponent>true</IsRoslynComponent>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" PrivateAssets="all" GeneratePathProperty="true" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" PrivateAssets="all" GeneratePathProperty="true" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@@ -14,7 +14,6 @@ namespace NadekoBot.Tests
|
|||||||
private const string responsesPath = "../../../../NadekoBot/data/strings/responses";
|
private const string responsesPath = "../../../../NadekoBot/data/strings/responses";
|
||||||
private const string commandsPath = "../../../../NadekoBot/data/strings/commands";
|
private const string commandsPath = "../../../../NadekoBot/data/strings/commands";
|
||||||
private const string aliasesPath = "../../../../NadekoBot/data/aliases.yml";
|
private const string aliasesPath = "../../../../NadekoBot/data/aliases.yml";
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void AllCommandNamesHaveStrings()
|
public void AllCommandNamesHaveStrings()
|
||||||
{
|
{
|
||||||
@@ -26,13 +25,15 @@ namespace NadekoBot.Tests
|
|||||||
var culture = new CultureInfo("en-US");
|
var culture = new CultureInfo("en-US");
|
||||||
|
|
||||||
var isSuccess = true;
|
var isSuccess = true;
|
||||||
foreach (var (methodName, _) in CommandNameLoadHelper.LoadAliases(aliasesPath))
|
foreach (var entry in CommandNameLoadHelper.LoadCommandNames(aliasesPath))
|
||||||
{
|
{
|
||||||
var cmdStrings = strings.GetCommandStrings(culture.Name, methodName);
|
var commandName = entry.Value[0];
|
||||||
|
|
||||||
|
var cmdStrings = strings.GetCommandStrings(culture.Name, commandName);
|
||||||
if (cmdStrings is null)
|
if (cmdStrings is null)
|
||||||
{
|
{
|
||||||
isSuccess = false;
|
isSuccess = false;
|
||||||
TestContext.Out.WriteLine($"{methodName} doesn't exist in commands.en-US.yml");
|
TestContext.Out.WriteLine($"{commandName} doesn't exist in commands.en-US.yml");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ namespace NadekoBot.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static string[] GetCommandMethodNames()
|
private static string[] GetCommandMethodNames()
|
||||||
=> typeof(Bot).Assembly
|
=> typeof(NadekoBot.Bot).Assembly
|
||||||
.GetExportedTypes()
|
.GetExportedTypes()
|
||||||
.Where(type => type.IsClass && !type.IsAbstract)
|
.Where(type => type.IsClass && !type.IsAbstract)
|
||||||
.Where(type => typeof(NadekoModule).IsAssignableFrom(type) // if its a top level module
|
.Where(type => typeof(NadekoModule).IsAssignableFrom(type) // if its a top level module
|
||||||
@@ -54,7 +55,7 @@ namespace NadekoBot.Tests
|
|||||||
[Test]
|
[Test]
|
||||||
public void AllCommandMethodsHaveNames()
|
public void AllCommandMethodsHaveNames()
|
||||||
{
|
{
|
||||||
var allAliases = CommandNameLoadHelper.LoadAliases(
|
var allAliases = CommandNameLoadHelper.LoadCommandNames(
|
||||||
aliasesPath);
|
aliasesPath);
|
||||||
|
|
||||||
var methodNames = GetCommandMethodNames();
|
var methodNames = GetCommandMethodNames();
|
||||||
@@ -62,7 +63,7 @@ namespace NadekoBot.Tests
|
|||||||
var isSuccess = true;
|
var isSuccess = true;
|
||||||
foreach (var methodName in methodNames)
|
foreach (var methodName in methodNames)
|
||||||
{
|
{
|
||||||
if (!allAliases.TryGetValue(methodName, out _))
|
if (!allAliases.TryGetValue(methodName, out var _))
|
||||||
{
|
{
|
||||||
TestContext.Error.WriteLine($"{methodName} is missing an alias.");
|
TestContext.Error.WriteLine($"{methodName} is missing an alias.");
|
||||||
isSuccess = false;
|
isSuccess = false;
|
||||||
@@ -75,7 +76,7 @@ namespace NadekoBot.Tests
|
|||||||
[Test]
|
[Test]
|
||||||
public void NoObsoleteAliases()
|
public void NoObsoleteAliases()
|
||||||
{
|
{
|
||||||
var allAliases = CommandNameLoadHelper.LoadAliases(aliasesPath);
|
var allAliases = CommandNameLoadHelper.LoadCommandNames(aliasesPath);
|
||||||
|
|
||||||
var methodNames = GetCommandMethodNames()
|
var methodNames = GetCommandMethodNames()
|
||||||
.ToHashSet();
|
.ToHashSet();
|
||||||
@@ -93,10 +94,7 @@ namespace NadekoBot.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isSuccess)
|
Assert.IsTrue(isSuccess);
|
||||||
Assert.Pass();
|
|
||||||
else
|
|
||||||
Assert.Warn("There are some unused entries in data/aliases.yml");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Test]
|
// [Test]
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NadekoBot.Extensions;
|
|
||||||
using NadekoBot.Services;
|
using NadekoBot.Services;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
@@ -12,7 +13,9 @@ namespace NadekoBot.Tests
|
|||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
=> _grouper = new GreetGrouper<int>();
|
{
|
||||||
|
_grouper = new GreetGrouper<int>();
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void CreateTest()
|
public void CreateTest()
|
||||||
@@ -59,8 +62,8 @@ namespace NadekoBot.Tests
|
|||||||
_grouper.CreateOrAdd(0, 5);
|
_grouper.CreateOrAdd(0, 5);
|
||||||
|
|
||||||
// add 15 items
|
// add 15 items
|
||||||
await Enumerable.Range(10, 15)
|
await Task.WhenAll(Enumerable.Range(10, 15)
|
||||||
.Select(x => Task.Run(() => _grouper.CreateOrAdd(0, x))).WhenAll();
|
.Select(x => Task.Run(() => _grouper.CreateOrAdd(0, x))));
|
||||||
|
|
||||||
// get 5 at most
|
// get 5 at most
|
||||||
_grouper.ClearGroup(0, 5, out var items);
|
_grouper.ClearGroup(0, 5, out var items);
|
||||||
|
@@ -62,10 +62,7 @@ namespace NadekoBot.Tests
|
|||||||
collection.Clear();
|
collection.Clear();
|
||||||
|
|
||||||
Assert.IsTrue(collection.Count == 0, "Collection has not been cleared.");
|
Assert.IsTrue(collection.Count == 0, "Collection has not been cleared.");
|
||||||
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
Assert.Throws<ArgumentOutOfRangeException>(() => collection.Contains(collection[0]), "Collection has not been cleared.");
|
||||||
{
|
|
||||||
_ = collection[0];
|
|
||||||
}, "Collection has not been cleared.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -118,7 +115,7 @@ namespace NadekoBot.Tests
|
|||||||
[Test]
|
[Test]
|
||||||
public void ContainsTest()
|
public void ContainsTest()
|
||||||
{
|
{
|
||||||
var subCol = new[]
|
var subCol = new ShopEntry[]
|
||||||
{
|
{
|
||||||
new ShopEntry() { Id = 111 },
|
new ShopEntry() { Id = 111 },
|
||||||
new ShopEntry() { Id = 222 },
|
new ShopEntry() { Id = 222 },
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using NadekoBot.Common;
|
using System.Linq;
|
||||||
|
using NadekoBot.Common;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace NadekoBot.Tests
|
namespace NadekoBot.Tests
|
||||||
|
@@ -1,16 +1,15 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<LangVersion>10.0</LangVersion>
|
<LangVersion>9.0</LangVersion>
|
||||||
<EnablePreviewFeatures>True</EnablePreviewFeatures>
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@@ -9,7 +9,9 @@ namespace NadekoBot.Tests
|
|||||||
{
|
{
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
=> Console.OutputEncoding = Encoding.UTF8;
|
{
|
||||||
|
Console.OutputEncoding = Encoding.UTF8;
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Utf8CodepointsToEmoji()
|
public void Utf8CodepointsToEmoji()
|
||||||
|
@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authentication;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using NadekoBot.VotesApi.Controllers;
|
||||||
|
|
||||||
namespace NadekoBot.VotesApi
|
namespace NadekoBot.VotesApi
|
||||||
{
|
{
|
||||||
@@ -23,7 +24,9 @@ namespace NadekoBot.VotesApi
|
|||||||
ISystemClock clock,
|
ISystemClock clock,
|
||||||
IConfiguration conf)
|
IConfiguration conf)
|
||||||
: base(options, logger, encoder, clock)
|
: base(options, logger, encoder, clock)
|
||||||
=> _conf = conf;
|
{
|
||||||
|
_conf = conf;
|
||||||
|
}
|
||||||
|
|
||||||
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||||
{
|
{
|
||||||
|
@@ -23,7 +23,7 @@
|
|||||||
public bool Weekend { get; set; }
|
public bool Weekend { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2.
|
/// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Query { get; set; }
|
public string Query { get; set; }
|
||||||
}
|
}
|
||||||
|
@@ -11,10 +11,10 @@ namespace NadekoBot.VotesApi.Controllers
|
|||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class DiscordsController : ControllerBase
|
public class DiscordsController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly ILogger<DiscordsController> _logger;
|
private readonly ILogger<TopGgController> _logger;
|
||||||
private readonly IVotesCache _cache;
|
private readonly IVotesCache _cache;
|
||||||
|
|
||||||
public DiscordsController(ILogger<DiscordsController> logger, IVotesCache cache)
|
public DiscordsController(ILogger<TopGgController> logger, IVotesCache cache)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
@@ -26,7 +26,7 @@ namespace NadekoBot.VotesApi.Controllers
|
|||||||
{
|
{
|
||||||
var votes = await _cache.GetNewDiscordsVotesAsync();
|
var votes = await _cache.GetNewDiscordsVotesAsync();
|
||||||
if(votes.Count > 0)
|
if(votes.Count > 0)
|
||||||
_logger.LogInformation("Sending {NewDiscordsVotes} new discords votes", votes.Count);
|
_logger.LogInformation("Sending {NewDiscordsVotes} new discords votes.", votes.Count);
|
||||||
return votes;
|
return votes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,7 @@ namespace NadekoBot.VotesApi.Controllers
|
|||||||
{
|
{
|
||||||
var votes = await _cache.GetNewTopGgVotesAsync();
|
var votes = await _cache.GetNewTopGgVotesAsync();
|
||||||
if(votes.Count > 0)
|
if(votes.Count > 0)
|
||||||
_logger.LogInformation("Sending {NewTopggVotes} new topgg votes", votes.Count);
|
_logger.LogInformation("Sending {NewTopggVotes} new topgg votes.", votes.Count);
|
||||||
|
|
||||||
return votes;
|
return votes;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NadekoBot.VotesApi.Services;
|
using NadekoBot.VotesApi.Services;
|
||||||
|
|
||||||
@@ -11,11 +13,13 @@ namespace NadekoBot.VotesApi.Controllers
|
|||||||
{
|
{
|
||||||
private readonly ILogger<WebhookController> _logger;
|
private readonly ILogger<WebhookController> _logger;
|
||||||
private readonly IVotesCache _votesCache;
|
private readonly IVotesCache _votesCache;
|
||||||
|
private readonly IConfiguration _conf;
|
||||||
|
|
||||||
public WebhookController(ILogger<WebhookController> logger, IVotesCache votesCache)
|
public WebhookController(ILogger<WebhookController> logger, IVotesCache votesCache, IConfiguration conf)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_votesCache = votesCache;
|
_votesCache = votesCache;
|
||||||
|
_conf = conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("/discordswebhook")]
|
[HttpPost("/discordswebhook")]
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MorseCode.ITask" Version="2.0.3" />
|
<PackageReference Include="MorseCode.ITask" Version="2.0.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -1,9 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using NadekoBot.VotesApi;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace NadekoBot.VotesApi
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
CreateHostBuilder(args).Build().Run();
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
static IHostBuilder CreateHostBuilder(string[] args) =>
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
Host.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@@ -9,33 +10,37 @@ namespace NadekoBot.VotesApi.Services
|
|||||||
{
|
{
|
||||||
public class FileVotesCache : IVotesCache
|
public class FileVotesCache : IVotesCache
|
||||||
{
|
{
|
||||||
// private const string STATS_FILE = "store/stats.json";
|
private const string statsFile = "store/stats.json";
|
||||||
private const string TOPGG_FILE = "store/topgg.json";
|
private const string topggFile = "store/topgg.json";
|
||||||
private const string DISCORDS_FILE = "store/discords.json";
|
private const string discordsFile = "store/discords.json";
|
||||||
|
|
||||||
private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1);
|
private readonly SemaphoreSlim locker = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
public FileVotesCache()
|
public FileVotesCache()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists("store"))
|
if (!Directory.Exists("store"))
|
||||||
Directory.CreateDirectory("store");
|
Directory.CreateDirectory("store");
|
||||||
|
|
||||||
if(!File.Exists(TOPGG_FILE))
|
if(!File.Exists(topggFile))
|
||||||
File.WriteAllText(TOPGG_FILE, "[]");
|
File.WriteAllText(topggFile, "[]");
|
||||||
|
|
||||||
if(!File.Exists(DISCORDS_FILE))
|
if(!File.Exists(discordsFile))
|
||||||
File.WriteAllText(DISCORDS_FILE, "[]");
|
File.WriteAllText(discordsFile, "[]");
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITask AddNewTopggVote(string userId)
|
public ITask AddNewTopggVote(string userId)
|
||||||
=> AddNewVote(TOPGG_FILE, userId);
|
{
|
||||||
|
return AddNewVote(topggFile, userId);
|
||||||
|
}
|
||||||
|
|
||||||
public ITask AddNewDiscordsVote(string userId)
|
public ITask AddNewDiscordsVote(string userId)
|
||||||
=> AddNewVote(DISCORDS_FILE, userId);
|
{
|
||||||
|
return AddNewVote(discordsFile, userId);
|
||||||
|
}
|
||||||
|
|
||||||
private async ITask AddNewVote(string file, string userId)
|
private async ITask AddNewVote(string file, string userId)
|
||||||
{
|
{
|
||||||
await _locker.WaitAsync();
|
await locker.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var votes = await GetVotesAsync(file);
|
var votes = await GetVotesAsync(file);
|
||||||
@@ -44,7 +49,7 @@ namespace NadekoBot.VotesApi.Services
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
_locker.Release();
|
locker.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,14 +66,14 @@ namespace NadekoBot.VotesApi.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ITask<List<Vote>> EvictTopggVotes()
|
private ITask<List<Vote>> EvictTopggVotes()
|
||||||
=> EvictVotes(TOPGG_FILE);
|
=> EvictVotes(topggFile);
|
||||||
|
|
||||||
private ITask<List<Vote>> EvictDiscordsVotes()
|
private ITask<List<Vote>> EvictDiscordsVotes()
|
||||||
=> EvictVotes(DISCORDS_FILE);
|
=> EvictVotes(discordsFile);
|
||||||
|
|
||||||
private async ITask<List<Vote>> EvictVotes(string file)
|
private async ITask<List<Vote>> EvictVotes(string file)
|
||||||
{
|
{
|
||||||
await _locker.WaitAsync();
|
await locker.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -86,7 +91,7 @@ namespace NadekoBot.VotesApi.Services
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
_locker.Release();
|
locker.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,18 +11,19 @@ namespace NadekoBot.VotesApi
|
|||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public IConfiguration Configuration { get; }
|
|
||||||
|
|
||||||
public Startup(IConfiguration configuration)
|
public Startup(IConfiguration configuration)
|
||||||
=> Configuration = configuration;
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddSingleton<IVotesCache, FileVotesCache>();
|
services.AddSingleton<IVotesCache, FileVotesCache>();
|
||||||
services.AddSwaggerGen(static c =>
|
services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "NadekoBot.VotesApi", Version = "v1" });
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "NadekoBot.VotesApi", Version = "v1" });
|
||||||
});
|
});
|
||||||
@@ -35,13 +36,13 @@ namespace NadekoBot.VotesApi
|
|||||||
});
|
});
|
||||||
|
|
||||||
services
|
services
|
||||||
.AddAuthorization(static opts =>
|
.AddAuthorization(opts =>
|
||||||
{
|
{
|
||||||
opts.DefaultPolicy = new AuthorizationPolicyBuilder(AuthHandler.SchemeName)
|
opts.DefaultPolicy = new AuthorizationPolicyBuilder(AuthHandler.SchemeName)
|
||||||
.RequireAssertion(static _ => false)
|
.RequireAssertion(x => false)
|
||||||
.Build();
|
.Build();
|
||||||
opts.AddPolicy(Policies.DiscordsAuth, static policy => policy.RequireClaim(AuthHandler.DiscordsClaim));
|
opts.AddPolicy(Policies.DiscordsAuth, policy => policy.RequireClaim(AuthHandler.DiscordsClaim));
|
||||||
opts.AddPolicy(Policies.TopggAuth, static policy => policy.RequireClaim(AuthHandler.TopggClaim));
|
opts.AddPolicy(Policies.TopggAuth, policy => policy.RequireClaim(AuthHandler.TopggClaim));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ namespace NadekoBot.VotesApi
|
|||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(static c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NadekoBot.VotesApi v1"));
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "NadekoBot.VotesApi v1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
@@ -62,7 +63,7 @@ namespace NadekoBot.VotesApi
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.UseEndpoints(static endpoints => { endpoints.MapControllers(); });
|
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
namespace NadekoBot.VotesApi
|
namespace NadekoBot.VotesApi
|
||||||
{
|
{
|
||||||
public class Vote
|
public class Vote
|
||||||
|
@@ -1,359 +0,0 @@
|
|||||||
root = true
|
|
||||||
# Remove the line below if you want to inherit .editorconfig settings from higher directories
|
|
||||||
|
|
||||||
[obj/**]
|
|
||||||
generated_code = true
|
|
||||||
|
|
||||||
# C# files
|
|
||||||
[*.cs]
|
|
||||||
|
|
||||||
|
|
||||||
#### Core EditorConfig Options ####
|
|
||||||
|
|
||||||
# Indentation and spacing
|
|
||||||
indent_size = 4
|
|
||||||
indent_style = space
|
|
||||||
tab_width = 4
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
end_of_line = crlf
|
|
||||||
insert_final_newline = false
|
|
||||||
|
|
||||||
#### .NET Coding Conventions ####
|
|
||||||
|
|
||||||
# Organize usings
|
|
||||||
dotnet_separate_import_directive_groups = false
|
|
||||||
dotnet_sort_system_directives_first = false
|
|
||||||
|
|
||||||
# this. and Me. preferences
|
|
||||||
dotnet_style_qualification_for_event = false
|
|
||||||
dotnet_style_qualification_for_field = false
|
|
||||||
dotnet_style_qualification_for_method = false
|
|
||||||
dotnet_style_qualification_for_property = false
|
|
||||||
|
|
||||||
# Language keywords vs BCL types preferences
|
|
||||||
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
|
|
||||||
dotnet_style_predefined_type_for_member_access = true:suggestion
|
|
||||||
|
|
||||||
# Parentheses preferences
|
|
||||||
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:warning
|
|
||||||
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning
|
|
||||||
dotnet_style_parentheses_in_other_operators = never_if_unnecessary
|
|
||||||
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:warning
|
|
||||||
|
|
||||||
# Modifier preferences
|
|
||||||
dotnet_style_require_accessibility_modifiers = always:error
|
|
||||||
|
|
||||||
# Expression-level preferences
|
|
||||||
dotnet_style_coalesce_expression = true
|
|
||||||
dotnet_style_collection_initializer = true
|
|
||||||
dotnet_style_explicit_tuple_names = true
|
|
||||||
dotnet_style_namespace_match_folder = true
|
|
||||||
dotnet_style_null_propagation = true
|
|
||||||
dotnet_style_object_initializer = true
|
|
||||||
dotnet_style_operator_placement_when_wrapping = beginning_of_line
|
|
||||||
dotnet_style_prefer_auto_properties = true:warning
|
|
||||||
dotnet_style_prefer_compound_assignment = true
|
|
||||||
dotnet_style_prefer_conditional_expression_over_assignment = false:suggestion
|
|
||||||
dotnet_style_prefer_conditional_expression_over_return = false:suggestion
|
|
||||||
dotnet_style_prefer_inferred_anonymous_type_member_names = true
|
|
||||||
dotnet_style_prefer_inferred_tuple_names = true
|
|
||||||
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:error
|
|
||||||
dotnet_style_prefer_simplified_boolean_expressions = true
|
|
||||||
dotnet_style_prefer_simplified_interpolation = true
|
|
||||||
|
|
||||||
# Field preferences
|
|
||||||
dotnet_style_readonly_field = true:suggestion
|
|
||||||
|
|
||||||
# Parameter preferences
|
|
||||||
dotnet_code_quality_unused_parameters = all:warning
|
|
||||||
|
|
||||||
#### C# Coding Conventions ####
|
|
||||||
|
|
||||||
# var preferences
|
|
||||||
csharp_style_var_elsewhere = true
|
|
||||||
csharp_style_var_for_built_in_types = true:suggestion
|
|
||||||
csharp_style_var_when_type_is_apparent = true:suggestion
|
|
||||||
|
|
||||||
# Expression-bodied members
|
|
||||||
csharp_style_expression_bodied_accessors = true:suggestion
|
|
||||||
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
|
|
||||||
csharp_style_expression_bodied_indexers = true:suggestion
|
|
||||||
csharp_style_expression_bodied_lambdas = true:suggestion
|
|
||||||
csharp_style_expression_bodied_local_functions = true:suggestion
|
|
||||||
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
|
|
||||||
csharp_style_expression_bodied_operators = when_on_single_line:suggestion
|
|
||||||
csharp_style_expression_bodied_properties = true:suggestion
|
|
||||||
|
|
||||||
# Pattern matching preferences
|
|
||||||
csharp_style_pattern_matching_over_as_with_null_check = true:error
|
|
||||||
csharp_style_pattern_matching_over_is_with_cast_check = true:error
|
|
||||||
csharp_style_prefer_not_pattern = true:error
|
|
||||||
csharp_style_prefer_pattern_matching = true:suggestion
|
|
||||||
csharp_style_prefer_switch_expression = true
|
|
||||||
|
|
||||||
# Null-checking preferences
|
|
||||||
csharp_style_conditional_delegate_call = true:error
|
|
||||||
|
|
||||||
# Modifier preferences
|
|
||||||
csharp_prefer_static_local_function = true
|
|
||||||
csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async
|
|
||||||
|
|
||||||
# Code-block preferences
|
|
||||||
csharp_prefer_braces = when_multiline:warning
|
|
||||||
csharp_prefer_simple_using_statement = true
|
|
||||||
|
|
||||||
# Expression-level preferences
|
|
||||||
csharp_prefer_simple_default_expression = true
|
|
||||||
csharp_style_deconstructed_variable_declaration = true
|
|
||||||
csharp_style_implicit_object_creation_when_type_is_apparent = true:error
|
|
||||||
csharp_style_inlined_variable_declaration = true:warning
|
|
||||||
csharp_style_pattern_local_over_anonymous_function = true
|
|
||||||
csharp_style_prefer_index_operator = true
|
|
||||||
csharp_style_prefer_range_operator = true
|
|
||||||
csharp_style_throw_expression = true:error
|
|
||||||
csharp_style_unused_value_assignment_preference = discard_variable:warning
|
|
||||||
csharp_style_unused_value_expression_statement_preference = discard_variable
|
|
||||||
|
|
||||||
# 'using' directive preferences
|
|
||||||
csharp_using_directive_placement = outside_namespace:error
|
|
||||||
|
|
||||||
# Enforce file-scoped namespaces
|
|
||||||
csharp_style_namespace_declarations = file_scoped:error
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true
|
|
||||||
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false
|
|
||||||
csharp_style_allow_embedded_statements_on_same_line_experimental = false
|
|
||||||
|
|
||||||
#### C# Formatting Rules ####
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
csharp_new_line_before_catch = true
|
|
||||||
csharp_new_line_before_else = true
|
|
||||||
csharp_new_line_before_finally = true
|
|
||||||
csharp_new_line_before_members_in_anonymous_types = true
|
|
||||||
csharp_new_line_before_members_in_object_initializers = true
|
|
||||||
csharp_new_line_before_open_brace = all
|
|
||||||
csharp_new_line_between_query_expression_clauses = true
|
|
||||||
|
|
||||||
# Indentation preferences
|
|
||||||
csharp_indent_block_contents = true
|
|
||||||
csharp_indent_braces = false
|
|
||||||
csharp_indent_case_contents = true
|
|
||||||
csharp_indent_case_contents_when_block = true
|
|
||||||
csharp_indent_labels = one_less_than_current
|
|
||||||
csharp_indent_switch_labels = true
|
|
||||||
|
|
||||||
# Space preferences
|
|
||||||
csharp_space_after_cast = false
|
|
||||||
csharp_space_after_colon_in_inheritance_clause = true
|
|
||||||
csharp_space_after_comma = true
|
|
||||||
csharp_space_after_dot = false
|
|
||||||
csharp_space_after_keywords_in_control_flow_statements = true
|
|
||||||
csharp_space_after_semicolon_in_for_statement = true
|
|
||||||
csharp_space_around_binary_operators = before_and_after
|
|
||||||
csharp_space_around_declaration_statements = false
|
|
||||||
csharp_space_before_colon_in_inheritance_clause = true
|
|
||||||
csharp_space_before_comma = false
|
|
||||||
csharp_space_before_dot = false
|
|
||||||
csharp_space_before_open_square_brackets = false
|
|
||||||
csharp_space_before_semicolon_in_for_statement = false
|
|
||||||
csharp_space_between_empty_square_brackets = false
|
|
||||||
csharp_space_between_method_call_empty_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_call_name_and_opening_parenthesis = false
|
|
||||||
csharp_space_between_method_call_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_declaration_name_and_open_parenthesis = false
|
|
||||||
csharp_space_between_method_declaration_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_parentheses = false
|
|
||||||
csharp_space_between_square_brackets = false
|
|
||||||
|
|
||||||
# Wrapping preferences
|
|
||||||
csharp_preserve_single_line_blocks = true
|
|
||||||
csharp_preserve_single_line_statements = false
|
|
||||||
|
|
||||||
#### Naming styles ####
|
|
||||||
|
|
||||||
# Naming rules
|
|
||||||
|
|
||||||
dotnet_naming_rule.private_readonly_field.symbols = private_readonly_field
|
|
||||||
dotnet_naming_rule.private_readonly_field.style = begins_with_underscore
|
|
||||||
dotnet_naming_rule.private_readonly_field.severity = warning
|
|
||||||
|
|
||||||
dotnet_naming_rule.private_field.symbols = private_field
|
|
||||||
dotnet_naming_rule.private_field.style = camel_case
|
|
||||||
dotnet_naming_rule.private_field.severity = warning
|
|
||||||
|
|
||||||
dotnet_naming_rule.const_fields.symbols = const_fields
|
|
||||||
dotnet_naming_rule.const_fields.style = all_upper
|
|
||||||
dotnet_naming_rule.const_fields.severity = warning
|
|
||||||
|
|
||||||
# dotnet_naming_rule.class_should_be_pascal_case.severity = error
|
|
||||||
# dotnet_naming_rule.class_should_be_pascal_case.symbols = class
|
|
||||||
# dotnet_naming_rule.class_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.struct_should_be_pascal_case.severity = error
|
|
||||||
dotnet_naming_rule.struct_should_be_pascal_case.symbols = struct
|
|
||||||
dotnet_naming_rule.struct_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.severity = error
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
|
|
||||||
|
|
||||||
# dotnet_naming_rule.types_should_be_pascal_case.severity = error
|
|
||||||
# dotnet_naming_rule.types_should_be_pascal_case.symbols = types
|
|
||||||
# dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
# dotnet_naming_rule.enum_should_be_pascal_case.severity = error
|
|
||||||
# dotnet_naming_rule.enum_should_be_pascal_case.symbols = enum
|
|
||||||
# dotnet_naming_rule.enum_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
# dotnet_naming_rule.property_should_be_pascal_case.severity = error
|
|
||||||
# dotnet_naming_rule.property_should_be_pascal_case.symbols = property
|
|
||||||
# dotnet_naming_rule.property_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.method_should_be_pascal_case.severity = error
|
|
||||||
dotnet_naming_rule.method_should_be_pascal_case.symbols = method
|
|
||||||
dotnet_naming_rule.method_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.async_method_should_be_ends_with_async.severity = error
|
|
||||||
dotnet_naming_rule.async_method_should_be_ends_with_async.symbols = async_method
|
|
||||||
dotnet_naming_rule.async_method_should_be_ends_with_async.style = ends_with_async
|
|
||||||
|
|
||||||
# dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = error
|
|
||||||
# dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
|
|
||||||
# dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.local_variable_should_be_camel_case.severity = error
|
|
||||||
dotnet_naming_rule.local_variable_should_be_camel_case.symbols = local_variable
|
|
||||||
dotnet_naming_rule.local_variable_should_be_camel_case.style = camel_case
|
|
||||||
|
|
||||||
# Symbol specifications
|
|
||||||
|
|
||||||
dotnet_naming_symbols.const_fields.required_modifiers = const
|
|
||||||
dotnet_naming_symbols.const_fields.applicable_kinds = field
|
|
||||||
|
|
||||||
dotnet_naming_symbols.class.applicable_kinds = class
|
|
||||||
dotnet_naming_symbols.class.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.class.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.interface.applicable_kinds = interface
|
|
||||||
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.interface.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.struct.applicable_kinds = struct
|
|
||||||
dotnet_naming_symbols.struct.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.struct.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.enum.applicable_kinds = enum
|
|
||||||
dotnet_naming_symbols.enum.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.enum.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.method.applicable_kinds = method
|
|
||||||
dotnet_naming_symbols.method.applicable_accessibilities = public
|
|
||||||
dotnet_naming_symbols.method.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.property.applicable_kinds = property
|
|
||||||
dotnet_naming_symbols.property.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.property.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
|
|
||||||
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.types.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
|
|
||||||
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.non_field_members.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.private_readonly_field.applicable_kinds = field
|
|
||||||
dotnet_naming_symbols.private_readonly_field.applicable_accessibilities = private, protected
|
|
||||||
dotnet_naming_symbols.private_readonly_field.required_modifiers = readonly
|
|
||||||
|
|
||||||
dotnet_naming_symbols.private_field.applicable_kinds = field
|
|
||||||
dotnet_naming_symbols.private_field.applicable_accessibilities = private, protected
|
|
||||||
dotnet_naming_symbols.private_field.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.async_method.applicable_kinds = method, local_function
|
|
||||||
dotnet_naming_symbols.async_method.applicable_accessibilities = *
|
|
||||||
dotnet_naming_symbols.async_method.required_modifiers = async
|
|
||||||
|
|
||||||
dotnet_naming_symbols.local_variable.applicable_kinds = parameter, local
|
|
||||||
dotnet_naming_symbols.local_variable.applicable_accessibilities = local
|
|
||||||
dotnet_naming_symbols.local_variable.required_modifiers =
|
|
||||||
|
|
||||||
# Naming styles
|
|
||||||
|
|
||||||
|
|
||||||
dotnet_naming_style.all_upper.capitalization = all_upper
|
|
||||||
|
|
||||||
dotnet_naming_style.pascal_case.required_prefix =
|
|
||||||
dotnet_naming_style.pascal_case.required_suffix =
|
|
||||||
dotnet_naming_style.pascal_case.word_separator =
|
|
||||||
dotnet_naming_style.pascal_case.capitalization = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_style.begins_with_i.required_prefix = I
|
|
||||||
dotnet_naming_style.begins_with_i.required_suffix =
|
|
||||||
dotnet_naming_style.begins_with_i.word_separator =
|
|
||||||
dotnet_naming_style.begins_with_i.capitalization = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_style.begins_with_underscore.required_prefix = _
|
|
||||||
dotnet_naming_style.begins_with_underscore.required_suffix =
|
|
||||||
dotnet_naming_style.begins_with_underscore.word_separator =
|
|
||||||
dotnet_naming_style.begins_with_underscore.capitalization = camel_case
|
|
||||||
|
|
||||||
dotnet_naming_style.ends_with_async.required_prefix =
|
|
||||||
# dotnet_naming_style.ends_with_async.required_suffix = Async
|
|
||||||
dotnet_naming_style.ends_with_async.word_separator =
|
|
||||||
dotnet_naming_style.ends_with_async.capitalization = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_style.camel_case.required_prefix =
|
|
||||||
dotnet_naming_style.camel_case.required_suffix =
|
|
||||||
dotnet_naming_style.camel_case.word_separator =
|
|
||||||
dotnet_naming_style.camel_case.capitalization = camel_case
|
|
||||||
|
|
||||||
# CA1822: Mark members as static
|
|
||||||
dotnet_diagnostic.ca1822.severity = suggestion
|
|
||||||
|
|
||||||
# IDE0004: Cast is redundant
|
|
||||||
dotnet_diagnostic.ide0004.severity = warning
|
|
||||||
|
|
||||||
# IDE0058: Expression value is never used
|
|
||||||
dotnet_diagnostic.ide0058.severity = none
|
|
||||||
|
|
||||||
# # IDE0011: Add braces to 'if'/'else' statement
|
|
||||||
# dotnet_diagnostic.ide0011.severity = none
|
|
||||||
|
|
||||||
resharper_wrap_after_invocation_lpar = false
|
|
||||||
resharper_wrap_before_invocation_rpar = false
|
|
||||||
|
|
||||||
# ReSharper properties
|
|
||||||
resharper_align_multiline_calls_chain = true
|
|
||||||
resharper_csharp_wrap_after_declaration_lpar = true
|
|
||||||
resharper_csharp_wrap_after_invocation_lpar = false
|
|
||||||
resharper_csharp_wrap_before_binary_opsign = true
|
|
||||||
resharper_csharp_wrap_before_invocation_rpar = false
|
|
||||||
resharper_csharp_wrap_parameters_style = chop_if_long
|
|
||||||
resharper_force_chop_compound_if_expression = false
|
|
||||||
resharper_keep_existing_linebreaks = true
|
|
||||||
resharper_keep_user_linebreaks = true
|
|
||||||
resharper_max_formal_parameters_on_line = 3
|
|
||||||
resharper_place_simple_embedded_statement_on_same_line = false
|
|
||||||
resharper_wrap_chained_binary_expressions = chop_if_long
|
|
||||||
resharper_wrap_chained_binary_patterns = chop_if_long
|
|
||||||
resharper_wrap_chained_method_calls = chop_if_long
|
|
||||||
resharper_wrap_object_and_collection_initializer_style = chop_always
|
|
||||||
|
|
||||||
resharper_csharp_wrap_before_first_type_parameter_constraint = true
|
|
||||||
resharper_csharp_place_type_constraints_on_same_line = false
|
|
||||||
resharper_csharp_wrap_before_extends_colon = true
|
|
||||||
resharper_csharp_place_constructor_initializer_on_same_line = false
|
|
||||||
resharper_force_attribute_style = separate
|
|
||||||
resharper_csharp_braces_for_ifelse = required_for_multiline_statement
|
|
||||||
resharper_csharp_braces_for_foreach = required_for_multiline
|
|
||||||
resharper_csharp_braces_for_while = required_for_multiline
|
|
||||||
resharper_csharp_braces_for_for = required_for_multiline
|
|
||||||
resharper_arrange_redundant_parentheses_highlighting = hint
|
|
||||||
|
|
||||||
# IDE0011: Add braces
|
|
||||||
dotnet_diagnostic.IDE0011.severity = warning
|
|
@@ -1,19 +1,36 @@
|
|||||||
#nullable disable
|
using Discord;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Discord.WebSocket;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using NadekoBot.Common.Configs;
|
using NadekoBot.Common;
|
||||||
using NadekoBot.Common.ModuleBehaviors;
|
using NadekoBot.Services;
|
||||||
using NadekoBot.Db;
|
|
||||||
using NadekoBot.Modules.Administration;
|
|
||||||
using NadekoBot.Services.Database.Models;
|
using NadekoBot.Services.Database.Models;
|
||||||
|
using NadekoBot.Extensions;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using RunMode = Discord.Commands.RunMode;
|
using System.Threading.Tasks;
|
||||||
|
using Discord.Net;
|
||||||
namespace NadekoBot;
|
using NadekoBot.Common.ModuleBehaviors;
|
||||||
|
using NadekoBot.Common.Configs;
|
||||||
|
using NadekoBot.Db;
|
||||||
|
using NadekoBot.Modules.Administration.Services;
|
||||||
|
using NadekoBot.Modules.Searches;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
|
namespace NadekoBot
|
||||||
|
{
|
||||||
public sealed class Bot
|
public sealed class Bot
|
||||||
{
|
{
|
||||||
|
private readonly IBotCredentials _creds;
|
||||||
|
private readonly CommandService _commandService;
|
||||||
|
private readonly DbService _db;
|
||||||
|
private readonly IBotCredsProvider _credsProvider;
|
||||||
|
|
||||||
public event Func<GuildConfig, Task> JoinedGuild = delegate { return Task.CompletedTask; };
|
public event Func<GuildConfig, Task> JoinedGuild = delegate { return Task.CompletedTask; };
|
||||||
|
|
||||||
public DiscordSocketClient Client { get; }
|
public DiscordSocketClient Client { get; }
|
||||||
@@ -23,87 +40,68 @@ public sealed class Bot
|
|||||||
|
|
||||||
public string Mention { get; private set; }
|
public string Mention { get; private set; }
|
||||||
public bool IsReady { get; private set; }
|
public bool IsReady { get; private set; }
|
||||||
public int ShardId { get; set; }
|
|
||||||
|
|
||||||
private readonly IBotCredentials _creds;
|
|
||||||
private readonly CommandService _commandService;
|
|
||||||
private readonly DbService _db;
|
|
||||||
|
|
||||||
private readonly IBotCredsProvider _credsProvider;
|
|
||||||
// private readonly InteractionService _interactionService;
|
|
||||||
|
|
||||||
public Bot(int shardId, int? totalShards)
|
public Bot(int shardId, int? totalShards)
|
||||||
{
|
{
|
||||||
if (shardId < 0)
|
if (shardId < 0)
|
||||||
throw new ArgumentOutOfRangeException(nameof(shardId));
|
throw new ArgumentOutOfRangeException(nameof(shardId));
|
||||||
|
|
||||||
ShardId = shardId;
|
|
||||||
_credsProvider = new BotCredsProvider(totalShards);
|
_credsProvider = new BotCredsProvider(totalShards);
|
||||||
_creds = _credsProvider.GetCreds();
|
_creds = _credsProvider.GetCreds();
|
||||||
|
|
||||||
_db = new(_credsProvider);
|
_db = new DbService(_creds);
|
||||||
|
|
||||||
var messageCacheSize =
|
if (shardId == 0)
|
||||||
#if GLOBAL_NADEKO
|
|
||||||
0;
|
|
||||||
#else
|
|
||||||
50;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if(!_creds.UsePrivilegedIntents)
|
|
||||||
Log.Warning("You are not using privileged intents. Some features will not work properly");
|
|
||||||
|
|
||||||
Client = new(new()
|
|
||||||
{
|
{
|
||||||
MessageCacheSize = messageCacheSize,
|
_db.Setup();
|
||||||
|
}
|
||||||
|
|
||||||
|
Client = new DiscordSocketClient(new DiscordSocketConfig
|
||||||
|
{
|
||||||
|
MessageCacheSize = 50,
|
||||||
LogLevel = LogSeverity.Warning,
|
LogLevel = LogSeverity.Warning,
|
||||||
ConnectionTimeout = int.MaxValue,
|
ConnectionTimeout = int.MaxValue,
|
||||||
TotalShards = _creds.TotalShards,
|
TotalShards = _creds.TotalShards,
|
||||||
ShardId = shardId,
|
ShardId = shardId,
|
||||||
AlwaysDownloadUsers = false,
|
AlwaysDownloadUsers = false,
|
||||||
AlwaysResolveStickers = false,
|
ExclusiveBulkDelete = true,
|
||||||
AlwaysDownloadDefaultStickers = false,
|
|
||||||
GatewayIntents = _creds.UsePrivilegedIntents
|
|
||||||
? GatewayIntents.All
|
|
||||||
: GatewayIntents.AllUnprivileged,
|
|
||||||
LogGatewayIntentWarnings = false,
|
|
||||||
FormatUsersInBidirectionalUnicode = false,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_commandService = new(new()
|
_commandService = new CommandService(new CommandServiceConfig()
|
||||||
{
|
{
|
||||||
CaseSensitiveCommands = false,
|
CaseSensitiveCommands = false,
|
||||||
DefaultRunMode = RunMode.Sync,
|
DefaultRunMode = RunMode.Sync,
|
||||||
});
|
});
|
||||||
|
|
||||||
// _interactionService = new(Client.Rest);
|
#if GLOBAL_NADEKO || DEBUG
|
||||||
|
|
||||||
Client.Log += Client_Log;
|
Client.Log += Client_Log;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<ulong> GetCurrentGuildIds()
|
public List<ulong> GetCurrentGuildIds()
|
||||||
=> Client.Guilds.Select(x => x.Id).ToList();
|
{
|
||||||
|
return Client.Guilds.Select(x => x.Id).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
private void AddServices()
|
private void AddServices()
|
||||||
{
|
{
|
||||||
var startingGuildIdList = GetCurrentGuildIds();
|
var startingGuildIdList = GetCurrentGuildIds();
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
var bot = Client.CurrentUser;
|
var _bot = Client.CurrentUser;
|
||||||
|
|
||||||
using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
uow.EnsureUserCreated(bot.Id, bot.Username, bot.Discriminator, bot.AvatarId);
|
uow.EnsureUserCreated(_bot.Id, _bot.Username, _bot.Discriminator, _bot.AvatarId);
|
||||||
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
var svcs = new ServiceCollection().AddTransient(_ => _credsProvider.GetCreds()) // bot creds
|
var svcs = new ServiceCollection()
|
||||||
.AddSingleton(_credsProvider)
|
.AddTransient<IBotCredentials>(_ => _credsProvider.GetCreds()) // bot creds
|
||||||
|
.AddSingleton<IBotCredsProvider>(_credsProvider)
|
||||||
.AddSingleton(_db) // database
|
.AddSingleton(_db) // database
|
||||||
.AddRedis(_creds.RedisOptions) // redis
|
.AddRedis(_creds.RedisOptions) // redis
|
||||||
.AddSingleton(Client) // discord socket client
|
.AddSingleton(Client) // discord socket client
|
||||||
.AddSingleton(_commandService)
|
.AddSingleton(_commandService)
|
||||||
// .AddSingleton(_interactionService)
|
|
||||||
.AddSingleton(this)
|
.AddSingleton(this)
|
||||||
.AddSingleton<ISeria, JsonSeria>()
|
.AddSingleton<ISeria, JsonSeria>()
|
||||||
.AddSingleton<IPubSub, RedisPubSub>()
|
.AddSingleton<IPubSub, RedisPubSub>()
|
||||||
@@ -113,21 +111,25 @@ public sealed class Bot
|
|||||||
.AddConfigMigrators()
|
.AddConfigMigrators()
|
||||||
.AddMemoryCache()
|
.AddMemoryCache()
|
||||||
// music
|
// music
|
||||||
.AddMusic();
|
.AddMusic()
|
||||||
// admin
|
// admin
|
||||||
#if GLOBAL_NADEKO
|
#if GLOBAL_NADEKO
|
||||||
svcs.AddSingleton<ILogCommandService, DummyLogCommandService>();
|
.AddSingleton<ILogCommandService, DummyLogCommandService>()
|
||||||
|
#else
|
||||||
|
.AddSingleton<ILogCommandService, LogCommandService>()
|
||||||
#endif
|
#endif
|
||||||
|
;
|
||||||
|
|
||||||
svcs.AddHttpClient();
|
svcs.AddHttpClient();
|
||||||
svcs.AddHttpClient("memelist")
|
svcs.AddHttpClient("memelist").ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
|
||||||
{
|
{
|
||||||
AllowAutoRedirect = false
|
AllowAutoRedirect = false
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Environment.GetEnvironmentVariable("NADEKOBOT_IS_COORDINATED") != "1")
|
if (Environment.GetEnvironmentVariable("NADEKOBOT_IS_COORDINATED") != "1")
|
||||||
|
{
|
||||||
svcs.AddSingleton<ICoordinator, SingleProcessCoordinator>();
|
svcs.AddSingleton<ICoordinator, SingleProcessCoordinator>();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
svcs.AddSingleton<RemoteGrpcCoordinator>()
|
svcs.AddSingleton<RemoteGrpcCoordinator>()
|
||||||
@@ -142,36 +144,40 @@ public sealed class Bot
|
|||||||
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RedisImagesCache>())
|
.AddSingleton<IReadyExecutor>(x => x.GetRequiredService<RedisImagesCache>())
|
||||||
.AddSingleton<IDataCache, RedisCache>();
|
.AddSingleton<IDataCache, RedisCache>();
|
||||||
|
|
||||||
svcs.Scan(scan => scan.FromAssemblyOf<IReadyExecutor>()
|
svcs.Scan(scan => scan
|
||||||
.AddClasses(classes => classes.AssignableToAny(
|
.FromAssemblyOf<IReadyExecutor>()
|
||||||
|
.AddClasses(classes => classes
|
||||||
|
.AssignableToAny(
|
||||||
// services
|
// services
|
||||||
typeof(INService),
|
typeof(INService),
|
||||||
|
|
||||||
// behaviours
|
// behaviours
|
||||||
typeof(IExecOnMessage),
|
typeof(IEarlyBehavior),
|
||||||
|
typeof(ILateBlocker),
|
||||||
typeof(IInputTransformer),
|
typeof(IInputTransformer),
|
||||||
typeof(IExecPreCommand),
|
typeof(ILateExecutor))
|
||||||
typeof(IExecPostCommand),
|
|
||||||
typeof(IExecNoCommand))
|
|
||||||
.WithoutAttribute<DontAddToIocContainerAttribute>()
|
|
||||||
#if GLOBAL_NADEKO
|
#if GLOBAL_NADEKO
|
||||||
.WithoutAttribute<NoPublicBotAttribute>()
|
.WithoutAttribute<NoPublicBotAttribute>()
|
||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
.AsSelfWithInterfaces()
|
.AsSelfWithInterfaces()
|
||||||
.WithSingletonLifetime());
|
.WithSingletonLifetime()
|
||||||
|
);
|
||||||
|
|
||||||
//initialize Services
|
//initialize Services
|
||||||
Services = svcs.BuildServiceProvider();
|
Services = svcs.BuildServiceProvider();
|
||||||
Services.GetRequiredService<IBehaviorHandler>().Initialize();
|
var exec = Services.GetRequiredService<IBehaviourExecutor>();
|
||||||
|
exec.Initialize();
|
||||||
|
|
||||||
if (Client.ShardId == 0)
|
if (Client.ShardId == 0)
|
||||||
|
{
|
||||||
ApplyConfigMigrations();
|
ApplyConfigMigrations();
|
||||||
|
}
|
||||||
|
|
||||||
_ = LoadTypeReaders(typeof(Bot).Assembly);
|
_ = LoadTypeReaders(typeof(Bot).Assembly);
|
||||||
|
|
||||||
sw.Stop();
|
sw.Stop();
|
||||||
Log.Information( "All services loaded in {ServiceLoadTime:F2}s", sw.Elapsed.TotalSeconds);
|
Log.Information($"All services loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ApplyConfigMigrations()
|
private void ApplyConfigMigrations()
|
||||||
@@ -179,8 +185,10 @@ public sealed class Bot
|
|||||||
// execute all migrators
|
// execute all migrators
|
||||||
var migrators = Services.GetServices<IConfigMigrator>();
|
var migrators = Services.GetServices<IConfigMigrator>();
|
||||||
foreach (var migrator in migrators)
|
foreach (var migrator in migrators)
|
||||||
|
{
|
||||||
migrator.EnsureMigrated();
|
migrator.EnsureMigrated();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private IEnumerable<object> LoadTypeReaders(Assembly assembly)
|
private IEnumerable<object> LoadTypeReaders(Assembly assembly)
|
||||||
{
|
{
|
||||||
@@ -194,9 +202,9 @@ public sealed class Bot
|
|||||||
Log.Warning(ex.LoaderExceptions[0], "Error getting types");
|
Log.Warning(ex.LoaderExceptions[0], "Error getting types");
|
||||||
return Enumerable.Empty<object>();
|
return Enumerable.Empty<object>();
|
||||||
}
|
}
|
||||||
|
var filteredTypes = allTypes
|
||||||
var filteredTypes = allTypes.Where(x => x.IsSubclassOf(typeof(TypeReader))
|
.Where(x => x.IsSubclassOf(typeof(TypeReader))
|
||||||
&& x.BaseType?.GetGenericArguments().Length > 0
|
&& x.BaseType.GetGenericArguments().Length > 0
|
||||||
&& !x.IsAbstract);
|
&& !x.IsAbstract);
|
||||||
|
|
||||||
var toReturn = new List<object>();
|
var toReturn = new List<object>();
|
||||||
@@ -204,8 +212,6 @@ public sealed class Bot
|
|||||||
{
|
{
|
||||||
var x = (TypeReader)ActivatorUtilities.CreateInstance(Services, ft);
|
var x = (TypeReader)ActivatorUtilities.CreateInstance(Services, ft);
|
||||||
var baseType = ft.BaseType;
|
var baseType = ft.BaseType;
|
||||||
if (baseType is null)
|
|
||||||
continue;
|
|
||||||
var typeArgs = baseType.GetGenericArguments();
|
var typeArgs = baseType.GetGenericArguments();
|
||||||
_commandService.AddTypeReader(typeArgs[0], x);
|
_commandService.AddTypeReader(typeArgs[0], x);
|
||||||
toReturn.Add(x);
|
toReturn.Add(x);
|
||||||
@@ -216,30 +222,34 @@ public sealed class Bot
|
|||||||
|
|
||||||
private async Task LoginAsync(string token)
|
private async Task LoginAsync(string token)
|
||||||
{
|
{
|
||||||
var clientReady = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
var clientReady = new TaskCompletionSource<bool>();
|
||||||
|
|
||||||
async Task SetClientReady()
|
Task SetClientReady()
|
||||||
|
{
|
||||||
|
var _ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
clientReady.TrySetResult(true);
|
clientReady.TrySetResult(true);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
foreach (var chan in await Client.GetDMChannelsAsync())
|
foreach (var chan in (await Client.GetDMChannelsAsync().ConfigureAwait(false)))
|
||||||
await chan.CloseAsync();
|
{
|
||||||
|
await chan.CloseAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
//connect
|
//connect
|
||||||
Log.Information("Shard {ShardId} logging in ...", Client.ShardId);
|
Log.Information("Shard {ShardId} logging in ...", Client.ShardId);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Client.Ready += SetClientReady;
|
await Client.LoginAsync(TokenType.Bot, token).ConfigureAwait(false);
|
||||||
|
await Client.StartAsync().ConfigureAwait(false);
|
||||||
await Client.LoginAsync(TokenType.Bot, token);
|
|
||||||
await Client.StartAsync();
|
|
||||||
}
|
}
|
||||||
catch (HttpException ex)
|
catch (HttpException ex)
|
||||||
{
|
{
|
||||||
@@ -252,45 +262,42 @@ public sealed class Bot
|
|||||||
Helpers.ReadErrorAndExit(4);
|
Helpers.ReadErrorAndExit(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Client.Ready += SetClientReady;
|
||||||
await clientReady.Task.ConfigureAwait(false);
|
await clientReady.Task.ConfigureAwait(false);
|
||||||
Client.Ready -= SetClientReady;
|
Client.Ready -= SetClientReady;
|
||||||
|
|
||||||
Client.JoinedGuild += Client_JoinedGuild;
|
Client.JoinedGuild += Client_JoinedGuild;
|
||||||
Client.LeftGuild += Client_LeftGuild;
|
Client.LeftGuild += Client_LeftGuild;
|
||||||
|
|
||||||
Log.Information("Shard {ShardId} logged in", Client.ShardId);
|
Log.Information("Shard {0} logged in.", Client.ShardId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Client_LeftGuild(SocketGuild arg)
|
private Task Client_LeftGuild(SocketGuild arg)
|
||||||
{
|
{
|
||||||
Log.Information("Left server: {GuildName} [{GuildId}]", arg?.Name, arg?.Id);
|
Log.Information("Left server: {0} [{1}]", arg?.Name, arg?.Id);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Client_JoinedGuild(SocketGuild arg)
|
private Task Client_JoinedGuild(SocketGuild arg)
|
||||||
{
|
{
|
||||||
Log.Information("Joined server: {GuildName} [{GuildId}]", arg.Name, arg.Id);
|
Log.Information($"Joined server: {0} [{1}]", arg.Name, arg.Id);
|
||||||
_ = Task.Run(async () =>
|
var _ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
GuildConfig gc;
|
GuildConfig gc;
|
||||||
await using (var uow = _db.GetDbContext())
|
using (var uow = _db.GetDbContext())
|
||||||
{
|
{
|
||||||
gc = uow.GuildConfigsForId(arg.Id, null);
|
gc = uow.GuildConfigsForId(arg.Id);
|
||||||
}
|
}
|
||||||
|
await JoinedGuild.Invoke(gc).ConfigureAwait(false);
|
||||||
await JoinedGuild.Invoke(gc);
|
|
||||||
});
|
});
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task RunAsync()
|
public async Task RunAsync()
|
||||||
{
|
{
|
||||||
if (ShardId == 0)
|
|
||||||
await _db.SetupAsync();
|
|
||||||
|
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
|
|
||||||
await LoginAsync(_creds.Token);
|
await LoginAsync(_creds.Token).ConfigureAwait(false);
|
||||||
|
|
||||||
Mention = Client.CurrentUser.Mention;
|
Mention = Client.CurrentUser.Mention;
|
||||||
Log.Information("Shard {ShardId} loading services...", Client.ShardId);
|
Log.Information("Shard {ShardId} loading services...", Client.ShardId);
|
||||||
@@ -309,10 +316,10 @@ public sealed class Bot
|
|||||||
var commandHandler = Services.GetRequiredService<CommandHandler>();
|
var commandHandler = Services.GetRequiredService<CommandHandler>();
|
||||||
|
|
||||||
// start handling messages received in commandhandler
|
// start handling messages received in commandhandler
|
||||||
await commandHandler.StartHandling();
|
await commandHandler.StartHandling().ConfigureAwait(false);
|
||||||
|
|
||||||
await _commandService.AddModulesAsync(typeof(Bot).Assembly, Services);
|
await _commandService.AddModulesAsync(typeof(Bot).Assembly, Services);
|
||||||
// await _interactionService.AddModulesAsync(typeof(Bot).Assembly, Services);
|
|
||||||
IsReady = true;
|
IsReady = true;
|
||||||
_ = Task.Run(ExecuteReadySubscriptions);
|
_ = Task.Run(ExecuteReadySubscriptions);
|
||||||
Log.Information("Shard {ShardId} ready", Client.ShardId);
|
Log.Information("Shard {ShardId} ready", Client.ShardId);
|
||||||
@@ -336,51 +343,23 @@ public sealed class Bot
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return tasks.WhenAll();
|
return Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task Client_Log(LogMessage arg)
|
private Task Client_Log(LogMessage arg)
|
||||||
{
|
{
|
||||||
if (arg.Message?.Contains("unknown dispatch", StringComparison.InvariantCultureIgnoreCase) ?? false)
|
if (arg.Exception != null)
|
||||||
return Task.CompletedTask;
|
Log.Warning(arg.Exception, arg.Source + " | " + arg.Message);
|
||||||
|
|
||||||
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
|
else
|
||||||
Log.Warning("{ErrorSource} | {ErrorMessage}", arg.Source, arg.Message);
|
Log.Warning(arg.Source + " | " + arg.Message);
|
||||||
#endif
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task RunAndBlockAsync()
|
public async Task RunAndBlockAsync()
|
||||||
{
|
{
|
||||||
await RunAsync();
|
await RunAsync().ConfigureAwait(false);
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,10 +0,0 @@
|
|||||||
#nullable disable
|
|
||||||
namespace NadekoBot.Common;
|
|
||||||
|
|
||||||
public enum AddRemove
|
|
||||||
{
|
|
||||||
Add = int.MinValue,
|
|
||||||
Remove = int.MinValue + 1,
|
|
||||||
Rem = int.MinValue + 1,
|
|
||||||
Rm = int.MinValue + 1
|
|
||||||
}
|
|
@@ -1,20 +1,20 @@
|
|||||||
#nullable disable
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace NadekoBot.Common;
|
namespace NadekoBot.Common
|
||||||
|
{
|
||||||
public class AsyncLazy<T> : Lazy<Task<T>>
|
public class AsyncLazy<T> : Lazy<Task<T>>
|
||||||
{
|
{
|
||||||
public AsyncLazy(Func<T> valueFactory)
|
public AsyncLazy(Func<T> valueFactory) :
|
||||||
: base(() => Task.Run(valueFactory))
|
base(() => Task.Run(valueFactory))
|
||||||
{
|
{ }
|
||||||
|
|
||||||
|
public AsyncLazy(Func<Task<T>> taskFactory) :
|
||||||
|
base(() => Task.Run(taskFactory))
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public TaskAwaiter<T> GetAwaiter() { return Value.GetAwaiter(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public AsyncLazy(Func<Task<T>> taskFactory)
|
|
||||||
: base(() => Task.Run(taskFactory))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public TaskAwaiter<T> GetAwaiter()
|
|
||||||
=> Value.GetAwaiter();
|
|
||||||
}
|
}
|
@@ -1,7 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using Discord.Commands;
|
||||||
namespace NadekoBot.Common.Attributes;
|
using NadekoBot.Services;
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
public sealed class AliasesAttribute : AliasAttribute
|
public sealed class AliasesAttribute : AliasAttribute
|
||||||
{
|
{
|
||||||
@@ -10,3 +15,4 @@ public sealed class AliasesAttribute : AliasAttribute
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
15
src/NadekoBot/Common/Attributes/BotPerm.cs
Normal file
15
src/NadekoBot/Common/Attributes/BotPerm.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using Discord.Commands;
|
||||||
|
|
||||||
|
namespace Discord
|
||||||
|
{
|
||||||
|
public class BotPermAttribute : RequireBotPermissionAttribute
|
||||||
|
{
|
||||||
|
public BotPermAttribute(GuildPerm permission) : base((GuildPermission)permission)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public BotPermAttribute(ChannelPerm permission) : base((ChannelPermission)permission)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,31 +1,36 @@
|
|||||||
using YamlDotNet.Serialization;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
namespace NadekoBot.Common.Attributes;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
public static class CommandNameLoadHelper
|
public static class CommandNameLoadHelper
|
||||||
{
|
{
|
||||||
private static readonly IDeserializer _deserializer = new Deserializer();
|
|
||||||
|
|
||||||
private static readonly Lazy<Dictionary<string, string[]>> _lazyCommandAliases
|
private static YamlDotNet.Serialization.IDeserializer _deserializer
|
||||||
= new(() => LoadAliases());
|
= new YamlDotNet.Serialization.Deserializer();
|
||||||
|
|
||||||
public static Dictionary<string, string[]> LoadAliases(string aliasesFilePath = "data/aliases.yml")
|
public static Lazy<Dictionary<string, string[]>> LazyCommandAliases
|
||||||
|
= new Lazy<Dictionary<string, string[]>>(() => LoadCommandNames());
|
||||||
|
public static Dictionary<string, string[]> LoadCommandNames(string aliasesFilePath = "data/aliases.yml")
|
||||||
{
|
{
|
||||||
var text = File.ReadAllText(aliasesFilePath);
|
var text = File.ReadAllText(aliasesFilePath);
|
||||||
return _deserializer.Deserialize<Dictionary<string, string[]>>(text);
|
return _deserializer.Deserialize<Dictionary<string, string[]>>(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string[] GetAliasesFor(string methodName)
|
public static string[] GetAliasesFor(string methodName)
|
||||||
=> _lazyCommandAliases.Value.TryGetValue(methodName.ToLowerInvariant(), out var aliases) && aliases.Length > 1
|
=> LazyCommandAliases.Value.TryGetValue(methodName.ToLowerInvariant(), out var aliases) && aliases.Length > 1
|
||||||
? aliases.Skip(1).ToArray()
|
? aliases.Skip(1).ToArray()
|
||||||
: Array.Empty<string>();
|
: Array.Empty<string>();
|
||||||
|
|
||||||
public static string GetCommandNameFor(string methodName)
|
public static string GetCommandNameFor(string methodName)
|
||||||
{
|
{
|
||||||
methodName = methodName.ToLowerInvariant();
|
methodName = methodName.ToLowerInvariant();
|
||||||
var toReturn = _lazyCommandAliases.Value.TryGetValue(methodName, out var aliases) && aliases.Length > 0
|
var toReturn = LazyCommandAliases.Value.TryGetValue(methodName, out var aliases) && aliases.Length > 0
|
||||||
? aliases[0]
|
? aliases[0]
|
||||||
: methodName;
|
: methodName;
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
16
src/NadekoBot/Common/Attributes/Description.cs
Normal file
16
src/NadekoBot/Common/Attributes/Description.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public sealed class DescriptionAttribute : SummaryAttribute
|
||||||
|
{
|
||||||
|
// Localization.LoadCommand(memberName.ToLowerInvariant()).Desc
|
||||||
|
public DescriptionAttribute(string text = "") : base(text)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
src/NadekoBot/Common/Attributes/LeftoverAttribute.cs
Normal file
9
src/NadekoBot/Common/Attributes/LeftoverAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Discord.Commands
|
||||||
|
{
|
||||||
|
public class LeftoverAttribute : RemainderAttribute
|
||||||
|
{
|
||||||
|
public LeftoverAttribute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,13 +1,19 @@
|
|||||||
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
|
||||||
namespace NadekoBot.Common.Attributes;
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
public sealed class NadekoCommandAttribute : CommandAttribute
|
public sealed class NadekoCommandAttribute : CommandAttribute
|
||||||
{
|
{
|
||||||
public string MethodName { get; }
|
|
||||||
|
|
||||||
public NadekoCommandAttribute([CallerMemberName] string memberName="")
|
public NadekoCommandAttribute([CallerMemberName] string memberName="")
|
||||||
: base(CommandNameLoadHelper.GetCommandNameFor(memberName))
|
: base(CommandNameLoadHelper.GetCommandNameFor(memberName))
|
||||||
=> MethodName = memberName.ToLowerInvariant();
|
{
|
||||||
|
this.MethodName = memberName.ToLowerInvariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string MethodName { get; }
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,30 +1,14 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using System;
|
||||||
|
using Discord.Commands;
|
||||||
namespace NadekoBot.Common.Attributes;
|
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
internal sealed class NadekoModuleAttribute : GroupAttribute
|
sealed class NadekoModuleAttribute : GroupAttribute
|
||||||
{
|
{
|
||||||
public NadekoModuleAttribute(string moduleName)
|
public NadekoModuleAttribute(string moduleName) : base(moduleName)
|
||||||
: base(moduleName)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
|
||||||
internal sealed class NadekoDescriptionAttribute : SummaryAttribute
|
|
||||||
{
|
|
||||||
public NadekoDescriptionAttribute([CallerMemberName] string name = "")
|
|
||||||
: base(name.ToLowerInvariant())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
|
||||||
internal sealed class NadekoUsageAttribute : RemarksAttribute
|
|
||||||
{
|
|
||||||
public NadekoUsageAttribute([CallerMemberName] string name = "")
|
|
||||||
: base(name.ToLowerInvariant())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,10 +1,15 @@
|
|||||||
namespace NadekoBot.Common.Attributes;
|
using System;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
public sealed class NadekoOptionsAttribute : Attribute
|
public sealed class NadekoOptionsAttribute : Attribute
|
||||||
{
|
{
|
||||||
public Type OptionType { get; set; }
|
public Type OptionType { get; set; }
|
||||||
|
|
||||||
public NadekoOptionsAttribute(Type t)
|
public NadekoOptionsAttribute(Type t)
|
||||||
=> OptionType = t;
|
{
|
||||||
|
this.OptionType = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,19 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord.Commands;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using NadekoBot.Extensions;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
|
||||||
namespace NadekoBot.Common.Attributes;
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||||
public sealed class OwnerOnlyAttribute : PreconditionAttribute
|
public sealed class OwnerOnlyAttribute : PreconditionAttribute
|
||||||
{
|
{
|
||||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo executingCommand, IServiceProvider services)
|
||||||
ICommandContext context,
|
|
||||||
CommandInfo command,
|
|
||||||
IServiceProvider services)
|
|
||||||
{
|
{
|
||||||
var creds = services.GetRequiredService<IBotCredsProvider>().GetCreds();
|
var creds = services.GetRequiredService<IBotCredsProvider>().GetCreds();
|
||||||
|
|
||||||
return Task.FromResult(creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id
|
return Task.FromResult((creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("Not owner")));
|
||||||
? PreconditionResult.FromSuccess()
|
}
|
||||||
: PreconditionResult.FromError("Not owner"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,7 +1,11 @@
|
|||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace NadekoBot.Common.Attributes;
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
public sealed class RatelimitAttribute : PreconditionAttribute
|
public sealed class RatelimitAttribute : PreconditionAttribute
|
||||||
{
|
{
|
||||||
@@ -15,10 +19,7 @@ public sealed class RatelimitAttribute : PreconditionAttribute
|
|||||||
Seconds = seconds;
|
Seconds = seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||||
ICommandContext context,
|
|
||||||
CommandInfo command,
|
|
||||||
IServiceProvider services)
|
|
||||||
{
|
{
|
||||||
if (Seconds == 0)
|
if (Seconds == 0)
|
||||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||||
@@ -34,3 +35,4 @@ public sealed class RatelimitAttribute : PreconditionAttribute
|
|||||||
return Task.FromResult(PreconditionResult.FromError(msgContent));
|
return Task.FromResult(PreconditionResult.FromError(msgContent));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
21
src/NadekoBot/Common/Attributes/Usage.cs
Normal file
21
src/NadekoBot/Common/Attributes/Usage.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Discord.Commands;
|
||||||
|
using NadekoBot.Services;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public sealed class UsageAttribute : RemarksAttribute
|
||||||
|
{
|
||||||
|
// public static string GetUsage(string memberName)
|
||||||
|
// {
|
||||||
|
// var usage = Localization.LoadCommand(memberName.ToLowerInvariant()).Usage;
|
||||||
|
// return JsonConvert.SerializeObject(usage);
|
||||||
|
// }
|
||||||
|
public UsageAttribute(string text = "") : base(text)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,30 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord.Commands;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using NadekoBot.Modules.Administration.Services;
|
using NadekoBot.Modules.Administration.Services;
|
||||||
|
|
||||||
namespace Discord;
|
namespace Discord
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||||
|
public class UserPermAttribute : PreconditionAttribute
|
||||||
|
{
|
||||||
|
public RequireUserPermissionAttribute UserPermissionAttribute { get; }
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
|
||||||
public class UserPermAttribute : RequireUserPermissionAttribute
|
|
||||||
{
|
|
||||||
public UserPermAttribute(GuildPerm permission)
|
public UserPermAttribute(GuildPerm permission)
|
||||||
: base(permission)
|
|
||||||
{
|
{
|
||||||
|
UserPermissionAttribute = new RequireUserPermissionAttribute((GuildPermission)permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserPermAttribute(ChannelPerm permission)
|
public UserPermAttribute(ChannelPerm permission)
|
||||||
: base(permission)
|
|
||||||
{
|
{
|
||||||
|
UserPermissionAttribute = new RequireUserPermissionAttribute((ChannelPermission)permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task<PreconditionResult> CheckPermissionsAsync(
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||||
ICommandContext context,
|
|
||||||
CommandInfo command,
|
|
||||||
IServiceProvider services)
|
|
||||||
{
|
{
|
||||||
var permService = services.GetRequiredService<DiscordPermOverrideService>();
|
var permService = services.GetRequiredService<DiscordPermOverrideService>();
|
||||||
if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out _))
|
if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out var _))
|
||||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||||
|
|
||||||
return base.CheckPermissionsAsync(context, command, services);
|
return UserPermissionAttribute.CheckPermissionsAsync(context, command, services);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,17 +1,20 @@
|
|||||||
#nullable disable
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace NadekoBot.Common;
|
|
||||||
|
|
||||||
|
namespace NadekoBot.Common
|
||||||
|
{
|
||||||
public class CmdStrings
|
public class CmdStrings
|
||||||
{
|
{
|
||||||
public string[] Usages { get; }
|
public string[] Usages { get; }
|
||||||
public string Description { get; }
|
public string Description { get; }
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public CmdStrings([JsonProperty("args")] string[] usages, [JsonProperty("desc")] string description)
|
public CmdStrings(
|
||||||
|
[JsonProperty("args")]string[] usages,
|
||||||
|
[JsonProperty("desc")]string description
|
||||||
|
)
|
||||||
{
|
{
|
||||||
Usages = usages;
|
Usages = usages;
|
||||||
Description = description;
|
Description = description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
@@ -1,12 +1,15 @@
|
|||||||
#nullable disable
|
// License MIT
|
||||||
#pragma warning disable
|
|
||||||
// License MIT
|
|
||||||
// Source: https://github.com/i3arnon/ConcurrentHashSet
|
// Source: https://github.com/i3arnon/ConcurrentHashSet
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace System.Collections.Generic;
|
namespace NadekoBot.Common.Collections
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a thread-safe hash-based unique collection.
|
/// Represents a thread-safe hash-based unique collection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -18,19 +21,55 @@ namespace System.Collections.Generic;
|
|||||||
[DebuggerDisplay("Count = {Count}")]
|
[DebuggerDisplay("Count = {Count}")]
|
||||||
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T>
|
public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T>
|
||||||
{
|
{
|
||||||
private const int DEFAULT_CAPACITY = 31;
|
private const int DefaultCapacity = 31;
|
||||||
private const int MAX_LOCK_NUMBER = 1024;
|
private const int MaxLockNumber = 1024;
|
||||||
|
|
||||||
private static int DefaultConcurrencyLevel
|
private readonly IEqualityComparer<T> _comparer;
|
||||||
=> PlatformHelper.ProcessorCount;
|
private readonly bool _growLockArray;
|
||||||
|
|
||||||
|
private int _budget;
|
||||||
|
private volatile Tables _tables;
|
||||||
|
|
||||||
|
private static int DefaultConcurrencyLevel => PlatformHelper.ProcessorCount;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the number of items contained in the <see
|
||||||
|
/// cref="ConcurrentHashSet{T}"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The number of items contained in the <see
|
||||||
|
/// cref="ConcurrentHashSet{T}"/>.</value>
|
||||||
|
/// <remarks>Count has snapshot semantics and represents the number of items in the <see
|
||||||
|
/// cref="ConcurrentHashSet{T}"/>
|
||||||
|
/// at the moment when Count was accessed.</remarks>
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var count = 0;
|
||||||
|
var acquiredLocks = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
AcquireAllLocks(ref acquiredLocks);
|
||||||
|
|
||||||
|
for (var i = 0; i < _tables.CountPerLock.Length; i++)
|
||||||
|
{
|
||||||
|
count += _tables.CountPerLock[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ReleaseLocks(0, acquiredLocks);
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value that indicates whether the <see cref="ConcurrentHashSet{T}"/> is empty.
|
/// Gets a value that indicates whether the <see cref="ConcurrentHashSet{T}"/> is empty.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>true if the <see cref="ConcurrentHashSet{T}"/> is empty; otherwise,
|
||||||
/// true if the <see cref="ConcurrentHashSet{T}" /> is empty; otherwise,
|
/// false.</value>
|
||||||
/// false.
|
|
||||||
/// </value>
|
|
||||||
public bool IsEmpty
|
public bool IsEmpty
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -40,12 +79,14 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
{
|
{
|
||||||
AcquireAllLocks(ref acquiredLocks);
|
AcquireAllLocks(ref acquiredLocks);
|
||||||
|
|
||||||
for (var i = 0; i < tables.CountPerLock.Length; i++)
|
for (var i = 0; i < _tables.CountPerLock.Length; i++)
|
||||||
|
{
|
||||||
|
if (_tables.CountPerLock[i] != 0)
|
||||||
{
|
{
|
||||||
if (tables.CountPerLock[i] != 0)
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
ReleaseLocks(0, acquiredLocks);
|
ReleaseLocks(0, acquiredLocks);
|
||||||
@@ -55,92 +96,32 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ICollection<T>.IsReadOnly
|
|
||||||
=> false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the number of items contained in the
|
/// Initializes a new instance of the <see
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}" />
|
|
||||||
/// .
|
|
||||||
/// </summary>
|
|
||||||
/// <value>
|
|
||||||
/// The number of items contained in the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}" />
|
|
||||||
/// .
|
|
||||||
/// </value>
|
|
||||||
/// <remarks>
|
|
||||||
/// Count has snapshot semantics and represents the number of items in the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}" />
|
|
||||||
/// at the moment when Count was accessed.
|
|
||||||
/// </remarks>
|
|
||||||
public int Count
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var count = 0;
|
|
||||||
var acquiredLocks = 0;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AcquireAllLocks(ref acquiredLocks);
|
|
||||||
|
|
||||||
for (var i = 0; i < tables.CountPerLock.Length; i++)
|
|
||||||
count += tables.CountPerLock[i];
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ReleaseLocks(0, acquiredLocks);
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly IEqualityComparer<T> _comparer;
|
|
||||||
private readonly bool _growLockArray;
|
|
||||||
|
|
||||||
private int budget;
|
|
||||||
private volatile Tables tables;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}"/>
|
/// cref="ConcurrentHashSet{T}"/>
|
||||||
/// class that is empty, has the default concurrency level, has the default initial capacity, and
|
/// class that is empty, has the default concurrency level, has the default initial capacity, and
|
||||||
/// uses the default comparer for the item type.
|
/// uses the default comparer for the item type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ConcurrentHashSet()
|
public ConcurrentHashSet()
|
||||||
: this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, EqualityComparer<T>.Default)
|
: this(DefaultConcurrencyLevel, DefaultCapacity, true, EqualityComparer<T>.Default)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the
|
/// Initializes a new instance of the <see
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}"/>
|
/// cref="ConcurrentHashSet{T}"/>
|
||||||
/// class that is empty, has the specified concurrency level and capacity, and uses the default
|
/// class that is empty, has the specified concurrency level and capacity, and uses the default
|
||||||
/// comparer for the item type.
|
/// comparer for the item type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="concurrencyLevel">
|
/// <param name="concurrencyLevel">The estimated number of threads that will update the
|
||||||
/// The estimated number of threads that will update the
|
/// <see cref="ConcurrentHashSet{T}"/> concurrently.</param>
|
||||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
/// <param name="capacity">The initial number of elements that the <see
|
||||||
/// </param>
|
|
||||||
/// <param name="capacity">
|
|
||||||
/// The initial number of elements that the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}"/>
|
/// cref="ConcurrentHashSet{T}"/>
|
||||||
/// can contain.
|
/// can contain.</param>
|
||||||
/// </param>
|
/// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="concurrencyLevel"/> is
|
||||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
/// less than 1.</exception>
|
||||||
/// <paramref name="concurrencyLevel" /> is
|
/// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="capacity"/> is less than
|
||||||
/// less than 1.
|
/// 0.</exception>
|
||||||
/// </exception>
|
|
||||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
|
||||||
/// <paramref name="capacity" /> is less than
|
|
||||||
/// 0.
|
|
||||||
/// </exception>
|
|
||||||
public ConcurrentHashSet(int concurrencyLevel, int capacity)
|
public ConcurrentHashSet(int concurrencyLevel, int capacity)
|
||||||
: this(concurrencyLevel, capacity, false, EqualityComparer<T>.Default)
|
: this(concurrencyLevel, capacity, false, EqualityComparer<T>.Default)
|
||||||
{
|
{
|
||||||
@@ -148,20 +129,14 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}"/>
|
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}"/>
|
||||||
/// class that contains elements copied from the specified
|
/// class that contains elements copied from the specified <see
|
||||||
/// <see
|
/// cref="T:System.Collections.IEnumerable{T}"/>, has the default concurrency
|
||||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
|
||||||
/// , has the default concurrency
|
|
||||||
/// level, has the default initial capacity, and uses the default comparer for the item type.
|
/// level, has the default initial capacity, and uses the default comparer for the item type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="collection">
|
/// <param name="collection">The <see
|
||||||
/// The
|
/// cref="T:System.Collections.IEnumerable{T}"/> whose elements are copied to
|
||||||
/// <see
|
|
||||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
|
||||||
/// whose elements are copied to
|
|
||||||
/// the new
|
/// the new
|
||||||
/// <see cref="ConcurrentHashSet{T}" />.
|
/// <see cref="ConcurrentHashSet{T}"/>.</param>
|
||||||
/// </param>
|
|
||||||
/// <exception cref="T:System.ArgumentNullException"><paramref name="collection"/> is a null reference.</exception>
|
/// <exception cref="T:System.ArgumentNullException"><paramref name="collection"/> is a null reference.</exception>
|
||||||
public ConcurrentHashSet(IEnumerable<T> collection)
|
public ConcurrentHashSet(IEnumerable<T> collection)
|
||||||
: this(collection, EqualityComparer<T>.Default)
|
: this(collection, EqualityComparer<T>.Default)
|
||||||
@@ -173,47 +148,35 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
/// class that is empty, has the specified concurrency level and capacity, and uses the specified
|
/// class that is empty, has the specified concurrency level and capacity, and uses the specified
|
||||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="comparer">
|
/// <param name="comparer">The <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>
|
||||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
/// implementation to use when comparing items.</param>
|
||||||
/// implementation to use when comparing items.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="T:System.ArgumentNullException"><paramref name="comparer"/> is a null reference.</exception>
|
/// <exception cref="T:System.ArgumentNullException"><paramref name="comparer"/> is a null reference.</exception>
|
||||||
public ConcurrentHashSet(IEqualityComparer<T> comparer)
|
public ConcurrentHashSet(IEqualityComparer<T> comparer)
|
||||||
: this(DefaultConcurrencyLevel, DEFAULT_CAPACITY, true, comparer)
|
: this(DefaultConcurrencyLevel, DefaultCapacity, true, comparer)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}"/>
|
/// Initializes a new instance of the <see cref="ConcurrentHashSet{T}"/>
|
||||||
/// class that contains elements copied from the specified
|
/// class that contains elements copied from the specified <see
|
||||||
/// <see
|
/// cref="T:System.Collections.IEnumerable"/>, has the default concurrency level, has the default
|
||||||
/// cref="T:System.Collections.IEnumerable" />
|
|
||||||
/// , has the default concurrency level, has the default
|
|
||||||
/// initial capacity, and uses the specified
|
/// initial capacity, and uses the specified
|
||||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="collection">
|
/// <param name="collection">The <see
|
||||||
/// The
|
/// cref="T:System.Collections.IEnumerable{T}"/> whose elements are copied to
|
||||||
/// <see
|
|
||||||
/// cref="T:System.Collections.IEnumerable{T}" />
|
|
||||||
/// whose elements are copied to
|
|
||||||
/// the new
|
/// the new
|
||||||
/// <see cref="ConcurrentHashSet{T}" />.
|
/// <see cref="ConcurrentHashSet{T}"/>.</param>
|
||||||
/// </param>
|
/// <param name="comparer">The <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>
|
||||||
/// <param name="comparer">
|
/// implementation to use when comparing items.</param>
|
||||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
/// <exception cref="T:System.ArgumentNullException"><paramref name="collection"/> is a null reference
|
||||||
/// implementation to use when comparing items.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="T:System.ArgumentNullException">
|
|
||||||
/// <paramref name="collection" /> is a null reference
|
|
||||||
/// (Nothing in Visual Basic). -or-
|
/// (Nothing in Visual Basic). -or-
|
||||||
/// <paramref name="comparer"/> is a null reference (Nothing in Visual Basic).
|
/// <paramref name="comparer"/> is a null reference (Nothing in Visual Basic).
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
||||||
: this(comparer)
|
: this(comparer)
|
||||||
{
|
{
|
||||||
if (collection is null)
|
if (collection is null) throw new ArgumentNullException(nameof(collection));
|
||||||
throw new ArgumentNullException(nameof(collection));
|
|
||||||
|
|
||||||
InitializeFromCollection(collection);
|
InitializeFromCollection(collection);
|
||||||
}
|
}
|
||||||
@@ -225,18 +188,12 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
/// has the specified concurrency level, has the specified initial capacity, and uses the specified
|
/// has the specified concurrency level, has the specified initial capacity, and uses the specified
|
||||||
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
/// <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="concurrencyLevel">
|
/// <param name="concurrencyLevel">The estimated number of threads that will update the
|
||||||
/// The estimated number of threads that will update the
|
/// <see cref="ConcurrentHashSet{T}"/> concurrently.</param>
|
||||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
/// <param name="collection">The <see cref="T:System.Collections.IEnumerable{T}"/> whose elements are copied to the new
|
||||||
/// </param>
|
/// <see cref="ConcurrentHashSet{T}"/>.</param>
|
||||||
/// <param name="collection">
|
/// <param name="comparer">The <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/> implementation to use
|
||||||
/// The <see cref="T:System.Collections.IEnumerable{T}" /> whose elements are copied to the new
|
/// when comparing items.</param>
|
||||||
/// <see cref="ConcurrentHashSet{T}" />.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="comparer">
|
|
||||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" /> implementation to use
|
|
||||||
/// when comparing items.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="T:System.ArgumentNullException">
|
/// <exception cref="T:System.ArgumentNullException">
|
||||||
/// <paramref name="collection"/> is a null reference.
|
/// <paramref name="collection"/> is a null reference.
|
||||||
/// -or-
|
/// -or-
|
||||||
@@ -246,12 +203,10 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
/// <paramref name="concurrencyLevel"/> is less than 1.
|
/// <paramref name="concurrencyLevel"/> is less than 1.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public ConcurrentHashSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
public ConcurrentHashSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer)
|
||||||
: this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer)
|
: this(concurrencyLevel, DefaultCapacity, false, comparer)
|
||||||
{
|
{
|
||||||
if (collection is null)
|
if (collection is null) throw new ArgumentNullException(nameof(collection));
|
||||||
throw new ArgumentNullException(nameof(collection));
|
if (comparer is null) throw new ArgumentNullException(nameof(comparer));
|
||||||
if (comparer is null)
|
|
||||||
throw new ArgumentNullException(nameof(comparer));
|
|
||||||
|
|
||||||
InitializeFromCollection(collection);
|
InitializeFromCollection(collection);
|
||||||
}
|
}
|
||||||
@@ -261,20 +216,13 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
/// class that is empty, has the specified concurrency level, has the specified initial capacity, and
|
/// class that is empty, has the specified concurrency level, has the specified initial capacity, and
|
||||||
/// uses the specified <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
/// uses the specified <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="concurrencyLevel">
|
/// <param name="concurrencyLevel">The estimated number of threads that will update the
|
||||||
/// The estimated number of threads that will update the
|
/// <see cref="ConcurrentHashSet{T}"/> concurrently.</param>
|
||||||
/// <see cref="ConcurrentHashSet{T}" /> concurrently.
|
/// <param name="capacity">The initial number of elements that the <see
|
||||||
/// </param>
|
|
||||||
/// <param name="capacity">
|
|
||||||
/// The initial number of elements that the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}"/>
|
/// cref="ConcurrentHashSet{T}"/>
|
||||||
/// can contain.
|
/// can contain.</param>
|
||||||
/// </param>
|
/// <param name="comparer">The <see cref="T:System.Collections.Generic.IEqualityComparer{T}"/>
|
||||||
/// <param name="comparer">
|
/// implementation to use when comparing items.</param>
|
||||||
/// The <see cref="T:System.Collections.Generic.IEqualityComparer{T}" />
|
|
||||||
/// implementation to use when comparing items.
|
|
||||||
/// </param>
|
|
||||||
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
/// <exception cref="T:System.ArgumentOutOfRangeException">
|
||||||
/// <paramref name="concurrencyLevel"/> is less than 1. -or-
|
/// <paramref name="concurrencyLevel"/> is less than 1. -or-
|
||||||
/// <paramref name="capacity"/> is less than 0.
|
/// <paramref name="capacity"/> is less than 0.
|
||||||
@@ -285,35 +233,44 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConcurrentHashSet(
|
private ConcurrentHashSet(int concurrencyLevel, int capacity, bool growLockArray, IEqualityComparer<T> comparer)
|
||||||
int concurrencyLevel,
|
|
||||||
int capacity,
|
|
||||||
bool growLockArray,
|
|
||||||
IEqualityComparer<T> comparer)
|
|
||||||
{
|
{
|
||||||
if (concurrencyLevel < 1)
|
if (concurrencyLevel < 1) throw new ArgumentOutOfRangeException(nameof(concurrencyLevel));
|
||||||
throw new ArgumentOutOfRangeException(nameof(concurrencyLevel));
|
if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity));
|
||||||
if (capacity < 0)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(capacity));
|
|
||||||
|
|
||||||
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
|
// The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
|
||||||
// any buckets.
|
// any buckets.
|
||||||
if (capacity < concurrencyLevel)
|
if (capacity < concurrencyLevel)
|
||||||
|
{
|
||||||
capacity = concurrencyLevel;
|
capacity = concurrencyLevel;
|
||||||
|
}
|
||||||
|
|
||||||
var locks = new object[concurrencyLevel];
|
var locks = new object[concurrencyLevel];
|
||||||
for (var i = 0; i < locks.Length; i++)
|
for (var i = 0; i < locks.Length; i++)
|
||||||
locks[i] = new();
|
{
|
||||||
|
locks[i] = new object();
|
||||||
|
}
|
||||||
|
|
||||||
var countPerLock = new int[locks.Length];
|
var countPerLock = new int[locks.Length];
|
||||||
var buckets = new Node[capacity];
|
var buckets = new Node[capacity];
|
||||||
tables = new(buckets, locks, countPerLock);
|
_tables = new Tables(buckets, locks, countPerLock);
|
||||||
|
|
||||||
_growLockArray = growLockArray;
|
_growLockArray = growLockArray;
|
||||||
budget = buckets.Length / locks.Length;
|
_budget = buckets.Length / locks.Length;
|
||||||
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the specified item to the <see cref="ConcurrentHashSet{T}"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item to add.</param>
|
||||||
|
/// <returns>true if the items was added to the <see cref="ConcurrentHashSet{T}"/>
|
||||||
|
/// successfully; false if it already exists.</returns>
|
||||||
|
/// <exception cref="T:System.OverflowException">The <see cref="ConcurrentHashSet{T}"/>
|
||||||
|
/// contains too many items.</exception>
|
||||||
|
public bool Add(T item) =>
|
||||||
|
AddInternal(item, _comparer.GetHashCode(item), true);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all items from the <see cref="ConcurrentHashSet{T}"/>.
|
/// Removes all items from the <see cref="ConcurrentHashSet{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -324,9 +281,9 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
{
|
{
|
||||||
AcquireAllLocks(ref locksAcquired);
|
AcquireAllLocks(ref locksAcquired);
|
||||||
|
|
||||||
var newTables = new Tables(new Node[DEFAULT_CAPACITY], tables.Locks, new int[tables.CountPerLock.Length]);
|
var newTables = new Tables(new Node[DefaultCapacity], _tables.Locks, new int[_tables.CountPerLock.Length]);
|
||||||
tables = newTables;
|
_tables = newTables;
|
||||||
budget = Math.Max(1, newTables.Buckets.Length / newTables.Locks.Length);
|
_budget = Math.Max(1, newTables.Buckets.Length / newTables.Locks.Length);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -342,113 +299,29 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
/// <returns>true if the <see cref="ConcurrentHashSet{T}"/> contains the item; otherwise, false.</returns>
|
/// <returns>true if the <see cref="ConcurrentHashSet{T}"/> contains the item; otherwise, false.</returns>
|
||||||
public bool Contains(T item)
|
public bool Contains(T item)
|
||||||
{
|
{
|
||||||
var hashcode = _comparer.GetHashCode(item!);
|
var hashcode = _comparer.GetHashCode(item);
|
||||||
|
|
||||||
// We must capture the _buckets field in a local variable. It is set to a new table on each table resize.
|
// We must capture the _buckets field in a local variable. It is set to a new table on each table resize.
|
||||||
var localTables = tables;
|
var tables = _tables;
|
||||||
|
|
||||||
var bucketNo = GetBucket(hashcode, localTables.Buckets.Length);
|
var bucketNo = GetBucket(hashcode, tables.Buckets.Length);
|
||||||
|
|
||||||
// We can get away w/out a lock here.
|
// We can get away w/out a lock here.
|
||||||
// The Volatile.Read ensures that the load of the fields of 'n' doesn't move before the load from buckets[i].
|
// The Volatile.Read ensures that the load of the fields of 'n' doesn't move before the load from buckets[i].
|
||||||
var current = Volatile.Read(ref localTables.Buckets[bucketNo]);
|
var current = Volatile.Read(ref tables.Buckets[bucketNo]);
|
||||||
|
|
||||||
while (current is not null)
|
while (current != null)
|
||||||
{
|
{
|
||||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
current = current.Next;
|
current = current.Next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ICollection<T>.Add(T item)
|
|
||||||
=> Add(item);
|
|
||||||
|
|
||||||
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
|
|
||||||
{
|
|
||||||
if (array is null)
|
|
||||||
throw new ArgumentNullException(nameof(array));
|
|
||||||
if (arrayIndex < 0)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
|
||||||
|
|
||||||
var locksAcquired = 0;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
AcquireAllLocks(ref locksAcquired);
|
|
||||||
|
|
||||||
var count = 0;
|
|
||||||
|
|
||||||
for (var i = 0; i < tables.Locks.Length && count >= 0; i++)
|
|
||||||
count += tables.CountPerLock[i];
|
|
||||||
|
|
||||||
if (array.Length - count < arrayIndex || count < 0) //"count" itself or "count + arrayIndex" can overflow
|
|
||||||
{
|
|
||||||
throw new ArgumentException(
|
|
||||||
"The index is equal to or greater than the length of the array, or the number of elements in the set is greater than the available space from index to the end of the destination array.");
|
|
||||||
}
|
|
||||||
|
|
||||||
CopyToItems(array, arrayIndex);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ReleaseLocks(0, locksAcquired);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ICollection<T>.Remove(T item)
|
|
||||||
=> TryRemove(item);
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
|
||||||
=> GetEnumerator();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns an enumerator that iterates through the
|
|
||||||
/// <see
|
|
||||||
/// cref="ConcurrentHashSet{T}" />
|
|
||||||
/// .
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>An enumerator for the <see cref="ConcurrentHashSet{T}" />.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// The enumerator returned from the collection is safe to use concurrently with
|
|
||||||
/// reads and writes to the collection, however it does not represent a moment-in-time snapshot
|
|
||||||
/// of the collection. The contents exposed through the enumerator may contain modifications
|
|
||||||
/// made to the collection after <see cref="GetEnumerator" /> was called.
|
|
||||||
/// </remarks>
|
|
||||||
public IEnumerator<T> GetEnumerator()
|
|
||||||
{
|
|
||||||
var buckets = tables.Buckets;
|
|
||||||
|
|
||||||
for (var i = 0; i < buckets.Length; i++)
|
|
||||||
{
|
|
||||||
// The Volatile.Read ensures that the load of the fields of 'current' doesn't move before the load from buckets[i].
|
|
||||||
var current = Volatile.Read(ref buckets[i]);
|
|
||||||
|
|
||||||
while (current is not null)
|
|
||||||
{
|
|
||||||
yield return current.Item;
|
|
||||||
current = current.Next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds the specified item to the <see cref="ConcurrentHashSet{T}" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item to add.</param>
|
|
||||||
/// <returns>
|
|
||||||
/// true if the items was added to the <see cref="ConcurrentHashSet{T}" />
|
|
||||||
/// successfully; false if it already exists.
|
|
||||||
/// </returns>
|
|
||||||
/// <exception cref="T:System.OverflowException">
|
|
||||||
/// The <see cref="ConcurrentHashSet{T}" />
|
|
||||||
/// contains too many items.
|
|
||||||
/// </exception>
|
|
||||||
public bool Add(T item)
|
|
||||||
=> AddInternal(item, _comparer.GetHashCode(item), true);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attempts to remove the item from the <see cref="ConcurrentHashSet{T}"/>.
|
/// Attempts to remove the item from the <see cref="ConcurrentHashSet{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -459,38 +332,38 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
var hashcode = _comparer.GetHashCode(item);
|
var hashcode = _comparer.GetHashCode(item);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var localTables = tables;
|
var tables = _tables;
|
||||||
|
|
||||||
GetBucketAndLockNo(hashcode,
|
GetBucketAndLockNo(hashcode, out var bucketNo, out var lockNo, tables.Buckets.Length, tables.Locks.Length);
|
||||||
out var bucketNo,
|
|
||||||
out var lockNo,
|
|
||||||
localTables.Buckets.Length,
|
|
||||||
localTables.Locks.Length);
|
|
||||||
|
|
||||||
lock (localTables.Locks[lockNo])
|
lock (tables.Locks[lockNo])
|
||||||
{
|
{
|
||||||
// If the table just got resized, we may not be holding the right lock, and must retry.
|
// If the table just got resized, we may not be holding the right lock, and must retry.
|
||||||
// This should be a rare occurrence.
|
// This should be a rare occurrence.
|
||||||
if (localTables != tables)
|
if (tables != _tables)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Node previous = null;
|
Node previous = null;
|
||||||
for (var current = localTables.Buckets[bucketNo]; current is not null; current = current.Next)
|
for (var current = tables.Buckets[bucketNo]; current != null; current = current.Next)
|
||||||
{
|
{
|
||||||
Debug.Assert((previous is null && current == localTables.Buckets[bucketNo])
|
Debug.Assert((previous is null && current == tables.Buckets[bucketNo]) || previous.Next == current);
|
||||||
|| previous!.Next == current);
|
|
||||||
|
|
||||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||||
{
|
{
|
||||||
if (previous is null)
|
if (previous is null)
|
||||||
Volatile.Write(ref localTables.Buckets[bucketNo], current.Next);
|
{
|
||||||
|
Volatile.Write(ref tables.Buckets[bucketNo], current.Next);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
previous.Next = current.Next;
|
previous.Next = current.Next;
|
||||||
|
|
||||||
localTables.CountPerLock[lockNo]--;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tables.CountPerLock[lockNo]--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
previous = current;
|
previous = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -499,55 +372,121 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
|
/// <summary>Returns an enumerator that iterates through the <see
|
||||||
|
/// cref="ConcurrentHashSet{T}"/>.</summary>
|
||||||
|
/// <returns>An enumerator for the <see cref="ConcurrentHashSet{T}"/>.</returns>
|
||||||
|
/// <remarks>
|
||||||
|
/// The enumerator returned from the collection is safe to use concurrently with
|
||||||
|
/// reads and writes to the collection, however it does not represent a moment-in-time snapshot
|
||||||
|
/// of the collection. The contents exposed through the enumerator may contain modifications
|
||||||
|
/// made to the collection after <see cref="GetEnumerator"/> was called.
|
||||||
|
/// </remarks>
|
||||||
|
public IEnumerator<T> GetEnumerator()
|
||||||
|
{
|
||||||
|
var buckets = _tables.Buckets;
|
||||||
|
|
||||||
|
for (var i = 0; i < buckets.Length; i++)
|
||||||
|
{
|
||||||
|
// The Volatile.Read ensures that the load of the fields of 'current' doesn't move before the load from buckets[i].
|
||||||
|
var current = Volatile.Read(ref buckets[i]);
|
||||||
|
|
||||||
|
while (current != null)
|
||||||
|
{
|
||||||
|
yield return current.Item;
|
||||||
|
current = current.Next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICollection<T>.Add(T item) => Add(item);
|
||||||
|
|
||||||
|
bool ICollection<T>.IsReadOnly => false;
|
||||||
|
|
||||||
|
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
if (array is null) throw new ArgumentNullException(nameof(array));
|
||||||
|
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
||||||
|
|
||||||
|
var locksAcquired = 0;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
AcquireAllLocks(ref locksAcquired);
|
||||||
|
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
for (var i = 0; i < _tables.Locks.Length && count >= 0; i++)
|
||||||
|
{
|
||||||
|
count += _tables.CountPerLock[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array.Length - count < arrayIndex || count < 0) //"count" itself or "count + arrayIndex" can overflow
|
||||||
|
{
|
||||||
|
throw new ArgumentException("The index is equal to or greater than the length of the array, or the number of elements in the set is greater than the available space from index to the end of the destination array.");
|
||||||
|
}
|
||||||
|
|
||||||
|
CopyToItems(array, arrayIndex);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ReleaseLocks(0, locksAcquired);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<T>.Remove(T item) => TryRemove(item);
|
||||||
|
|
||||||
private void InitializeFromCollection(IEnumerable<T> collection)
|
private void InitializeFromCollection(IEnumerable<T> collection)
|
||||||
{
|
{
|
||||||
foreach (var item in collection)
|
foreach (var item in collection)
|
||||||
|
{
|
||||||
AddInternal(item, _comparer.GetHashCode(item), false);
|
AddInternal(item, _comparer.GetHashCode(item), false);
|
||||||
|
}
|
||||||
|
|
||||||
if (budget == 0)
|
if (_budget == 0)
|
||||||
budget = tables.Buckets.Length / tables.Locks.Length;
|
{
|
||||||
|
_budget = _tables.Buckets.Length / _tables.Locks.Length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool AddInternal(T item, int hashcode, bool acquireLock)
|
private bool AddInternal(T item, int hashcode, bool acquireLock)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var localTables = tables;
|
var tables = _tables;
|
||||||
GetBucketAndLockNo(hashcode,
|
GetBucketAndLockNo(hashcode, out var bucketNo, out var lockNo, tables.Buckets.Length, tables.Locks.Length);
|
||||||
out var bucketNo,
|
|
||||||
out var lockNo,
|
|
||||||
localTables.Buckets.Length,
|
|
||||||
localTables.Locks.Length);
|
|
||||||
|
|
||||||
var resizeDesired = false;
|
var resizeDesired = false;
|
||||||
var lockTaken = false;
|
var lockTaken = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (acquireLock)
|
if (acquireLock)
|
||||||
Monitor.Enter(localTables.Locks[lockNo], ref lockTaken);
|
Monitor.Enter(tables.Locks[lockNo], ref lockTaken);
|
||||||
|
|
||||||
// If the table just got resized, we may not be holding the right lock, and must retry.
|
// If the table just got resized, we may not be holding the right lock, and must retry.
|
||||||
// This should be a rare occurrence.
|
// This should be a rare occurrence.
|
||||||
if (localTables != tables)
|
if (tables != _tables)
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Try to find this item in the bucket
|
// Try to find this item in the bucket
|
||||||
Node previous = null;
|
Node previous = null;
|
||||||
for (var current = localTables.Buckets[bucketNo]; current is not null; current = current.Next)
|
for (var current = tables.Buckets[bucketNo]; current != null; current = current.Next)
|
||||||
{
|
{
|
||||||
Debug.Assert((previous is null && current == localTables.Buckets[bucketNo])
|
Debug.Assert((previous is null && current == tables.Buckets[bucketNo]) || previous.Next == current);
|
||||||
|| previous!.Next == current);
|
|
||||||
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
if (hashcode == current.Hashcode && _comparer.Equals(current.Item, item))
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
previous = current;
|
previous = current;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The item was not found in the bucket. Insert the new item.
|
// The item was not found in the bucket. Insert the new item.
|
||||||
Volatile.Write(ref localTables.Buckets[bucketNo], new(item, hashcode, localTables.Buckets[bucketNo]));
|
Volatile.Write(ref tables.Buckets[bucketNo], new Node(item, hashcode, tables.Buckets[bucketNo]));
|
||||||
checked
|
checked
|
||||||
{
|
{
|
||||||
localTables.CountPerLock[lockNo]++;
|
tables.CountPerLock[lockNo]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -555,13 +494,15 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
// It is also possible that GrowTable will increase the budget but won't resize the bucket table.
|
// It is also possible that GrowTable will increase the budget but won't resize the bucket table.
|
||||||
// That happens if the bucket table is found to be poorly utilized due to a bad hash function.
|
// That happens if the bucket table is found to be poorly utilized due to a bad hash function.
|
||||||
//
|
//
|
||||||
if (localTables.CountPerLock[lockNo] > budget)
|
if (tables.CountPerLock[lockNo] > _budget)
|
||||||
|
{
|
||||||
resizeDesired = true;
|
resizeDesired = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (lockTaken)
|
if (lockTaken)
|
||||||
Monitor.Exit(localTables.Locks[lockNo]);
|
Monitor.Exit(tables.Locks[lockNo]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -573,7 +514,9 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
// and then verify that the table we passed to it as the argument is still the current table.
|
// and then verify that the table we passed to it as the argument is still the current table.
|
||||||
//
|
//
|
||||||
if (resizeDesired)
|
if (resizeDesired)
|
||||||
GrowTable(localTables);
|
{
|
||||||
|
GrowTable(tables);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -586,12 +529,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
return bucketNo;
|
return bucketNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GetBucketAndLockNo(
|
private static void GetBucketAndLockNo(int hashcode, out int bucketNo, out int lockNo, int bucketCount, int lockCount)
|
||||||
int hashcode,
|
|
||||||
out int bucketNo,
|
|
||||||
out int lockNo,
|
|
||||||
int bucketCount,
|
|
||||||
int lockCount)
|
|
||||||
{
|
{
|
||||||
bucketNo = (hashcode & 0x7fffffff) % bucketCount;
|
bucketNo = (hashcode & 0x7fffffff) % bucketCount;
|
||||||
lockNo = bucketNo % lockCount;
|
lockNo = bucketNo % lockCount;
|
||||||
@@ -600,7 +538,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
Debug.Assert(lockNo >= 0 && lockNo < lockCount);
|
Debug.Assert(lockNo >= 0 && lockNo < lockCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GrowTable(Tables localTables)
|
private void GrowTable(Tables tables)
|
||||||
{
|
{
|
||||||
const int maxArrayLength = 0X7FEFFFFF;
|
const int maxArrayLength = 0X7FEFFFFF;
|
||||||
var locksAcquired = 0;
|
var locksAcquired = 0;
|
||||||
@@ -610,26 +548,31 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
AcquireLocks(0, 1, ref locksAcquired);
|
AcquireLocks(0, 1, ref locksAcquired);
|
||||||
|
|
||||||
// Make sure nobody resized the table while we were waiting for lock 0:
|
// Make sure nobody resized the table while we were waiting for lock 0:
|
||||||
if (localTables != tables)
|
if (tables != _tables)
|
||||||
|
{
|
||||||
// We assume that since the table reference is different, it was already resized (or the budget
|
// We assume that since the table reference is different, it was already resized (or the budget
|
||||||
// was adjusted). If we ever decide to do table shrinking, or replace the table for other reasons,
|
// was adjusted). If we ever decide to do table shrinking, or replace the table for other reasons,
|
||||||
// we will have to revisit this logic.
|
// we will have to revisit this logic.
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Compute the (approx.) total size. Use an Int64 accumulation variable to avoid an overflow.
|
// Compute the (approx.) total size. Use an Int64 accumulation variable to avoid an overflow.
|
||||||
long approxCount = 0;
|
long approxCount = 0;
|
||||||
for (var i = 0; i < localTables.CountPerLock.Length; i++)
|
for (var i = 0; i < tables.CountPerLock.Length; i++)
|
||||||
approxCount += localTables.CountPerLock[i];
|
{
|
||||||
|
approxCount += tables.CountPerLock[i];
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// If the bucket array is too empty, double the budget instead of resizing the table
|
// If the bucket array is too empty, double the budget instead of resizing the table
|
||||||
//
|
//
|
||||||
if (approxCount < localTables.Buckets.Length / 4)
|
if (approxCount < tables.Buckets.Length / 4)
|
||||||
{
|
{
|
||||||
budget = 2 * budget;
|
_budget = 2 * _budget;
|
||||||
if (budget < 0)
|
if (_budget < 0)
|
||||||
budget = int.MaxValue;
|
{
|
||||||
|
_budget = int.MaxValue;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -642,19 +585,23 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
checked
|
checked
|
||||||
{
|
{
|
||||||
// Double the size of the buckets table and add one, so that we have an odd integer.
|
// Double the size of the buckets table and add one, so that we have an odd integer.
|
||||||
newLength = (localTables.Buckets.Length * 2) + 1;
|
newLength = tables.Buckets.Length * 2 + 1;
|
||||||
|
|
||||||
// Now, we only need to check odd integers, and find the first that is not divisible
|
// Now, we only need to check odd integers, and find the first that is not divisible
|
||||||
// by 3, 5 or 7.
|
// by 3, 5 or 7.
|
||||||
while (newLength % 3 == 0 || newLength % 5 == 0 || newLength % 7 == 0)
|
while (newLength % 3 == 0 || newLength % 5 == 0 || newLength % 7 == 0)
|
||||||
|
{
|
||||||
newLength += 2;
|
newLength += 2;
|
||||||
|
}
|
||||||
|
|
||||||
Debug.Assert(newLength % 2 != 0);
|
Debug.Assert(newLength % 2 != 0);
|
||||||
|
|
||||||
if (newLength > maxArrayLength)
|
if (newLength > maxArrayLength)
|
||||||
|
{
|
||||||
maximizeTableSize = true;
|
maximizeTableSize = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (OverflowException)
|
catch (OverflowException)
|
||||||
{
|
{
|
||||||
maximizeTableSize = true;
|
maximizeTableSize = true;
|
||||||
@@ -669,40 +616,38 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
//
|
//
|
||||||
// (There is one special case that would allow GrowTable() to be called in the future:
|
// (There is one special case that would allow GrowTable() to be called in the future:
|
||||||
// calling Clear() on the ConcurrentHashSet will shrink the table and lower the budget.)
|
// calling Clear() on the ConcurrentHashSet will shrink the table and lower the budget.)
|
||||||
budget = int.MaxValue;
|
_budget = int.MaxValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now acquire all other locks for the table
|
// Now acquire all other locks for the table
|
||||||
AcquireLocks(1, localTables.Locks.Length, ref locksAcquired);
|
AcquireLocks(1, tables.Locks.Length, ref locksAcquired);
|
||||||
|
|
||||||
var newLocks = localTables.Locks;
|
var newLocks = tables.Locks;
|
||||||
|
|
||||||
// Add more locks
|
// Add more locks
|
||||||
if (_growLockArray && localTables.Locks.Length < MAX_LOCK_NUMBER)
|
if (_growLockArray && tables.Locks.Length < MaxLockNumber)
|
||||||
{
|
{
|
||||||
newLocks = new object[localTables.Locks.Length * 2];
|
newLocks = new object[tables.Locks.Length * 2];
|
||||||
Array.Copy(localTables.Locks, 0, newLocks, 0, localTables.Locks.Length);
|
Array.Copy(tables.Locks, 0, newLocks, 0, tables.Locks.Length);
|
||||||
for (var i = localTables.Locks.Length; i < newLocks.Length; i++)
|
for (var i = tables.Locks.Length; i < newLocks.Length; i++)
|
||||||
newLocks[i] = new();
|
{
|
||||||
|
newLocks[i] = new object();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var newBuckets = new Node[newLength];
|
var newBuckets = new Node[newLength];
|
||||||
var newCountPerLock = new int[newLocks.Length];
|
var newCountPerLock = new int[newLocks.Length];
|
||||||
|
|
||||||
// Copy all data into a new table, creating new nodes for all elements
|
// Copy all data into a new table, creating new nodes for all elements
|
||||||
for (var i = 0; i < localTables.Buckets.Length; i++)
|
for (var i = 0; i < tables.Buckets.Length; i++)
|
||||||
{
|
{
|
||||||
var current = localTables.Buckets[i];
|
var current = tables.Buckets[i];
|
||||||
while (current is not null)
|
while (current != null)
|
||||||
{
|
{
|
||||||
var next = current.Next;
|
var next = current.Next;
|
||||||
GetBucketAndLockNo(current.Hashcode,
|
GetBucketAndLockNo(current.Hashcode, out var newBucketNo, out var newLockNo, newBuckets.Length, newLocks.Length);
|
||||||
out var newBucketNo,
|
|
||||||
out var newLockNo,
|
|
||||||
newBuckets.Length,
|
|
||||||
newLocks.Length);
|
|
||||||
|
|
||||||
newBuckets[newBucketNo] = new(current.Item, current.Hashcode, newBuckets[newBucketNo]);
|
newBuckets[newBucketNo] = new Node(current.Item, current.Hashcode, newBuckets[newBucketNo]);
|
||||||
|
|
||||||
checked
|
checked
|
||||||
{
|
{
|
||||||
@@ -714,10 +659,10 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Adjust the budget
|
// Adjust the budget
|
||||||
budget = Math.Max(1, newBuckets.Length / newLocks.Length);
|
_budget = Math.Max(1, newBuckets.Length / newLocks.Length);
|
||||||
|
|
||||||
// Replace tables with the new versions
|
// Replace tables with the new versions
|
||||||
tables = new(newBuckets, newLocks, newCountPerLock);
|
_tables = new Tables(newBuckets, newLocks, newCountPerLock);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -732,10 +677,9 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
var removed = 0;
|
var removed = 0;
|
||||||
foreach (var elem in elems)
|
foreach (var elem in elems)
|
||||||
{
|
{
|
||||||
if (TryRemove(elem))
|
if (this.TryRemove(elem))
|
||||||
removed++;
|
removed++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -746,14 +690,14 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
|
|
||||||
// Now that we have lock 0, the _locks array will not change (i.e., grow),
|
// Now that we have lock 0, the _locks array will not change (i.e., grow),
|
||||||
// and so we can safely read _locks.Length.
|
// and so we can safely read _locks.Length.
|
||||||
AcquireLocks(1, tables.Locks.Length, ref locksAcquired);
|
AcquireLocks(1, _tables.Locks.Length, ref locksAcquired);
|
||||||
Debug.Assert(locksAcquired == tables.Locks.Length);
|
Debug.Assert(locksAcquired == _tables.Locks.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired)
|
private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired)
|
||||||
{
|
{
|
||||||
Debug.Assert(fromInclusive <= toExclusive);
|
Debug.Assert(fromInclusive <= toExclusive);
|
||||||
var locks = tables.Locks;
|
var locks = _tables.Locks;
|
||||||
|
|
||||||
for (var i = fromInclusive; i < toExclusive; i++)
|
for (var i = fromInclusive; i < toExclusive; i++)
|
||||||
{
|
{
|
||||||
@@ -765,29 +709,35 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (lockTaken)
|
if (lockTaken)
|
||||||
|
{
|
||||||
locksAcquired++;
|
locksAcquired++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void ReleaseLocks(int fromInclusive, int toExclusive)
|
private void ReleaseLocks(int fromInclusive, int toExclusive)
|
||||||
{
|
{
|
||||||
Debug.Assert(fromInclusive <= toExclusive);
|
Debug.Assert(fromInclusive <= toExclusive);
|
||||||
|
|
||||||
for (var i = fromInclusive; i < toExclusive; i++)
|
for (var i = fromInclusive; i < toExclusive; i++)
|
||||||
Monitor.Exit(tables.Locks[i]);
|
{
|
||||||
|
Monitor.Exit(_tables.Locks[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CopyToItems(T[] array, int index)
|
private void CopyToItems(T[] array, int index)
|
||||||
{
|
{
|
||||||
var buckets = tables.Buckets;
|
var buckets = _tables.Buckets;
|
||||||
for (var i = 0; i < buckets.Length; i++)
|
for (var i = 0; i < buckets.Length; i++)
|
||||||
for (var current = buckets[i]; current is not null; current = current.Next)
|
{
|
||||||
|
for (var current = buckets[i]; current != null; current = current.Next)
|
||||||
{
|
{
|
||||||
array[index] = current.Item;
|
array[index] = current.Item;
|
||||||
index++; //this should never flow, CopyToItems is only called when there's no overflow risk
|
index++; //this should never flow, CopyToItems is only called when there's no overflow risk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private sealed class Tables
|
private sealed class Tables
|
||||||
{
|
{
|
||||||
@@ -806,8 +756,8 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
|
|
||||||
private sealed class Node
|
private sealed class Node
|
||||||
{
|
{
|
||||||
public readonly int Hashcode;
|
|
||||||
public readonly T Item;
|
public readonly T Item;
|
||||||
|
public readonly int Hashcode;
|
||||||
|
|
||||||
public volatile Node Next;
|
public volatile Node Next;
|
||||||
|
|
||||||
@@ -819,3 +769,4 @@ public sealed class ConcurrentHashSet<T> : IReadOnlyCollection<T>, ICollection<T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
77
src/NadekoBot/Common/Collections/DisposableImutableList.cs
Normal file
77
src/NadekoBot/Common/Collections/DisposableImutableList.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common.Collections
|
||||||
|
{
|
||||||
|
public static class DisposableReadOnlyListExtensions
|
||||||
|
{
|
||||||
|
public static IDisposableReadOnlyList<T> AsDisposable<T>(this IReadOnlyList<T> arr) where T : IDisposable
|
||||||
|
=> new DisposableReadOnlyList<T>(arr);
|
||||||
|
|
||||||
|
public static IDisposableReadOnlyList<KeyValuePair<TKey, TValue>> AsDisposable<TKey, TValue>(this IReadOnlyList<KeyValuePair<TKey, TValue>> arr) where TValue : IDisposable
|
||||||
|
=> new DisposableReadOnlyList<TKey, TValue>(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IDisposableReadOnlyList<T> : IReadOnlyList<T>, IDisposable
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class DisposableReadOnlyList<T> : IDisposableReadOnlyList<T>
|
||||||
|
where T : IDisposable
|
||||||
|
{
|
||||||
|
private readonly IReadOnlyList<T> _arr;
|
||||||
|
|
||||||
|
public int Count => _arr.Count;
|
||||||
|
|
||||||
|
public T this[int index] => _arr[index];
|
||||||
|
|
||||||
|
public DisposableReadOnlyList(IReadOnlyList<T> arr)
|
||||||
|
{
|
||||||
|
this._arr = arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator()
|
||||||
|
=> _arr.GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
=> _arr.GetEnumerator();
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var item in _arr)
|
||||||
|
{
|
||||||
|
item.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class DisposableReadOnlyList<T, U> : IDisposableReadOnlyList<KeyValuePair<T, U>>
|
||||||
|
where U : IDisposable
|
||||||
|
{
|
||||||
|
private readonly IReadOnlyList<KeyValuePair<T, U>> _arr;
|
||||||
|
|
||||||
|
public int Count => _arr.Count;
|
||||||
|
|
||||||
|
KeyValuePair<T, U> IReadOnlyList<KeyValuePair<T, U>>.this[int index] => _arr[index];
|
||||||
|
|
||||||
|
public DisposableReadOnlyList(IReadOnlyList<KeyValuePair<T, U>> arr)
|
||||||
|
{
|
||||||
|
this._arr = arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<KeyValuePair<T, U>> GetEnumerator() =>
|
||||||
|
_arr.GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() =>
|
||||||
|
_arr.GetEnumerator();
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var item in _arr)
|
||||||
|
{
|
||||||
|
item.Value.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,37 +1,23 @@
|
|||||||
#nullable disable
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using NadekoBot.Services.Database.Models;
|
using NadekoBot.Services.Database.Models;
|
||||||
using System.Collections;
|
|
||||||
|
|
||||||
namespace NadekoBot.Common.Collections;
|
namespace NadekoBot.Common.Collections
|
||||||
|
{
|
||||||
public class IndexedCollection<T> : IList<T>
|
public class IndexedCollection<T> : IList<T> where T : class, IIndexed
|
||||||
where T : class, IIndexed
|
|
||||||
{
|
{
|
||||||
public List<T> Source { get; }
|
public List<T> Source { get; }
|
||||||
|
private readonly object _locker = new object();
|
||||||
|
|
||||||
public int Count
|
public int Count => Source.Count;
|
||||||
=> Source.Count;
|
public bool IsReadOnly => false;
|
||||||
|
public int IndexOf(T item) => item.Index;
|
||||||
public bool IsReadOnly
|
|
||||||
=> false;
|
|
||||||
|
|
||||||
public virtual T this[int index]
|
|
||||||
{
|
|
||||||
get => Source[index];
|
|
||||||
set
|
|
||||||
{
|
|
||||||
lock (_locker)
|
|
||||||
{
|
|
||||||
value.Index = index;
|
|
||||||
Source[index] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly object _locker = new();
|
|
||||||
|
|
||||||
public IndexedCollection()
|
public IndexedCollection()
|
||||||
=> Source = new();
|
{
|
||||||
|
Source = new List<T>();
|
||||||
|
}
|
||||||
|
|
||||||
public IndexedCollection(IEnumerable<T> source)
|
public IndexedCollection(IEnumerable<T> source)
|
||||||
{
|
{
|
||||||
@@ -42,19 +28,31 @@ public class IndexedCollection<T> : IList<T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int IndexOf(T item)
|
public void UpdateIndexes()
|
||||||
=> item?.Index ?? -1;
|
{
|
||||||
|
lock (_locker)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < Source.Count; i++)
|
||||||
|
{
|
||||||
|
if (Source[i].Index != i)
|
||||||
|
Source[i].Index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator()
|
public static implicit operator List<T>(IndexedCollection<T> x) =>
|
||||||
=> Source.GetEnumerator();
|
x.Source;
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
public List<T> ToList() => Source.ToList();
|
||||||
=> Source.GetEnumerator();
|
|
||||||
|
public IEnumerator<T> GetEnumerator() =>
|
||||||
|
Source.GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() =>
|
||||||
|
Source.GetEnumerator();
|
||||||
|
|
||||||
public void Add(T item)
|
public void Add(T item)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(item);
|
|
||||||
|
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
item.Index = Source.Count;
|
item.Index = Source.Count;
|
||||||
@@ -88,21 +86,19 @@ public class IndexedCollection<T> : IList<T>
|
|||||||
|
|
||||||
public virtual bool Remove(T item)
|
public virtual bool Remove(T item)
|
||||||
{
|
{
|
||||||
|
bool removed;
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
if (Source.Remove(item))
|
if (removed = Source.Remove(item))
|
||||||
{
|
{
|
||||||
for (var i = 0; i < Source.Count; i++)
|
for (int i = 0; i < Source.Count; i++)
|
||||||
{
|
{
|
||||||
if (Source[i].Index != i)
|
if (Source[i].Index != i)
|
||||||
Source[i].Index = i;
|
Source[i].Index = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return removed;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Insert(int index, T item)
|
public virtual void Insert(int index, T item)
|
||||||
@@ -110,36 +106,36 @@ public class IndexedCollection<T> : IList<T>
|
|||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
Source.Insert(index, item);
|
Source.Insert(index, item);
|
||||||
for (var i = index; i < Source.Count; i++)
|
for (int i = index; i < Source.Count; i++)
|
||||||
|
{
|
||||||
Source[i].Index = i;
|
Source[i].Index = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public virtual void RemoveAt(int index)
|
public virtual void RemoveAt(int index)
|
||||||
{
|
{
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
Source.RemoveAt(index);
|
Source.RemoveAt(index);
|
||||||
for (var i = index; i < Source.Count; i++)
|
for (int i = index; i < Source.Count; i++)
|
||||||
|
{
|
||||||
Source[i].Index = i;
|
Source[i].Index = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateIndexes()
|
public virtual T this[int index]
|
||||||
|
{
|
||||||
|
get { return Source[index]; }
|
||||||
|
set
|
||||||
{
|
{
|
||||||
lock (_locker)
|
lock (_locker)
|
||||||
{
|
{
|
||||||
for (var i = 0; i < Source.Count; i++)
|
value.Index = index;
|
||||||
{
|
Source[index] = value;
|
||||||
if (Source[i].Index != i)
|
}
|
||||||
Source[i].Index = i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator List<T>(IndexedCollection<T> x)
|
|
||||||
=> x.Source;
|
|
||||||
|
|
||||||
public List<T> ToList()
|
|
||||||
=> Source.ToList();
|
|
||||||
}
|
}
|
@@ -1,9 +1,9 @@
|
|||||||
#nullable disable
|
namespace NadekoBot.Common
|
||||||
namespace NadekoBot.Common;
|
{
|
||||||
|
|
||||||
public class CommandData
|
public class CommandData
|
||||||
{
|
{
|
||||||
public string Cmd { get; set; }
|
public string Cmd { get; set; }
|
||||||
public string Desc { get; set; }
|
public string Desc { get; set; }
|
||||||
public string[] Usage { get; set; }
|
public string[] Usage { get; set; }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
#nullable disable
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using Cloneable;
|
using Cloneable;
|
||||||
using NadekoBot.Common.Yml;
|
using NadekoBot.Common.Yml;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
using System.Globalization;
|
|
||||||
using YamlDotNet.Core;
|
using YamlDotNet.Core;
|
||||||
using YamlDotNet.Serialization;
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
namespace NadekoBot.Common.Configs;
|
namespace NadekoBot.Common.Configs
|
||||||
|
{
|
||||||
[Cloneable]
|
[Cloneable]
|
||||||
public sealed partial class BotConfig : ICloneable<BotConfig>
|
public sealed partial class BotConfig : ICloneable<BotConfig>
|
||||||
{
|
{
|
||||||
@@ -39,8 +39,7 @@ Allowed values: Simple, Normal, None")]
|
|||||||
[Comment(@"Do you want any messages sent by users in Bot's DM to be forwarded to the owner(s)?")]
|
[Comment(@"Do you want any messages sent by users in Bot's DM to be forwarded to the owner(s)?")]
|
||||||
public bool ForwardMessages { get; set; }
|
public bool ForwardMessages { get; set; }
|
||||||
|
|
||||||
[Comment(
|
[Comment(@"Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml),
|
||||||
@"Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml),
|
|
||||||
or all owners? (this might cause the bot to lag if there's a lot of owners specified)")]
|
or all owners? (this might cause the bot to lag if there's a lot of owners specified)")]
|
||||||
public bool ForwardToAllOwners { get; set; }
|
public bool ForwardToAllOwners { get; set; }
|
||||||
|
|
||||||
@@ -58,7 +57,6 @@ Leave empty to reply with DmHelpText to every DM.")]
|
|||||||
[Comment(@"This is the response for the .h command")]
|
[Comment(@"This is the response for the .h command")]
|
||||||
[YamlMember(ScalarStyle = ScalarStyle.Literal)]
|
[YamlMember(ScalarStyle = ScalarStyle.Literal)]
|
||||||
public string HelpText { get; set; }
|
public string HelpText { get; set; }
|
||||||
|
|
||||||
[Comment(@"List of modules and commands completely blocked on the bot")]
|
[Comment(@"List of modules and commands completely blocked on the bot")]
|
||||||
public BlockedConfig Blocked { get; set; }
|
public BlockedConfig Blocked { get; set; }
|
||||||
|
|
||||||
@@ -77,15 +75,28 @@ note: This setting is primarily used if you're afraid of raids, or you're runnin
|
|||||||
public bool GroupGreets { get; set; }
|
public bool GroupGreets { get; set; }
|
||||||
|
|
||||||
[Comment(@"Whether the bot will rotate through all specified statuses.
|
[Comment(@"Whether the bot will rotate through all specified statuses.
|
||||||
This setting can be changed via .ropl command.
|
This setting can be changed via .rots command.
|
||||||
See RotatingStatuses submodule in Administration.")]
|
See RotatingStatuses submodule in Administration.")]
|
||||||
public bool RotateStatuses { get; set; }
|
public bool RotateStatuses { get; set; }
|
||||||
|
|
||||||
|
// [Comment(@"Whether the prefix will be a suffix, or prefix.
|
||||||
|
// For example, if your prefix is ! you will run a command called 'cash' by typing either
|
||||||
|
// '!cash @Someone' if your prefixIsSuffix: false or
|
||||||
|
// 'cash @Someone!' if your prefixIsSuffix: true")]
|
||||||
|
// public bool PrefixIsSuffix { get; set; }
|
||||||
|
|
||||||
|
// public string Prefixed(string text) => PrefixIsSuffix
|
||||||
|
// ? text + Prefix
|
||||||
|
// : Prefix + text;
|
||||||
|
|
||||||
|
public string Prefixed(string text)
|
||||||
|
=> Prefix + text;
|
||||||
|
|
||||||
public BotConfig()
|
public BotConfig()
|
||||||
{
|
{
|
||||||
var color = new ColorConfig();
|
var color = new ColorConfig();
|
||||||
Color = color;
|
Color = color;
|
||||||
DefaultLocale = new("en-US");
|
DefaultLocale = new CultureInfo("en-US");
|
||||||
ConsoleOutputType = ConsoleOutputType.Normal;
|
ConsoleOutputType = ConsoleOutputType.Normal;
|
||||||
ForwardMessages = false;
|
ForwardMessages = false;
|
||||||
ForwardToAllOwners = false;
|
ForwardToAllOwners = false;
|
||||||
@@ -120,7 +131,7 @@ See RotatingStatuses submodule in Administration.")]
|
|||||||
Prefix = ".";
|
Prefix = ".";
|
||||||
RotateStatuses = false;
|
RotateStatuses = false;
|
||||||
GroupGreets = false;
|
GroupGreets = false;
|
||||||
DmHelpTextKeywords = new()
|
DmHelpTextKeywords = new List<string>()
|
||||||
{
|
{
|
||||||
"help",
|
"help",
|
||||||
"commands",
|
"commands",
|
||||||
@@ -129,19 +140,6 @@ See RotatingStatuses submodule in Administration.")]
|
|||||||
"can you do"
|
"can you do"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Comment(@"Whether the prefix will be a suffix, or prefix.
|
|
||||||
// For example, if your prefix is ! you will run a command called 'cash' by typing either
|
|
||||||
// '!cash @Someone' if your prefixIsSuffix: false or
|
|
||||||
// 'cash @Someone!' if your prefixIsSuffix: true")]
|
|
||||||
// public bool PrefixIsSuffix { get; set; }
|
|
||||||
|
|
||||||
// public string Prefixed(string text) => PrefixIsSuffix
|
|
||||||
// ? text + Prefix
|
|
||||||
// : Prefix + text;
|
|
||||||
|
|
||||||
public string Prefixed(string text)
|
|
||||||
=> Prefix + text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cloneable]
|
[Cloneable]
|
||||||
@@ -152,8 +150,8 @@ public sealed partial class BlockedConfig
|
|||||||
|
|
||||||
public BlockedConfig()
|
public BlockedConfig()
|
||||||
{
|
{
|
||||||
Modules = new();
|
Modules = new HashSet<string>();
|
||||||
Commands = new();
|
Commands = new HashSet<string>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,5 +179,6 @@ public enum ConsoleOutputType
|
|||||||
{
|
{
|
||||||
Normal = 0,
|
Normal = 0,
|
||||||
Simple = 1,
|
Simple = 1,
|
||||||
None = 2
|
None = 2,
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
namespace NadekoBot.Common.Configs;
|
namespace NadekoBot.Common.Configs
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base interface for available config serializers
|
/// Base interface for available config serializers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -8,11 +8,11 @@ public interface IConfigSeria
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Serialize the object to string
|
/// Serialize the object to string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Serialize<T>(T obj)
|
public string Serialize<T>(T obj);
|
||||||
where T : notnull;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserialize string data into an object of the specified type
|
/// Deserialize string data into an object of the specified type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T Deserialize<T>(string data);
|
public T Deserialize<T>(string data);
|
||||||
}
|
}
|
||||||
|
}
|
@@ -1,10 +1,36 @@
|
|||||||
#nullable disable
|
using System.Collections.Generic;
|
||||||
using NadekoBot.Common.Yml;
|
using NadekoBot.Common.Yml;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
namespace NadekoBot.Common;
|
namespace NadekoBot.Common
|
||||||
|
{
|
||||||
public sealed class Creds : IBotCredentials
|
public sealed class Creds : IBotCredentials
|
||||||
{
|
{
|
||||||
|
public Creds()
|
||||||
|
{
|
||||||
|
Version = 1;
|
||||||
|
Token = string.Empty;
|
||||||
|
OwnerIds = new List<ulong>();
|
||||||
|
TotalShards = 1;
|
||||||
|
GoogleApiKey = string.Empty;
|
||||||
|
Votes = new(string.Empty, string.Empty, string.Empty, string.Empty);
|
||||||
|
Patreon = new(string.Empty, string.Empty, string.Empty, string.Empty);
|
||||||
|
BotListToken = string.Empty;
|
||||||
|
CleverbotApiKey = string.Empty;
|
||||||
|
RedisOptions = "localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password=";
|
||||||
|
Db = new()
|
||||||
|
{
|
||||||
|
Type = "sqlite",
|
||||||
|
ConnectionString = "Data Source=data/NadekoBot.db"
|
||||||
|
};
|
||||||
|
|
||||||
|
CoordinatorUrl = "http://localhost:3442";
|
||||||
|
|
||||||
|
RestartCommand = new()
|
||||||
|
{
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
[Comment(@"DO NOT CHANGE")]
|
[Comment(@"DO NOT CHANGE")]
|
||||||
public int Version { get; set; }
|
public int Version { get; set; }
|
||||||
|
|
||||||
@@ -15,15 +41,11 @@ public sealed class Creds : IBotCredentials
|
|||||||
**DO NOT ADD PEOPLE YOU DON'T TRUST**")]
|
**DO NOT ADD PEOPLE YOU DON'T TRUST**")]
|
||||||
public ICollection<ulong> OwnerIds { get; set; }
|
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.
|
[Comment(@"The number of shards that the bot will running on.
|
||||||
Leave at 1 if you don't know what you're doing.")]
|
Leave at 1 if you don't know what you're doing.")]
|
||||||
public int TotalShards { get; set; }
|
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.
|
||||||
@"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.
|
Then, go to APIs and Services -> Credentials and click Create credentials -> API key.
|
||||||
Used only for Youtube Data Api (at the moment).")]
|
Used only for Youtube Data Api (at the moment).")]
|
||||||
public string GoogleApiKey { get; set; }
|
public string GoogleApiKey { get; set; }
|
||||||
@@ -51,8 +73,7 @@ go to https://www.patreon.com/portal -> my clients -> create client")]
|
|||||||
Change only if you've changed the coordinator address or port.")]
|
Change only if you've changed the coordinator address or port.")]
|
||||||
public string CoordinatorUrl { get; set; }
|
public string CoordinatorUrl { get; set; }
|
||||||
|
|
||||||
[Comment(
|
[Comment(@"Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)")]
|
||||||
@"Api key obtained on https://rapidapi.com (go to MyApps -> Add New App -> Enter Name -> Application key)")]
|
|
||||||
public string RapidApiKey { get; set; }
|
public string RapidApiKey { get; set; }
|
||||||
|
|
||||||
[Comment(@"https://locationiq.com api key (register and you will receive the token in the email).
|
[Comment(@"https://locationiq.com api key (register and you will receive the token in the email).
|
||||||
@@ -67,23 +88,9 @@ Used only for .time command")]
|
|||||||
Used for cryptocurrency related commands.")]
|
Used for cryptocurrency related commands.")]
|
||||||
public string CoinmarketcapApiKey { get; set; }
|
public string CoinmarketcapApiKey { get; set; }
|
||||||
|
|
||||||
// [Comment(@"https://polygon.io/dashboard/api-keys api key. Free plan allows for 5 queries per minute.
|
|
||||||
// Used for stocks related commands.")]
|
|
||||||
// public string PolygonIoApiKey { get; set; }
|
|
||||||
|
|
||||||
[Comment(@"Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api")]
|
[Comment(@"Api key used for Osu related commands. Obtain this key at https://osu.ppy.sh/p/api")]
|
||||||
public string OsuApiKey { get; set; }
|
public string OsuApiKey { get; set; }
|
||||||
|
|
||||||
[Comment(@"Optional Trovo client id.
|
|
||||||
You should use this if Trovo stream notifications stopped working or you're getting ratelimit errors.")]
|
|
||||||
public string TrovoClientId { get; set; }
|
|
||||||
|
|
||||||
[Comment(@"Obtain by creating an application at https://dev.twitch.tv/console/apps")]
|
|
||||||
public string TwitchClientId { get; set; }
|
|
||||||
|
|
||||||
[Comment(@"Obtain by creating an application at https://dev.twitch.tv/console/apps")]
|
|
||||||
public string TwitchClientSecret { get; set; }
|
|
||||||
|
|
||||||
[Comment(@"Command and args which will be used to restart the bot.
|
[Comment(@"Command and args which will be used to restart the bot.
|
||||||
Only used if bot is executed directly (NOT through the coordinator)
|
Only used if bot is executed directly (NOT through the coordinator)
|
||||||
placeholders:
|
placeholders:
|
||||||
@@ -97,45 +104,16 @@ Windows default
|
|||||||
args: {0}")]
|
args: {0}")]
|
||||||
public RestartConfig RestartCommand { get; set; }
|
public RestartConfig RestartCommand { get; set; }
|
||||||
|
|
||||||
public Creds()
|
|
||||||
{
|
|
||||||
Version = 5;
|
|
||||||
Token = string.Empty;
|
|
||||||
UsePrivilegedIntents = true;
|
|
||||||
OwnerIds = new List<ulong>();
|
|
||||||
TotalShards = 1;
|
|
||||||
GoogleApiKey = string.Empty;
|
|
||||||
Votes = new(string.Empty, string.Empty, string.Empty, string.Empty);
|
|
||||||
Patreon = new(string.Empty, string.Empty, string.Empty, string.Empty);
|
|
||||||
BotListToken = string.Empty;
|
|
||||||
CleverbotApiKey = string.Empty;
|
|
||||||
RedisOptions = "localhost:6379,syncTimeout=30000,responseTimeout=30000,allowAdmin=true,password=";
|
|
||||||
Db = new()
|
|
||||||
{
|
|
||||||
Type = "sqlite",
|
|
||||||
ConnectionString = "Data Source=data/NadekoBot.db"
|
|
||||||
};
|
|
||||||
|
|
||||||
CoordinatorUrl = "http://localhost:3442";
|
|
||||||
|
|
||||||
RestartCommand = new();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public class DbOptions
|
public class DbOptions
|
||||||
{
|
{
|
||||||
[Comment(@"Database type. ""sqlite"", ""mysql"" and ""postgresql"" are supported.
|
[Comment(@"Database type. Only sqlite supported atm")]
|
||||||
Default is ""sqlite""")]
|
|
||||||
public string Type { get; set; }
|
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; }
|
public string ConnectionString { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo fixup patreon
|
||||||
public sealed record PatreonSettings
|
public sealed record PatreonSettings
|
||||||
{
|
{
|
||||||
public string ClientId { get; set; }
|
public string ClientId { get; set; }
|
||||||
@@ -143,15 +121,10 @@ Example for postgresql: ""Server=localhost;Port=5432;User Id=postgres;Password=m
|
|||||||
public string RefreshToken { get; set; }
|
public string RefreshToken { get; set; }
|
||||||
public string ClientSecret { get; set; }
|
public string ClientSecret { get; set; }
|
||||||
|
|
||||||
[Comment(
|
[Comment(@"Campaign ID of your patreon page. Go to your patreon page (make sure you're logged in) and type ""prompt('Campaign ID', window.patreon.bootstrap.creator.data.id);"" in the console. (ctrl + shift + i)")]
|
||||||
@"Campaign ID of your patreon page. Go to your patreon page (make sure you're logged in) and type ""prompt('Campaign ID', window.patreon.bootstrap.creator.data.id);"" in the console. (ctrl + shift + i)")]
|
|
||||||
public string CampaignId { get; set; }
|
public string CampaignId { get; set; }
|
||||||
|
|
||||||
public PatreonSettings(
|
public PatreonSettings(string accessToken, string refreshToken, string clientSecret, string campaignId)
|
||||||
string accessToken,
|
|
||||||
string refreshToken,
|
|
||||||
string clientSecret,
|
|
||||||
string campaignId)
|
|
||||||
{
|
{
|
||||||
AccessToken = accessToken;
|
AccessToken = accessToken;
|
||||||
RefreshToken = refreshToken;
|
RefreshToken = refreshToken;
|
||||||
@@ -161,6 +134,7 @@ Example for postgresql: ""Server=localhost;Port=5432;User Id=postgres;Password=m
|
|||||||
|
|
||||||
public PatreonSettings()
|
public PatreonSettings()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,13 +160,10 @@ This should be equivalent to the DiscordsKey in your NadekoBot.Votes api appsett
|
|||||||
|
|
||||||
public VotesSettings()
|
public VotesSettings()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public VotesSettings(
|
public VotesSettings(string topggServiceUrl, string topggKey, string discordsServiceUrl, string discordsKey)
|
||||||
string topggServiceUrl,
|
|
||||||
string topggKey,
|
|
||||||
string discordsServiceUrl,
|
|
||||||
string discordsKey)
|
|
||||||
{
|
{
|
||||||
TopggServiceUrl = topggServiceUrl;
|
TopggServiceUrl = topggServiceUrl;
|
||||||
TopggKey = topggKey;
|
TopggKey = topggKey;
|
||||||
@@ -200,4 +171,49 @@ This should be equivalent to the DiscordsKey in your NadekoBot.Votes api appsett
|
|||||||
DiscordsKey = discordsKey;
|
DiscordsKey = discordsKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Old
|
||||||
|
{
|
||||||
|
public string Token { get; set; } = string.Empty;
|
||||||
|
public ulong[] OwnerIds { get; set; } = new ulong[1];
|
||||||
|
public string LoLApiKey { get; set; } = string.Empty;
|
||||||
|
public string GoogleApiKey { get; set; } = string.Empty;
|
||||||
|
public string MashapeKey { get; set; } = string.Empty;
|
||||||
|
public string OsuApiKey { get; set; } = string.Empty;
|
||||||
|
public string SoundCloudClientId { get; set; } = string.Empty;
|
||||||
|
public string CleverbotApiKey { get; set; } = string.Empty;
|
||||||
|
public string CarbonKey { get; set; } = string.Empty;
|
||||||
|
public int TotalShards { get; set; } = 1;
|
||||||
|
public string PatreonAccessToken { get; set; } = string.Empty;
|
||||||
|
public string PatreonCampaignId { get; set; } = "334038";
|
||||||
|
public RestartConfig RestartCommand { get; set; } = null;
|
||||||
|
|
||||||
|
public string ShardRunCommand { get; set; } = string.Empty;
|
||||||
|
public string ShardRunArguments { get; set; } = string.Empty;
|
||||||
|
public int? ShardRunPort { get; set; } = null;
|
||||||
|
public string MiningProxyUrl { get; set; } = string.Empty;
|
||||||
|
public string MiningProxyCreds { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string BotListToken { get; set; } = string.Empty;
|
||||||
|
public string TwitchClientId { get; set; } = string.Empty;
|
||||||
|
public string VotesToken { get; set; } = string.Empty;
|
||||||
|
public string VotesUrl { get; set; } = string.Empty;
|
||||||
|
public string RedisOptions { get; set; } = string.Empty;
|
||||||
|
public string LocationIqApiKey { get; set; } = string.Empty;
|
||||||
|
public string TimezoneDbApiKey { get; set; } = string.Empty;
|
||||||
|
public string CoinmarketcapApiKey { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public class RestartConfig
|
||||||
|
{
|
||||||
|
public RestartConfig(string cmd, string args)
|
||||||
|
{
|
||||||
|
this.Cmd = cmd;
|
||||||
|
this.Args = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Cmd { get; set; }
|
||||||
|
public string Args { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,10 +1,16 @@
|
|||||||
#nullable disable
|
using NadekoBot.Services;
|
||||||
namespace NadekoBot.Common;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Discord;
|
||||||
|
|
||||||
|
namespace NadekoBot.Common
|
||||||
|
{
|
||||||
public class DownloadTracker : INService
|
public class DownloadTracker : INService
|
||||||
{
|
{
|
||||||
private ConcurrentDictionary<ulong, DateTime> LastDownloads { get; } = new();
|
private ConcurrentDictionary<ulong, DateTime> LastDownloads { get; } = new ConcurrentDictionary<ulong, DateTime>();
|
||||||
private readonly SemaphoreSlim _downloadUsersSemaphore = new(1, 1);
|
private SemaphoreSlim downloadUsersSemaphore = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ensures all users on the specified guild were downloaded within the last hour.
|
/// Ensures all users on the specified guild were downloaded within the last hour.
|
||||||
@@ -16,15 +22,16 @@ public class DownloadTracker : INService
|
|||||||
#if GLOBAL_NADEKO
|
#if GLOBAL_NADEKO
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
await _downloadUsersSemaphore.WaitAsync();
|
await downloadUsersSemaphore.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var now = DateTime.UtcNow;
|
var now = DateTime.UtcNow;
|
||||||
|
|
||||||
// download once per hour at most
|
// download once per hour at most
|
||||||
var added = LastDownloads.AddOrUpdate(guild.Id,
|
var added = LastDownloads.AddOrUpdate(
|
||||||
|
guild.Id,
|
||||||
now,
|
now,
|
||||||
(_, old) => now - old > TimeSpan.FromHours(1) ? now : old);
|
(key, old) => (now - old) > TimeSpan.FromHours(1) ? now : old);
|
||||||
|
|
||||||
// means that this entry was just added - download the users
|
// means that this entry was just added - download the users
|
||||||
if (added == now)
|
if (added == now)
|
||||||
@@ -32,7 +39,8 @@ public class DownloadTracker : INService
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
_downloadUsersSemaphore.Release();
|
downloadUsersSemaphore.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user