Skip to content

Commit

Permalink
[#132] Bot owner/team admin checks
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Oct 27, 2023
1 parent 8cc21e9 commit 2fabb5f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2fabb5f

Please sign in to comment.