From 2fabb5fc19fbfd50d76cb6d427da42fc2e9b6150 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Fri, 27 Oct 2023 18:49:19 +0100 Subject: [PATCH] [#132] Bot owner/team admin checks --- .../kord/extensions/checks/MiscChecks.kt | 82 +++++++++++++++++++ .../translations/kordex/strings.properties | 2 + .../kordex/strings_en_GB.properties | 2 + 3 files changed, 86 insertions(+) diff --git a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/checks/MiscChecks.kt b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/checks/MiscChecks.kt index fb082ee456..de8f1eb01f 100644 --- a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/checks/MiscChecks.kt +++ b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/checks/MiscChecks.kt @@ -4,13 +4,95 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +@file:Suppress("StringLiteralDuplication") + package com.kotlindiscord.kord.extensions.checks import com.kotlindiscord.kord.extensions.checks.types.CheckContext +import dev.kord.common.entity.TeamMemberRole import dev.kord.core.behavior.channel.threads.ThreadChannelBehavior import dev.kord.core.event.Event import io.github.oshai.kotlinlogging.KotlinLogging +/** + * For bots with single owners, check asserting the user for an [Event] is the bot's owner. + * + * Will fail if the event doesn't concern a user, or the bot doesn't have a single owner (e.g. it is part of a team). + */ +public suspend fun CheckContext<*>.isBotOwner() { + if (!passed) { + return + } + + val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.isBotOwner") + val owner = event.kord.getApplicationInfo().ownerId + + if (owner == null) { + logger.failed("Bot does not have an owner.") + + return fail() + } + + val user = userFor(event)?.asUserOrNull() + + if (user == null) { + logger.failed("Event did not concern a user.") + + fail() + } else if (user.id == owner) { + logger.passed() + + pass() + } else { + logger.failed("User does not own this bot.") + + fail( + translate("checks.isBotOwner.failed") + ) + } +} + +/** + * For bots owned by a team, check asserting the user for an [Event] is one of the bot's admins. + * + * Will fail if the event doesn't concern a user, or the bot doesn't have any admins (e.g. it has a single owner). + */ +public suspend fun CheckContext<*>.isBotAdmin() { + if (!passed) { + return + } + + val logger = KotlinLogging.logger("com.kotlindiscord.kord.extensions.checks.isBotAdmin") + val admins = event.kord.getApplicationInfo().team + ?.members + ?.filter { it.role == TeamMemberRole.Admin } + ?.map { it.userId } + + if (admins.isNullOrEmpty()) { + logger.failed("Bot does not have any admins.") + + return fail() + } + + val user = userFor(event)?.asUserOrNull() + + if (user == null) { + logger.failed("Event did not concern a user.") + + fail() + } else if (user.id in admins) { + logger.passed() + + pass() + } else { + logger.failed("User does not administrate this bot.") + + fail( + translate("checks.isBotAdmin.failed") + ) + } +} + /** * Check asserting the user for an [Event] is a bot. Will fail if the event doesn't concern a user. */ diff --git a/kord-extensions/src/main/resources/translations/kordex/strings.properties b/kord-extensions/src/main/resources/translations/kordex/strings.properties index 0060a41648..a2135ae492 100644 --- a/kord-extensions/src/main/resources/translations/kordex/strings.properties +++ b/kord-extensions/src/main/resources/translations/kordex/strings.properties @@ -55,6 +55,8 @@ checks.guildNsfwLevelLower.failed=Must be in a server with an NSFW level lower t checks.guildNsfwLevelLowerOrEqual.failed=Must be in a server with an NSFW level of **{0}**, or lower checks.isBot.failed=Must be a bot +checks.isBotAdmin.failed=Must be one of this bot's admins +checks.isBotOwner.failed=Must be this bot's owner checks.isNotBot.failed=Must not be a bot checks.isInThread.failed=Must be in a thread checks.isNotInThread.failed=Must not be in a thread diff --git a/kord-extensions/src/main/resources/translations/kordex/strings_en_GB.properties b/kord-extensions/src/main/resources/translations/kordex/strings_en_GB.properties index df70bdb7e0..9b3345a40b 100644 --- a/kord-extensions/src/main/resources/translations/kordex/strings_en_GB.properties +++ b/kord-extensions/src/main/resources/translations/kordex/strings_en_GB.properties @@ -53,6 +53,8 @@ checks.guildNsfwLevelHigherOrEqual.failed=Must be in a server with an NSFW level checks.guildNsfwLevelLower.failed=Must be in a server with an NSFW level lower than: **{0}** checks.guildNsfwLevelLowerOrEqual.failed=Must be in a server with an NSFW level of **{0}**, or lower checks.isBot.failed=Must be a bot +checks.isBotAdmin.failed=Must be one of this bot's admins +checks.isBotOwner.failed=Must be this bot's owner checks.isNotBot.failed=Must not be a bot checks.isInThread.failed=Must be in a thread checks.isNotInThread.failed=Must not be in a thread