diff --git a/.editorconfig b/.editorconfig index 6887b9a8..04a71793 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,8 +12,6 @@ indent_style = tab [*.kt] indent_style = tab -ij_kotlin_name_count_to_use_star_import = 99999 -ij_kotlin_name_count_to_use_star_import_for_members = 99999 [*.properties] indent_style = space diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 9408667c..4954de0d 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -14,21 +14,21 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Set up JDK 17 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: 17 + java-version: 21 - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Build Artifacts - uses: gradle/gradle-build-action@v3 - with: - arguments: build --stacktrace - gradle-home-cache-cleanup: true + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v4 + + - name: Build Project + run: ./gradlew build --stacktrace - name: Upload build artifacts uses: actions/upload-artifact@v4 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 95758155..6b6b9837 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,21 +12,21 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Set up JDK 17 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: 17 + java-version: 21 - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Build Artifacts - uses: gradle/gradle-build-action@v3 - with: - arguments: build --stacktrace - gradle-home-cache-cleanup: true + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v4 + + - name: Build Project + run: ./gradlew build --stacktrace - name: Upload build artifacts uses: actions/upload-artifact@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d30bf7af..9dbf6e9d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,21 +13,21 @@ jobs: - name: Checkout sources uses: actions/checkout@v4 - - name: Set up JDK 17 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: 17 + java-version: 21 - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Build Artifacts - uses: gradle/gradle-build-action@v3 - with: - arguments: build --stacktrace - gradle-home-cache-cleanup: true + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v4 + + - name: Build Project + run: ./gradlew build --stacktrace - name: Upload artifacts GitHub uses: AButler/upload-release-assets@v3.0 diff --git a/.gitignore b/.gitignore index 16f36025..9d4af58b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,9 @@ build/ out/ classes/ +# kotlin +.kotlin/ + # eclipse *.launch diff --git a/Dockerfile b/Dockerfile index 09b9f635..6ac53d0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM openjdk:17-jdk-slim +FROM openjdk:21-jdk-slim RUN mkdir /bot RUN mkdir /data diff --git a/build.gradle.kts b/build.gradle.kts index 3bc9a704..40ba2bbd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,9 @@ plugins { } group = "org.hyacinthbots.lilybot" -version = "4.9.0" +version = "5.0.0" + +val className = "org.hyacinthbots.lilybot.LilyBotKt" repositories { mavenCentral() @@ -30,6 +32,11 @@ repositories { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } + maven { + name = "Kord Extensions (Snapshots)" + url = uri("https://snapshots-repo.kordex.dev") + } + maven { name = "JitPack" url = uri("https://jitpack.io") @@ -61,7 +68,7 @@ dependencies { } application { - mainClass.set("org.hyacinthbots.lilybot.LilyBotKt") + mainClass.set(className) } gitHooks { @@ -73,16 +80,21 @@ gitHooks { tasks { withType { compilerOptions { - jvmTarget.set(JvmTarget.fromTarget("17")) + jvmTarget.set(JvmTarget.fromTarget("21")) languageVersion.set(KotlinVersion.fromVersion(libs.plugins.kotlin.get().version.requiredVersion.substringBeforeLast("."))) incremental = true freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn") } } + java { // Should match the Kotlin compiler options ideally + sourceCompatibility = JavaVersion.toVersion("21") + targetCompatibility = JavaVersion.toVersion("21") + } + jar { manifest { - attributes("Main-Class" to "org.hyacinthbots.lilybot.LilyBotKt") + attributes("Main-Class" to className) } } diff --git a/detekt.yml b/detekt.yml index 4fa127fc..2f5db37a 100644 --- a/detekt.yml +++ b/detekt.yml @@ -175,6 +175,7 @@ formatting: active: true autoCorrect: true NoWildcardImports: + active: false packagesToUseImportOnDemandProperty: "" PackageName: active: true @@ -361,3 +362,5 @@ style: active: true UseRequireNotNull: active: true + WildcardImport: + active: false diff --git a/docs/commands.md b/docs/commands.md index b5e9e29d..37d4a9ce 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -1,5 +1,11 @@ ## Slash Commands +### Command name: `about` +**Description**: Learn about this bot + +* Arguments: +None +--- ### Command name: `auto-threading enable` **Description**: Automatically create a thread for each message sent in this channel. @@ -37,6 +43,28 @@ None * **Arguments**: * `channel` - The channel to view the auto-threading settings for. - Channel +--- +### Command name: `auto-threading add-roles` +**Description**: Add extra to threads in auto-threaded channels + +**Additional Information**: This command will add roles to be pinged alongside the default ping role for this auto-threaded channel + +**Required Member Permissions**: Manage Channels + +* **Arguments**: + * `role` - A role to invite to threads in this channel - Optional Role + +--- +### Command name: `auto-threading remove-roles` +**Description**: Remove extra from threads in auto-threaded channels + +**Additional Information**: This command will remove roles that have been added to be pinged alongside the default ping role for this auto-threaded channel + +**Required Member Permissions**: Manage Channels + +* **Arguments**: + * `role` - A role to invite to threads in this channel - Optional Role + --- ### Command name: `clear count` **Description**: Clear a specific count of messages @@ -80,21 +108,6 @@ None * `before` - The ID of the message to clear before - Snowflake * `author` - The author of the messages to clear - Optional User ---- -### Command name: `config moderation` -**Description**: Configure Lily's moderation system - -**Required Member Permissions**: Manage Server - -* **Arguments**: - * `enable-moderation` - Whether to enable the moderation system - Boolean - * `moderator-role` - The role of your moderators, used for pinging in message logs. - Optional Role - * `action-log` - The channel used to store moderator actions. - Optional Channel - * `quick-timeout-length` - The length of timeouts to use for quick timeouts - Coalescing Optional Duration - * `warn-auto-punishments` - Whether to automatically punish users for reach a certain threshold on warns - Optional Boolean - * `log-publicly` - Whether to log moderation publicly or not. - Optional Boolean - * `ban-dm-message` - A custom message to send to users when they are banned. - Optional String - --- ### Command name: `config logging` **Description**: Configure Lily's logging system @@ -110,6 +123,22 @@ None * `member-log` - The channel for logging members joining and leaving the guild - Optional Channel * `public-member-log` - The channel for the public logging of members joining and leaving the guild - Optional Channel +--- +### Command name: `config moderation` +**Description**: Configure Lily's moderation system + +**Required Member Permissions**: Manage Server + +* **Arguments**: + * `enable-moderation` - Whether to enable the moderation system - Boolean + * `moderator-role` - The role of your moderators, used for pinging in message logs. - Optional Role + * `action-log` - The channel used to store moderator actions. - Optional Channel + * `quick-timeout-length` - The length of timeouts to use for quick timeouts - Coalescing Optional Duration + * `warn-auto-punishments` - Whether to automatically punish users for reach a certain threshold on warns - Optional Boolean + * `log-publicly` - Whether to log moderation publicly or not. - Optional Boolean + * `ban-dm-message` - A custom message to send to users when they are banned. - Optional String + * `auto-invite-moderator-role` - Silently ping moderators to invite them to new threads. - Optional Boolean + --- ### Command name: `config utility` **Description**: Configure Lily's utility settings @@ -211,12 +240,6 @@ None ### Command name: `help` **Description**: Get help with using Lily! -* Arguments: -None ---- -### Command name: `info` -**Description**: Learn about Lily, and get uptime data! - * Arguments: None --- @@ -322,22 +345,32 @@ None * `user` - Person to ban - User * `delete-message-days` - The number of days worth of messages to delete - Int * `reason` - The reason for the ban - Defaulting String + * `soft-ban` - Weather to soft-ban this user (unban them once messages are deleted) - Defaulting Boolean * `dm` - Whether to send a direct message to the user about the ban - Defaulting Boolean * `image` - An image you'd like to provide as extra context for the action - Optional Attachment --- -### Command name: `soft-ban` -**Description**: Soft-bans a user. +### Command name: `temp-ban add` +**Description**: Temporarily bans a user **Required Member Permissions**: Ban Members -* Arguments: - * `user` - Person to Soft ban - User - * `delete-message-days` - The number of days worth of messages to delete - Optional Int/Long +* **Arguments**: + * `user` - Person to ban - User + * `delete-message-days` - The number of days worth of messages to delete - Int + * `duration` - The duration of the temporary ban. - Coalescing Duration * `reason` - The reason for the ban - Defaulting String - * `dm` - Whether to send a direct message to the user about the soft-ban - Defaulting Boolean + * `dm` - Whether to send a direct message to the user about the ban - Defaulting Boolean * `image` - An image you'd like to provide as extra context for the action - Optional Attachment +--- +### Command name: `temp-ban view-all` +**Description**: View all temporary bans for this guild + +**Required Member Permissions**: Ban Members + +* **Arguments**: +None --- ### Command name: `unban` **Description**: Unbans a user. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 852daae7..ed2ea567 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,26 +1,27 @@ [versions] # Plugins -kotlin = "1.9.22" +kotlin = "2.0.10" shadow = "8.1.1" -detekt = "1.23.5" +detekt = "1.23.7" git-hooks = "0.0.2" grgit = "5.2.2" blossom = "2.1.0" # Libraries -kord-extensions = "1.8.0-20240319.115836-21" -logging = "6.0.3" -logback = "1.5.0" -github-api = "1.318" -kmongo = "4.11.0" -docgenerator = "0.1.5-SNAPSHOT" +kord-extensions = "2.2.0-20240824.203242-3" +logging = "7.0.0" +logback = "1.5.8" +github-api = "1.325" +kmongo = "5.1.0" +docgenerator = "0.2.2-SNAPSHOT" [libraries] -kord-extensions-core = { module = "com.kotlindiscord.kord.extensions:kord-extensions", version.ref = "kord-extensions" } -kord-extensions-phishing = { module = "com.kotlindiscord.kord.extensions:extra-phishing", version.ref = "kord-extensions" } -kord-extensions-pluralkit = { module = "com.kotlindiscord.kord.extensions:extra-pluralkit", version.ref = "kord-extensions"} -kord-extensions-unsafe = { module = "com.kotlindiscord.kord.extensions:unsafe", version.ref = "kord-extensions"} -kord-extensions-welcome = { module = "com.kotlindiscord.kord.extensions:extra-welcome", version.ref = "kord-extensions"} +kord-extensions-core = { module = "dev.kordex:kord-extensions", version.ref = "kord-extensions" } +kord-extensions-phishing = { module = "dev.kordex.modules:func-phishing", version.ref = "kord-extensions" } +kord-extensions-pluralkit = { module = "dev.kordex.modules:pluralkit", version.ref = "kord-extensions"} +kord-extensions-unsafe = { module = "dev.kordex.modules:dev-unsafe", version.ref = "kord-extensions"} +kord-extensions-welcome = { module = "dev.kordex.modules:func-welcome", version.ref = "kord-extensions"} + kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib" } logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } logging = { module = "io.github.oshai:kotlin-logging", version.ref = "logging" } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index d64cd491..a4b76b95 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a80b22ce..9355b415 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index 1aa94a42..f5feea6d 100755 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -84,7 +86,8 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum diff --git a/gradlew.bat b/gradlew.bat index 25da30db..9d21a218 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,6 +13,8 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem @if "%DEBUG%"=="" @echo off @rem ########################################################################## diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt b/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt index 6b2d0815..e1be17d7 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt @@ -2,48 +2,60 @@ package org.hyacinthbots.lilybot -import com.kotlindiscord.kord.extensions.ExtensibleBot -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.modules.extra.phishing.DetectionAction -import com.kotlindiscord.kord.extensions.modules.extra.phishing.extPhishing -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.extPluralKit -import com.kotlindiscord.kord.extensions.modules.extra.welcome.welcomeChannel import dev.kord.common.entity.Permission import dev.kord.gateway.Intent import dev.kord.gateway.PrivilegedIntent +import dev.kord.rest.builder.message.actionRow +import dev.kord.rest.builder.message.embed +import dev.kordex.core.ExtensibleBot +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import dev.kordex.data.api.DataCollection +import dev.kordex.modules.func.phishing.DetectionAction +import dev.kordex.modules.func.phishing.extPhishing +import dev.kordex.modules.func.welcome.welcomeChannel +import dev.kordex.modules.pluralkit.extPluralKit import io.github.oshai.kotlinlogging.KotlinLogging +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toLocalDateTime import org.hyacinthbots.docgenerator.docsGenerator import org.hyacinthbots.docgenerator.enums.CommandTypes import org.hyacinthbots.docgenerator.enums.SupportedFileFormat +import org.hyacinthbots.lilybot.database.collections.UptimeCollection import org.hyacinthbots.lilybot.database.collections.WelcomeChannelCollection import org.hyacinthbots.lilybot.database.storage.MongoDBDataAdapter -import org.hyacinthbots.lilybot.extensions.config.Config +import org.hyacinthbots.lilybot.extensions.config.ConfigExtension import org.hyacinthbots.lilybot.extensions.config.ConfigOptions -import org.hyacinthbots.lilybot.extensions.config.GuildLogging -import org.hyacinthbots.lilybot.extensions.events.AutoThreading -import org.hyacinthbots.lilybot.extensions.events.MemberLogging -import org.hyacinthbots.lilybot.extensions.events.MessageDelete -import org.hyacinthbots.lilybot.extensions.events.MessageEdit -import org.hyacinthbots.lilybot.extensions.events.ModThreadInviting -import org.hyacinthbots.lilybot.extensions.moderation.ClearCommands -import org.hyacinthbots.lilybot.extensions.moderation.LockingCommands -import org.hyacinthbots.lilybot.extensions.moderation.ModerationCommands -import org.hyacinthbots.lilybot.extensions.moderation.Report -import org.hyacinthbots.lilybot.extensions.util.GalleryChannel -import org.hyacinthbots.lilybot.extensions.util.Github -import org.hyacinthbots.lilybot.extensions.util.GuildAnnouncements -import org.hyacinthbots.lilybot.extensions.util.InfoCommands -import org.hyacinthbots.lilybot.extensions.util.ModUtilities -import org.hyacinthbots.lilybot.extensions.util.NewsChannelPublishing -import org.hyacinthbots.lilybot.extensions.util.PublicUtilities -import org.hyacinthbots.lilybot.extensions.util.Reminders -import org.hyacinthbots.lilybot.extensions.util.RoleMenu -import org.hyacinthbots.lilybot.extensions.util.StartupHooks -import org.hyacinthbots.lilybot.extensions.util.StatusPing -import org.hyacinthbots.lilybot.extensions.util.Tags -import org.hyacinthbots.lilybot.extensions.util.ThreadControl +import org.hyacinthbots.lilybot.extensions.logging.events.GuildLogging +import org.hyacinthbots.lilybot.extensions.logging.events.MemberLogging +import org.hyacinthbots.lilybot.extensions.logging.events.MessageDelete +import org.hyacinthbots.lilybot.extensions.logging.events.MessageEdit +import org.hyacinthbots.lilybot.extensions.moderation.commands.ClearCommands +import org.hyacinthbots.lilybot.extensions.moderation.commands.LockingCommands +import org.hyacinthbots.lilybot.extensions.moderation.commands.ModUtilities +import org.hyacinthbots.lilybot.extensions.moderation.commands.ModerationCommands +import org.hyacinthbots.lilybot.extensions.moderation.commands.Report +import org.hyacinthbots.lilybot.extensions.moderation.events.ModerationEvents +import org.hyacinthbots.lilybot.extensions.threads.AutoThreading +import org.hyacinthbots.lilybot.extensions.threads.ModThreadInviting +import org.hyacinthbots.lilybot.extensions.threads.ThreadControl +import org.hyacinthbots.lilybot.extensions.utility.commands.GalleryChannel +import org.hyacinthbots.lilybot.extensions.utility.commands.Github +import org.hyacinthbots.lilybot.extensions.utility.commands.GuildAnnouncements +import org.hyacinthbots.lilybot.extensions.utility.commands.InfoCommands +import org.hyacinthbots.lilybot.extensions.utility.commands.NewsChannelPublishing +import org.hyacinthbots.lilybot.extensions.utility.commands.PublicUtilities +import org.hyacinthbots.lilybot.extensions.utility.commands.Reminders +import org.hyacinthbots.lilybot.extensions.utility.commands.RoleMenu +import org.hyacinthbots.lilybot.extensions.utility.commands.StartupHooks +import org.hyacinthbots.lilybot.extensions.utility.commands.StatusPing +import org.hyacinthbots.lilybot.extensions.utility.commands.Tags +import org.hyacinthbots.lilybot.extensions.utility.events.UtilityEvents +import org.hyacinthbots.lilybot.internal.BuildInfo import org.hyacinthbots.lilybot.utils.BOT_TOKEN import org.hyacinthbots.lilybot.utils.ENVIRONMENT +import org.hyacinthbots.lilybot.utils.HYACINTH_GITHUB import org.hyacinthbots.lilybot.utils.database import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms import org.kohsuke.github.GitHub @@ -60,6 +72,8 @@ val docFile = Path("./docs/commands.md") suspend fun main() { val bot = ExtensibleBot(BOT_TOKEN) { + dataCollectionMode = DataCollection.None + database(true) dataAdapter(::MongoDBDataAdapter) @@ -73,11 +87,89 @@ suspend fun main() { +Intent.GuildMembers } + about { + ephemeral = false + general { + message { locale -> + embed { + title = "Info about LilyBot" + + thumbnail { + url = + "https://github.com/HyacinthBots/LilyBot/blob/main/docs/lily-logo-transparent.png?raw=true" + } + + description = + "Lily is a FOSS multi-purpose bot for Discord created by the HyacinthBots organization. " + + "Use `/help` for support or `/invite` to get an invite link." + field { + name = "How can I support the continued development of Lily?" + + value = "Lily is developed primarily by NoComment#6411 in their free time. Hyacinth " + + "doesn't have the resources to invest in hosting, so financial donations via " + + "[Buy Me a Coffee](https://buymeacoffee.com/Hyacinthbots) help keep Lily afloat. " + + "Currently, we run lily on a Hetzner cloud server, which we can afford in our " + + "current situation. We also have domain costs for our website.\n\nContributions of " + + "code & documentation are also incredibly appreciated, and you can read our " + + "[contributing guide]($HYACINTH_GITHUB/LilyBot/blob/main/CONTRIBUTING.md) or " + + "[development guide]($HYACINTH_GITHUB/LilyBot/blob/main/docs/development-guide.md) " + + "to get started." + } + + field { + name = "Version" + // To avoid IntelliJ shouting about build errors, use https://plugins.jetbrains.com/plugin/9407-pebble + value = "${BuildInfo.LILY_VERSION} (${BuildInfo.BUILD_ID})" + inline = true + } + + field { + name = "Up Since" + value = "${ + UptimeCollection().get()?.onTime?.toLocalDateTime(TimeZone.UTC) + ?.time.toString().split(".")[0] + } ${UptimeCollection().get()?.onTime?.toLocalDateTime(TimeZone.UTC)?.date} UTC\n " + + "(${UptimeCollection().get()?.onTime?.toDiscord(TimestampType.RelativeTime) ?: "??"})" + inline = true + } + + field { + name = "Useful links" + value = + "Website: Coming Soon™️\n" + + "GitHub: ${HYACINTH_GITHUB}\n" + + "Buy Me a Coffee: https://buymeacoffee.com/HyacinthBots\n" + + "Twitter: https://twitter.com/HyacinthBots\n" + + "Email: `hyacinthbots@outlook.com`\n" + + "Discord: https://discord.gg/hy2329fcTZ" + } + } + + actionRow { + linkButton( + "https://discord.com/api/oauth2/authorize?client_id=876278900836139008&" + + "permissions=1151990787078&scope=bot%20applications.commands" + ) { + label = "extensions.about.buttons.invite" + } + + linkButton("$HYACINTH_GITHUB/LilyBot/blob/main/docs/privacy-policy.md") { + label = "Privacy Policy" + } + + linkButton("$HYACINTH_GITHUB/.github/blob/main/terms-of-service.md") { + label = "Terms of Service" + } + } + } + } + } + // Add the extensions to the bot extensions { add(::AutoThreading) add(::ClearCommands) - add(::Config) + add(::ConfigExtension) add(::GalleryChannel) add(::Github) add(::GuildAnnouncements) @@ -87,6 +179,7 @@ suspend fun main() { add(::MemberLogging) add(::MessageDelete) add(::MessageEdit) + add(::ModerationEvents) add(::ModThreadInviting) add(::ModUtilities) add(::ModerationCommands) @@ -99,22 +192,23 @@ suspend fun main() { add(::StatusPing) add(::Tags) add(::ThreadControl) + add(::UtilityEvents) /* The welcome channel extension allows users to designate a YAML file to create a channel with a variety of pre-built blocks. */ - welcomeChannel(WelcomeChannelCollection()) { - staffCommandCheck { - hasPermission(Permission.BanMembers) - } + welcomeChannel(WelcomeChannelCollection()) { + staffCommandCheck { + hasPermission(Permission.BanMembers) + } - getLogChannel { _, guild -> - getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild) - } + getLogChannel { _, guild -> + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild) + } - refreshDuration = 5.minutes - } + refreshDuration = 5.minutes + } /* The anti-phishing extension automatically deletes and logs scam links. It also allows users to check links diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt index 7d5671ac..0d82cb81 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt @@ -1,15 +1,14 @@ package org.hyacinthbots.lilybot.database -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.core.Kord import dev.kord.core.behavior.getChannelOfOrNull import dev.kord.core.entity.channel.thread.ThreadChannel import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.koin.KordExKoinComponent +import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.datetime.Clock -import mu.KotlinLogging -import org.hyacinthbots.lilybot.database.Cleanups.cleanupGuildData -import org.hyacinthbots.lilybot.database.Cleanups.cleanupThreadData import org.hyacinthbots.lilybot.database.collections.GithubCollection +import org.hyacinthbots.lilybot.database.collections.LockedChannelCollection import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.database.collections.NewsChannelPublishingCollection @@ -53,17 +52,19 @@ object Cleanups : KordExKoinComponent { * @since 3.2.0 */ suspend fun cleanupGuildData(kord: Kord) { - cleanupsLogger.info("Starting guild cleanup...") + cleanupsLogger.info { "Starting guild cleanup..." } val leaveTimeData = guildLeaveTimeCollection.find().toList() var deletedGuildData = 0 + val now = Clock.System.now() leaveTimeData.forEach { // Calculate the time since Lily left the guild. - val leaveDuration = Clock.System.now() - it.guildLeaveTime + val leaveDuration = now - it.guildLeaveTime if (leaveDuration.inWholeDays > 30) { // If the bot has been out of the guild for more than 30 days, delete any related data. GithubCollection().removeDefaultRepo(it.guildId) + LockedChannelCollection().removeAllLockedChannels(it.guildId) LoggingConfigCollection().clearConfig(it.guildId) ModerationConfigCollection().clearConfig(it.guildId) NewsChannelPublishingCollection().clearAutoPublishingForGuild(it.guildId) @@ -80,7 +81,7 @@ object Cleanups : KordExKoinComponent { } } - cleanupsLogger.info("Deleted old data for $deletedGuildData guilds from the database") + cleanupsLogger.info { "Deleted old data for $deletedGuildData guilds from the database" } } /** @@ -90,7 +91,7 @@ object Cleanups : KordExKoinComponent { * @since 3.2.0 */ suspend fun cleanupThreadData(kordInstance: Kord) { - cleanupsLogger.info("Starting thread cleanup...") + cleanupsLogger.info { "Starting thread cleanup..." } val threads = threadDataCollection.find().toList() var deletedThreads = 0 for (it in threads) { @@ -114,6 +115,6 @@ object Cleanups : KordExKoinComponent { continue } } - cleanupsLogger.info("Deleted $deletedThreads old threads from the database") + cleanupsLogger.info { "Deleted $deletedThreads old threads from the database" } } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt index db1c6104..c7d7489c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt @@ -1,11 +1,12 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.AutoThreadingData import org.koin.core.component.inject import org.litote.kmongo.eq +import org.litote.kmongo.setValue /** * This class contains the functions for interacting with the [AutoThreading Database][AutoThreadingData]. This @@ -58,6 +59,21 @@ class AutoThreadingCollection : KordExKoinComponent { collection.insertOne(inputAutoThreadData) } + /** + * Updates the extra roles on an auto-threaded channel. + * + * @param inputChannelId The channel to update extra roles for + * @param newRoleIds The new list of role IDs to set + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun updateExtraRoles(inputChannelId: Snowflake, newRoleIds: List) { + collection.updateOne( + AutoThreadingData::channelId eq inputChannelId, + setValue(AutoThreadingData::extraRoleIds, newRoleIds) + ) + } + /** * Deletes an auto thread based off of the [inputChannelId]. * diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt index 61752bca..2702c26d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.LoggingConfigData import org.hyacinthbots.lilybot.database.entities.ModerationConfigData diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt index c9b75e79..ee27c237 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GalleryChannelData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt index 98732e2f..0df3551c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GithubData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt index 60b21c6b..0213c191 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import kotlinx.datetime.Instant import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GuildLeaveTimeData diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/LockedChannelCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/LockedChannelCollection.kt new file mode 100644 index 00000000..cf783ae5 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/LockedChannelCollection.kt @@ -0,0 +1,71 @@ +package org.hyacinthbots.lilybot.database.collections + +import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent +import org.hyacinthbots.lilybot.database.Database +import org.hyacinthbots.lilybot.database.entities.LockedChannelData +import org.koin.core.component.inject +import org.litote.kmongo.eq + +/** + * This class contains the function for interacting with the [Locked Channel Database][LockedChannelData]. This class + * contains functions for getting, setting and removing locked channels + * + * @since 5.0.0 + * @see addLockedChannel + * @see removeLockedChannel + * @see removeAllLockedChannels + * @see getLockedChannel + */ +class LockedChannelCollection : KordExKoinComponent { + private val db: Database by inject() + + @PublishedApi + internal val collection = db.mainDatabase.getCollection() + + /** + * Adds the data about a newly locked channel to the database. + * + * @param data The [LockedChannelData] for the locked channel + * + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun addLockedChannel(data: LockedChannelData) = collection.insertOne(data) + + /** + * Removes a locked channel from the database. This is usually called when a channel is unlocked. + * + * @param inputGuildId The ID of the guild the locked channel is in + * @param inputChannelId The ID of the locked channel itself + * + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun removeLockedChannel(inputGuildId: Snowflake, inputChannelId: Snowflake) = + collection.deleteOne(LockedChannelData::guildId eq inputGuildId, LockedChannelData::channelId eq inputChannelId) + + /** + * Removes all locked channels for a given guild from the database. Used in guild cleanups. + * + * @param inputGuildId The ID of the guild to remove the locked channels from + * + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun removeAllLockedChannels(inputGuildId: Snowflake) = + collection.deleteMany(LockedChannelData::guildId eq inputGuildId) + + /** + * Gets a locked channel based on the input parameters. + * + * @param inputGuildId The ID of the guild the locked channel is in + * @param inputChannelId The ID of the channel to get the locked data for + * @return A [LockedChannelData] object for the given channel + * + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun getLockedChannel(inputGuildId: Snowflake, inputChannelId: Snowflake): LockedChannelData? = + collection.findOne(LockedChannelData::guildId eq inputGuildId, LockedChannelData::channelId eq inputChannelId) +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt index 01ae94aa..79cc1733 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt @@ -1,6 +1,6 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.ConfigMetaData import org.hyacinthbots.lilybot.database.entities.MainMetaData diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ModerationActionCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ModerationActionCollection.kt new file mode 100644 index 00000000..da0dba27 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ModerationActionCollection.kt @@ -0,0 +1,117 @@ +package org.hyacinthbots.lilybot.database.collections + +import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent +import org.hyacinthbots.lilybot.database.Database +import org.hyacinthbots.lilybot.database.entities.ActionData +import org.hyacinthbots.lilybot.database.entities.ModerationActionData +import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction +import org.koin.core.component.inject +import org.litote.kmongo.eq + +/** + * This class contains the function for interacting with the [Moderation Action Database][ModerationActionData]. This + * class contains functions for getting, setting, removing and ignoring actions + * + * @since 5.0.0 + * @see addAction + * @see removeAction + * @see getAction + * @see declareActionToIgnore + * @see shouldIgnoreAction + */ +class ModerationActionCollection : KordExKoinComponent { + private val db: Database by inject() + + @PublishedApi + internal val collection = db.mainDatabase.getCollection() + + /** + * Adds an action that occurred. + * + * @param action The type of action you're adding + * @param guildId The ID of the guild the action occurred in + * @param targetUserId The ID of the user this action happened to + * @param data The [ActionData] for the action + * @param ignore Whether to ignore the action or not. Defaults to false + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun addAction( + action: ModerationAction, + guildId: Snowflake, + targetUserId: Snowflake, + data: ActionData, + ignore: Boolean = false + ) = collection.insertOne(ModerationActionData(action, guildId, targetUserId, data, ignore)) + + /** + * Removes an action that occurred. + * + * @param type The type of action you're removing + * @param guildId The ID of the guild the action occurred in + * @param targetUserId The ID of the user this action happened to + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun removeAction(type: ModerationAction, guildId: Snowflake, targetUserId: Snowflake) = + collection.deleteOne( + ModerationActionData::actionType eq type, + ModerationActionData::guildId eq guildId, + ModerationActionData::targetUserId eq targetUserId + ) + + /** + * Gets an action that occurred. + * + * @param type The type of action you're looking for + * @param guildId The ID of the guild the action occurred in + * @param targetUserId The ID of the user this action happened to + * @return The [data][ModerationActionData] for the event. Can be null if there is no action. + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun getAction( + type: ModerationAction, + guildId: Snowflake, + targetUserId: Snowflake + ): ModerationActionData? = + collection.findOne( + ModerationActionData::actionType eq type, + ModerationActionData::guildId eq guildId, + ModerationActionData::targetUserId eq targetUserId + ) + + /** + * Sets an action as ignored. Convenience function more than anything + * + * @param type The type of action you're looking for + * @param guildId The ID of the guild the action occurred in + * @param targetUserId The ID of the user this action happened to + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun declareActionToIgnore(type: ModerationAction, guildId: Snowflake, targetUserId: Snowflake) = + addAction(type, guildId, targetUserId, ActionData(null, null, null, null, null, null, null), true) + + /** + * Checks if an action should be ignored or not. Convenience function more than anything. + * + * @param type The type of action you're looking for + * @param guildId The ID of the guild the action occurred in + * @param targetUserId The ID of the user this action happened to + * @return True if the action should be ignored, false if otherwise + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun shouldIgnoreAction( + type: ModerationAction, + guildId: Snowflake, + targetUserId: Snowflake + ): Boolean? = + collection.findOne( + ModerationActionData::actionType eq type, + ModerationActionData::guildId eq guildId, + ModerationActionData::targetUserId eq targetUserId + )?.ignore +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt index 2851da56..84ab4a51 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.NewsChannelPublishingData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt index 90e719d2..e32aa144 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.TimeZone import kotlinx.datetime.plus @@ -11,7 +11,7 @@ import org.koin.core.component.inject import org.litote.kmongo.eq /** - * This class contains the functions for interacting with []the reminder database][ReminderData]. This + * This class contains the functions for interacting with [the reminder database][ReminderData]. This * class contains functions for setting reminders, getting reminders based of various parameters, removing reminders and * repeating them. * diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt index f8319ee5..037c5d11 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.RoleMenuData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt index 027e5272..d103759e 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.RoleSubscriptionData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt index aaccbba8..c6554147 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt @@ -1,6 +1,6 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.StatusData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt index 89584046..a006389a 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.TagsData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TemporaryBanCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TemporaryBanCollection.kt new file mode 100644 index 00000000..01fb1de6 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TemporaryBanCollection.kt @@ -0,0 +1,79 @@ +package org.hyacinthbots.lilybot.database.collections + +import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent +import org.hyacinthbots.lilybot.database.Database +import org.hyacinthbots.lilybot.database.entities.TemporaryBanData +import org.koin.core.component.inject +import org.litote.kmongo.eq + +/** + * This class contains the functions for interacting with [the temporary ban database][TemporaryBanData]. This class + * contains functions for settings temporary bans, getting temporary bans based off of various parameters and removing + * them. + * + * @since 5.0.0 + * @see getAllTempBans + * @see getTempBansForGuild + * @see getUserTempBan + * @see setTempBan + * @see removeTempBan + */ +class TemporaryBanCollection : KordExKoinComponent { + private val db: Database by inject() + + @PublishedApi + internal val collection = db.mainDatabase.getCollection() + + /** + * Gets all the temporary bans currently in the database. + * + * @return A list of temporary bans in the database + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun getAllTempBans(): List = collection.find().toList() + + /** + * Gets all the temporary bans for a given guild. + * + * @param guildId The ID of the guild to get the bans in + * @return A list of Temporary bans for the given [guildId] + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun getTempBansForGuild(guildId: Snowflake): List = + collection.find(TemporaryBanData::guildId eq guildId).toList() + + /** + * Gets a temporary ban for a given user. + * + * @param guildId The ID of the guild the temporary ban occurred in + * @param bannedUserId The ID of the user that was temporarily banned + * @return The [TemporaryBanData] for the [bannedUserId] + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun getUserTempBan(guildId: Snowflake, bannedUserId: Snowflake): TemporaryBanData? = + collection.findOne(TemporaryBanData::guildId eq guildId, TemporaryBanData::bannedUserId eq bannedUserId) + + /** + * Sets a temporary ban. + * + * @param tempBanData The data for the temporary ban + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun setTempBan(tempBanData: TemporaryBanData) = collection.insertOne(tempBanData) + + /** + * Removes the temporary ban for a user in a given guild. This is called once a temporary ban is completed. + * + * @param guildId The guild the temporary ban is being removed from + * @param bannedUserId The ID of the user to remove the temporary ban from + * @author NoComment1105 + * @since 5.0.0 + */ + suspend inline fun removeTempBan(guildId: Snowflake, bannedUserId: Snowflake) = + collection.deleteOne(TemporaryBanData::guildId eq guildId, TemporaryBanData::bannedUserId eq bannedUserId) +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt index c65ad4ee..a66899a3 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.ThreadData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt index f7f4057e..3cae0c85 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt @@ -1,6 +1,6 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import dev.kordex.core.koin.KordExKoinComponent import kotlinx.datetime.Instant import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.UptimeData diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt index cb64da4d..602363f5 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt @@ -1,7 +1,7 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.WarnData import org.koin.core.component.inject diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt index 2b98e2e2..da0cd6c9 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt @@ -7,14 +7,14 @@ package org.hyacinthbots.lilybot.database.collections -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import dev.kord.common.entity.Snowflake import dev.kord.core.Kord +import dev.kordex.core.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.WelcomeChannelData import org.koin.core.component.inject import org.litote.kmongo.eq -import com.kotlindiscord.kord.extensions.modules.extra.welcome.data.WelcomeChannelData as KordExWelcomeChannelData +import dev.kordex.modules.func.welcome.data.WelcomeChannelData as KordExWelcomeChannelData /** * This class contains the functions for interacting with the [Welcome channel database][WelcomeChannelData]. This class diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt index c41a4716..34e14542 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt @@ -1,12 +1,12 @@ /* * This code was utilized from [cozy](https://github.com/QuiltMC/cozy-discord) by QuiltMC -* and hence is subject to the terms of the Mozilla Public License V. 2.0 -* A copy of this license can be found at https://mozilla.org/MPL/2.0/. +* and hence is subject to the terms of the Mozilla Public Licence V. 2.0 +* A copy of this licence can be found at https://mozilla.org/MPL/2.0/. */ package org.hyacinthbots.lilybot.database.entities -import com.kotlindiscord.kord.extensions.storage.StorageType import dev.kord.common.entity.Snowflake +import dev.kordex.core.storage.StorageType import kotlinx.serialization.Serializable @Serializable diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt index c736f76a..cd6f2e33 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt @@ -28,5 +28,6 @@ data class AutoThreadingData( val contentAwareNaming: Boolean, val mention: Boolean, val creationMessage: String?, - val addModsAndRole: Boolean + val addModsAndRole: Boolean, + val extraRoleIds: MutableList ) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt index 84a256af..6ffe64af 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt @@ -55,7 +55,9 @@ data class ModerationConfigData( val quickTimeoutLength: DateTimePeriod?, val autoPunishOnWarn: Boolean?, val publicLogging: Boolean?, + val dmDefault: Boolean?, val banDmMessage: String?, + val autoInviteModeratorRole: Boolean? ) /** @@ -69,5 +71,9 @@ data class ModerationConfigData( @Serializable data class UtilityConfigData( val guildId: Snowflake, - val utilityLogChannel: Snowflake? + val utilityLogChannel: Snowflake?, + val logChannelUpdates: Boolean, + val logEventUpdates: Boolean, + val logInviteUpdates: Boolean, + val logRoleUpdates: Boolean ) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/LockedChannelData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/LockedChannelData.kt new file mode 100644 index 00000000..a739485a --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/LockedChannelData.kt @@ -0,0 +1,21 @@ +package org.hyacinthbots.lilybot.database.entities + +import dev.kord.common.entity.Snowflake +import kotlinx.serialization.Serializable + +/** + * The data for locked channels. + * + * @property guildId The ID of the guild the locked channel is in + * @property channelId The ID of the channel that is locked + * @property allowed The Discord Bit Set code for the allowed permissions, formatted as a string + * @property denied The Discord Bit Set code for the denied permissions, formatted as a string + * @since 5.0.0 + */ +@Serializable +data class LockedChannelData( + val guildId: Snowflake, + val channelId: Snowflake, + val allowed: String, + val denied: String, +) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ModerationActionData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ModerationActionData.kt new file mode 100644 index 00000000..834b76ed --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ModerationActionData.kt @@ -0,0 +1,66 @@ +package org.hyacinthbots.lilybot.database.entities + +import dev.kord.common.entity.Snowflake +import kotlinx.datetime.DateTimePeriod +import kotlinx.datetime.Instant +import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction + +/** + * The data for Moderation action. + * + * @property actionType The type of action you're adding + * @property guildId The ID of the guild the action occurred in + * @property targetUserId The ID of the user this action happened to + * @property data The [ActionData] for the action + * @property ignore Whether to ignore the action or not. Defaults to false + * @since 5.0.0 + */ +@Serializable +data class ModerationActionData( + val actionType: ModerationAction, + val guildId: Snowflake, + val targetUserId: Snowflake, + val data: ActionData, + val ignore: Boolean = false +) + +/** + * Further, more in-depth data about a [moderation action][ModerationActionData]. + * + * @property actioner The ID of the user that requested the action + * @property deletedMessages The amount of messages deleted in the action + * @property timeData The [TimeData] for the action + * @property reason The reason for the action + * @property dmOutcome The outcome of trying to send a DM to the user + * @property dmOverride Whether the DM sending function was override + * @property imageUrl The URL for the image attached to the action + * @since 5.0.0 + */ +@Serializable +data class ActionData( + val actioner: Snowflake?, + val deletedMessages: Int?, + val timeData: TimeData?, + val reason: String?, + val dmOutcome: Boolean?, + val dmOverride: Boolean?, + val imageUrl: String? +) + +/** + * Further, more in-depth data about the [time data for actions][ActionData.timeData]. + * + * @property durationDtp The Duration as a [DateTimePeriod] + * @property durationInst The Duration as an [Instant] + * @property start The start [Instant] of the action + * @property end The end [Instant] of the action + * @since 5.0.0 + */ +@Serializable +data class TimeData( + val durationDtp: DateTimePeriod?, + val durationInst: Instant?, + val start: Instant? = null, + val end: Instant? = null, +) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TemporaryBanData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TemporaryBanData.kt new file mode 100644 index 00000000..e955ca9f --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TemporaryBanData.kt @@ -0,0 +1,25 @@ +package org.hyacinthbots.lilybot.database.entities + +import dev.kord.common.entity.Snowflake +import kotlinx.datetime.Instant +import kotlinx.serialization.Serializable + +/** + * The data for temporary bans in a guild. + * + * @property guildId The ID of the guild the ban occurred in + * @property bannedUserId The ID of the user that was banned + * @property moderatorUserId The ID of the moderator that applied the ban + * @property startTime The time the ban was applied + * @property endTime The time the ban will complete + * + * @since 5.0.0 + */ +@Serializable +data class TemporaryBanData( + val guildId: Snowflake, + val bannedUserId: Snowflake, + val moderatorUserId: Snowflake, + val startTime: Instant, + val endTime: Instant +) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt index 3c8af692..96f3cee6 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt @@ -11,8 +11,8 @@ package org.hyacinthbots.lilybot.database.migrations -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent -import mu.KotlinLogging +import dev.kordex.core.koin.KordExKoinComponent +import io.github.oshai.kotlinlogging.KotlinLogging import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.collections.ConfigMetaCollection import org.hyacinthbots.lilybot.database.collections.MainMetaCollection @@ -24,7 +24,9 @@ import org.hyacinthbots.lilybot.database.migrations.config.configV3 import org.hyacinthbots.lilybot.database.migrations.config.configV4 import org.hyacinthbots.lilybot.database.migrations.config.configV5 import org.hyacinthbots.lilybot.database.migrations.config.configV6 +import org.hyacinthbots.lilybot.database.migrations.config.configV7 import org.hyacinthbots.lilybot.database.migrations.main.mainV1 +import org.hyacinthbots.lilybot.database.migrations.main.mainV10 import org.hyacinthbots.lilybot.database.migrations.main.mainV2 import org.hyacinthbots.lilybot.database.migrations.main.mainV3 import org.hyacinthbots.lilybot.database.migrations.main.mainV4 @@ -72,6 +74,7 @@ object Migrator : KordExKoinComponent { 7 -> ::mainV7 8 -> ::mainV8 9 -> ::mainV9 + 10 -> ::mainV10 else -> break }(db.mainDatabase) @@ -121,6 +124,7 @@ object Migrator : KordExKoinComponent { 4 -> ::configV4 5 -> ::configV5 6 -> ::configV6 + 7 -> ::configV7 else -> break }(db.configDatabase) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt new file mode 100644 index 00000000..7c25c303 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt @@ -0,0 +1,38 @@ +package org.hyacinthbots.lilybot.database.migrations.config + +import org.hyacinthbots.lilybot.database.entities.ModerationConfigData +import org.hyacinthbots.lilybot.database.entities.UtilityConfigData +import org.litote.kmongo.coroutine.CoroutineDatabase +import org.litote.kmongo.exists +import org.litote.kmongo.setValue + +suspend fun configV7(db: CoroutineDatabase) { + with(db.getCollection("moderationConfigData")) { + updateMany( + ModerationConfigData::autoInviteModeratorRole exists false, + setValue(ModerationConfigData::autoInviteModeratorRole, null) + ) + updateMany( + ModerationConfigData::dmDefault exists false, + setValue(ModerationConfigData::dmDefault, true) + ) + } + with(db.getCollection("utilityConfigData")) { + updateMany( + UtilityConfigData::logChannelUpdates exists false, + setValue(UtilityConfigData::logChannelUpdates, false) + ) + updateMany( + UtilityConfigData::logEventUpdates exists false, + setValue(UtilityConfigData::logEventUpdates, false) + ) + updateMany( + UtilityConfigData::logInviteUpdates exists false, + setValue(UtilityConfigData::logInviteUpdates, false) + ) + updateMany( + UtilityConfigData::logRoleUpdates exists false, + setValue(UtilityConfigData::logRoleUpdates, false) + ) + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV10.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV10.kt new file mode 100644 index 00000000..b5780198 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV10.kt @@ -0,0 +1,15 @@ +package org.hyacinthbots.lilybot.database.migrations.main + +import org.hyacinthbots.lilybot.database.entities.AutoThreadingData +import org.litote.kmongo.coroutine.CoroutineDatabase +import org.litote.kmongo.exists +import org.litote.kmongo.setValue + +suspend fun mainV10(db: CoroutineDatabase) { + with(db.getCollection()) { + updateMany(AutoThreadingData::extraRoleIds exists false, setValue(AutoThreadingData::extraRoleIds, emptyList())) + } + db.createCollection("lockedChannelData") + db.createCollection("temporaryBanData") + db.createCollection("moderationActionCacheData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt index 6790aa07..d3b443bd 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt @@ -6,11 +6,11 @@ package org.hyacinthbots.lilybot.database.storage -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent -import com.kotlindiscord.kord.extensions.storage.Data -import com.kotlindiscord.kord.extensions.storage.DataAdapter -import com.kotlindiscord.kord.extensions.storage.StorageUnit import com.mongodb.client.model.Filters.and +import dev.kordex.core.koin.KordExKoinComponent +import dev.kordex.core.storage.Data +import dev.kordex.core.storage.DataAdapter +import dev.kordex.core.storage.StorageUnit import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.json.Json import kotlinx.serialization.serializer diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/Config.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/Config.kt deleted file mode 100644 index d8e42dc7..00000000 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/Config.kt +++ /dev/null @@ -1,860 +0,0 @@ -package org.hyacinthbots.lilybot.extensions.config - -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.boolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingOptionalDuration -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalRole -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString -import com.kotlindiscord.kord.extensions.components.forms.ModalForm -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.modules.unsafe.annotations.UnsafeAPI -import com.kotlindiscord.kord.extensions.modules.unsafe.extensions.unsafeSubCommand -import com.kotlindiscord.kord.extensions.modules.unsafe.types.InitialSlashCommandResponse -import com.kotlindiscord.kord.extensions.modules.unsafe.types.ackEphemeral -import com.kotlindiscord.kord.extensions.modules.unsafe.types.respondEphemeral -import com.kotlindiscord.kord.extensions.utils.botHasPermissions -import dev.kord.common.entity.Permission -import dev.kord.core.behavior.channel.createMessage -import dev.kord.core.behavior.getChannelOfOrNull -import dev.kord.core.behavior.interaction.modal -import dev.kord.core.entity.channel.TextChannel -import dev.kord.rest.builder.message.EmbedBuilder -import dev.kord.rest.builder.message.embed -import kotlinx.datetime.Clock -import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection -import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection -import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection -import org.hyacinthbots.lilybot.database.entities.LoggingConfigData -import org.hyacinthbots.lilybot.database.entities.ModerationConfigData -import org.hyacinthbots.lilybot.database.entities.PublicMemberLogData -import org.hyacinthbots.lilybot.database.entities.UtilityConfigData -import org.hyacinthbots.lilybot.utils.canPingRole -import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms -import org.hyacinthbots.lilybot.utils.interval -import org.hyacinthbots.lilybot.utils.trimmedContents - -class Config : Extension() { - override val name: String = "config" - - @OptIn(UnsafeAPI::class) - override suspend fun setup() { - ephemeralSlashCommand { - name = "config" - description = "Configure Lily's settings" - - ephemeralSubCommand(::ModerationArgs) { - name = "moderation" - description = "Configure Lily's moderation system" - - requirePermission(Permission.ManageGuild) - - check { - anyGuild() - hasPermission(Permission.ManageGuild) - } - - action { - val moderationConfig = ModerationConfigCollection().getConfig(guild!!.id) - if (moderationConfig != null) { - respond { - content = "You already have a moderation configuration set. " + - "Please clear it before attempting to set a new one." - } - return@action - } - - if (!arguments.enabled) { - ModerationConfigCollection().setConfig( - ModerationConfigData( - guild!!.id, - false, - null, - null, - null, - null, - null, - null - ) - ) - respond { - content = "Moderation system disabled." - } - return@action - } - - if ( - arguments.moderatorRole != null && arguments.modActionLog == null || - arguments.moderatorRole == null && arguments.modActionLog != null - ) { - respond { - content = - "You must set both the moderator role and the action log channel to use the moderation configuration." - } - return@action - } - - if (!canPingRole(arguments.moderatorRole, guild!!.id, this@ephemeralSubCommand.kord)) { - respond { - content = - "I cannot use the role: ${arguments.moderatorRole!!.mention}, because it is not mentionable by " + - "regular users. Please enable this in the role settings, or use a different role." - } - return@action - } - - val modActionLog: TextChannel? - if (arguments.enabled && arguments.modActionLog != null) { - modActionLog = guild!!.getChannelOfOrNull(arguments.modActionLog!!.id) - if (modActionLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { - respond { - content = "The mod action log you've selected is invalid, or I can't view it. " + - "Please attempt to resolve this and try again." - } - return@action - } - } - - suspend fun EmbedBuilder.moderationEmbed() { - title = "Configuration: Moderation" - field { - name = "Moderators" - value = arguments.moderatorRole?.mention ?: "Disabled" - } - field { - name = "Action log" - value = arguments.modActionLog?.mention ?: "Disabled" - } - field { - name = "Log publicly" - value = when (arguments.logPublicly) { - true -> "True" - false -> "Disabled" - null -> "Disabled" - } - } - field { - name = "Quick timeout length" - value = arguments.quickTimeoutLength.interval() ?: "No quick timeout length set" - } - field { - name = "Warning Auto-punishments" - value = when (arguments.warnAutoPunishments) { - true -> "Enabled" - false -> "Disabled" - null -> "Disabled" - } - } - field { - name = "Ban DM Message" - value = arguments.banDmMessage ?: "No custom Ban DM message set" - } - footer { - text = "Configured by ${user.asUserOrNull()?.username}" - } - } - - respond { - embed { - moderationEmbed() - } - } - - ModerationConfigCollection().setConfig( - ModerationConfigData( - guild!!.id, - arguments.enabled, - arguments.modActionLog?.id, - arguments.moderatorRole?.id, - arguments.quickTimeoutLength, - arguments.warnAutoPunishments, - arguments.logPublicly, - arguments.banDmMessage - ) - ) - - val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!) - - if (utilityLog == null) { - respond { - content = "Consider setting a utility config to log changes to configurations." - } - return@action - } - - utilityLog.createMessage { - embed { - moderationEmbed() - } - } - } - } - - unsafeSubCommand(::LoggingArgs) { - name = "logging" - description = "Configure Lily's logging system" - - initialResponse = InitialSlashCommandResponse.None - - requirePermission(Permission.ManageGuild) - - check { - anyGuild() - hasPermission(Permission.ManageGuild) - } - - action { - val loggingConfig = LoggingConfigCollection().getConfig(guild!!.id) - if (loggingConfig != null) { - ackEphemeral() - respondEphemeral { - content = "You already have a logging configuration set. " + - "Please clear it before attempting to set a new one." - } - return@action - } - - if (arguments.enableMemberLogging && arguments.memberLog == null) { - ackEphemeral() - respondEphemeral { - content = "You must specify a channel to log members joining and leaving to!" - } - return@action - } else if ((arguments.enableMessageDeleteLogs || arguments.enableMessageEditLogs) && - arguments.messageLogs == null - ) { - ackEphemeral() - respondEphemeral { content = "You must specify a channel to log deleted/edited messages to!" } - return@action - } else if (arguments.enablePublicMemberLogging && arguments.publicMemberLog == null) { - ackEphemeral() - respondEphemeral { - content = "You must specify a channel to publicly log members joining and leaving to!" - } - return@action - } - - val memberLog: TextChannel? - if (arguments.enableMemberLogging && arguments.memberLog != null) { - memberLog = guild!!.getChannelOfOrNull(arguments.memberLog!!.id) - if (memberLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { - ackEphemeral() - respondEphemeral { - content = "The member log you've selected is invalid, or I can't view it. " + - "Please attempt to resolve this and try again." - } - return@action - } - } - - val messageLog: TextChannel? - if ((arguments.enableMessageDeleteLogs || arguments.enableMessageEditLogs) && arguments.messageLogs != null) { - messageLog = guild!!.getChannelOfOrNull(arguments.messageLogs!!.id) - if (messageLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { - ackEphemeral() - respondEphemeral { - content = "The message log you've selected is invalid, or I can't view it. " + - "Please attempt to resolve this and try again." - } - return@action - } - } - - val publicMemberLog: TextChannel? - if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) { - publicMemberLog = guild!!.getChannelOfOrNull(arguments.publicMemberLog!!.id) - if (publicMemberLog?.botHasPermissions( - Permission.ViewChannel, - Permission.SendMessages - ) != true - ) { - ackEphemeral() - respondEphemeral { - content = "The public member log you've selected is invalid, or I can't view it. " + - "Please attempt to resolve this and try again." - } - return@action - } - } - - suspend fun EmbedBuilder.loggingEmbed() { - title = "Configuration: Logging" - field { - name = "Message Delete Logs" - value = if (arguments.enableMessageDeleteLogs && arguments.messageLogs != null) { - arguments.messageLogs!!.mention - } else { - "Disabled" - } - } - field { - name = "Message Edit Logs" - value = if (arguments.enableMessageEditLogs && arguments.messageLogs != null) { - arguments.messageLogs!!.mention - } else { - "Disabled" - } - } - field { - name = "Member Logs" - value = if (arguments.enableMemberLogging && arguments.memberLog != null) { - arguments.memberLog!!.mention - } else { - "Disabled" - } - } - - field { - name = "Public Member logs" - value = if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) { - arguments.publicMemberLog!!.mention - } else { - "Disabled" - } - } - if (arguments.enableMemberLogging && arguments.publicMemberLog != null) { - val config = LoggingConfigCollection().getConfig(guild!!.id) - if (config != null) { - field { - name = "Join Message" - value = config.publicMemberLogData?.joinMessage.trimmedContents(256)!! - } - field { - name = "Leave Message" - value = config.publicMemberLogData?.leaveMessage.trimmedContents(256)!! - } - field { - name = "Ping on join" - value = config.publicMemberLogData?.pingNewUsers.toString() - } - } - } - - footer { - text = "Configured by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - - var publicMemberLogData: PublicMemberLogData? = null - if (arguments.enablePublicMemberLogging) { - val modalObj = LoggingModal() - - this@unsafeSubCommand.componentRegistry.register(modalObj) - - event.interaction.modal( - modalObj.title, - modalObj.id - ) { - modalObj.applyToBuilder(this, getLocale(), null) - } - - modalObj.awaitCompletion { modalSubmitInteraction -> - interactionResponse = modalSubmitInteraction?.deferEphemeralMessageUpdate() - } - - publicMemberLogData = PublicMemberLogData( - modalObj.ping.value == "yes", - modalObj.joinMessage.value, - modalObj.leaveMessage.value - ) - } - - LoggingConfigCollection().setConfig( - LoggingConfigData( - guild!!.id, - arguments.enableMessageDeleteLogs, - arguments.enableMessageEditLogs, - arguments.messageLogs?.id, - arguments.enableMemberLogging, - arguments.memberLog?.id, - arguments.enablePublicMemberLogging, - arguments.publicMemberLog?.id, - publicMemberLogData - ) - ) - - if (!arguments.enablePublicMemberLogging) { - ackEphemeral() - } - respondEphemeral { - embed { loggingEmbed() } - } - - val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!) - - if (utilityLog == null) { - respondEphemeral { - content = "Consider setting a utility config to log changes to configurations." - } - return@action - } - - utilityLog.createMessage { - embed { - loggingEmbed() - } - } - } - } - - ephemeralSubCommand(::UtilityArgs) { - name = "utility" - description = "Configure Lily's utility settings" - - requirePermission(Permission.ManageGuild) - - check { - anyGuild() - hasPermission(Permission.ManageGuild) - } - - action { - val utilityConfig = UtilityConfigCollection().getConfig(guild!!.id) - - if (utilityConfig != null) { - respond { - content = "You already have a utility configuration set. " + - "Please clear it before attempting to set a new one." - } - return@action - } - - var utilityLog: TextChannel? = null - if (arguments.utilityLogChannel != null) { - utilityLog = guild!!.getChannelOfOrNull(arguments.utilityLogChannel!!.id) - if (utilityLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { - respond { - content = "The utility log you've selected is invalid, or I can't view it. " + - "Please attempt to resolve this and try again." - } - return@action - } - } - - suspend fun EmbedBuilder.utilityEmbed() { - title = "Configuration: Utility" - field { - name = "Utility Log" - value = if (arguments.utilityLogChannel != null) { - "${arguments.utilityLogChannel!!.mention} ${arguments.utilityLogChannel!!.data.name.value}" - } else { - "Disabled" - } - } - - footer { - text = "Configured by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - - respond { - embed { - utilityEmbed() - } - } - - UtilityConfigCollection().setConfig( - UtilityConfigData( - guild!!.id, - arguments.utilityLogChannel?.id - ) - ) - - utilityLog?.createMessage { - embed { - utilityEmbed() - } - } - } - } - - ephemeralSubCommand(::ClearArgs) { - name = "clear" - description = "Clear a config type" - - requirePermission(Permission.ManageGuild) - - check { - anyGuild() - hasPermission(Permission.ManageGuild) - } - - action { - suspend fun logClear() { - val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!) - - if (utilityLog == null) { - respond { - content = "Consider setting a utility config to log changes to configurations." - } - return - } - - utilityLog.createMessage { - embed { - title = "Configuration Cleared: ${arguments.config[0]}${ - arguments.config.substring(1, arguments.config.length).lowercase() - }" - footer { - text = "Config cleared by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - } - } - - when (arguments.config) { - ConfigType.MODERATION.name -> { - ModerationConfigCollection().getConfig(guild!!.id) ?: run { - respond { - content = "No moderation configuration exists to clear!" - } - return@action - } - - logClear() - - ModerationConfigCollection().clearConfig(guild!!.id) - respond { - embed { - title = "Config cleared: Moderation" - footer { - text = "Config cleared by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - } - } - - ConfigType.LOGGING.name -> { - LoggingConfigCollection().getConfig(guild!!.id) ?: run { - respond { - content = "No logging configuration exists to clear!" - } - return@action - } - - logClear() - - LoggingConfigCollection().clearConfig(guild!!.id) - respond { - embed { - title = "Config cleared: Logging" - footer { - text = "Config cleared by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - } - } - - ConfigType.UTILITY.name -> { - UtilityConfigCollection().getConfig(guild!!.id) ?: run { - respond { - content = "No utility configuration exists to clear" - } - return@action - } - - logClear() - - UtilityConfigCollection().clearConfig(guild!!.id) - respond { - embed { - title = "Config cleared: Utility" - footer { - text = "Config cleared by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - } - } - - ConfigType.ALL.name -> { - ModerationConfigCollection().clearConfig(guild!!.id) - LoggingConfigCollection().clearConfig(guild!!.id) - UtilityConfigCollection().clearConfig(guild!!.id) - respond { - embed { - title = "All configs cleared" - footer { - text = "Configs cleared by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - } - } - } - } - } - } - - ephemeralSubCommand(::ViewArgs) { - name = "view" - description = "View the current config that you have set" - - requirePermission(Permission.ManageGuild) - - check { - anyGuild() - hasPermission(Permission.ManageGuild) - } - - action { - when (arguments.config) { - ConfigType.MODERATION.name -> { - val config = ModerationConfigCollection().getConfig(guild!!.id) - if (config == null) { - respond { - content = "There is no moderation config for this guild" - } - return@action - } - - respond { - embed { - title = "Current moderation config" - description = "This is the current moderation config for this guild" - field { - name = "Enabled/Disabled" - value = if (config.enabled) "Enabled" else "Disabled" - } - field { - name = "Moderators" - value = config.role?.let { guild!!.getRoleOrNull(it)?.mention } ?: "Disabled" - } - field { - name = "Action log" - value = - config.channel?.let { guild!!.getChannelOrNull(it)?.mention } ?: "Disabled" - } - field { - name = "Log publicly" - value = when (config.publicLogging) { - true -> "True" - false -> "Disabled" - null -> "Disabled" - } - } - field { - name = "Ban DM Message" - value = config.banDmMessage ?: "None" - } - timestamp = Clock.System.now() - } - } - } - - ConfigType.LOGGING.name -> { - val config = LoggingConfigCollection().getConfig(guild!!.id) - if (config == null) { - respond { - content = "There is no logging config for this guild" - } - return@action - } - - respond { - embed { - title = "Current logging config" - description = "This is the current logging config for this guild" - field { - name = "Message delete logs" - value = if (config.enableMessageDeleteLogs) { - "Enabled\n" + - "* ${guild!!.getChannelOrNull(config.messageChannel!!)?.mention ?: "Unable to get channel mention"} (" + - "${guild!!.getChannelOrNull(config.messageChannel)?.name ?: "Unable to get channel name"})" - } else { - "Disabled" - } - } - field { - name = "Message edit logs" - value = if (config.enableMessageEditLogs) { - "Enabled\n" + - "* ${guild!!.getChannelOrNull(config.messageChannel!!)?.mention ?: "Unable to get channel mention"} (" + - "${guild!!.getChannelOrNull(config.messageChannel)?.name ?: "Unable to get channel mention"})" - } else { - "Disabled" - } - } - field { - name = "Member logs" - value = if (config.enableMemberLogs) { - "Enabled\n" + - "* ${guild!!.getChannelOrNull(config.memberLog!!)?.mention ?: "Unable to get channel mention"} (" + - "${guild!!.getChannelOrNull(config.memberLog)?.name ?: "Unable to get channel mention."})" - } else { - "Disabled" - } - } - timestamp = Clock.System.now() - } - } - } - - ConfigType.UTILITY.name -> { - val config = UtilityConfigCollection().getConfig(guild!!.id) - if (config == null) { - respond { - content = "There is no utility config for this guild" - } - return@action - } - - respond { - embed { - title = "Current utility config" - description = "This is the current utility config for this guild" - field { - name = "Channel" - value = - "${ - config.utilityLogChannel?.let { guild!!.getChannelOrNull(it)?.mention } ?: "None" - } ${config.utilityLogChannel?.let { guild!!.getChannelOrNull(it)?.name } ?: ""}" - } - timestamp = Clock.System.now() - } - } - } - } - } - } - } - } - - inner class ModerationArgs : Arguments() { - val enabled by boolean { - name = "enable-moderation" - description = "Whether to enable the moderation system" - } - - val moderatorRole by optionalRole { - name = "moderator-role" - description = "The role of your moderators, used for pinging in message logs." - } - - val modActionLog by optionalChannel { - name = "action-log" - description = "The channel used to store moderator actions." - } - - val quickTimeoutLength by coalescingOptionalDuration { - name = "quick-timeout-length" - description = "The length of timeouts to use for quick timeouts" - } - - val warnAutoPunishments by optionalBoolean { - name = "warn-auto-punishments" - description = "Whether to automatically punish users for reach a certain threshold on warns" - } - - val logPublicly by optionalBoolean { - name = "log-publicly" - description = "Whether to log moderation publicly or not." - } - - val banDmMessage by optionalString { - name = "ban-dm-message" - description = "A custom message to send to users when they are banned." - } - } - - inner class LoggingArgs : Arguments() { - val enableMessageDeleteLogs by boolean { - name = "enable-delete-logs" - description = "Enable logging of message deletions" - } - - val enableMessageEditLogs by boolean { - name = "enable-edit-logs" - description = "Enable logging of message edits" - } - - val enableMemberLogging by boolean { - name = "enable-member-logging" - description = "Enable logging of members joining and leaving the guild" - } - - val enablePublicMemberLogging by boolean { - name = "enable-public-member-logging" - description = - "Enable logging of members joining and leaving the guild with a public message and ping if enabled" - } - - val messageLogs by optionalChannel { - name = "message-logs" - description = "The channel for logging message deletions" - } - - val memberLog by optionalChannel { - name = "member-log" - description = "The channel for logging members joining and leaving the guild" - } - - val publicMemberLog by optionalChannel { - name = "public-member-log" - description = "The channel for the public logging of members joining and leaving the guild" - } - } - - inner class UtilityArgs : Arguments() { - val utilityLogChannel by optionalChannel { - name = "utility-log" - description = "The channel to log various utility actions too." - } - } - - inner class ClearArgs : Arguments() { - val config by stringChoice { - name = "config-type" - description = "The type of config to clear" - choices = mutableMapOf( - "moderation" to ConfigType.MODERATION.name, - "logging" to ConfigType.LOGGING.name, - "utility" to ConfigType.UTILITY.name, - "all" to ConfigType.ALL.name - ) - } - } - - inner class ViewArgs : Arguments() { - val config by stringChoice { - name = "config-type" - description = "The type of config to clear" - choices = mutableMapOf( - "moderation" to ConfigType.MODERATION.name, - "logging" to ConfigType.LOGGING.name, - "utility" to ConfigType.UTILITY.name, - ) - } - } - - inner class LoggingModal : ModalForm() { - override var title = "Public logging configuration" - - val joinMessage = paragraphText { - label = "What would you like sent when a user joins" - placeholder = "Welcome to the server!" - required = true - } - - val leaveMessage = paragraphText { - label = "What would you like sent when a user leaves" - placeholder = "Adiós amigo!" - required = true - } - - val ping = lineText { - label = "Type `yes` to ping new users when they join" - placeholder = "Defaults to false if input is invalid or not `yes`" - } - } -} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/ConfigExtension.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/ConfigExtension.kt new file mode 100644 index 00000000..454b113b --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/ConfigExtension.kt @@ -0,0 +1,32 @@ +package org.hyacinthbots.lilybot.extensions.config + +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI +import org.hyacinthbots.lilybot.extensions.config.commands.configClearCommand +import org.hyacinthbots.lilybot.extensions.config.commands.configViewCommand +import org.hyacinthbots.lilybot.extensions.logging.config.loggingCommand +import org.hyacinthbots.lilybot.extensions.moderation.config.moderationCommand +import org.hyacinthbots.lilybot.extensions.utility.config.utilityCommand + +class ConfigExtension : Extension() { + override val name: String = "config" + + @OptIn(UnsafeAPI::class) + override suspend fun setup() { + ephemeralSlashCommand { + name = "config" + description = "Configure Lily's settings" + + loggingCommand() + + moderationCommand() + + utilityCommand() + + configClearCommand() + + configViewCommand() + } + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigClearCommand.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigClearCommand.kt new file mode 100644 index 00000000..2e637d52 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigClearCommand.kt @@ -0,0 +1,150 @@ +package org.hyacinthbots.lilybot.extensions.config.commands + +import dev.kord.common.entity.Permission +import dev.kord.core.behavior.channel.createMessage +import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.EphemeralSlashCommandContext +import dev.kordex.core.commands.application.slash.SlashCommand +import dev.kordex.core.commands.application.slash.converters.impl.stringChoice +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection +import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection +import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection +import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.config.ConfigType +import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms + +suspend fun SlashCommand<*, *, *>.configClearCommand() = ephemeralSubCommand(::ClearArgs) { + name = "clear" + description = "Clear a config type" + + requirePermission(Permission.ManageGuild) + + check { + anyGuild() + hasPermission(Permission.ManageGuild) + } + + action { + when (arguments.config) { + ConfigType.MODERATION.name -> clearConfig(ConfigType.MODERATION, arguments) + + ConfigType.LOGGING.name -> clearConfig(ConfigType.LOGGING, arguments) + + ConfigType.UTILITY.name -> clearConfig(ConfigType.UTILITY, arguments) + + ConfigType.ALL.name -> clearConfig(ConfigType.ALL, arguments) + } + + respond { + embed { + title = if (arguments.config == ConfigType.ALL.name) { + "All configs cleared" + } else { + "Config cleared: ${arguments.config}" + } + footer { + text = "Config cleared by ${user.asUserOrNull()?.username}" + icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + } + } + } + } +} + +/** + * Handles the clearing of a configuration(s) based on the [args] and [type]. + * + * @param type The type of config to clear + * @param args The [ClearArgs] for the clearing of the config + * @author NoComment1105 + * @since 5.0.0 + */ +private suspend fun EphemeralSlashCommandContext<*, *>.clearConfig(type: ConfigType, args: ClearArgs) { + when (type) { + ConfigType.MODERATION -> { + ModerationConfigCollection().getConfig(guild!!.id) ?: run { + respond { + content = "No moderation configuration exists to clear" + } + return + } + logClear(args) + ModerationConfigCollection().clearConfig(guild!!.id) + } + + ConfigType.LOGGING -> { + LoggingConfigCollection().getConfig(guild!!.id) ?: run { + respond { + content = "No logging configuration exists to clear" + } + } + logClear(args) + LoggingConfigCollection().clearConfig(guild!!.id) + } + + ConfigType.UTILITY -> { + UtilityConfigCollection().getConfig(guild!!.id) ?: run { + respond { + content = "No utility configuration exists to clear" + } + } + logClear(args) + UtilityConfigCollection().clearConfig(guild!!.id) + } + + ConfigType.ALL -> { + ModerationConfigCollection().clearConfig(guild!!.id) + LoggingConfigCollection().clearConfig(guild!!.id) + UtilityConfigCollection().clearConfig(guild!!.id) + } + } +} + +/** + * Log the clearing of a configuration to the utility log, should there still be one. + * + * @param arguments The [ClearArgs] for the clearing of the config + * @author NoComment1105 + * @since 5.0.0 + */ +suspend fun EphemeralSlashCommandContext<*, *>.logClear(arguments: ClearArgs) { + // Skip this if the utility config is cleared or all configs are cleared + if (arguments.config == ConfigType.UTILITY.name || arguments.config == ConfigType.ALL.name) return + val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!) + + if (utilityLog == null) { + respond { + content = "Consider setting a utility config to log changes to configurations." + } + return + } + + utilityLog.createMessage { + embed { + title = "Configuration Cleared: ${arguments.config[0]}${ + arguments.config.substring(1, arguments.config.length).lowercase() + }" + footer { + text = "Config cleared by ${user.asUserOrNull()?.username}" + icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + } + } + } +} + +class ClearArgs : Arguments() { + val config by stringChoice { + name = "config-type" + description = "The type of config to clear" + choices = mutableMapOf( + "moderation" to ConfigType.MODERATION.name, + "logging" to ConfigType.LOGGING.name, + "utility" to ConfigType.UTILITY.name, + "all" to ConfigType.ALL.name + ) + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigViewCommand.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigViewCommand.kt new file mode 100644 index 00000000..79fa7ea0 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/commands/ConfigViewCommand.kt @@ -0,0 +1,180 @@ +package org.hyacinthbots.lilybot.extensions.config.commands + +import dev.kord.common.entity.Permission +import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.SlashCommand +import dev.kordex.core.commands.application.slash.converters.impl.stringChoice +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import kotlinx.datetime.Clock +import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection +import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection +import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection +import org.hyacinthbots.lilybot.extensions.config.ConfigType +import org.hyacinthbots.lilybot.utils.interval + +suspend fun SlashCommand<*, *, *>.configViewCommand() = ephemeralSubCommand(::ViewArgs) { + name = "view" + description = "View the current config that you have set" + + requirePermission(Permission.ManageGuild) + + check { + anyGuild() + hasPermission(Permission.ManageGuild) + } + + action { + when (arguments.config) { + ConfigType.MODERATION.name -> { + val config = ModerationConfigCollection().getConfig(guild!!.id) + if (config == null) { + respond { + content = "There is no moderation config for this guild" + } + return@action + } + + respond { + embed { + title = "Current moderation config" + description = "This is the current moderation config for this guild" + field { + name = "Enabled/Disabled" + value = if (config.enabled) "Enabled" else "Disabled" + } + field { + name = "Moderators" + value = config.role?.let { guild!!.getRoleOrNull(it)?.mention } ?: "Disabled" + } + field { + name = "Action log" + value = + config.channel?.let { guild!!.getChannelOrNull(it)?.mention } ?: "Disabled" + } + field { + name = "Log publicly" + value = when (config.publicLogging) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + field { + name = "Quick timeout length" + value = config.quickTimeoutLength.interval() ?: "No quick timeout length set" + } + field { + name = "Warning Auto-punishments" + value = when (config.autoPunishOnWarn) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + field { + name = "Ban DM Message" + value = config.banDmMessage ?: "No custom Ban DM message set" + } + field { + name = "Auto-invite Moderator Role" + value = when (config.autoInviteModeratorRole) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + timestamp = Clock.System.now() + } + } + } + + ConfigType.LOGGING.name -> { + val config = LoggingConfigCollection().getConfig(guild!!.id) + if (config == null) { + respond { + content = "There is no logging config for this guild" + } + return@action + } + + respond { + embed { + title = "Current logging config" + description = "This is the current logging config for this guild" + field { + name = "Message delete logs" + value = if (config.enableMessageDeleteLogs) { + "Enabled\n" + + "* ${guild!!.getChannelOrNull(config.messageChannel!!)?.mention ?: "Unable to get channel mention"} (" + + "${guild!!.getChannelOrNull(config.messageChannel)?.name ?: "Unable to get channel name"})" + } else { + "Disabled" + } + } + field { + name = "Message edit logs" + value = if (config.enableMessageEditLogs) { + "Enabled\n" + + "* ${guild!!.getChannelOrNull(config.messageChannel!!)?.mention ?: "Unable to get channel mention"} (" + + "${guild!!.getChannelOrNull(config.messageChannel)?.name ?: "Unable to get channel mention"})" + } else { + "Disabled" + } + } + field { + name = "Member logs" + value = if (config.enableMemberLogs) { + "Enabled\n" + + "* ${guild!!.getChannelOrNull(config.memberLog!!)?.mention ?: "Unable to get channel mention"} (" + + "${guild!!.getChannelOrNull(config.memberLog)?.name ?: "Unable to get channel mention."})" + } else { + "Disabled" + } + } + timestamp = Clock.System.now() + } + } + } + + ConfigType.UTILITY.name -> { + val config = UtilityConfigCollection().getConfig(guild!!.id) + if (config == null) { + respond { + content = "There is no utility config for this guild" + } + return@action + } + + respond { + embed { + title = "Current utility config" + description = "This is the current utility config for this guild" + field { + name = "Channel" + value = + "${ + config.utilityLogChannel?.let { guild!!.getChannelOrNull(it)?.mention } ?: "None" + } ${config.utilityLogChannel?.let { guild!!.getChannelOrNull(it)?.name } ?: ""}" + } + timestamp = Clock.System.now() + } + } + } + } + } +} + +class ViewArgs : Arguments() { + val config by stringChoice { + name = "config-type" + description = "The type of config to clear" + choices = mutableMapOf( + "moderation" to ConfigType.MODERATION.name, + "logging" to ConfigType.LOGGING.name, + "utility" to ConfigType.UTILITY.name, + ) + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/utils/ConfigEmbeds.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/utils/ConfigEmbeds.kt new file mode 100644 index 00000000..2fc8d697 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/utils/ConfigEmbeds.kt @@ -0,0 +1,160 @@ +@file:Suppress("DuplicatedCode") + +package org.hyacinthbots.lilybot.extensions.config.utils + +import dev.kord.core.behavior.GuildBehavior +import dev.kord.core.behavior.UserBehavior +import dev.kord.rest.builder.message.EmbedBuilder +import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection +import org.hyacinthbots.lilybot.extensions.logging.config.LoggingArgs +import org.hyacinthbots.lilybot.extensions.moderation.config.ModerationArgs +import org.hyacinthbots.lilybot.extensions.utility.config.UtilityArgs +import org.hyacinthbots.lilybot.utils.interval +import org.hyacinthbots.lilybot.utils.trimmedContents + +suspend fun EmbedBuilder.utilityEmbed(arguments: UtilityArgs, user: UserBehavior) { + title = "Configuration: Utility" + field { + name = "Utility Log" + value = if (arguments.utilityLogChannel != null) { + "${arguments.utilityLogChannel!!.mention} ${arguments.utilityLogChannel!!.data.name.value}" + } else { + "Disabled" + } + } + field { + name = "Log Channel updates" + value = if (arguments.logChannelUpdates) "Yes" else "No" + } + field { + name = "Log Event updates" + value = if (arguments.logEventUpdates) "Yes" else "No" + } + field { + name = "Log Invite updates" + value = if (arguments.logInviteUpdates) "Yes" else "No" + } + field { + name = "Log Role updates" + value = if (arguments.logRoleUpdates) "Yes" else "No" + } + + footer { + text = "Configured by ${user.asUserOrNull()?.username}" + icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + } +} + +suspend fun EmbedBuilder.moderationEmbed(arguments: ModerationArgs, user: UserBehavior) { + title = "Configuration: Moderation" + field { + name = "Moderators" + value = arguments.moderatorRole?.mention ?: "Disabled" + } + field { + name = "Action log" + value = arguments.modActionLog?.mention ?: "Disabled" + } + field { + name = "Log publicly" + value = when (arguments.logPublicly) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + field { + name = "Quick timeout length" + value = arguments.quickTimeoutLength.interval() ?: "No quick timeout length set" + } + field { + name = "Warning Auto-punishments" + value = when (arguments.warnAutoPunishments) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + field { + name = "DM Default" + value = when (arguments.dmDefault) { + true -> "DM argument will default to true" + false -> "DM argument will default to false" + null -> "DM argument will default to false" + } + } + field { + name = "Ban DM Message" + value = arguments.banDmMessage ?: "No custom Ban DM message set" + } + field { + name = "Auto-invite Moderator Role" + value = when (arguments.autoInviteModeratorRole) { + true -> "Enabled" + false -> "Disabled" + null -> "Disabled" + } + } + footer { + text = "Configured by ${user.asUserOrNull()?.username}" + } +} + +suspend fun EmbedBuilder.loggingEmbed(arguments: LoggingArgs, guild: GuildBehavior?, user: UserBehavior) { + title = "Configuration: Logging" + field { + name = "Message Delete Logs" + value = if (arguments.enableMessageDeleteLogs && arguments.messageLogs != null) { + arguments.messageLogs!!.mention + } else { + "Disabled" + } + } + field { + name = "Message Edit Logs" + value = if (arguments.enableMessageEditLogs && arguments.messageLogs != null) { + arguments.messageLogs!!.mention + } else { + "Disabled" + } + } + field { + name = "Member Logs" + value = if (arguments.enableMemberLogging && arguments.memberLog != null) { + arguments.memberLog!!.mention + } else { + "Disabled" + } + } + + field { + name = "Public Member logs" + value = if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) { + arguments.publicMemberLog!!.mention + } else { + "Disabled" + } + } + if (arguments.enableMemberLogging && arguments.publicMemberLog != null) { + val config = LoggingConfigCollection().getConfig(guild!!.id) + if (config != null) { + field { + name = "Join Message" + value = config.publicMemberLogData?.joinMessage.trimmedContents(256)!! + } + field { + name = "Leave Message" + value = config.publicMemberLogData?.leaveMessage.trimmedContents(256)!! + } + field { + name = "Ping on join" + value = config.publicMemberLogData?.pingNewUsers.toString() + } + } + } + + footer { + text = "Configured by ${user.asUserOrNull()?.username}" + icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingArgs.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingArgs.kt new file mode 100644 index 00000000..9ecb08d1 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingArgs.kt @@ -0,0 +1,43 @@ +package org.hyacinthbots.lilybot.extensions.logging.config + +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.converters.impl.boolean +import dev.kordex.core.commands.converters.impl.optionalChannel + +class LoggingArgs : Arguments() { + val enableMessageDeleteLogs by boolean { + name = "enable-delete-logs" + description = "Enable logging of message deletions" + } + + val enableMessageEditLogs by boolean { + name = "enable-edit-logs" + description = "Enable logging of message edits" + } + + val enableMemberLogging by boolean { + name = "enable-member-logging" + description = "Enable logging of members joining and leaving the guild" + } + + val enablePublicMemberLogging by boolean { + name = "enable-public-member-logging" + description = + "Enable logging of members joining and leaving the guild with a public message and ping if enabled" + } + + val messageLogs by optionalChannel { + name = "message-logs" + description = "The channel for logging message deletions" + } + + val memberLog by optionalChannel { + name = "member-log" + description = "The channel for logging members joining and leaving the guild" + } + + val publicMemberLog by optionalChannel { + name = "public-member-log" + description = "The channel for the public logging of members joining and leaving the guild" + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingCommand.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingCommand.kt new file mode 100644 index 00000000..91c25685 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/LoggingCommand.kt @@ -0,0 +1,165 @@ +package org.hyacinthbots.lilybot.extensions.logging.config + +import dev.kord.common.entity.Permission +import dev.kord.core.behavior.channel.createMessage +import dev.kord.core.behavior.getChannelOfOrNull +import dev.kord.core.behavior.interaction.modal +import dev.kord.core.entity.channel.TextChannel +import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.application.slash.SlashCommand +import dev.kordex.core.utils.botHasPermissions +import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI +import dev.kordex.modules.dev.unsafe.commands.slash.InitialSlashCommandResponse +import dev.kordex.modules.dev.unsafe.extensions.unsafeSubCommand +import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection +import org.hyacinthbots.lilybot.database.entities.LoggingConfigData +import org.hyacinthbots.lilybot.database.entities.PublicMemberLogData +import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.config.utils.loggingEmbed +import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms + +@OptIn(UnsafeAPI::class) +suspend fun SlashCommand<*, *, *>.loggingCommand() = unsafeSubCommand(::LoggingArgs) { + name = "logging" + description = "Configure Lily's logging system" + + initialResponse = InitialSlashCommandResponse.None + + requirePermission(Permission.ManageGuild) + + check { + anyGuild() + hasPermission(Permission.ManageGuild) + } + + action { + val loggingConfig = LoggingConfigCollection().getConfig(guild!!.id) + if (loggingConfig != null) { + ackEphemeral() + respondEphemeral { + content = "You already have a logging configuration set. " + + "Please clear it before attempting to set a new one." + } + return@action + } + + if (arguments.enableMemberLogging && arguments.memberLog == null) { + ackEphemeral() + respondEphemeral { + content = "You must specify a channel to log members joining and leaving to!" + } + return@action + } else if ((arguments.enableMessageDeleteLogs || arguments.enableMessageEditLogs) && + arguments.messageLogs == null + ) { + ackEphemeral() + respondEphemeral { content = "You must specify a channel to log deleted/edited messages to!" } + return@action + } else if (arguments.enablePublicMemberLogging && arguments.publicMemberLog == null) { + ackEphemeral() + respondEphemeral { + content = "You must specify a channel to publicly log members joining and leaving to!" + } + return@action + } + + val memberLog: TextChannel? + if (arguments.enableMemberLogging && arguments.memberLog != null) { + memberLog = guild!!.getChannelOfOrNull(arguments.memberLog!!.id) + if (memberLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { + ackEphemeral() + respondEphemeral { + content = "The member log you've selected is invalid, or I can't view it. " + + "Please attempt to resolve this and try again." + } + return@action + } + } + + val messageLog: TextChannel? + if ((arguments.enableMessageDeleteLogs || arguments.enableMessageEditLogs) && arguments.messageLogs != null) { + messageLog = guild!!.getChannelOfOrNull(arguments.messageLogs!!.id) + if (messageLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { + ackEphemeral() + respondEphemeral { + content = "The message log you've selected is invalid, or I can't view it. " + + "Please attempt to resolve this and try again." + } + return@action + } + } + + val publicMemberLog: TextChannel? + if (arguments.enablePublicMemberLogging && arguments.publicMemberLog != null) { + publicMemberLog = guild!!.getChannelOfOrNull(arguments.publicMemberLog!!.id) + if (publicMemberLog?.botHasPermissions( + Permission.ViewChannel, + Permission.SendMessages + ) != true + ) { + ackEphemeral() + respondEphemeral { + content = "The public member log you've selected is invalid, or I can't view it. " + + "Please attempt to resolve this and try again." + } + return@action + } + } + + var publicMemberLogData: PublicMemberLogData? = null + if (arguments.enablePublicMemberLogging) { + val modalObj = PublicLoggingModal() + + this@unsafeSubCommand.componentRegistry.register(modalObj) + + event.interaction.modal( + modalObj.title, + modalObj.id + ) { + modalObj.applyToBuilder(this, getLocale(), null) + } + + modalObj.awaitCompletion { modalSubmitInteraction -> + interactionResponse = modalSubmitInteraction?.deferEphemeralMessageUpdate() + } + + publicMemberLogData = PublicMemberLogData( + modalObj.ping.value == "yes", + modalObj.joinMessage.value, + modalObj.leaveMessage.value + ) + } + + LoggingConfigCollection().setConfig( + LoggingConfigData( + guild!!.id, + arguments.enableMessageDeleteLogs, + arguments.enableMessageEditLogs, + arguments.messageLogs?.id, + arguments.enableMemberLogging, + arguments.memberLog?.id, + arguments.enablePublicMemberLogging, + arguments.publicMemberLog?.id, + publicMemberLogData + ) + ) + + if (!arguments.enablePublicMemberLogging) { + ackEphemeral() + } + respondEphemeral { embed { loggingEmbed(arguments, guild, user) } } + + val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!) + + if (utilityLog == null) { + respondEphemeral { + content = "Consider setting a utility config to log changes to configurations." + } + return@action + } + + utilityLog.createMessage { embed { loggingEmbed(arguments, guild, user) } } + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/PublicLoggingModal.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/PublicLoggingModal.kt new file mode 100644 index 00000000..28928317 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/config/PublicLoggingModal.kt @@ -0,0 +1,24 @@ +package org.hyacinthbots.lilybot.extensions.logging.config + +import dev.kordex.core.components.forms.ModalForm + +class PublicLoggingModal : ModalForm() { + override var title = "Public logging configuration" + + val joinMessage = paragraphText { + label = "What would you like sent when a user joins" + placeholder = "Welcome to the server!" + required = true + } + + val leaveMessage = paragraphText { + label = "What would you like sent when a user leaves" + placeholder = "Adiós amigo!" + required = true + } + + val ping = lineText { + label = "Type `yes` to ping new users when they join" + placeholder = "Defaults to false if input is invalid or not `yes`" + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/GuildLogging.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/GuildLogging.kt similarity index 88% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/GuildLogging.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/GuildLogging.kt index c51e7b21..35a84898 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/config/GuildLogging.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/GuildLogging.kt @@ -1,10 +1,10 @@ -package org.hyacinthbots.lilybot.extensions.config +package org.hyacinthbots.lilybot.extensions.logging.events -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event import dev.kord.core.event.guild.GuildCreateEvent import dev.kord.core.event.guild.GuildDeleteEvent import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.GuildLeaveTimeCollection import java.util.concurrent.CancellationException diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MemberLogging.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MemberLogging.kt similarity index 75% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MemberLogging.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MemberLogging.kt index fc899de9..49c10cff 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MemberLogging.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MemberLogging.kt @@ -1,12 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.events +package org.hyacinthbots.lilybot.extensions.logging.events -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.utils.botHasPermissions import dev.kord.common.entity.Permission import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.behavior.channel.createMessage @@ -15,9 +8,20 @@ import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.event.guild.MemberJoinEvent import dev.kord.core.event.guild.MemberLeaveEvent import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.guildFor +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.botHasPermissions import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection +import org.hyacinthbots.lilybot.database.collections.ModerationActionCollection import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction +import org.hyacinthbots.lilybot.utils.baseModerationEmbed +import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms import org.hyacinthbots.lilybot.utils.requiredConfigs @@ -117,6 +121,21 @@ class MemberLogging : Extension() { color = DISCORD_RED } + // Check if the member was kicked, and send a log if they were. + val kickData = ModerationActionCollection().getAction(ModerationAction.KICK, event.guildId, event.user.id) + if (kickData != null) { + val targetUser = event.kord.getUser(kickData.targetUserId)!! + val actioner = kickData.data.actioner?.let { event.kord.getUser(it) }!! + getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, event.guild)?.createEmbed { + title = "Kicked a user" + description = "${targetUser.mention} has been kicked" + image = kickData.data.imageUrl + baseModerationEmbed(kickData.data.reason, targetUser, actioner) + dmNotificationStatusEmbedField(kickData.data.dmOutcome != null, kickData.data.dmOutcome) + timestamp = Clock.System.now() + } + } + if (config != null && config.enablePublicMemberLogs) { var publicLog = guildFor(event)?.getChannelOfOrNull(config.publicMemberLog!!) val permissions = publicLog?.botHasPermissions(Permission.SendMessages, Permission.EmbedLinks) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageDelete.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageDelete.kt similarity index 89% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageDelete.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageDelete.kt index e2516609..9c602189 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageDelete.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageDelete.kt @@ -1,12 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.events +package org.hyacinthbots.lilybot.extensions.logging.events -import com.kotlindiscord.kord.extensions.DISCORD_PINK -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PKMessage -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.ProxiedMessageDeleteEvent -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.UnProxiedMessageDeleteEvent import dev.kord.core.behavior.channel.asChannelOfOrNull import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.behavior.channel.createMessage @@ -15,6 +8,13 @@ import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.event.message.MessageBulkDeleteEvent import dev.kord.rest.builder.message.create.UserMessageCreateBuilder import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_PINK +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.modules.pluralkit.api.PKMessage +import dev.kordex.modules.pluralkit.events.ProxiedMessageDeleteEvent +import dev.kordex.modules.pluralkit.events.UnProxiedMessageDeleteEvent import io.ktor.client.request.forms.ChannelProvider import io.ktor.util.cio.toByteReadChannel import kotlinx.datetime.Clock diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageEdit.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageEdit.kt similarity index 75% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageEdit.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageEdit.kt index 7b839516..ecb05524 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/MessageEdit.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/logging/events/MessageEdit.kt @@ -1,20 +1,21 @@ -package org.hyacinthbots.lilybot.extensions.events +package org.hyacinthbots.lilybot.extensions.logging.events -import com.kotlindiscord.kord.extensions.DISCORD_YELLOW -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.linkButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PKMessage -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.ProxiedMessageUpdateEvent -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.UnProxiedMessageUpdateEvent -import com.kotlindiscord.kord.extensions.utils.getJumpUrl import dev.kord.core.behavior.channel.asChannelOfOrNull import dev.kord.core.behavior.channel.createMessage import dev.kord.core.entity.Message import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.components.components +import dev.kordex.core.components.linkButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.getJumpUrl +import dev.kordex.core.utils.isNullOrBot +import dev.kordex.modules.pluralkit.api.PKMessage +import dev.kordex.modules.pluralkit.events.ProxiedMessageUpdateEvent +import dev.kordex.modules.pluralkit.events.UnProxiedMessageUpdateEvent import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.extensions.config.ConfigOptions import org.hyacinthbots.lilybot.utils.attachmentsAndProxiedMessageInfo @@ -40,10 +41,9 @@ class MessageEdit : Extension() { check { anyGuild() requiredConfigs(ConfigOptions.MESSAGE_EDIT_LOGGING_ENABLED, ConfigOptions.MESSAGE_LOG) - failIf { - val message = event.message.asMessageOrNull() - message?.author?.isBot == true || event.old?.content == message?.content - } + failIf(event.message.asMessageOrNull()?.author.isNullOrBot()) + failIf(event.old?.content == event.message.asMessageOrNull()?.content) + failIf(event.old.trimmedContents() == null) } action { onMessageEdit(event.getMessageOrNull(), event.old, null) @@ -59,9 +59,8 @@ class MessageEdit : Extension() { check { anyGuild() requiredConfigs(ConfigOptions.MESSAGE_EDIT_LOGGING_ENABLED, ConfigOptions.MESSAGE_LOG) - failIf { - event.old?.content == event.message.asMessageOrNull()?.content - } + failIf(event.old?.content == event.message.asMessageOrNull()?.content) + failIf(event.old.trimmedContents() == null) } action { onMessageEdit(event.getMessageOrNull(), event.old, event.pkMessage) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationActions.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationActions.kt deleted file mode 100644 index 86ca719d..00000000 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationActions.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.hyacinthbots.lilybot.extensions.moderation - -enum class ModerationActions { - BAN, - SOFT_BAN, - KICK, - WARN, - TIMEOUT -} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ClearCommands.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ClearCommands.kt similarity index 89% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ClearCommands.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ClearCommands.kt index db8aa988..9ad4cc7c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ClearCommands.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ClearCommands.kt @@ -1,17 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.moderation - -import com.kotlindiscord.kord.extensions.DISCORD_BLACK -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.int -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalInt -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalUser -import com.kotlindiscord.kord.extensions.commands.converters.impl.snowflake -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand +package org.hyacinthbots.lilybot.extensions.moderation.commands + import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions import dev.kord.common.entity.Snowflake @@ -20,6 +8,18 @@ import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.entity.User import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.supplier.EntitySupplyStrategy +import dev.kordex.core.DISCORD_BLACK +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.EphemeralSlashCommandContext +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.int +import dev.kordex.core.commands.converters.impl.optionalInt +import dev.kordex.core.commands.converters.impl.optionalUser +import dev.kordex.core.commands.converters.impl.snowflake +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/LockingCommands.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/LockingCommands.kt similarity index 76% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/LockingCommands.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/LockingCommands.kt index abd3c9d8..b7994e75 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/LockingCommands.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/LockingCommands.kt @@ -1,27 +1,31 @@ -package org.hyacinthbots.lilybot.extensions.moderation - -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingString -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.types.EphemeralInteractionContext +package org.hyacinthbots.lilybot.extensions.moderation.commands + +import dev.kord.common.DiscordBitSet import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions import dev.kord.core.behavior.channel.asChannelOfOrNull import dev.kord.core.behavior.channel.createEmbed +import dev.kord.core.behavior.channel.editMemberPermission import dev.kord.core.behavior.channel.editRolePermission import dev.kord.core.behavior.edit import dev.kord.core.entity.channel.Channel import dev.kord.core.entity.channel.TextChannel import dev.kord.core.entity.channel.ThreadParentChannel import dev.kord.core.entity.channel.thread.TextChannelThread +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.defaultingString +import dev.kordex.core.commands.converters.impl.optionalChannel +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.types.EphemeralInteractionContext import kotlinx.datetime.Clock +import org.hyacinthbots.lilybot.database.collections.LockedChannelCollection +import org.hyacinthbots.lilybot.database.entities.LockedChannelData import org.hyacinthbots.lilybot.extensions.config.ConfigOptions import org.hyacinthbots.lilybot.utils.botHasChannelPerms import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms @@ -49,15 +53,9 @@ class LockingCommands : Extension() { check { anyGuild() - requiredConfigs( - ConfigOptions.MODERATION_ENABLED - ) + requiredConfigs(ConfigOptions.MODERATION_ENABLED) hasPermission(Permission.ModerateMembers) - requireBotPermissions( - Permission.ManageChannels, - Permission.ManageRoles, - Permission.SendMessages - ) + requireBotPermissions(Permission.ManageChannels, Permission.ManageRoles) botHasChannelPerms(Permissions(Permission.ManageChannels)) } @@ -66,12 +64,28 @@ class LockingCommands : Extension() { val channelParent = getChannelParent(channelArg) val targetChannel = getTargetChannel(channelParent, channelArg) - val channelPerms = targetChannel!!.getPermissionOverwritesForRole(guild!!.id) - if (channelPerms != null && channelPerms.denied.contains(Permission.SendMessages)) { - respond { content = "This channel is already locked!" } + val currentChannelPerms = targetChannel?.getPermissionOverwritesForRole(guild!!.id) + if (currentChannelPerms == null) { + respond { + content = "There was an error getting the permissions for this channel. Please try again." + } return@action } + if (LockedChannelCollection().getLockedChannel(guild!!.id, targetChannel.id) != null) { + respond { content = "This channel is already locked" } + return@action + } + + LockedChannelCollection().addLockedChannel( + LockedChannelData( + guildId = guild!!.id, + channelId = targetChannel.id, + allowed = currentChannelPerms.data.allowed.code.value, + denied = currentChannelPerms.data.denied.code.value + ) + ) + val everyoneRole = guild!!.getRoleOrNull(guild!!.id) if (everyoneRole == null) { respond { content = "I was unable to get the `@everyone` role. Please try again." } @@ -94,6 +108,11 @@ class LockingCommands : Extension() { denied += Permission.UseApplicationCommands } + // Explicitly allow Lily send messages, so she can unlock the server again + targetChannel.editMemberPermission(this@ephemeralSlashCommand.kord.selfId) { + allowed += Permission.SendMessages + } + respond { content = "${targetChannel.mention} has been locked." } val actionLog = @@ -119,15 +138,9 @@ class LockingCommands : Extension() { check { anyGuild() - requiredConfigs( - ConfigOptions.MODERATION_ENABLED - ) + requiredConfigs(ConfigOptions.MODERATION_ENABLED) hasPermission(Permission.ModerateMembers) - requireBotPermissions( - Permission.ManageChannels, - Permission.ManageRoles, - Permission.SendMessages - ) + requireBotPermissions(Permission.ManageChannels, Permission.ManageRoles, Permission.SendMessages) } action { @@ -187,11 +200,7 @@ class LockingCommands : Extension() { anyGuild() requiredConfigs(ConfigOptions.MODERATION_ENABLED) hasPermission(Permission.ModerateMembers) - requireBotPermissions( - Permission.ManageChannels, - Permission.ManageRoles, - Permission.SendMessages - ) + requireBotPermissions(Permission.ManageChannels, Permission.ManageRoles) botHasChannelPerms(Permissions(Permission.ManageChannels)) } @@ -209,30 +218,36 @@ class LockingCommands : Extension() { return@action } - val channelPerms = targetChannel!!.getPermissionOverwritesForRole(guild!!.id) + val channelPerms = targetChannel?.getPermissionOverwritesForRole(guild!!.id) if (channelPerms == null) { respond { content = "This channel is not locked!" } return@action } - if (!channelPerms.denied.contains(Permission.SendMessages)) { + val lockedChannel = LockedChannelCollection().getLockedChannel(guild!!.id, targetChannel.id) + if (lockedChannel == null) { respond { content = "This channel is not locked!" } return@action } targetChannel.editRolePermission(guild!!.id) { - denied -= Permission.SendMessages - denied -= Permission.SendMessagesInThreads - denied -= Permission.AddReactions - denied -= Permission.UseApplicationCommands + denied = Permissions.Builder(DiscordBitSet(lockedChannel.denied)).build() + allowed = Permissions.Builder(DiscordBitSet(lockedChannel.allowed)).build() + } + + // Remove the explicit allow to avoid any issues with the servers permission system + targetChannel.editMemberPermission(this@ephemeralSlashCommand.kord.selfId) { + allowed -= Permission.SendMessages } targetChannel.createEmbed { title = "Channel Unlocked" description = "This channel has been unlocked by a moderator.\n" + - "Please be aware of the rules when continuing discussion." + "Please be aware of the rules when continuing discussion." color = DISCORD_GREEN } + LockedChannelCollection().removeLockedChannel(guild!!.id, targetChannel.id) + respond { content = "${targetChannel.mention} has been unlocked." } val actionLog = @@ -258,15 +273,9 @@ class LockingCommands : Extension() { check { anyGuild() - requiredConfigs( - ConfigOptions.MODERATION_ENABLED - ) + requiredConfigs(ConfigOptions.MODERATION_ENABLED) hasPermission(Permission.ModerateMembers) - requireBotPermissions( - Permission.ManageChannels, - Permission.ManageRoles, - Permission.SendMessages - ) + requireBotPermissions(Permission.ManageChannels, Permission.ManageRoles, Permission.SendMessages) } action { @@ -334,9 +343,9 @@ class LockingCommands : Extension() { * @since 4.8.0 */ private suspend inline fun EphemeralInteractionContext.getTargetChannel( - channelParent: TextChannel?, - channelArg: Channel? - ): TextChannel? { + channelParent: TextChannel?, + channelArg: Channel? + ): TextChannel? { val targetChannel = channelParent ?: channelArg?.asChannelOfOrNull() if (targetChannel == null) { respond { diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ModUtilities.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModUtilities.kt similarity index 91% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ModUtilities.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModUtilities.kt index de71d44a..1654b12f 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ModUtilities.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModUtilities.kt @@ -1,30 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_BLACK -import com.kotlindiscord.kord.extensions.DISCORD_BLURPLE -import com.kotlindiscord.kord.extensions.DISCORD_WHITE -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingColor -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalChannel -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalColour -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString -import com.kotlindiscord.kord.extensions.commands.converters.impl.snowflake -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.components.forms.ModalForm -import com.kotlindiscord.kord.extensions.components.linkButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.utils.getJumpUrl -import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler -import com.kotlindiscord.kord.extensions.utils.scheduling.Task +package org.hyacinthbots.lilybot.extensions.moderation.commands + import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions @@ -40,6 +15,31 @@ import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.entity.interaction.followup.EphemeralFollowupMessage import dev.kord.rest.builder.message.embed import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.DISCORD_BLACK +import dev.kordex.core.DISCORD_BLURPLE +import dev.kordex.core.DISCORD_WHITE +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.guildFor +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.defaultingColor +import dev.kordex.core.commands.converters.impl.optionalBoolean +import dev.kordex.core.commands.converters.impl.optionalChannel +import dev.kordex.core.commands.converters.impl.optionalColour +import dev.kordex.core.commands.converters.impl.optionalString +import dev.kordex.core.commands.converters.impl.snowflake +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.components.forms.ModalForm +import dev.kordex.core.components.linkButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.utils.getJumpUrl +import dev.kordex.core.utils.scheduling.Scheduler +import dev.kordex.core.utils.scheduling.Task import kotlinx.coroutines.flow.toList import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.AutoThreadingCollection diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationCommands.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModerationCommands.kt similarity index 66% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationCommands.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModerationCommands.kt index 5ef381bf..274285a8 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/ModerationCommands.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/ModerationCommands.kt @@ -1,32 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.moderation - -import com.kotlindiscord.kord.extensions.DISCORD_BLACK -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.annotations.DoNotChain -import com.kotlindiscord.kord.extensions.checks.hasPermissions -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingOptionalDuration -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingString -import com.kotlindiscord.kord.extensions.commands.converters.impl.int -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalAttachment -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalInt -import com.kotlindiscord.kord.extensions.commands.converters.impl.user -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralStringSelectMenu -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralMessageCommand -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PluralKit -import com.kotlindiscord.kord.extensions.time.TimestampType -import com.kotlindiscord.kord.extensions.time.toDiscord -import com.kotlindiscord.kord.extensions.utils.dm -import com.kotlindiscord.kord.extensions.utils.isNullOrBot -import com.kotlindiscord.kord.extensions.utils.kordExUserAgent -import com.kotlindiscord.kord.extensions.utils.timeout -import com.kotlindiscord.kord.extensions.utils.timeoutUntil -import com.kotlindiscord.kord.extensions.utils.toDuration +package org.hyacinthbots.lilybot.extensions.moderation.commands + import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions import dev.kord.common.entity.Snowflake @@ -36,19 +9,60 @@ import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.edit import dev.kord.core.behavior.interaction.followup.edit import dev.kord.core.behavior.reply +import dev.kord.core.entity.Guild import dev.kord.core.entity.Message import dev.kord.core.entity.User import dev.kord.core.entity.interaction.followup.EphemeralFollowupMessage import dev.kord.rest.builder.message.EmbedBuilder import dev.kord.rest.builder.message.embed import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.DISCORD_BLACK +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.annotations.DoNotChain +import dev.kordex.core.checks.hasPermissions +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.coalescingDuration +import dev.kordex.core.commands.converters.impl.coalescingOptionalDuration +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.defaultingString +import dev.kordex.core.commands.converters.impl.int +import dev.kordex.core.commands.converters.impl.optionalAttachment +import dev.kordex.core.commands.converters.impl.optionalBoolean +import dev.kordex.core.commands.converters.impl.user +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralStringSelectMenu +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralMessageCommand +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.pagination.EphemeralResponsePaginator +import dev.kordex.core.pagination.pages.Page +import dev.kordex.core.pagination.pages.Pages +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import dev.kordex.core.utils.dm +import dev.kordex.core.utils.isNullOrBot +import dev.kordex.core.utils.kordExUserAgent +import dev.kordex.core.utils.scheduling.Scheduler +import dev.kordex.core.utils.scheduling.Task +import dev.kordex.core.utils.timeout +import dev.kordex.core.utils.timeoutUntil +import dev.kordex.core.utils.toDuration +import dev.kordex.modules.pluralkit.api.PluralKit import kotlinx.datetime.Clock import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.TimeZone import kotlinx.datetime.plus +import org.hyacinthbots.lilybot.database.collections.ModerationActionCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection +import org.hyacinthbots.lilybot.database.collections.TemporaryBanCollection import org.hyacinthbots.lilybot.database.collections.WarnCollection +import org.hyacinthbots.lilybot.database.entities.ActionData +import org.hyacinthbots.lilybot.database.entities.TemporaryBanData +import org.hyacinthbots.lilybot.database.entities.TimeData import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction import org.hyacinthbots.lilybot.utils.HYACINTH_GITHUB import org.hyacinthbots.lilybot.utils.baseModerationEmbed import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField @@ -65,8 +79,15 @@ class ModerationCommands : Extension() { "For more information about the warn system, please see [this document]" + "($HYACINTH_GITHUB/LilyBot/blob/main/docs/commands.md#name-warn)" + /** The scheduler that will track the time for un-banning in temp bans. */ + private val tempBanScheduler = Scheduler() + + /** The task that will run the [tempBanScheduler]. */ + private lateinit var tempBanTask: Task + @OptIn(DoNotChain::class) override suspend fun setup() { + tempBanTask = tempBanScheduler.schedule(120, repeat = true, callback = ::removeTempBans) ephemeralMessageCommand { name = "Moderate" locking = true @@ -118,23 +139,23 @@ class ModerationCommands : Extension() { placeholder = "Select action..." maximumChoices = 1 // Prevent selecting multiple options at once - option(label = "Ban user", ModerationActions.BAN.name) { + option("Ban user", ModerationAction.BAN.name) { description = "Ban the user that sent this message" } - option("Soft-ban", ModerationActions.SOFT_BAN.name) { + option("Soft-ban", ModerationAction.SOFT_BAN.name) { description = "Soft-ban the user that sent this message" } - option("Kick", ModerationActions.KICK.name) { + option("Kick", ModerationAction.KICK.name) { description = "Kick the user that sent this message" } - option("Timeout", ModerationActions.TIMEOUT.name) { + option("Timeout", ModerationAction.TIMEOUT.name) { description = "Timeout the user that sent this message" } - option("Warn", ModerationActions.WARN.name) { + option("Warn", ModerationAction.WARN.name) { description = "Warn the user that sent this message" } @@ -150,7 +171,7 @@ class ModerationCommands : Extension() { val modConfig = ModerationConfigCollection().getConfig(guild!!.id) when (option) { - ModerationActions.BAN.name -> { + ModerationAction.BAN.name -> { val dm = sender.dm { embed { title = "You have been banned from ${guild?.asGuildOrNull()?.name}" @@ -159,6 +180,18 @@ class ModerationCommands : Extension() { color = DISCORD_GREEN } } + ModerationActionCollection().addAction( + ModerationAction.BAN, guild!!.id, senderId, + ActionData( + user.id, + null, + null, + "Quick banned $reasonSuffix", + dm != null, + true, + null + ) + ) sender.ban { reason = @@ -174,7 +207,7 @@ class ModerationCommands : Extension() { "for sending this message." } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { channel.createEmbed { title = "Banned." description = "${sender.mention} user was banned " + @@ -183,27 +216,13 @@ class ModerationCommands : Extension() { } } - loggingChannel.createEmbed { - title = "Banned a user" - description = "${ - sender.mention - } has been banned!" - baseModerationEmbed( - "Quick banned via moderate menu $reasonSuffix", - sender, - user - ) - dmNotificationStatusEmbedField(dm, true) - timestamp = Clock.System.now() - } - menuMessage?.edit { content = "Banned a user." components { removeAll() } } } - ModerationActions.SOFT_BAN.name -> { + ModerationAction.SOFT_BAN.name -> { val dm = sender.dm { embed { title = "You have been soft-banned from ${guild?.asGuildOrNull()?.name}" @@ -213,11 +232,28 @@ class ModerationCommands : Extension() { } } + ModerationActionCollection().addAction( + ModerationAction.SOFT_BAN, guild!!.id, senderId, + ActionData( + user.id, + null, + null, + "Quick banned $reasonSuffix", + dm != null, + true, + null + ) + ) + sender.ban { reason = "Quick soft-banned $reasonSuffix" } + ModerationActionCollection().shouldIgnoreAction( + ModerationAction.SOFT_BAN, guild!!.id, senderId + ) + guild!!.unban(senderId, "Quick soft-ban unban") if (modConfig?.publicLogging != null && modConfig.publicLogging == true) { @@ -229,7 +265,7 @@ class ModerationCommands : Extension() { "for sending this message." } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { channel.createEmbed { title = "Soft-Banned." description = "${sender.mention} user was soft-banned " + @@ -238,27 +274,13 @@ class ModerationCommands : Extension() { } } - loggingChannel.createEmbed { - title = "Soft-Banned a user" - description = "${ - sender.mention - } has been soft-banned!" - baseModerationEmbed( - "Quick soft-banned via moderate menu $reasonSuffix", - sender, - user - ) - dmNotificationStatusEmbedField(dm, true) - timestamp = Clock.System.now() - } - menuMessage?.edit { content = "Soft-Banned a user." components { removeAll() } } } - ModerationActions.KICK.name -> { + ModerationAction.KICK.name -> { val dm = sender.dm { embed { title = "You have been kicked from ${guild?.asGuildOrNull()?.name}" @@ -277,7 +299,7 @@ class ModerationCommands : Extension() { "for sending this message." } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { channel.createEmbed { title = "Kicked." description = "${sender.mention} user was kicked " + @@ -286,19 +308,18 @@ class ModerationCommands : Extension() { } } - loggingChannel.createEmbed { - title = "Kicked a user" - description = "${ - sender.mention - } has been kicked!" - baseModerationEmbed( + ModerationActionCollection().addAction( + ModerationAction.KICK, guild!!.id, senderId, + ActionData( + user.id, + null, + null, "Quick kicked via moderate menu $reasonSuffix", - sender, - user + dm != null, + true, + null ) - dmNotificationStatusEmbedField(dm, true) - timestamp = Clock.System.now() - } + ) menuMessage?.edit { content = "Kicked a user." @@ -306,7 +327,7 @@ class ModerationCommands : Extension() { } } - ModerationActions.TIMEOUT.name -> { + ModerationAction.TIMEOUT.name -> { val timeoutTime = ModerationConfigCollection().getConfig(guild!!.id)?.quickTimeoutLength if (timeoutTime == null) { @@ -337,7 +358,7 @@ class ModerationCommands : Extension() { "${timeoutTime.interval()} for sending this message." } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { channel.createEmbed { title = "Timed-out." description = "${sender.mention} user was timed-out for " + @@ -346,24 +367,18 @@ class ModerationCommands : Extension() { } } - loggingChannel.createEmbed { - title = "Timed-out a user" - description = "${ - sender.mention - } has be timed-out!" - baseModerationEmbed( + ModerationActionCollection().addAction( + ModerationAction.TIMEOUT, guild!!.id, senderId, + ActionData( + user.id, + null, + TimeData(timeoutTime, null, null, null), "Quick timed-out via moderate menu $reasonSuffix", - sender, - user + dm != null, + null, + null ) - dmNotificationStatusEmbedField(dm, true) - field { - name = "Length" - value = modConfig?.quickTimeoutLength.interval() - ?: "Failed to load timeout length" - } - timestamp = Clock.System.now() - } + ) menuMessage?.edit { content = "Timed-out a user." @@ -371,7 +386,7 @@ class ModerationCommands : Extension() { } } - ModerationActions.WARN.name -> { + ModerationAction.WARN.name -> { WarnCollection().setWarn(senderId, guild!!.id, false) val strikes = WarnCollection().getWarn(senderId, guild!!.id)?.strikes @@ -435,7 +450,7 @@ class ModerationCommands : Extension() { "for sending this message." } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { channel.createEmbed { title = "Warned." description = "${sender.mention} user was warned " + @@ -474,7 +489,8 @@ class ModerationCommands : Extension() { val modConfig = ModerationConfigCollection().getConfig(guild!!.id) var dmStatus: Message? = null - if (arguments.dm) { + val dmControl = if (arguments.dm != null) arguments.dm!! else modConfig?.dmDefault == true + if (dmControl) { dmStatus = arguments.userArgument.dm { embed { title = "You have been banned from ${guild?.asGuildOrNull()?.name}" @@ -486,111 +502,187 @@ class ModerationCommands : Extension() { } else { arguments.reason } + }\n${ + if (arguments.softBan) { + "You were soft-banned. You are free to rejoin without the need to be unbanned" + } else { + "" + } }" } } } - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Banned a User" - description = "${arguments.userArgument.mention} has been banned!" - baseModerationEmbed(arguments.reason, arguments.userArgument, user) - image = arguments.image?.url - dmNotificationStatusEmbedField(dmStatus, arguments.dm) - timestamp = Clock.System.now() - field { - name = "Days of messages deleted:" - value = arguments.messages.toString() - inline = false - } - } - } + + ModerationActionCollection().addAction( + if (arguments.softBan) ModerationAction.SOFT_BAN else ModerationAction.BAN, + guild!!.id, arguments.userArgument.id, + ActionData( + user.id, + arguments.messages, + null, + arguments.reason, + dmStatus != null, + arguments.dm, + arguments.image?.url + ) + ) if (modConfig?.publicLogging == true) { event.interaction.channel.createEmbed { - title = "Banned a user" - description = "${arguments.userArgument.mention} has been banned!" + if (arguments.softBan) { + title = "Soft-Banned a user" + description = "${arguments.userArgument.mention} has been soft-banned!" + } else { + title = "Banned a user" + description = "${arguments.userArgument.mention} has been banned!" + } color = DISCORD_BLACK } } guild?.ban(arguments.userArgument.id) { - reason = arguments.reason - deleteMessageDuration = DateTimePeriod(days = arguments.messages).toDuration(TimeZone.UTC) + reason = arguments.reason + if (arguments.softBan) " **SOFT-BAN**" else "" + deleteMessageDuration = if (arguments.softBan && arguments.messages == 0) { + DateTimePeriod(days = 3).toDuration(TimeZone.UTC) + } else { + DateTimePeriod(days = arguments.messages).toDuration(TimeZone.UTC) + } + } + + if (arguments.softBan) { + ModerationActionCollection().declareActionToIgnore( + ModerationAction.UNBAN, guild?.id!!, arguments.userArgument.id + ) + guild?.unban(arguments.userArgument.id, "User was soft-banned. **SOFT-BAN**") } respond { - content = "Banned ${arguments.userArgument.mention}" + content = if (arguments.softBan) "Soft-banned " else "Banned " + arguments.userArgument.mention } } } - ephemeralSlashCommand(::SoftBanArgs) { - name = "soft-ban" - description = "Soft-bans a user." - - requirePermission(Permission.BanMembers) - - check { - modCommandChecks(Permission.BanMembers) - requireBotPermissions(Permission.BanMembers) - } + ephemeralSlashCommand { + name = "temp-ban" + description = "The parent command for temporary ban commands" - action { - isBotOrModerator(event.kord, arguments.userArgument, guild, "soft-ban") ?: return@action + ephemeralSubCommand(::TempBanArgs) { + name = "add" + description = "Temporarily bans a user" - // The discord limit for deleting days of messages in a ban is 7, so we should catch invalid inputs. - if (arguments.messages != null && (arguments.messages!! > 7 || arguments.messages!! < 0)) { - respond { - content = "Invalid `delete-message-days` parameter! This number must be between 0 and 7 days!" - } - return@action + requirePermission(Permission.BanMembers) + check { + modCommandChecks(Permission.BanMembers) + requireBotPermissions(Permission.BanMembers) } - val modConfig = ModerationConfigCollection().getConfig(guild!!.id) - var dmStatus: Message? = null - if (arguments.dm) { - dmStatus = arguments.userArgument.dm { - embed { - title = "You have been soft-banned from ${guild?.fetchGuild()?.name}" - description = "**Reason:**\n${arguments.reason}\n\n" + - "You are free to rejoin without the need to be unbanned" + action { + isBotOrModerator(event.kord, arguments.userArgument, guild, "temp-ban add") + val now = Clock.System.now() + val duration = now.plus(arguments.duration, TimeZone.UTC) + val modConfig = ModerationConfigCollection().getConfig(guild!!.id) + var dmStatus: Message? = null + val dmControl = if (arguments.dm != null) arguments.dm!! else modConfig?.dmDefault == true + if (dmControl) { + dmStatus = arguments.userArgument.dm { + embed { + title = "You have been temporarily banned from ${guild?.fetchGuild()?.name}" + description = "**Reason:**\n${arguments.reason}\n\n" + + "You are banned until $duration" + } } } - } - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Soft-Banned a User" - description = "${arguments.userArgument.mention} has been soft-banned!" - baseModerationEmbed(arguments.reason, arguments.userArgument, user) - image = arguments.image?.url - dmNotificationStatusEmbedField(dmStatus, arguments.dm) - timestamp = Clock.System.now() - field { - name = "Days of messages deleted:" - value = "${arguments.messages ?: "3"}" - inline = false + + ModerationActionCollection().addAction( + ModerationAction.TEMP_BAN, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, + arguments.messages, + TimeData(arguments.duration, duration), + arguments.reason, + dmStatus != null, + arguments.dm, + arguments.image?.url + ) + ) + + if (modConfig?.publicLogging == true) { + event.interaction.channel.createEmbed { + title = "Temp Banned a user" + description = "${arguments.userArgument.mention} has been Temp Banned!" + color = DISCORD_BLACK } } - } - if (modConfig?.publicLogging == true) { - event.interaction.channel.createEmbed { - title = "Soft-Banned a user" - description = "${arguments.userArgument.mention} has been soft-banned!" - color = DISCORD_BLACK + TemporaryBanCollection().setTempBan( + TemporaryBanData(guild!!.id, arguments.userArgument.id, user.id, now, duration) + ) + + guild?.ban(arguments.userArgument.id) { + reason = arguments.reason + " **TEMPORARY-BAN**" + deleteMessageDuration = DateTimePeriod(days = arguments.messages).toDuration(TimeZone.UTC) + } + + respond { + content = "Temporarily banned ${arguments.userArgument.mention}" } } + } - guild?.ban(arguments.userArgument.id) { - reason = arguments.reason + " **SOFT-BAN**" - deleteMessageDuration = DateTimePeriod(days = arguments.messages ?: 3).toDuration(TimeZone.UTC) + ephemeralSubCommand { + name = "view-all" + description = "View all temporary bans for this guild" + + requirePermission(Permission.BanMembers) + + check { + modCommandChecks(Permission.BanMembers) + requireBotPermissions(Permission.BanMembers) } - guild?.unban(arguments.userArgument.id, "Soft-ban unban") + action { + val pagesObj = Pages() + val tempBans = TemporaryBanCollection().getTempBansForGuild(guild!!.id) + if (tempBans.isEmpty()) { + pagesObj.addPage( + Page { + description = "There are no temporary bans in this guild." + } + ) + } else { + tempBans.chunked(4).forEach { tempBan -> + var content = "" + tempBan.forEach { + content = """ + User: ${this@ephemeralSubCommand.kord.getUser(it.bannedUserId)?.username} + Moderator: ${guild?.getMemberOrNull(it.moderatorUserId)?.username} + Start Time: ${it.startTime.toDiscord(TimestampType.ShortDateTime)} (${ + it.startTime.toDiscord(TimestampType.RelativeTime) + }) + End Time: ${it.endTime.toDiscord(TimestampType.ShortDateTime)} (${ + it.endTime.toDiscord(TimestampType.RelativeTime) + }) + --- + """.trimIndent() + } - respond { - content = "Soft-banned ${arguments.userArgument.mention}" + pagesObj.addPage( + Page { + title = "Temporary bans for ${guild?.asGuildOrNull()?.name ?: "this guild"}" + description = content + } + ) + } + } + + val paginator = EphemeralResponsePaginator( + pages = pagesObj, + owner = event.interaction.user, + timeoutSeconds = 300, + interaction = interactionResponse + ) + + paginator.send() } } } @@ -607,23 +699,24 @@ class ModerationCommands : Extension() { } action { - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Unbanned a user" - description = "${arguments.userArgument.mention} has been unbanned!\n${ - arguments.userArgument.id - } (${arguments.userArgument.username})" - field { - name = "Reason:" - value = arguments.reason - } - footer { - text = user.asUserOrNull()?.username ?: "Unable to get user username" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - timestamp = Clock.System.now() - color = DISCORD_GREEN - } + val tempBan = TemporaryBanCollection().getUserTempBan(this.getGuild()!!.id, arguments.userArgument.id) + if (tempBan == null) { + ModerationActionCollection().addAction( + ModerationAction.UNBAN, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, null, null, arguments.reason, null, null, null + ) + ) + guild?.unban(arguments.userArgument.id, arguments.reason) + } else { + ModerationActionCollection().addAction( + ModerationAction.UNBAN, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, null, null, arguments.reason + "**TEMPORARY-BAN", null, null, null + ) + ) + guild?.unban(arguments.userArgument.id, arguments.reason + "**TEMPORARY-BAN**") + TemporaryBanCollection().removeTempBan(guild!!.id, arguments.userArgument.id) } respond { content = "Unbanned user." @@ -647,7 +740,8 @@ class ModerationCommands : Extension() { val modConfig = ModerationConfigCollection().getConfig(guild!!.id) var dmStatus: Message? = null - if (arguments.dm) { + val dmControl = if (arguments.dm != null) arguments.dm!! else modConfig?.dmDefault == true + if (dmControl) { dmStatus = arguments.userArgument.dm { embed { title = "You have been kicked from ${guild?.fetchGuild()?.name}" @@ -655,16 +749,12 @@ class ModerationCommands : Extension() { } } } - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Kicked a user" - description = "${arguments.userArgument.mention} has been kicked!" - image = arguments.image?.url - baseModerationEmbed(arguments.reason, arguments.userArgument, user) - dmNotificationStatusEmbedField(dmStatus, arguments.dm) - timestamp = Clock.System.now() - } - } + ModerationActionCollection().addAction( + ModerationAction.KICK, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, null, null, arguments.reason, dmStatus != null, arguments.dm, arguments.image?.url + ) + ) if (modConfig?.publicLogging == true) { event.interaction.channel.createEmbed { @@ -701,7 +791,8 @@ class ModerationCommands : Extension() { isBotOrModerator(event.kord, arguments.userArgument, guild, "timeout") ?: return@action var dmStatus: Message? = null - if (arguments.dm) { + val dmControl = if (arguments.dm != null) arguments.dm!! else modConfig?.dmDefault == true + if (dmControl) { dmStatus = arguments.userArgument.dm { embed { title = "You have been timed out in ${guild?.fetchGuild()?.name}" @@ -712,20 +803,19 @@ class ModerationCommands : Extension() { } } - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Timeout" - image = arguments.image?.url - baseModerationEmbed(arguments.reason, arguments.userArgument, user) - dmNotificationStatusEmbedField(dmStatus, arguments.dm) - timestamp = Clock.System.now() - field { - name = "Duration:" - value = duration.toDiscord(TimestampType.Default) + " (${durationArg.interval()})" - inline = false - } - } - } + ModerationActionCollection().addAction( + ModerationAction.TIMEOUT, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, + null, + TimeData(durationArg, duration, Clock.System.now(), duration), + arguments.reason, + dmStatus != null, + arguments.dm, + arguments.image?.url + ) + ) + if (modConfig?.publicLogging == true) { event.interaction.channel.createEmbed { title = "Timeout" @@ -761,8 +851,10 @@ class ModerationCommands : Extension() { } action { + val config = ModerationConfigCollection().getConfig(guild!!.id) var dmStatus: Message? = null - if (arguments.dm) { + val dmControl = if (arguments.dm != null) arguments.dm!! else config?.dmDefault == true + if (dmControl) { dmStatus = arguments.userArgument.dm { embed { title = "Timeout removed in ${guild!!.asGuildOrNull()?.name}" @@ -770,23 +862,13 @@ class ModerationCommands : Extension() { } } } - getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, guild!!)?.createMessage { - embed { - title = "Timeout Removed" - dmNotificationStatusEmbedField(dmStatus, arguments.dm) - field { - name = "User:" - value = "${arguments.userArgument.username} \n${arguments.userArgument.id}" - inline = false - } - footer { - text = "Requested by ${user.asUserOrNull()?.username}" - icon = user.asUserOrNull()?.avatar?.cdnUrl?.toUrl() - } - timestamp = Clock.System.now() - color = DISCORD_BLACK - } - } + + ModerationActionCollection().addAction( + ModerationAction.REMOVE_TIMEOUT, guild!!.id, arguments.userArgument.id, + ActionData( + user.id, null, null, null, dmStatus != null, arguments.dm, null + ) + ) arguments.userArgument.asMemberOrNull(guild!!.id)?.edit { timeoutUntil = null @@ -826,7 +908,9 @@ class ModerationCommands : Extension() { var dmStatus: Message? = null - if (arguments.dm) { + val dmControl = if (arguments.dm != null) arguments.dm!! else config.dmDefault == true + + if (dmControl) { val warnText = if (config.autoPunishOnWarn == false) { "No moderation action has been taken.\n $warnSuffix" } else { @@ -862,7 +946,7 @@ class ModerationCommands : Extension() { title = "Warning" image = arguments.image?.url baseModerationEmbed(arguments.reason, arguments.userArgument, user) - dmNotificationStatusEmbedField(dmStatus, arguments.dm) + dmNotificationStatusEmbedField(dmStatus, dmControl) timestamp = Clock.System.now() field { name = "Total strikes" @@ -964,6 +1048,43 @@ class ModerationCommands : Extension() { } } + private suspend fun removeTempBans() { + val tempBans = TemporaryBanCollection().getAllTempBans() + val dueTempBans = + tempBans.filter { it.endTime.toEpochMilliseconds() - Clock.System.now().toEpochMilliseconds() <= 0 } + + for (it in dueTempBans) { + var guild: Guild? + try { + guild = kord.getGuildOrNull(it.guildId) + } catch (_: KtorRequestException) { + TemporaryBanCollection().removeTempBan(it.guildId, it.bannedUserId) + continue + } + + if (guild == null) { + TemporaryBanCollection().removeTempBan(it.guildId, it.bannedUserId) + continue + } + + ModerationActionCollection().addAction( + ModerationAction.UNBAN, guild.id, it.bannedUserId, + ActionData( + it.moderatorUserId, + null, + TimeData(null, null, it.startTime, it.endTime), + "**temporary-ban-expire**", + null, + null, + null + ) + ) + + guild.unban(it.bannedUserId, "Temporary Ban expired") + TemporaryBanCollection().removeTempBan(it.guildId, it.bannedUserId) + } + } + inner class BanArgs : Arguments() { /** The user to ban. */ val userArgument by user { @@ -984,11 +1105,17 @@ class ModerationCommands : Extension() { defaultValue = "No reason provided" } + /** Weather to softban this user or not. */ + val softBan by defaultingBoolean { + name = "soft-ban" + description = "Weather to soft-ban this user (unban them once messages are deleted)" + defaultValue = false + } + /** Whether to DM the user or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" description = "Whether to send a direct message to the user about the ban" - defaultValue = true } /** An image that the user wishes to provide for context to the ban. */ @@ -998,20 +1125,26 @@ class ModerationCommands : Extension() { } } - inner class SoftBanArgs : Arguments() { - /** The user to soft-ban. */ + inner class TempBanArgs : Arguments() { + /** The user to ban. */ val userArgument by user { name = "user" - description = "Person to Soft ban" + description = "Person to ban" } - /** The number of days worth of messages to delete, defaults to 3 days. */ - val messages by optionalInt { + /** The number of days worth of messages to delete. */ + val messages by int { name = "delete-message-days" description = "The number of days worth of messages to delete" } - /** The reason for the soft-ban. */ + /** The duration of the temporary ban. */ + val duration by coalescingDuration { + name = "duration" + description = "The duration of the temporary ban." + } + + /** The reason for the ban. */ val reason by defaultingString { name = "reason" description = "The reason for the ban" @@ -1019,13 +1152,12 @@ class ModerationCommands : Extension() { } /** Whether to DM the user or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" - description = "Whether to send a direct message to the user about the soft-ban" - defaultValue = true + description = "Whether to send a direct message to the user about the ban" } - /** An image that the user wishes to provide for context to the soft-ban. */ + /** An image that the user wishes to provide for context to the ban. */ val image by optionalAttachment { name = "image" description = "An image you'd like to provide as extra context for the action" @@ -1062,10 +1194,9 @@ class ModerationCommands : Extension() { } /** Whether to DM the user or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" description = "Whether to send a direct message to the user about the kick" - defaultValue = true } /** An image that the user wishes to provide for context to the kick. */ @@ -1096,10 +1227,9 @@ class ModerationCommands : Extension() { } /** Whether to DM the user or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" description = "Whether to send a direct message to the user about the timeout" - defaultValue = true } /** An image that the user wishes to provide for context to the kick. */ @@ -1117,10 +1247,9 @@ class ModerationCommands : Extension() { } /** Whether to DM the user about the timeout removal or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" description = "Whether to dm the user about this or not" - defaultValue = true } } @@ -1139,10 +1268,9 @@ class ModerationCommands : Extension() { } /** Whether to DM the user or not. */ - val dm by defaultingBoolean { + val dm by optionalBoolean { name = "dm" description = "Whether to send a direct message to the user about the warning" - defaultValue = true } /** An image that the user wishes to provide for context to the kick. */ diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/Report.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/Report.kt similarity index 88% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/Report.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/Report.kt index 19dcfe21..a78b5fe3 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/Report.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/commands/Report.kt @@ -1,17 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.moderation - -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.components.forms.ModalForm -import com.kotlindiscord.kord.extensions.components.linkButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralMessageCommand -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.utils.getJumpUrl +package org.hyacinthbots.lilybot.extensions.moderation.commands + import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Snowflake import dev.kord.core.behavior.UserBehavior @@ -21,6 +9,18 @@ import dev.kord.core.entity.Message import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.rest.builder.message.create.FollowupMessageCreateBuilder import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.components.forms.ModalForm +import dev.kordex.core.components.linkButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralMessageCommand +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.utils.getJumpUrl import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.extensions.config.ConfigOptions diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationArgs.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationArgs.kt new file mode 100644 index 00000000..f5cb47fb --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationArgs.kt @@ -0,0 +1,56 @@ +package org.hyacinthbots.lilybot.extensions.moderation.config + +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.converters.impl.boolean +import dev.kordex.core.commands.converters.impl.coalescingOptionalDuration +import dev.kordex.core.commands.converters.impl.optionalBoolean +import dev.kordex.core.commands.converters.impl.optionalChannel +import dev.kordex.core.commands.converters.impl.optionalRole +import dev.kordex.core.commands.converters.impl.optionalString + +class ModerationArgs : Arguments() { + val enabled by boolean { + name = "enable-moderation" + description = "Whether to enable the moderation system" + } + + val moderatorRole by optionalRole { + name = "moderator-role" + description = "The role of your moderators, used for pinging in message logs." + } + + val modActionLog by optionalChannel { + name = "action-log" + description = "The channel used to store moderator actions." + } + + val quickTimeoutLength by coalescingOptionalDuration { + name = "quick-timeout-length" + description = "The length of timeouts to use for quick timeouts" + } + + val warnAutoPunishments by optionalBoolean { + name = "warn-auto-punishments" + description = "Whether to automatically punish users for reach a certain threshold on warns" + } + + val logPublicly by optionalBoolean { + name = "log-publicly" + description = "Whether to log moderation publicly or not." + } + + val dmDefault by optionalBoolean { + name = "dm-default" + description = "The default value for whether to DM a user in a ban action or not." + } + + val banDmMessage by optionalString { + name = "ban-dm-message" + description = "A custom message to send to users when they are banned." + } + + val autoInviteModeratorRole by optionalBoolean { + name = "auto-invite-moderator-role" + description = "Silently ping moderators to invite them to new threads." + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationCommand.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationCommand.kt new file mode 100644 index 00000000..9660b247 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/config/ModerationCommand.kt @@ -0,0 +1,131 @@ +package org.hyacinthbots.lilybot.extensions.moderation.config + +import dev.kord.common.entity.Permission +import dev.kord.core.behavior.channel.createMessage +import dev.kord.core.behavior.getChannelOfOrNull +import dev.kord.core.entity.channel.TextChannel +import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.application.slash.SlashCommand +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.utils.botHasPermissions +import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection +import org.hyacinthbots.lilybot.database.entities.ModerationConfigData +import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.config.utils.moderationEmbed +import org.hyacinthbots.lilybot.utils.canPingRole +import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms + +suspend fun SlashCommand<*, *, *>.moderationCommand() = + ephemeralSubCommand(::ModerationArgs) { + name = "moderation" + description = "Configure Lily's moderation system" + + requirePermission(Permission.ManageGuild) + + check { + anyGuild() + hasPermission(Permission.ManageGuild) + } + + action { + val moderationConfig = ModerationConfigCollection().getConfig(guild!!.id) + if (moderationConfig != null) { + respond { + content = "You already have a moderation configuration set. " + + "Please clear it before attempting to set a new one." + } + return@action + } + + if (!arguments.enabled) { + ModerationConfigCollection().setConfig( + ModerationConfigData( + guild!!.id, + false, + null, + null, + null, + null, + null, + null, + null, + null + ) + ) + respond { + content = "Moderation system disabled." + } + return@action + } + + if ( + arguments.moderatorRole != null && arguments.modActionLog == null || + arguments.moderatorRole == null && arguments.modActionLog != null + ) { + respond { + content = + "You must set both the moderator role and the action log channel to use the moderation configuration." + } + return@action + } + + if (!canPingRole(arguments.moderatorRole, guild!!.id, this@moderationCommand.kord)) { + respond { + content = + "I cannot use the role: ${arguments.moderatorRole!!.mention}, because it is not mentionable by " + + "regular users. Please enable this in the role settings, or use a different role." + } + return@action + } + + val modActionLog: TextChannel? + if (arguments.enabled && arguments.modActionLog != null) { + modActionLog = guild!!.getChannelOfOrNull(arguments.modActionLog!!.id) + if (modActionLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { + respond { + content = "The mod action log you've selected is invalid, or I can't view it. " + + "Please attempt to resolve this and try again." + } + return@action + } + } + + respond { + embed { + moderationEmbed(arguments, user) + } + } + + ModerationConfigCollection().setConfig( + ModerationConfigData( + guild!!.id, + arguments.enabled, + arguments.modActionLog?.id, + arguments.moderatorRole?.id, + arguments.quickTimeoutLength, + arguments.warnAutoPunishments, + arguments.logPublicly, + arguments.dmDefault, + arguments.banDmMessage, + arguments.autoInviteModeratorRole + ) + ) + + val utilityLog = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, this.getGuild()!!) + + if (utilityLog == null) { + respond { + content = "Consider setting a utility config to log changes to configurations." + } + return@action + } + + utilityLog.createMessage { + embed { + moderationEmbed(arguments, user) + } + } + } + } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/events/ModerationEvents.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/events/ModerationEvents.kt new file mode 100644 index 00000000..8b4d551d --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/events/ModerationEvents.kt @@ -0,0 +1,304 @@ +package org.hyacinthbots.lilybot.extensions.moderation.events + +import dev.kord.core.behavior.UserBehavior +import dev.kord.core.behavior.channel.createEmbed +import dev.kord.core.event.guild.BanAddEvent +import dev.kord.core.event.guild.BanRemoveEvent +import dev.kord.core.event.guild.MemberUpdateEvent +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import dev.kordex.core.utils.timeoutUntil +import kotlinx.datetime.Clock +import org.hyacinthbots.lilybot.database.collections.ModerationActionCollection +import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection +import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.extensions.moderation.utils.ModerationAction +import org.hyacinthbots.lilybot.utils.baseModerationEmbed +import org.hyacinthbots.lilybot.utils.dmNotificationStatusEmbedField +import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms +import org.hyacinthbots.lilybot.utils.interval + +class ModerationEvents : Extension() { + override val name: String = "moderation-events" + + override suspend fun setup() { + event { + check { anyGuild() } + action { + // Do not log if the moderation system is disabled + if (ModerationConfigCollection().getConfig(event.guildId)?.enabled != true) return@action + // If the ban doesn't exist then... ???? + event.getBanOrNull() ?: return@action + var existingAction = ModerationActionCollection().getAction( + ModerationAction.BAN, event.guildId, event.user.id + ) + if (existingAction == null) { + existingAction = ModerationActionCollection().getAction( + ModerationAction.SOFT_BAN, event.guildId, event.user.id + ) + } + + if (existingAction == null) { + existingAction = ModerationActionCollection().getAction( + ModerationAction.TEMP_BAN, event.guildId, event.user.id + ) + } + + if (existingAction != null && existingAction.targetUserId != event.user.id) { + return@action + } + + if (existingAction != null) { + getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, event.getGuild())?.createEmbed { + title = when (existingAction.actionType) { + ModerationAction.BAN -> "Banned a user" + ModerationAction.SOFT_BAN -> "Soft-banned a user" + ModerationAction.TEMP_BAN -> "Temp-banned a user" + else -> null // Theoretically this should never occur but the compiler cries otherwise + } + + description = "${event.user.mention} has been ${ + if (existingAction.data.reason?.contains("quick ban", true) == false) { + when (existingAction.actionType) { + ModerationAction.BAN -> "banned" + ModerationAction.SOFT_BAN -> "soft-banned" + ModerationAction.TEMP_BAN -> "temporarily banned" + else -> null // Again should theoretically never occur, but compiler + } + } else { + when (existingAction.actionType) { + ModerationAction.BAN -> existingAction.data.reason + ModerationAction.SOFT_BAN -> existingAction.data.reason + else -> null + } + } + }" + baseModerationEmbed( + existingAction.data.reason, + event.user, + UserBehavior(existingAction.data.actioner!!, kord) + ) + image = existingAction.data.imageUrl + dmNotificationStatusEmbedField(existingAction.data.dmOutcome, existingAction.data.dmOverride) + timestamp = Clock.System.now() + if (existingAction.data.deletedMessages != null) { + field { + name = "Days of messages deleted" + value = + if (existingAction.actionType == ModerationAction.SOFT_BAN && + existingAction.data.deletedMessages == 0 + ) { + "3" + } else { + existingAction.data.deletedMessages.toString() + } + inline = false + } + } + if (existingAction.data.timeData != null) { + field { + name = "Duration:" + value = + existingAction.data.timeData.durationInst?.toDiscord(TimestampType.Default) + + " (${existingAction.data.timeData.durationDtp?.interval()})" + } + } + } + + ModerationActionCollection().removeAction( + existingAction.actionType, + event.guildId, + event.user.id + ) + } else { + getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, event.getGuild())?.createEmbed { + title = "Banned a User" + description = "${event.user.mention} has been banned!" + baseModerationEmbed(event.getBan().reason, event.user, null) + timestamp = Clock.System.now() + } + } + } + } + event { + check { anyGuild() } + action { + // Do not log if the moderation system is disabled + if (ModerationConfigCollection().getConfig(event.guildId)?.enabled != true) return@action + val ignore = ModerationActionCollection().shouldIgnoreAction( + ModerationAction.UNBAN, + event.guildId, + event.user.id + ) + if (ignore != null && ignore) { + return@action + } + + val existingAction = + ModerationActionCollection().getAction(ModerationAction.UNBAN, event.guildId, event.user.id) + if (existingAction != null && existingAction.targetUserId != event.user.id) { + return@action + } + + if (existingAction != null) { + val isTempUnban = existingAction.data.reason?.contains("**temporary-ban**", true) == true + + getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, event.getGuild())?.createEmbed { + title = if (isTempUnban) "Temporary ban removed" else "Unbanned a user" + description = "${event.user.mention} has " + + "${if (isTempUnban) "had their temporary ban removed!" else "been unbanned!"}\n" + + "${event.user.id} (${event.user.username})" + if (existingAction.data.reason?.contains("**temporary-ban-expire**") == false) { + field { + name = "Reason:" + value = existingAction.data.reason.toString() + } + } else { + field { + name = "Initial Ban date:" + value = existingAction.data.timeData?.start?.toDiscord(TimestampType.ShortDateTime) + .toString() + } + } + + if (existingAction.data.actioner != null) { + val user = UserBehavior(existingAction.data.actioner, kord).asUserOrNull() + footer { + text = user?.username ?: "Unable to get username" + icon = user?.avatar?.cdnUrl?.toUrl() + } + } + timestamp = Clock.System.now() + color = DISCORD_GREEN + } + ModerationActionCollection().removeAction( + existingAction.actionType, + event.guildId, + event.user.id + ) + } + } + } + event { + check { anyGuild() } + action { + // Do not log if the moderation system is disabled + if (ModerationConfigCollection().getConfig(event.guildId)?.enabled != true) return@action + val channel = getLoggingChannelWithPerms(ConfigOptions.ACTION_LOG, event.guild) + if (event.old?.timeoutUntil != event.member.timeoutUntil) { + var existingAction = + ModerationActionCollection().getAction(ModerationAction.TIMEOUT, event.guildId, event.member.id) + + if (existingAction == null) { + existingAction = + ModerationActionCollection().getAction( + ModerationAction.REMOVE_TIMEOUT, + event.guildId, + event.member.id + ) + } + + if (existingAction != null) { + val data = existingAction.data + val isRemove = existingAction.actionType == ModerationAction.REMOVE_TIMEOUT + val user = UserBehavior(existingAction.targetUserId, kord).asUserOrNull() + val actioner = data.actioner?.let { UserBehavior(it, kord) }?.asUserOrNull() + channel?.createEmbed { + if (isRemove) { + title = "Timeout removed" + dmNotificationStatusEmbedField(data.dmOutcome, data.dmOverride) + field { + name = "User:" + value = "${user?.username} (${user?.id})" + } + footer { + text = "Requested by ${actioner?.username}" + icon = user?.avatar?.cdnUrl?.toUrl() + } + timestamp = Clock.System.now() + color = DISCORD_GREEN + } else { + title = "Timeout" + image = data.imageUrl + baseModerationEmbed(data.reason, event.member, user) + dmNotificationStatusEmbedField(data.dmOutcome, data.dmOverride) + timestamp = data.timeData?.start + color = DISCORD_RED + field { + name = "Duration" + value = data.timeData?.durationInst?.toDiscord(TimestampType.Default) + + " (${data.timeData?.durationDtp.interval()})" + } + } + } + + ModerationActionCollection().removeAction( + existingAction.actionType, + event.guild.id, + event.member.id + ) + } else { + channel?.createEmbed { + if (event.member.timeoutUntil != null) { + title = "Timeout" + color = DISCORD_RED + field { + name = "Duration" + value = event.member.timeoutUntil?.toDiscord(TimestampType.Default) ?: "0" + } + } else { + title = "Removed timeout" + color = DISCORD_GREEN + } + field { + name = "User" + value = event.member.mention + "(${event.member.id})" + } + description = "via Discord menus" + timestamp = Clock.System.now() + } + } + } else { + val newRoles = mutableListOf() + val oldRoles = mutableListOf() + event.member.roleBehaviors.forEach { newRoles.add(it.mention) } + event.old?.roleBehaviors?.forEach { oldRoles.add(it.mention) } + channel?.createEmbed { + title = "Member updated" + description = "${event.member.username} has been updated" + if (event.member.nickname != event.old?.nickname) { + field { + name = "Nickname change" + value = "Old: ${event.old?.nickname}\nNew: ${event.member.nickname}" + } + } + if (event.member.isOwner() != event.old?.isOwner()) { + field { + name = "Became server owner" + } + } + if (event.member.roles != event.old?.roles) { + field { + name = "New Roles" + value = newRoles.joinToString(", ") + inline = true + } + field { + name = "Old roles" + value = oldRoles.joinToString(", ") + inline = true + } + } + color = DISCORD_YELLOW + } + } + } + } + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/utils/ModerationAction.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/utils/ModerationAction.kt new file mode 100644 index 00000000..7d655770 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/moderation/utils/ModerationAction.kt @@ -0,0 +1,13 @@ +package org.hyacinthbots.lilybot.extensions.moderation.utils + +enum class ModerationAction { + BAN, + SOFT_BAN, + TEMP_BAN, + UNBAN, + KICK, + TIMEOUT, + REMOVE_TIMEOUT, + WARN, + REMOVE_WARN +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/AutoThreading.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/AutoThreading.kt similarity index 77% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/AutoThreading.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/AutoThreading.kt index 1cf17ece..6ce9928b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/AutoThreading.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/AutoThreading.kt @@ -1,28 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.events - -import com.kotlindiscord.kord.extensions.DISCORD_BLACK -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.channel -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalRole -import com.kotlindiscord.kord.extensions.components.forms.ModalForm -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PKMessage -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.PKMessageCreateEvent -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.ProxiedMessageCreateEvent -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.events.UnProxiedMessageCreateEvent -import com.kotlindiscord.kord.extensions.modules.unsafe.annotations.UnsafeAPI -import com.kotlindiscord.kord.extensions.modules.unsafe.extensions.unsafeSubCommand -import com.kotlindiscord.kord.extensions.modules.unsafe.types.InitialSlashCommandResponse -import com.kotlindiscord.kord.extensions.modules.unsafe.types.ackEphemeral -import com.kotlindiscord.kord.extensions.modules.unsafe.types.respondEphemeral -import com.kotlindiscord.kord.extensions.utils.delete -import com.kotlindiscord.kord.extensions.utils.respond +package org.hyacinthbots.lilybot.extensions.threads + import dev.kord.common.entity.ArchiveDuration import dev.kord.common.entity.ChannelType import dev.kord.common.entity.MessageType @@ -44,7 +21,29 @@ import dev.kord.core.entity.channel.thread.ThreadChannel import dev.kord.core.event.channel.thread.ThreadChannelCreateEvent import dev.kord.core.supplier.EntitySupplyStrategy import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_BLACK +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.channel +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.optionalRole +import dev.kordex.core.components.forms.ModalForm +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.delete +import dev.kordex.core.utils.respond +import dev.kordex.modules.dev.unsafe.annotations.UnsafeAPI +import dev.kordex.modules.dev.unsafe.commands.slash.InitialSlashCommandResponse +import dev.kordex.modules.dev.unsafe.extensions.unsafeSubCommand +import dev.kordex.modules.pluralkit.api.PKMessage +import dev.kordex.modules.pluralkit.events.PKMessageCreateEvent +import dev.kordex.modules.pluralkit.events.ProxiedMessageCreateEvent +import dev.kordex.modules.pluralkit.events.UnProxiedMessageCreateEvent import kotlinx.datetime.Clock +import org.hyacinthbots.docgenerator.subCommandAdditionalDocumentation import org.hyacinthbots.lilybot.database.collections.AutoThreadingCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.database.collections.ThreadsCollection @@ -135,7 +134,8 @@ class AutoThreading : Extension() { contentAwareNaming = arguments.contentAwareNaming, mention = arguments.mention, creationMessage = message, - addModsAndRole = arguments.addModsAndRole + addModsAndRole = arguments.addModsAndRole, + extraRoleIds = mutableListOf() ) ) @@ -257,7 +257,7 @@ class AutoThreading : Extension() { var responseContent: String? = null autoThreads.forEach { responseContent += "\n<#${it.channelId}>" - if (responseContent!!.length > 4080) { + if (responseContent.length > 4080) { responseContent += "(List trimmed.)" return@forEach } @@ -311,6 +311,16 @@ class AutoThreading : Extension() { "None" } } + if (autoThread.extraRoleIds.isNotEmpty()) { + var mentions = "" + autoThread.extraRoleIds.forEach { + mentions += "${guild!!.getRoleOrNull(it)?.mention} " + } + field { + name = "Extra Ping Roles" + value = mentions + } + } field { name = "Prevent duplicates" value = autoThread.preventDuplicates.toString() @@ -339,6 +349,103 @@ class AutoThreading : Extension() { } } } + + ephemeralSubCommand(::ExtraRolesArgs) { + name = "add-roles" + description = "Add extra to threads in auto-threaded channels" + + subCommandAdditionalDocumentation { + extraInformation = "This command will add roles to be pinged alongside the default ping role " + + "for this auto-threaded channel" + } + + requirePermission(Permission.ManageChannels) + + check { + anyGuild() + hasPermission(Permission.ManageChannels) + requireBotPermissions(Permission.SendMessages) + botHasChannelPerms(Permissions(Permission.SendMessages)) + } + + action { + val autoThread = AutoThreadingCollection().getSingleAutoThread(event.interaction.channelId) + if (autoThread == null) { + respond { + content = "**Error:** This is not an auto-threaded channel!" + } + return@action + } + + if (!canPingRole(arguments.role, guild!!.id, this@ephemeralSubCommand.kord)) { + respond { + content = "Lily cannot mention this role. Please fix the role's permissions and try again." + } + return@action + } + + if (autoThread.extraRoleIds.contains(arguments.role!!.id)) { + respond { + content = "This role has already been added." + } + return@action + } + + val updatedRoles = autoThread.extraRoleIds + updatedRoles.add(arguments.role!!.id) + + AutoThreadingCollection().updateExtraRoles(event.interaction.channelId, updatedRoles) + + respond { + content = "Role (${arguments.role!!.mention}) added" + } + } + } + + ephemeralSubCommand(::ExtraRolesArgs) { + name = "remove-roles" + description = "Remove extra from threads in auto-threaded channels" + + subCommandAdditionalDocumentation { + extraInformation = "This command will remove roles that have been added to be pinged alongside " + + "the default ping role for this auto-threaded channel" + } + + requirePermission(Permission.ManageChannels) + + check { + anyGuild() + hasPermission(Permission.ManageChannels) + requireBotPermissions(Permission.SendMessages) + botHasChannelPerms(Permissions(Permission.SendMessages)) + } + + action { + val autoThread = AutoThreadingCollection().getSingleAutoThread(event.interaction.channelId) + if (autoThread == null) { + respond { + content = "**Error:** This is not an auto-threaded channel!" + } + return@action + } + + if (!autoThread.extraRoleIds.contains(arguments.role!!.id)) { + respond { + content = "This role has not been added." + } + return@action + } + + val updatedRoles = autoThread.extraRoleIds + updatedRoles.remove(arguments.role!!.id) + + AutoThreadingCollection().updateExtraRoles(event.interaction.channelId, updatedRoles) + + respond { + content = "Role (${arguments.role!!.mention}) removed" + } + } + } } event { @@ -456,6 +563,13 @@ class AutoThreading : Extension() { } } + inner class ExtraRolesArgs : Arguments() { + val role by optionalRole { + name = "role" + description = "A role to invite to threads in this channel" + } + } + inner class AutoThreadingViewArgs : Arguments() { val channel by channel { name = "channel" @@ -571,14 +685,18 @@ class AutoThreading : Extension() { if (moderatorRoleId != null) { moderatorRole = inputThread.guild.getRole(moderatorRoleId) } + var mentions = "" + inputOptions.extraRoleIds.forEach { + mentions += inputThread.guild.getRole(it).mention + } if (moderatorRole != null && moderatorRole.mentionable && inputOptions.addModsAndRole) { threadMessage.edit { - content = role.mention + moderatorRole.mention + content = role.mention + mentions + moderatorRole.mention } } else { threadMessage.edit { - content = role.mention + content = role.mention + mentions } } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/ModThreadInviting.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ModThreadInviting.kt similarity index 81% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/ModThreadInviting.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ModThreadInviting.kt index e4b6cd97..cc07b5b3 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/events/ModThreadInviting.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ModThreadInviting.kt @@ -1,12 +1,12 @@ -package org.hyacinthbots.lilybot.extensions.events +package org.hyacinthbots.lilybot.extensions.threads -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event import dev.kord.core.behavior.channel.createMessage import dev.kord.core.behavior.edit import dev.kord.core.event.channel.thread.ThreadChannelCreateEvent import dev.kord.core.supplier.EntitySupplyStrategy +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event import org.hyacinthbots.lilybot.database.collections.AutoThreadingCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.utils.canPingRole @@ -20,7 +20,7 @@ class ModThreadInviting : Extension() { anyGuild() failIf { event.channel.ownerId == kord.selfId || - event.channel.member != null + event.channel.member != null } } @@ -35,7 +35,7 @@ class ModThreadInviting : Extension() { val config = ModerationConfigCollection().getConfig(channel.guildId) ?: return@action - if (!config.enabled || config.role == null) return@action + if (!config.enabled || config.role == null || config.autoInviteModeratorRole != true) return@action val moderatorRole = channel.guild.getRoleOrNull(config.role) ?: return@action diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ThreadControl.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ThreadControl.kt similarity index 92% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ThreadControl.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ThreadControl.kt index 9c212578..64aff562 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/ThreadControl.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/threads/ThreadControl.kt @@ -4,22 +4,8 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_FUCHSIA -import com.kotlindiscord.kord.extensions.checks.isInThread -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.member -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.utils.hasPermission +package org.hyacinthbots.lilybot.extensions.threads + import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions @@ -36,6 +22,20 @@ import dev.kord.core.entity.interaction.response.EphemeralMessageInteractionResp import dev.kord.core.event.channel.thread.ThreadUpdateEvent import dev.kord.core.exception.EntityNotFoundException import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_FUCHSIA +import dev.kordex.core.checks.isInThread +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.EphemeralSlashCommandContext +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.member +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.hasPermission import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.database.collections.ThreadsCollection diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GalleryChannel.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GalleryChannel.kt similarity index 88% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GalleryChannel.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GalleryChannel.kt index b1b0b083..b4c9c903 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GalleryChannel.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GalleryChannel.kt @@ -1,17 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.utils.delete -import com.kotlindiscord.kord.extensions.utils.permissionsForMember -import com.kotlindiscord.kord.extensions.utils.respond +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.entity.MessageType import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions @@ -22,6 +10,18 @@ import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.event.message.MessageCreateEvent import dev.kord.core.exception.EntityNotFoundException import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.guildFor +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.delete +import dev.kordex.core.utils.permissionsForMember +import dev.kordex.core.utils.respond import kotlinx.coroutines.delay import org.hyacinthbots.lilybot.database.collections.GalleryChannelCollection import org.hyacinthbots.lilybot.extensions.config.ConfigOptions @@ -222,7 +222,7 @@ class GalleryChannel : Extension() { // Delete the explanation after 3 seconds. If an exception is thrown, the // message has already been deleted response.delete(2.5.seconds.inWholeMilliseconds) - } catch (e: EntityNotFoundException) { + } catch (_: EntityNotFoundException) { // The message that we're attempting to delete has already been deleted. } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Github.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Github.kt similarity index 87% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Github.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Github.kt index db957cc4..9171d11d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Github.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Github.kt @@ -1,19 +1,19 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.application.slash.publicSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.int -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand -import com.kotlindiscord.kord.extensions.sentry.BreadcrumbType +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.entity.Permission import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.application.slash.publicSubCommand +import dev.kordex.core.commands.converters.impl.int +import dev.kordex.core.commands.converters.impl.optionalString +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.publicSlashCommand +import dev.kordex.core.sentry.BreadcrumbType import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.GithubCollection import org.hyacinthbots.lilybot.github @@ -69,10 +69,6 @@ class Github : Extension() { } // Clarify the input is formatted correctly, inform the user if not. if (!repository.contains("/")) { - sentry.breadcrumb(BreadcrumbType.Error) { - category = "extensions.util.Github.issue.InputCheck" - message = "Input missing /" - } respond { embed { title = "Make sure your repository input is formatted like this:" @@ -93,7 +89,7 @@ class Github : Extension() { } else { try { github.getRepository(repository)?.getIssue(arguments.issue) - } catch (e: GHFileNotFoundException) { + } catch (_: GHFileNotFoundException) { respond { embed { title = "Unable to find issue number! Make sure this issue exists" @@ -102,7 +98,7 @@ class Github : Extension() { return@action } } - } catch (e: IOException) { + } catch (_: IOException) { val iterator: PagedIterator? = github.searchIssues() ?.q("${arguments.issue} repo:$repository") ?.order(GHDirection.DESC) @@ -112,7 +108,7 @@ class Github : Extension() { // Run a quick check on the iterator, in case the repository owner org/user doesn't exist try { iterator!!.hasNext() - } catch (e: GHException) { + } catch (_: GHException) { respond { embed { title = "Unable to access repository, make sure this repository exists!" @@ -124,11 +120,6 @@ class Github : Extension() { if (iterator.hasNext()) { issue = iterator.next() } else { - sentry.breadcrumb(BreadcrumbType.Error) { - category = "extensions.util.Github.issue.getIssue" - message = "Unable to find issue" - } - respond { embed { title = "Invalid issue number. Make sure this issue exists!" @@ -140,7 +131,7 @@ class Github : Extension() { val num = issue!!.number try { issue = github.getRepository(repository)?.getIssue(num) - } catch (e: GHFileNotFoundException) { + } catch (_: GHFileNotFoundException) { respond { embed { title = "Unable to find issue number! Make sure this issue exists" @@ -167,10 +158,6 @@ class Github : Extension() { merged = pull.isMerged draft = pull.isDraft } catch (ioException: IOException) { - sentry.breadcrumb(BreadcrumbType.Error) { - category = "extensions.util.Github.issue.CheckPRStatus" - message = "Error initializing PR wtf" - } ioException.printStackTrace() title = "Error!" description = "Error occurred initializing Pull Request. How did this happen?" @@ -245,7 +232,7 @@ class Github : Extension() { inline = false } } - } catch (ioException: IOException) { + } catch (_: IOException) { field { name = "Author:" value = "Unknown Author" @@ -266,7 +253,7 @@ class Github : Extension() { labels.add(label.name) } - if (labels.size > 0) { + if (labels.isNotEmpty()) { field { name = "Labels:" value = labels.joinToString(", ") @@ -295,12 +282,8 @@ class Github : Extension() { } return@action } - // Clarify the input is formatted correctly, inform the user if not + if (!repository.contains("/")) { - sentry.breadcrumb(BreadcrumbType.Error) { - category = "extensions.util.Github.repository.InputCheck" - message = "Input missing /" - } respond { embed { title = "Make sure your input is formatted like this:" @@ -321,11 +304,7 @@ class Github : Extension() { } else { github.getRepository(repository) } - sentry.breadcrumb(BreadcrumbType.Info) { - category = "extensions.util.Github.repository.getRepository" - message = "Repository found" - } - } catch (exception: IOException) { + } catch (_: IOException) { sentry.breadcrumb(BreadcrumbType.Error) { category = "extensions.util.Github.repository.getRepository" message = "Repository not found" @@ -402,15 +381,7 @@ class Github : Extension() { } else { github.getUser(arguments.username) } - sentry.breadcrumb(BreadcrumbType.Info) { - category = "extensions.util.Github.user.getUser" - message = "User found" - } - } catch (exception: IOException) { - sentry.breadcrumb(BreadcrumbType.Error) { - category = "extensions.util.Github.user.getUser" - message = "Unable to find user" - } + } catch (_: IOException) { respond { embed { title = "Invalid Username. Make sure this user exists!" @@ -550,7 +521,7 @@ class Github : Extension() { } else { github.getRepository(arguments.defaultRepo) } - } catch (e: IOException) { + } catch (_: IOException) { respond { content = "GitHub repository not found! Please make sure this repository exists" } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GuildAnnouncements.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GuildAnnouncements.kt similarity index 87% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GuildAnnouncements.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GuildAnnouncements.kt index 26ad758b..08e5e67b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/GuildAnnouncements.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/GuildAnnouncements.kt @@ -1,13 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalSnowflake -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.components.forms.ModalForm -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.Color import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Permission @@ -15,6 +7,14 @@ import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.behavior.interaction.followup.edit import dev.kord.core.entity.interaction.followup.EphemeralFollowupMessage import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.converters.impl.optionalSnowflake +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.components.forms.ModalForm +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.extensions.config.ConfigOptions import org.hyacinthbots.lilybot.utils.TEST_GUILD_ID diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/InfoCommands.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/InfoCommands.kt similarity index 50% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/InfoCommands.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/InfoCommands.kt index fad8ef8f..35a61b85 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/InfoCommands.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/InfoCommands.kt @@ -1,18 +1,12 @@ -package org.hyacinthbots.lilybot.extensions.util +package org.hyacinthbots.lilybot.extensions.utility.commands -import com.kotlindiscord.kord.extensions.DISCORD_BLURPLE -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.linkButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand -import com.kotlindiscord.kord.extensions.time.TimestampType -import com.kotlindiscord.kord.extensions.time.toDiscord import dev.kord.rest.builder.message.create.MessageCreateBuilder import dev.kord.rest.builder.message.embed -import kotlinx.datetime.TimeZone -import kotlinx.datetime.toLocalDateTime -import org.hyacinthbots.lilybot.database.collections.UptimeCollection -import org.hyacinthbots.lilybot.internal.BuildInfo +import dev.kordex.core.DISCORD_BLURPLE +import dev.kordex.core.components.components +import dev.kordex.core.components.linkButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.publicSlashCommand import org.hyacinthbots.lilybot.utils.HYACINTH_GITHUB /** @@ -44,7 +38,7 @@ class InfoCommands : Extension() { title = "What is LilyBot?" description = "Lily is a FOSS multi-purpose bot for Discord created by " + "the HyacinthBots organization. " + - "Use `/info` to learn more, or `/invite` to get an invite link." + "Use `/about` to learn more, or `/invite` to get an invite link." field { name = "How do I configure Lily?" @@ -86,77 +80,6 @@ class InfoCommands : Extension() { } } - /** - * A command that creates an embed providing basic info about Lily and uptime data. - * - * @author NoComment1105 - * @author tempest15 - * @since 4.4.0 - */ - publicSlashCommand { - name = "info" - description = "Learn about Lily, and get uptime data!" - - action { - respond { - embed { - thumbnail { - url = event.kord.getSelf().avatar?.cdnUrl!!.toUrl() - } - title = "Info about LilyBot" - description = "Lily is a FOSS multi-purpose bot for Discord created by " + - "the HyacinthBots organization. " + - "Use `/help` for support or `/invite` to get an invite link." - - field { - name = "How can I support the continued development of Lily?" - value = "Lily is developed primarily by NoComment#6411 and tempest#4510 " + - "in our free time. Neither of us have resources to invest in hosting, " + - "so financial donations via [Buy Me a Coffee]" + - "(https://buymeacoffee.com/Hyacinthbots) help keep Lily afloat. Currently, we run" + - "lily on a Hetzner cloud server, which we can afford in our current situation. " + - "We will also have domain costs for our website.\n\n" + - "Contributions of code & documentation are also incredibly appreciated, " + - "and you can read our [contributing guide]" + - "($HYACINTH_GITHUB/LilyBot/blob/main/CONTRIBUTING.md) " + - "or [development guide]" + - "($HYACINTH_GITHUB/LilyBot/blob/main/docs/development-guide.md) " + - "to get started." - } - field { - name = "Version" - // To avoid IntelliJ shouting about build errors, use https://plugins.jetbrains.com/plugin/9407-pebble - value = - "${BuildInfo.LILY_VERSION} (${BuildInfo.BUILD_ID})" - inline = true - } - field { - name = "Up Since" - value = "${ - UptimeCollection().get()?.onTime?.toLocalDateTime(TimeZone.UTC) - ?.time.toString().split(".")[0] - } ${UptimeCollection().get()?.onTime?.toLocalDateTime(TimeZone.UTC)?.date} UTC\n " + - "(${UptimeCollection().get()?.onTime?.toDiscord(TimestampType.RelativeTime) ?: "??"})" - inline = true - } - field { - name = "Useful links" - value = - "Website: Coming Soon™️\n" + - "GitHub: ${HYACINTH_GITHUB}\n" + - "Buy Me a Coffee: https://buymeacoffee.com/HyacinthBots\n" + - "Twitter: https://twitter.com/HyacinthBots\n" + - "Email: `hyacinthbots@outlook.com`\n" + - "Discord: https://discord.gg/hy2329fcTZ" - } - color = DISCORD_BLURPLE - } - - buttons() - } - } - } - /** * A command that responds to the user with a link to invite Lily to their server. * diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/NewsChannelPublishing.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/NewsChannelPublishing.kt similarity index 88% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/NewsChannelPublishing.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/NewsChannelPublishing.kt index 936bf597..7f79935d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/NewsChannelPublishing.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/NewsChannelPublishing.kt @@ -1,19 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.DISCORD_YELLOW -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.channelType -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.channel -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.pagination.EphemeralResponsePaginator -import com.kotlindiscord.kord.extensions.pagination.pages.Page -import com.kotlindiscord.kord.extensions.pagination.pages.Pages +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.Locale import dev.kord.common.asJavaLocale import dev.kord.common.entity.ChannelType @@ -23,6 +9,20 @@ import dev.kord.core.behavior.channel.asChannelOfOrNull import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.entity.channel.NewsChannel import dev.kord.core.event.message.MessageCreateEvent +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.channelType +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.channel +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.event +import dev.kordex.core.pagination.EphemeralResponsePaginator +import dev.kordex.core.pagination.pages.Page +import dev.kordex.core.pagination.pages.Pages import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.NewsChannelPublishingCollection import org.hyacinthbots.lilybot.extensions.config.ConfigOptions diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/PublicUtilities.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/PublicUtilities.kt similarity index 89% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/PublicUtilities.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/PublicUtilities.kt index b9dd03e3..9b51cc7e 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/PublicUtilities.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/PublicUtilities.kt @@ -1,21 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.DISCORD_YELLOW -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand -import com.kotlindiscord.kord.extensions.utils.dm -import com.kotlindiscord.kord.extensions.utils.getTopRole -import com.kotlindiscord.kord.extensions.utils.hasPermission +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Permission import dev.kord.core.behavior.channel.createEmbed @@ -26,6 +10,22 @@ import dev.kord.core.entity.Message import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.rest.builder.message.embed import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.guildFor +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.publicSlashCommand +import dev.kordex.core.utils.dm +import dev.kordex.core.utils.getTopRole +import dev.kordex.core.utils.hasPermission import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection import org.hyacinthbots.lilybot.extensions.config.ConfigOptions @@ -281,7 +281,7 @@ class PublicUtilities : Extension() { } } } - } catch (e: KtorRequestException) { + } catch (_: KtorRequestException) { // Avoid hard failing on permission error, since the public won't know what it means respond { content = "Error sending message to moderators. Please ask the moderators to check" + diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Reminders.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Reminders.kt similarity index 91% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Reminders.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Reminders.kt index a212ee6d..99aa2d62 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Reminders.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Reminders.kt @@ -1,31 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.application.slash.publicSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.boolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingDuration -import com.kotlindiscord.kord.extensions.commands.converters.impl.coalescingOptionalDuration -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.long -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString -import com.kotlindiscord.kord.extensions.commands.converters.impl.user -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand -import com.kotlindiscord.kord.extensions.pagination.EphemeralResponsePaginator -import com.kotlindiscord.kord.extensions.pagination.pages.Page -import com.kotlindiscord.kord.extensions.pagination.pages.Pages -import com.kotlindiscord.kord.extensions.time.TimestampType -import com.kotlindiscord.kord.extensions.time.toDiscord -import com.kotlindiscord.kord.extensions.utils.botHasPermissions -import com.kotlindiscord.kord.extensions.utils.dm -import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler -import com.kotlindiscord.kord.extensions.utils.scheduling.Task -import com.kotlindiscord.kord.extensions.utils.toDuration +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions import dev.kord.common.entity.Snowflake @@ -38,6 +12,32 @@ import dev.kord.core.event.interaction.ChatInputCommandInteractionCreateEvent import dev.kord.rest.builder.message.create.MessageCreateBuilder import dev.kord.rest.builder.message.embed import dev.kord.rest.request.KtorRequestException +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.guildFor +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.converters.impl.stringChoice +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.application.slash.publicSubCommand +import dev.kordex.core.commands.converters.impl.boolean +import dev.kordex.core.commands.converters.impl.coalescingDuration +import dev.kordex.core.commands.converters.impl.coalescingOptionalDuration +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.long +import dev.kordex.core.commands.converters.impl.optionalString +import dev.kordex.core.commands.converters.impl.user +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.publicSlashCommand +import dev.kordex.core.pagination.EphemeralResponsePaginator +import dev.kordex.core.pagination.pages.Page +import dev.kordex.core.pagination.pages.Pages +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import dev.kordex.core.utils.botHasPermissions +import dev.kordex.core.utils.dm +import dev.kordex.core.utils.scheduling.Scheduler +import dev.kordex.core.utils.scheduling.Task +import dev.kordex.core.utils.toDuration import kotlinx.datetime.Clock import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.TimeZone @@ -384,7 +384,7 @@ class Reminders : Extension() { } } - ReminderCollection().removeReminder(user.id, arguments.reminder) + ReminderCollection().removeReminder(arguments.user.id, arguments.reminder) markReminderCompleteOrCancelled( reminder.guildId, reminder.channelId, reminder.messageId, wasCancelled = true, diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/RoleMenu.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/RoleMenu.kt similarity index 93% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/RoleMenu.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/RoleMenu.kt index 4f83ff5e..5ce716c2 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/RoleMenu.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/RoleMenu.kt @@ -1,26 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_BLACK -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.checks.hasPermissions -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext -import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingBoolean -import com.kotlindiscord.kord.extensions.commands.converters.impl.defaultingColor -import com.kotlindiscord.kord.extensions.commands.converters.impl.role -import com.kotlindiscord.kord.extensions.commands.converters.impl.snowflake -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.components.components -import com.kotlindiscord.kord.extensions.components.ephemeralButton -import com.kotlindiscord.kord.extensions.components.ephemeralStringSelectMenu -import com.kotlindiscord.kord.extensions.components.linkButton -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.utils.getJumpUrl -import com.kotlindiscord.kord.extensions.utils.getTopRole +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.entity.ButtonStyle import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions @@ -35,6 +14,27 @@ import dev.kord.core.entity.Message import dev.kord.core.entity.Role import dev.kord.core.event.interaction.GuildButtonInteractionCreateEvent import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_BLACK +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.checks.hasPermissions +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.EphemeralSlashCommandContext +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.defaultingColor +import dev.kordex.core.commands.converters.impl.role +import dev.kordex.core.commands.converters.impl.snowflake +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.components.components +import dev.kordex.core.components.ephemeralButton +import dev.kordex.core.components.ephemeralStringSelectMenu +import dev.kordex.core.components.linkButton +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.event +import dev.kordex.core.utils.getJumpUrl +import dev.kordex.core.utils.getTopRole import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.toList @@ -100,13 +100,13 @@ class RoleMenu : Extension() { } // While we don't normally edit in components, in this case we need the message ID. - menuMessage!!.edit { + menuMessage.edit { val components = components { ephemeralButton { label = "Select roles" style = ButtonStyle.Primary - id = "role-menu${menuMessage!!.id}" + id = "role-menu${menuMessage.id}" action { } } @@ -116,7 +116,7 @@ class RoleMenu : Extension() { } RoleMenuCollection().setRoleMenu( - menuMessage!!.id, + menuMessage.id, channel.id, guild!!.id, mutableListOf(arguments.initialRole.id) @@ -154,7 +154,7 @@ class RoleMenu : Extension() { components { linkButton { label = "Jump to role menu" - url = menuMessage!!.getJumpUrl() + url = menuMessage.getJumpUrl() } } } @@ -371,7 +371,7 @@ class RoleMenu : Extension() { roles.add(newRole.id) } else { - utilsLogger.debug("skipped creating new roles") + utilsLogger.debug { "skipped creating new roles" } roles.add(existingRole.id) } } @@ -707,7 +707,7 @@ class RoleMenu : Extension() { return@action } - if (!config!!.subscribableRoles.contains(arguments.role.id)) { + if (!config.subscribableRoles.contains(arguments.role.id)) { respond { content = "That is not a subscribable role." } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StartupHooks.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StartupHooks.kt similarity index 82% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StartupHooks.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StartupHooks.kt index 186b1bcc..3fc602b7 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StartupHooks.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StartupHooks.kt @@ -1,16 +1,16 @@ -package org.hyacinthbots.lilybot.extensions.util +package org.hyacinthbots.lilybot.extensions.utility.commands -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.event -import com.kotlindiscord.kord.extensions.time.TimestampType -import com.kotlindiscord.kord.extensions.time.toDiscord -import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler -import com.kotlindiscord.kord.extensions.utils.scheduling.Task import dev.kord.core.behavior.channel.createEmbed import dev.kord.core.behavior.getChannelOfOrNull import dev.kord.core.entity.channel.NewsChannel import dev.kord.core.event.gateway.ReadyEvent +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import dev.kordex.core.utils.scheduling.Scheduler +import dev.kordex.core.utils.scheduling.Task import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.Cleanups import org.hyacinthbots.lilybot.database.collections.UptimeCollection diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StatusPing.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StatusPing.kt similarity index 70% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StatusPing.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StatusPing.kt index 2422aacd..bfac3ca0 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/StatusPing.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/StatusPing.kt @@ -1,11 +1,11 @@ -package org.hyacinthbots.lilybot.extensions.util +package org.hyacinthbots.lilybot.extensions.utility.commands -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.utils.scheduling.Scheduler -import com.kotlindiscord.kord.extensions.utils.scheduling.Task +import dev.kordex.core.extensions.Extension +import dev.kordex.core.utils.scheduling.Scheduler +import dev.kordex.core.utils.scheduling.Task +import io.github.oshai.kotlinlogging.KotlinLogging import io.ktor.client.HttpClient import io.ktor.client.request.get -import mu.KotlinLogging import org.hyacinthbots.lilybot.utils.ENV import kotlin.time.Duration.Companion.seconds diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Tags.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Tags.kt similarity index 93% rename from src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Tags.kt rename to src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Tags.kt index 61893e06..20c1a808 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/util/Tags.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/commands/Tags.kt @@ -1,24 +1,5 @@ -package org.hyacinthbots.lilybot.extensions.util - -import com.kotlindiscord.kord.extensions.DISCORD_BLURPLE -import com.kotlindiscord.kord.extensions.DISCORD_GREEN -import com.kotlindiscord.kord.extensions.DISCORD_RED -import com.kotlindiscord.kord.extensions.DISCORD_YELLOW -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.optionalStringChoice -import com.kotlindiscord.kord.extensions.commands.application.slash.converters.impl.stringChoice -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalString -import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalUser -import com.kotlindiscord.kord.extensions.commands.converters.impl.string -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand -import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand -import com.kotlindiscord.kord.extensions.pagination.EphemeralResponsePaginator -import com.kotlindiscord.kord.extensions.pagination.pages.Page -import com.kotlindiscord.kord.extensions.pagination.pages.Pages -import com.kotlindiscord.kord.extensions.utils.suggestStringMap +package org.hyacinthbots.lilybot.extensions.utility.commands + import dev.kord.common.Locale import dev.kord.common.asJavaLocale import dev.kord.common.entity.Permission @@ -30,6 +11,25 @@ import dev.kord.core.behavior.getChannelOfOrNull import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.rest.builder.message.EmbedBuilder import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_BLURPLE +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.application.slash.converters.impl.optionalStringChoice +import dev.kordex.core.commands.application.slash.converters.impl.stringChoice +import dev.kordex.core.commands.converters.impl.optionalString +import dev.kordex.core.commands.converters.impl.optionalUser +import dev.kordex.core.commands.converters.impl.string +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.ephemeralSlashCommand +import dev.kordex.core.extensions.publicSlashCommand +import dev.kordex.core.pagination.EphemeralResponsePaginator +import dev.kordex.core.pagination.pages.Page +import dev.kordex.core.pagination.pages.Pages +import dev.kordex.core.utils.suggestStringMap import kotlinx.datetime.Clock import org.hyacinthbots.lilybot.database.collections.TagsCollection import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityArgs.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityArgs.kt new file mode 100644 index 00000000..36c30a76 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityArgs.kt @@ -0,0 +1,29 @@ +package org.hyacinthbots.lilybot.extensions.utility.config + +import dev.kordex.core.commands.Arguments +import dev.kordex.core.commands.converters.impl.defaultingBoolean +import dev.kordex.core.commands.converters.impl.optionalChannel + +class UtilityArgs : Arguments() { + val utilityLogChannel by optionalChannel { + name = "utility-log" + description = "The channel to log various utility actions too." + } + val logChannelUpdates by defaultingBoolean { + name = "log-channel-updates" + description = "Whether to log changes made to channels in this guild." + defaultValue = false + } + val logEventUpdates by defaultingBoolean { + name = "log-event-updates" + description = "Whether to log changes made to scheduled events in this guild." + } + val logInviteUpdates by defaultingBoolean { + name = "log-invite-updates" + description = "Whether to log changes made to invites in this guild." + } + val logRoleUpdates by defaultingBoolean { + name = "log-role-updates" + description = "Whether to log changes made to roles in this guild." + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityCommand.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityCommand.kt new file mode 100644 index 00000000..f3f77feb --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/config/UtilityCommand.kt @@ -0,0 +1,74 @@ +package org.hyacinthbots.lilybot.extensions.utility.config + +import dev.kord.common.entity.Permission +import dev.kord.core.behavior.channel.createMessage +import dev.kord.core.behavior.getChannelOfOrNull +import dev.kord.core.entity.channel.TextChannel +import dev.kord.rest.builder.message.embed +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.commands.application.slash.SlashCommand +import dev.kordex.core.commands.application.slash.ephemeralSubCommand +import dev.kordex.core.utils.botHasPermissions +import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection +import org.hyacinthbots.lilybot.database.entities.UtilityConfigData +import org.hyacinthbots.lilybot.extensions.config.utils.utilityEmbed + +suspend fun SlashCommand<*, *, *>.utilityCommand() = ephemeralSubCommand(::UtilityArgs) { + name = "utility" + description = "Configure Lily's utility settings" + + requirePermission(Permission.ManageGuild) + + check { + anyGuild() + hasPermission(Permission.ManageGuild) + } + + action { + val utilityConfig = UtilityConfigCollection().getConfig(guild!!.id) + + if (utilityConfig != null) { + respond { + content = "You already have a utility configuration set. " + + "Please clear it before attempting to set a new one." + } + return@action + } + + var utilityLog: TextChannel? = null + if (arguments.utilityLogChannel != null) { + utilityLog = guild!!.getChannelOfOrNull(arguments.utilityLogChannel!!.id) + if (utilityLog?.botHasPermissions(Permission.ViewChannel, Permission.SendMessages) != true) { + respond { + content = "The utility log you've selected is invalid, or I can't view it. " + + "Please attempt to resolve this and try again." + } + return@action + } + } + + respond { + embed { + utilityEmbed(arguments, user) + } + } + + UtilityConfigCollection().setConfig( + UtilityConfigData( + guild!!.id, + arguments.utilityLogChannel?.id, + arguments.logChannelUpdates, + arguments.logEventUpdates, + arguments.logInviteUpdates, + arguments.logRoleUpdates + ) + ) + + utilityLog?.createMessage { + embed { + utilityEmbed(arguments, user) + } + } + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/events/UtilityEvents.kt b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/events/UtilityEvents.kt new file mode 100644 index 00000000..fe84294c --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/extensions/utility/events/UtilityEvents.kt @@ -0,0 +1,677 @@ +package org.hyacinthbots.lilybot.extensions.utility.events + +import dev.kord.common.entity.ChannelType +import dev.kord.common.entity.ForumTag +import dev.kord.common.entity.GuildScheduledEventPrivacyLevel +import dev.kord.common.entity.optional.value +import dev.kord.core.behavior.GuildBehavior +import dev.kord.core.behavior.channel.asChannelOfOrNull +import dev.kord.core.behavior.channel.createEmbed +import dev.kord.core.behavior.channel.createMessage +import dev.kord.core.entity.channel.Category +import dev.kord.core.entity.channel.Channel +import dev.kord.core.entity.channel.ForumChannel +import dev.kord.core.event.channel.ChannelCreateEvent +import dev.kord.core.event.channel.ChannelDeleteEvent +import dev.kord.core.event.channel.ChannelUpdateEvent +import dev.kord.core.event.channel.thread.ThreadChannelCreateEvent +import dev.kord.core.event.channel.thread.ThreadChannelDeleteEvent +import dev.kord.core.event.guild.GuildScheduledEventCreateEvent +import dev.kord.core.event.guild.GuildScheduledEventDeleteEvent +import dev.kord.core.event.guild.GuildScheduledEventEvent +import dev.kord.core.event.guild.GuildScheduledEventUpdateEvent +import dev.kord.core.event.guild.InviteCreateEvent +import dev.kord.core.event.guild.InviteDeleteEvent +import dev.kord.core.event.role.RoleCreateEvent +import dev.kord.core.event.role.RoleDeleteEvent +import dev.kord.core.event.role.RoleUpdateEvent +import dev.kord.rest.builder.message.EmbedBuilder +import dev.kord.rest.builder.message.embed +import dev.kordex.core.DISCORD_GREEN +import dev.kordex.core.DISCORD_RED +import dev.kordex.core.DISCORD_YELLOW +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.extensions.Extension +import dev.kordex.core.extensions.event +import dev.kordex.core.time.TimestampType +import dev.kordex.core.time.toDiscord +import kotlinx.datetime.Clock +import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection +import org.hyacinthbots.lilybot.extensions.config.ConfigOptions +import org.hyacinthbots.lilybot.utils.afterDot +import org.hyacinthbots.lilybot.utils.formatPermissionSet +import org.hyacinthbots.lilybot.utils.getLoggingChannelWithPerms +import org.hyacinthbots.lilybot.utils.ifNullOrEmpty +import kotlin.collections.forEach + +/** A String identifier to use for the permission map to get allowed permissions. */ +private const val ALLOWED = "Allowed" + +/** A String identifier to use for the permission map to get denied permissions. */ +private const val DENIED = "Denied" + +class UtilityEvents : Extension() { + override val name: String = "utility-events" + + override suspend fun setup() { + event { + check { + failIf { + // We get more detail from the specific event + event.channel.type != ChannelType.PublicGuildThread || event.channel.type != ChannelType.PrivateThread + } + } + action { + val guildId = event.channel.data.guildId.value + // Do not log if the channel logging option is false + if (guildId?.let { UtilityConfigCollection().getConfig(it)?.logChannelUpdates } == false) return@action + val guild = guildId?.let { GuildBehavior(it, event.kord) } + val perms = formatPermissionsForDisplay(guild, event.channel) + var allowed = perms.getValue(ALLOWED) + var denied = perms.getValue(DENIED) + + if (allowed.isBlank()) allowed = "None overrides set" + if (denied.isBlank()) denied = "None overrides set" + + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!)?.createEmbed { + title = "${writeChannelType(event.channel.type)} Created" + description = "${event.channel.mention} (${event.channel.data.name.value}) was created." + field { + name = "Allowed Permissions" + value = allowed + inline = true + } + field { + name = "Denied Permissions" + value = denied + inline = true + } + timestamp = Clock.System.now() + color = DISCORD_GREEN + } + } + } + event { + action { + val guildId = event.channel.data.guildId.value + // Do not log if the channel logging option is false + if (guildId?.let { UtilityConfigCollection().getConfig(it)?.logChannelUpdates } == false) return@action + val guild = guildId?.let { GuildBehavior(it, event.kord) } + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!)?.createEmbed { + title = "${writeChannelType(event.channel.type)} Deleted" + description = "`${event.channel.data.name.value}` was deleted." + timestamp = Clock.System.now() + color = DISCORD_RED + } + } + } + event { + check { + failIf { + // Data from old threads is almost always null, so we should not try to display changes + event.channel.type != ChannelType.PublicGuildThread || event.channel.type != ChannelType.PrivateThread + } + } + action { + val guildId = event.channel.data.guildId.value + // Do not log if the channel logging option is false + if (guildId?.let { UtilityConfigCollection().getConfig(it)?.logChannelUpdates } == false) return@action + val guild = guildId?.let { GuildBehavior(it, event.kord) } + val oldPerms = formatPermissionsForDisplay(guild, event.old) + val newPerms = formatPermissionsForDisplay(guild, event.channel) + val oldAllowed = oldPerms.getValue(ALLOWED) + val oldDenied = oldPerms.getValue(DENIED) + val newAllowed = newPerms.getValue(ALLOWED) + val newDenied = newPerms.getValue(DENIED) + val oldData = event.old?.data + val newData = event.channel.data + val oldAppliedTags = mutableListOf() + newData.appliedTags.value?.forEach { tag -> + event.old?.asChannelOrNull()?.data?.availableTags?.value?.filter { it.id == tag } + ?.get(0)?.name?.let { oldAppliedTags.add(it) } + } + val newAppliedTags = mutableListOf() + newData.appliedTags.value?.forEach { tag -> + event.channel.asChannelOrNull()?.data?.availableTags?.value?.filter { it.id == tag } + ?.get(0)?.name?.let { newAppliedTags.add(it) } + } + + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild!!)?.createEmbed { + title = "${writeChannelType(event.channel.type)} Updated" + oldNewEmbedField( + "Type Change", writeChannelType(event.old?.type), writeChannelType(event.channel.type) + ) + oldNewEmbedField("Name Change", oldData?.name?.value, newData.name.value) + oldNewEmbedField("Topic Changed", oldData?.topic?.value, newData.topic.value) + oldNewEmbedField( + "Parent Category Change", + kord.getChannelOf(oldData?.parentId.value!!)?.mention, + kord.getChannelOf(newData.parentId.value!!)?.mention + ) + if (event.channel.data.nsfw != event.old?.data?.nsfw) { + field { + name = "NSFW Setting" + value = event.channel.data.nsfw.discordBoolean.toString() + } + } + oldNewEmbedField("Position Changed", oldData?.position.value, newData.position.value) + oldNewEmbedField( + "Slowmode time changed", + oldData?.rateLimitPerUser?.value?.toString() ?: "0", + newData.rateLimitPerUser.value?.toString() ?: "0" + ) + oldNewEmbedField("Bitrate changed", oldData?.bitrate.value, newData.bitrate.value) + oldNewEmbedField("User limit changed", oldData?.userLimit.value ?: 0, newData.userLimit.value ?: 0) + oldNewEmbedField( + "Region Changed", + oldData?.rtcRegion?.value ?: "Automatic", + newData.rtcRegion.value ?: "Automatic" + ) + oldNewEmbedField( + "Video Quality Changed", + oldData?.videoQualityMode?.value.afterDot(), + newData.videoQualityMode.value.afterDot() + ) + oldNewEmbedField( + "Default Auto-Archive Duration", + oldData?.defaultAutoArchiveDuration?.value?.duration.toString(), + newData.defaultAutoArchiveDuration.value?.duration.toString() + ) + oldNewEmbedField( + "Default Sort Changed", + oldData?.defaultSortOrder?.value.afterDot(), + newData.defaultSortOrder.value.afterDot() + ) + oldNewEmbedField( + "Default Layout Changed", + oldData?.defaultForumLayout?.value.afterDot(), + newData.defaultForumLayout.value.afterDot() + ) + oldNewEmbedField( + "Available tags Changed", + formatAvailableTags(oldData?.availableTags?.value), + formatAvailableTags(newData.availableTags.value) + ) + oldNewEmbedField( + "Applied tags Changed", + oldAppliedTags.joinToString(", "), + newAppliedTags.joinToString(", ") + ) + oldNewEmbedField( + "Default Reaction Emoji Changed", + oldData?.defaultReactionEmoji?.value?.emojiName, + newData.defaultReactionEmoji.value?.emojiName + ) + oldNewEmbedField( + "Default Thread Slowmode Changed", + oldData?.defaultThreadRateLimitPerUser?.value.toString(), + newData.defaultThreadRateLimitPerUser.value.toString() + ) + if (oldAllowed != newAllowed) { + field { + name = "New Allowed Permissions" + value = newAllowed + inline = true + } + field { + name = "Old Allowed Permissions" + value = oldAllowed + inline = true + } + } + if (oldDenied != newDenied) { + field { + name = "New Denied Permissions" + value = newDenied + inline = false + } + field { + name = "Old Denied Permissions" + value = oldDenied + inline = true + } + } + color = DISCORD_YELLOW + timestamp = Clock.System.now() + } + } + } + event { + check { anyGuild() } + action { + // Do not log if event updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logEventUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Scheduled Event Created!" + description = "An event has been created!" + guildEventEmbed(event, guild) + // This appears to exist in the front end but the api doesn't have it anywhere, api payloads contain + // a recurrence field that would fill this but the api doesn't mention it +// field { +// name = "Repeating Frequency" +// value = event.scheduledEvent. +// } + field { + name = "Guild Members only" + value = if (event.scheduledEvent.privacyLevel == GuildScheduledEventPrivacyLevel.GuildOnly) { + "True" + } else { + "False" + } + } + color = DISCORD_GREEN + footer { + text = + "Created by ${event.scheduledEvent.creatorId?.let { guild.getMemberOrNull(it) }?.username}" + icon = + event.scheduledEvent.creatorId?.let { guild.getMemberOrNull(it) }?.avatar?.cdnUrl?.toUrl() + } + } + } + } + event { + check { anyGuild() } + action { + // Do not log if event updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logEventUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Scheduled Event Deleted!" + description = "An event has been deleted" + guildEventEmbed(event, guild) + color = DISCORD_RED + footer { + text = + "Originally created by ${event.scheduledEvent.creatorId?.let { guild.getMemberOrNull(it) }?.username}" + icon = + event.scheduledEvent.creatorId?.let { guild.getMemberOrNull(it) }?.avatar?.cdnUrl?.toUrl() + } + } + } + } + event { + check { anyGuild() } + action { + // Do not log if event updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logEventUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + val oldEvent = event.oldEvent + val newEvent = event.scheduledEvent + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Scheduled Event Updated!" + description = "An event has been updated" + oldNewEmbedField("Name Changed", oldEvent?.name, newEvent.name) + oldNewEmbedField("Description Changed", oldEvent?.description, newEvent.description) + oldNewEmbedField( + "Location Changed", + oldEvent?.channelId?.let { guild.getChannelOrNull(it) }?.mention ?: "Unable to get channel", + newEvent.channelId?.let { guild.getChannelOrNull(it) }?.mention ?: "Unable to get channel" + ) + oldNewEmbedField( + "Start time changed", + oldEvent?.scheduledStartTime?.toDiscord(TimestampType.ShortDateTime), + newEvent.scheduledStartTime.toDiscord(TimestampType.ShortDateTime) + ) + color = DISCORD_YELLOW + } + } + } + event { + check { anyGuild() } + action { + // Do not log if invite updates are disabled + if (event.guildId?.let { UtilityConfigCollection().getConfig(it) }?.logInviteUpdates == false) return@action + val guild = event.guildId?.let { GuildBehavior(it, kord) } ?: return@action + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Invite link created" + description = "An invite has been created" + field { + name = "Code" + value = event.code + } + field { + name = "Target Channel" + value = event.channel.mention + } + field { + name = "Max uses" + value = event.maxUses.toString() + } + field { + name = "Duration of Invite" + value = event.maxAge.toIsoString() + } + field { + name = "Temporary Membership invite" + value = event.isTemporary.toString() + } + footer { + text = "Created by ${event.getInviterAsMemberOrNull()?.mention}" + icon = event.getInviterAsMemberOrNull()?.avatar?.cdnUrl?.toUrl() + } + timestamp = Clock.System.now() + color = DISCORD_GREEN + } + } + } + event { + check { anyGuild() } + action { + // Do not log if invite updates are disabled + if (event.guildId?.let { UtilityConfigCollection().getConfig(it) }?.logInviteUpdates == false) return@action + val guild = event.guildId?.let { GuildBehavior(it, kord) } ?: return@action + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Invite link deleted" + description = "An invite has been deleted" + field { + name = "Code" + value = event.code + } + field { + name = "Target Channel" + value = event.channel.mention + } + timestamp = Clock.System.now() + color = DISCORD_RED + } + } + } + event { + action { + // Do not log if role updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logRoleUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Created a Role" + description = "A new role has been created" + field { + name = "Role name" + value = event.role.name + inline = true + } + field { + name = "Role Color" + value = event.role.color.rgb.toString() + inline = true + } + field { + name = "Position" + value = event.role.rawPosition.toString() + inline = true + } + field { + name = "Display separately?" + value = event.role.hoisted.toString() + inline = true + } + field { + name = "Mentionable" + value = event.role.mentionable.toString() + inline = true + } + field { + name = "Icon" + value = event.role.icon?.cdnUrl?.toUrl() ?: "No icon" + inline = true + } + field { + name = "Emoji" + value = event.role.unicodeEmoji ?: "No emoji" + inline = true + } + field { + name = "Managed by integration?" + value = event.role.managed.toString() + inline = true + } + field { + name = "Permissions" + value = formatPermissionSet(event.role.permissions) + } + color = DISCORD_GREEN + timestamp = Clock.System.now() + } + } + } + event { + action { + // Do not log if role updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logRoleUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild)?.createEmbed { + title = "Role deleted" + description = "A role has been deleted" + field { + name = "Role name" + value = event.role?.name ?: "Unable to get name" + } + color = DISCORD_RED + timestamp = Clock.System.now() + } + } + } + event { + action { + // Do not log if role updates are disabled + if (UtilityConfigCollection().getConfig(event.guildId)?.logRoleUpdates == false) return@action + val guild = GuildBehavior(event.guildId, kord) + val channel = getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, guild) + channel?.createMessage { + embed { + title = "Updated a Role" + description = "A role has been updated" + oldNewEmbedField("Name changed", event.old?.name, event.role.name) + oldNewEmbedField("Display separately setting changed", event.old?.hoisted, event.role.hoisted) + oldNewEmbedField("Mentionable setting changed", event.old?.mentionable, event.role.mentionable) + oldNewEmbedField("Position changed", event.old?.getPosition(), event.role.getPosition()) + oldNewEmbedField( + "Icon changed", + event.old?.icon?.cdnUrl?.toUrl() ?: "No icon", + event.role.icon?.cdnUrl?.toUrl() ?: "No icon" + ) + oldNewEmbedField( + "Emoji changed", event.old?.unicodeEmoji ?: "No icon", event.role.unicodeEmoji ?: "No icon" + ) + oldNewEmbedField( + "Permissions changed", + event.old?.permissions?.let { formatPermissionSet(it) } ?: "Unable to get permissions", + formatPermissionSet(event.role.permissions) + ) + color = DISCORD_GREEN + timestamp = Clock.System.now() + } + if (event.old?.color != event.role.color) { + embed { + description = "Old color" + color = if (event.old?.color?.rgb != 0) event.old?.color else null + } + embed { + description = "New color" + color = event.role.color + } + } + } + } + } + event { + check { anyGuild() } + action { + // Do not log if the channel logging option is false + if (UtilityConfigCollection().getConfig(event.channel.guild.id)?.logChannelUpdates == false) return@action + val appliedTags = mutableListOf() + event.channel.appliedTags.forEach { tag -> + event.channel.parent.asChannelOfOrNull() + ?.availableTags?.filter { it.id == tag }?.get(0)?.name?.let { appliedTags.add(it) } + } + + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, event.channel.guild)?.createEmbed { + title = "${writeChannelType(event.channel.type)} Created" + description = "${event.channel.mention} (${event.channel.data.name.value}) was created." + field { + name = "Parent Channel" + value = "${event.channel.parent.mention} (`${event.channel.parent.asChannelOrNull()?.name}`)" + } + field { + name = "Archive duration" + value = event.channel.autoArchiveDuration.duration.toString() + } + if (appliedTags.isNotEmpty()) { + field { + name = "Applied tags" + value = appliedTags.joinToString(", ") + } + } + timestamp = Clock.System.now() + color = DISCORD_GREEN + } + } + } + event { + check { anyGuild() } + action { + // Do not log if the channel logging option is false + if (UtilityConfigCollection().getConfig(event.channel.guild.id)?.logChannelUpdates == false) return@action + getLoggingChannelWithPerms(ConfigOptions.UTILITY_LOG, event.channel.guild)?.createEmbed { + title = "${writeChannelType(event.channel.type)} Deleted" + description = "`${event.channel.data.name.value}` was deleted." + timestamp = Clock.System.now() + color = DISCORD_RED + } + } + } + // No thread update event because the old object is almost always null, meaning that displaying the changes is + // effectively pointless because no original values are available. + } + + /** + * Compare two values to see if they've changed and format an embed field indicating the old value versus the new + * value. + * + * @param detailName The title for the embed field, the information on what has changed + * @param oldValue The original value + * @param newValue The new value + * @author NoComment1105 + * @since 5.0.0 + */ + private fun EmbedBuilder.oldNewEmbedField(detailName: String, oldValue: String?, newValue: String?) { + if (newValue != oldValue) { + field { + name = detailName + value = "Old: $oldValue\nNew: $newValue" + } + } + } + + /** + * A version of [oldNewEmbedField] that takes integers and converts them itself. + * + * @see oldNewEmbedField + * @author NoComment1105 + * @since 5.0.0 + */ + private fun EmbedBuilder.oldNewEmbedField(detailName: String, oldValue: Int?, newValue: Int?) = + oldNewEmbedField(detailName, oldValue.toString(), newValue.toString()) + + /** + * A version of [oldNewEmbedField] that takes booleans and converts them itself. + * + * @see oldNewEmbedField + * @author NoComment1105 + * @since 5.0.0 + */ + private fun EmbedBuilder.oldNewEmbedField(detailName: String, oldValue: Boolean?, newValue: Boolean?) = + oldNewEmbedField(detailName, oldValue.toString(), newValue.toString()) + + /** + * Writes a [ChannelType] into a String to use as a reasonable title. + * + * @param type The type of the channel + * @return A String for the channel title + * @author NoComment1105 + * @since 5.0.0 + */ + private fun writeChannelType(type: ChannelType?): String? = when (type) { + ChannelType.GuildCategory -> "Category" + ChannelType.GuildNews -> "Announcement Channel" + ChannelType.GuildForum -> "Forum Channel" + ChannelType.GuildStageVoice -> "Stage Channel" + ChannelType.GuildText -> "Text Channel" + ChannelType.GuildVoice -> "Voice Channel" + ChannelType.PublicGuildThread -> "Thread" + ChannelType.PrivateThread -> "Private Thread" + else -> null + } + + /** + * Formats the permission overwrites for a [channel] to a string map of allowed and denied permissions. + * + * @param guild The guild the channel is in + * @param channel The channel object to get the permissions for + * @return A [Map] of strings for allowed and denied permissions + * @author NoComment1105 + * @since 5.0.0 + */ + private suspend inline fun formatPermissionsForDisplay( + guild: GuildBehavior?, + channel: Channel? + ): Map { + val map = mutableMapOf() + map[ALLOWED] = "" + map[DENIED] = "" + channel?.data?.permissionOverwrites?.value?.forEach { + map[ALLOWED] += "${guild!!.getRoleOrNull(it.id)?.mention}: ${formatPermissionSet(it.allow)}\n" + map[DENIED] += "${guild.getRoleOrNull(it.id)?.mention}: ${formatPermissionSet(it.deny)}\n" + } + return map + } + + /** + * Formats the Available tags ([ForumTag]) into a readable bullet pointed display for the update embed. + * + * @param tagList A List of [ForumTag]s to format + * @return The formated string from [tagList] + * @author NoComment1105 + * @since 5.0.0 + */ + private fun formatAvailableTags(tagList: List?): String { + var tagString = "" + tagList?.forEach { + tagString += "\n* Name: ${it.name}\n* Moderated: ${it.moderated}\n" + + "* Emoji: ${if (it.emojiId != null) "" else it.emojiName}\n---" + } + return tagString.ifNullOrEmpty { "None" } + } + + /** + * Fills out the content for Guild event updates to avoid repeating code. + * + * @param event The event instance + * @param guild The guild for the event + * @author NoComment1105 + * @since 5.0.0 + */ + private suspend fun EmbedBuilder.guildEventEmbed(event: GuildScheduledEventEvent, guild: GuildBehavior) { + field { + name = "Event Name" + value = event.scheduledEvent.name + } + field { + name = "Event Description" + value = event.scheduledEvent.description ?: "No description provided" + } + field { + name = "Event location" + value = if (event.scheduledEvent.channelId != null) { + guild.getChannelOrNull(event.scheduledEvent.channelId!!)?.mention ?: "Unable to get channel" + } else { + "External event, no channel." + } + } + field { + name = "Start time" + value = event.scheduledEvent.scheduledStartTime.toDiscord(TimestampType.ShortDateTime) + } + image = event.scheduledEvent.image?.cdnUrl?.toUrl().ifNullOrEmpty { "No image" } + } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/utils/ResponseHelper.kt b/src/main/kotlin/org/hyacinthbots/lilybot/utils/ResponseHelper.kt index d79f7d77..5c86d07b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/utils/ResponseHelper.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/utils/ResponseHelper.kt @@ -1,11 +1,11 @@ package org.hyacinthbots.lilybot.utils -import com.kotlindiscord.kord.extensions.modules.extra.pluralkit.api.PKMessage import dev.kord.core.behavior.UserBehavior import dev.kord.core.entity.Guild import dev.kord.core.entity.Message import dev.kord.core.entity.User import dev.kord.rest.builder.message.EmbedBuilder +import dev.kordex.modules.pluralkit.api.PKMessage /** * This is the base moderation embed for all moderation actions. This should be posted to the action log of a guild. @@ -17,7 +17,7 @@ import dev.kord.rest.builder.message.EmbedBuilder * @author NoComment1105 * @since 3.0.0 */ -suspend inline fun EmbedBuilder.baseModerationEmbed(reason: String?, targetUser: User?, commandUser: UserBehavior) { +suspend inline fun EmbedBuilder.baseModerationEmbed(reason: String?, targetUser: User?, commandUser: UserBehavior?) { field { name = "User:" value = "${targetUser?.username ?: "Unable to get user"}\n${targetUser?.id ?: ""}" @@ -28,9 +28,11 @@ suspend inline fun EmbedBuilder.baseModerationEmbed(reason: String?, targetUser: value = reason ?: "No reason provided" inline = false } - footer { - text = "Requested by ${commandUser.asUserOrNull()?.username}" - icon = commandUser.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + if (commandUser != null) { + footer { + text = "Requested by ${commandUser.asUserOrNull()?.username}" + icon = commandUser.asUserOrNull()?.avatar?.cdnUrl?.toUrl() + } } } @@ -57,6 +59,28 @@ fun EmbedBuilder.dmNotificationStatusEmbedField(dm: Message?, override: Boolean) } } +/** + * This function uses a success variable and checks to see if it succeeded in sending the user a DM. + * + * @param success Whether the DM was a success or not + * @param override Whether the DM was forcefully disabled by the command runner. + * @author NoComment1105 + * @since 5.0.0 + */ +fun EmbedBuilder.dmNotificationStatusEmbedField(success: Boolean?, override: Boolean?) { + field { + name = "User Notification:" + value = if (success != null && success) { + "User notified with direct message" + } else if (override != null && !override) { + "DM Notification Disabled" + } else { + "Failed to notify user with direct message" + } + inline = false + } +} + /** * This function removed duplicated code from MessageDelete and MessageEdit. * It holds attachment and PluralKit info fields for the logging embeds. diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_ConfigUtils.kt b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_ConfigUtils.kt index aac26629..2ea5b0e6 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_ConfigUtils.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_ConfigUtils.kt @@ -1,8 +1,8 @@ package org.hyacinthbots.lilybot.utils -import com.kotlindiscord.kord.extensions.checks.guildFor -import com.kotlindiscord.kord.extensions.checks.types.CheckContext import dev.kord.common.entity.Snowflake +import dev.kordex.core.checks.guildFor +import dev.kordex.core.checks.types.CheckContext import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.database.collections.UtilityConfigCollection @@ -28,15 +28,29 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO fail("There are no config options provided in the code. Please inform the developers immediately!") } + val moderationConfig = ModerationConfigCollection().getConfig(guildFor(event)!!.id) + if (moderationConfig == null) { + fail("Unable to access moderation config for this guild! Please inform a member of staff.") + return + } + + val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) + if (loggingConfig == null) { + fail("Unable to access logging config for this guild! Please inform a member of staff.") + return + } + + val utilityConfig = UtilityConfigCollection().getConfig(guildFor(event)!!.id) + if (utilityConfig == null) { + fail("Unable to access logging config for this guild! Please inform a member of staff.") + return + } + // Look at the config options and check the presence of the config in the database. for (option in configOptions) { when (option) { ConfigOptions.MODERATION_ENABLED -> { - val moderationConfig = ModerationConfigCollection().getConfig(guildFor(event)!!.id) - if (moderationConfig == null) { - fail("Unable to access moderation config for this guild! Please inform a member of staff.") - break - } else if (!moderationConfig.enabled) { + if (!moderationConfig.enabled) { fail("Moderation is disabled for this guild!") break } else { @@ -45,11 +59,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MODERATOR_ROLE -> { - val moderationConfig = ModerationConfigCollection().getConfig(guildFor(event)!!.id) - if (moderationConfig == null) { - fail("Unable to access moderation config for this guild! Please inform a member of staff.") - break - } else if (moderationConfig.role == null) { + if (moderationConfig.role == null) { fail("A moderator role has not been set for this guild!") break } else { @@ -58,11 +68,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.ACTION_LOG -> { - val moderationConfig = ModerationConfigCollection().getConfig(guildFor(event)!!.id) - if (moderationConfig == null) { - fail("Unable to access moderation config for this guild! Please inform a member of staff.") - break - } else if (moderationConfig.channel == null) { + if (moderationConfig.channel == null) { fail("An action log has not been set for this guild!") break } else { @@ -71,11 +77,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.LOG_PUBLICLY -> { - val moderationConfig = ModerationConfigCollection().getConfig(guildFor(event)!!.id) - if (moderationConfig == null) { - fail("Unable to access moderation config for this guild! Please inform a member of staff.") - break - } else if (moderationConfig.publicLogging == null) { + if (moderationConfig.publicLogging == null) { fail("Public logging has not been enabled for this guild!") break } else { @@ -84,11 +86,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MESSAGE_DELETE_LOGGING_ENABLED -> { - val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) - if (loggingConfig == null) { - fail("Unable to access logging config for this guild! Please inform a member of staff.") - break - } else if (!loggingConfig.enableMessageDeleteLogs) { + if (!loggingConfig.enableMessageDeleteLogs) { fail("Message delete logging is disabled for this guild!") break } else { @@ -97,11 +95,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MESSAGE_EDIT_LOGGING_ENABLED -> { - val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) - if (loggingConfig == null) { - fail("Unable to access logging config for this guild! Please inform a member of staff.") - break - } else if (!loggingConfig.enableMessageEditLogs) { + if (!loggingConfig.enableMessageEditLogs) { fail("Message edit logging is disabled for this guild!") break } else { @@ -110,11 +104,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MESSAGE_LOG -> { - val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) - if (loggingConfig == null) { - fail("Unable to access logging config for this guild! Please inform a member of staff.") - break - } else if (loggingConfig.messageChannel == null) { + if (loggingConfig.messageChannel == null) { fail("A message log has not been set for this guild!") break } else { @@ -123,11 +113,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MEMBER_LOGGING_ENABLED -> { - val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) - if (loggingConfig == null) { - fail("Unable to access logging config for this guild! Please inform a member of staff.") - break - } else if (!loggingConfig.enableMemberLogs) { + if (!loggingConfig.enableMemberLogs) { fail("Member logging is disabled for this guild!") break } else { @@ -136,11 +122,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.MEMBER_LOG -> { - val loggingConfig = LoggingConfigCollection().getConfig(guildFor(event)!!.id) - if (loggingConfig == null) { - fail("Unable to access logging config for this guild! Please inform a member of staff.") - break - } else if (loggingConfig.memberLog == null) { + if (loggingConfig.memberLog == null) { fail("A member log has not been set for this guild") break } else { @@ -149,11 +131,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO } ConfigOptions.UTILITY_LOG -> { - val utilityConfig = UtilityConfigCollection().getConfig(guildFor(event)!!.id) - if (utilityConfig == null) { - fail("Unable to access utility config for this guild! Please inform a member of staff.") - break - } else if (utilityConfig.utilityLogChannel == null) { + if (utilityConfig.utilityLogChannel == null) { fail("A utility log has not been set for this guild") break } else { @@ -174,7 +152,7 @@ suspend inline fun CheckContext<*>.requiredConfigs(vararg configOptions: ConfigO */ suspend inline fun configIsUsable(guildId: Snowflake, option: ConfigOptions): Boolean { when (option) { - ConfigOptions.MODERATION_ENABLED -> return ModerationConfigCollection().getConfig(guildId)?.enabled ?: false + ConfigOptions.MODERATION_ENABLED -> return ModerationConfigCollection().getConfig(guildId)?.enabled == true ConfigOptions.MODERATOR_ROLE -> { val moderationConfig = ModerationConfigCollection().getConfig(guildId) ?: return false @@ -192,18 +170,17 @@ suspend inline fun configIsUsable(guildId: Snowflake, option: ConfigOptions): Bo } ConfigOptions.MESSAGE_DELETE_LOGGING_ENABLED -> - return LoggingConfigCollection().getConfig(guildId)?.enableMessageDeleteLogs ?: false + return LoggingConfigCollection().getConfig(guildId)?.enableMessageDeleteLogs == true ConfigOptions.MESSAGE_EDIT_LOGGING_ENABLED -> - return LoggingConfigCollection().getConfig(guildId)?.enableMessageEditLogs ?: false + return LoggingConfigCollection().getConfig(guildId)?.enableMessageEditLogs == true ConfigOptions.MESSAGE_LOG -> { val loggingConfig = LoggingConfigCollection().getConfig(guildId) ?: return false return loggingConfig.messageChannel != null } - ConfigOptions.MEMBER_LOGGING_ENABLED -> return LoggingConfigCollection().getConfig(guildId)?.enableMemberLogs - ?: false + ConfigOptions.MEMBER_LOGGING_ENABLED -> return LoggingConfigCollection().getConfig(guildId)?.enableMemberLogs == true ConfigOptions.MEMBER_LOG -> { val loggingConfig = LoggingConfigCollection().getConfig(guildId) ?: return false diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Constants.kt b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Constants.kt index 0bcc99a4..33d960a4 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Constants.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Constants.kt @@ -1,8 +1,8 @@ package org.hyacinthbots.lilybot.utils -import com.kotlindiscord.kord.extensions.utils.env -import com.kotlindiscord.kord.extensions.utils.envOrNull import dev.kord.common.entity.Snowflake +import dev.kordex.core.utils.env +import dev.kordex.core.utils.envOrNull /** The Bot Token. */ val BOT_TOKEN = env("TOKEN") diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_PermissionUtils.kt b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_PermissionUtils.kt index a6a7fab3..bbb4e912 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_PermissionUtils.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_PermissionUtils.kt @@ -1,13 +1,5 @@ package org.hyacinthbots.lilybot.utils -import com.kotlindiscord.kord.extensions.checks.anyGuild -import com.kotlindiscord.kord.extensions.checks.channelFor -import com.kotlindiscord.kord.extensions.checks.hasPermission -import com.kotlindiscord.kord.extensions.checks.types.CheckContext -import com.kotlindiscord.kord.extensions.checks.types.CheckContextWithCache -import com.kotlindiscord.kord.extensions.types.EphemeralInteractionContext -import com.kotlindiscord.kord.extensions.utils.botHasPermissions -import com.kotlindiscord.kord.extensions.utils.getTopRole import dev.kord.common.entity.Permission import dev.kord.common.entity.Permissions import dev.kord.core.Kord @@ -20,6 +12,14 @@ import dev.kord.core.entity.channel.GuildMessageChannel import dev.kord.core.entity.channel.NewsChannel import dev.kord.core.entity.channel.TextChannel import dev.kord.core.entity.channel.thread.ThreadChannel +import dev.kordex.core.checks.anyGuild +import dev.kordex.core.checks.channelFor +import dev.kordex.core.checks.hasPermission +import dev.kordex.core.checks.types.CheckContext +import dev.kordex.core.checks.types.CheckContextWithCache +import dev.kordex.core.types.EphemeralInteractionContext +import dev.kordex.core.utils.botHasPermissions +import dev.kordex.core.utils.getTopRole import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection @@ -131,18 +131,7 @@ suspend inline fun CheckContext<*>.botHasChannelPerms(permissions: Permissions) val eventChannel = channelFor(event)?.asChannelOrNull() ?: return - val permissionsSet: MutableSet = mutableSetOf() - var count = 0 - permissions.values.forEach { _ -> - permissionsSet.add( - permissions.values.toString() - .split(",")[count] - .split(".")[1] - .replace("[", "`") - .replace("]", "`") - ) - count++ - } + val permissionsSet: String = formatPermissionSet(permissions) /* Use `TextChannel` when the channel is a Text channel */ if (eventChannel is TextChannel) { @@ -187,7 +176,7 @@ suspend inline fun CheckContext<*>.botHasChannelPerms(permissions: Permissions) * point the check fails, null is returned. This should be handled with an elvis operator to return the action in the * code. * - * @param kord The kord instance so the self of the bot can be gotten if needed + * @param kord The kord instance so the self of the bot can be got if needed * @param user The target user in the command * @param guild The guild the command was run in * @param commandName The name of the command. Used for the responses and error message @@ -268,3 +257,26 @@ suspend fun CheckContextWithCache<*>.modCommandChecks(actionPermission: Permissi requiredConfigs(ConfigOptions.MODERATION_ENABLED) hasPermission(actionPermission) } + +/** + * Formats [Permissions] into a readable string list, returning "None" if there are no permissions there. + * + * @param permissions The [Permissions] to format + * @return A string containing the permissions + * @author NoComment1105 + * @since 5.0.0 + */ +fun formatPermissionSet(permissions: Permissions): String { + val permissionsSet: MutableSet = mutableSetOf() + var count = 0 + permissions.values.forEach { _ -> + permissionsSet.add( + permissions.values.toString() + .split(",")[count] + .split(".")[1] + .replace("]", "") + ) + count++ + } + return permissionsSet.toString().replace("[", "").replace("]", "").ifEmpty { "None" } +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Utils.kt b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Utils.kt index 22d5b62d..33700ddd 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Utils.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/utils/_Utils.kt @@ -1,9 +1,5 @@ package org.hyacinthbots.lilybot.utils -import com.kotlindiscord.kord.extensions.builders.ExtensibleBotBuilder -import com.kotlindiscord.kord.extensions.extensions.Extension -import com.kotlindiscord.kord.extensions.utils.hasPermission -import com.kotlindiscord.kord.extensions.utils.loadModule import dev.kord.common.entity.Permission import dev.kord.common.entity.Snowflake import dev.kord.core.Kord @@ -11,12 +7,16 @@ import dev.kord.core.behavior.GuildBehavior import dev.kord.core.behavior.RoleBehavior import dev.kord.core.entity.Message import dev.kord.core.supplier.EntitySupplyStrategy +import dev.kordex.core.builders.ExtensibleBotBuilder +import dev.kordex.core.extensions.Extension +import dev.kordex.core.utils.hasPermission +import dev.kordex.core.utils.loadModule +import io.github.oshai.kotlinlogging.KotlinLogging import kotlinx.coroutines.flow.count import kotlinx.coroutines.runBlocking import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime -import mu.KotlinLogging import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.collections.ConfigMetaCollection import org.hyacinthbots.lilybot.database.collections.GalleryChannelCollection @@ -24,6 +24,7 @@ import org.hyacinthbots.lilybot.database.collections.GithubCollection import org.hyacinthbots.lilybot.database.collections.GuildLeaveTimeCollection import org.hyacinthbots.lilybot.database.collections.LoggingConfigCollection import org.hyacinthbots.lilybot.database.collections.MainMetaCollection +import org.hyacinthbots.lilybot.database.collections.ModerationActionCollection import org.hyacinthbots.lilybot.database.collections.ModerationConfigCollection import org.hyacinthbots.lilybot.database.collections.NewsChannelPublishingCollection import org.hyacinthbots.lilybot.database.collections.ReminderCollection @@ -237,6 +238,7 @@ suspend inline fun ExtensibleBotBuilder.database(migrate: Boolean) { single { GuildLeaveTimeCollection() } bind GuildLeaveTimeCollection::class single { LoggingConfigCollection() } bind LoggingConfigCollection::class single { MainMetaCollection() } bind MainMetaCollection::class + single { ModerationActionCollection() } bind ModerationActionCollection::class single { ModerationConfigCollection() } bind ModerationConfigCollection::class single { NewsChannelPublishingCollection() } bind NewsChannelPublishingCollection::class single { ReminderCollection() } bind ReminderCollection::class @@ -259,3 +261,12 @@ suspend inline fun ExtensibleBotBuilder.database(migrate: Boolean) { } } } + +/** + * Takes a value [T], converts it to a string and returns the part after a full-stop/period. + * Generally used on class values, i.e. ArchiveDuration.Day to return just "Day" + * + * @author NoComment1105 + * @since 5.0.0 + */ +fun T?.afterDot() = this.toString().substringAfter(".")