-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00132c0
commit 8a3c3e7
Showing
7 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 75 additions & 7 deletions
82
plugin/src/main/kotlin/space/votebot/util/PermissionUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,99 @@ | ||
package space.votebot.util | ||
|
||
import com.kotlindiscord.kord.extensions.commands.Arguments | ||
import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext | ||
import com.kotlindiscord.kord.extensions.commands.application.slash.PublicSlashCommandContext | ||
import com.kotlindiscord.kord.extensions.commands.application.slash.SlashCommandContext | ||
import com.kotlindiscord.kord.extensions.components.components | ||
import com.kotlindiscord.kord.extensions.components.ephemeralButton | ||
import com.kotlindiscord.kord.extensions.utils.translate | ||
import dev.kord.common.entity.ButtonStyle | ||
import dev.kord.common.entity.Permission | ||
import dev.kord.common.entity.Permissions | ||
import dev.kord.common.entity.Snowflake | ||
import dev.kord.core.entity.channel.GuildMessageChannel | ||
import dev.kord.core.entity.channel.TopGuildMessageChannel | ||
import dev.kord.core.entity.channel.thread.ThreadChannel | ||
import dev.schlaubi.mikbot.plugin.api.util.discordError | ||
import dev.kord.core.entity.interaction.followup.FollowupMessage | ||
import dev.kord.rest.builder.message.create.MessageCreateBuilder | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
suspend fun <A : Arguments> SlashCommandContext<*, A, *>.checkPermissions(channel: GuildMessageChannel) { | ||
private val requiredPermissions = | ||
Permissions(Permission.SendMessages, Permission.EmbedLinks, Permission.AttachFiles, Permission.ViewChannel) | ||
|
||
suspend fun <A : Arguments> SlashCommandContext<*, A, *>.checkPermissions(channel: GuildMessageChannel): Boolean { | ||
val selfPermissions = channel.getEffectivePermissions(channel.kord.selfId) | ||
val requiredPermissions = | ||
Permissions(Permission.SendMessages, Permission.EmbedLinks, Permission.AttachFiles, Permission.ViewChannel) | ||
if (requiredPermissions !in selfPermissions) { | ||
discordError(translate("vote.create.missing_permissions.bot", arrayOf(channel.mention))) | ||
sendMissingPermissions("vote.create.missing_permissions.bot", channel, channel.kord.selfId, selfPermissions) | ||
return false | ||
} | ||
|
||
val userPermissions = channel.getEffectivePermissions(user.id) | ||
if ((requiredPermissions - Permission.ViewChannel) !in userPermissions) { | ||
discordError(translate("vote.create.missing_permissions.user", arrayOf(channel.mention))) | ||
if ((requiredPermissions - Permission.ViewChannel) !in userPermissions) { | ||
sendMissingPermissions("vote.create.missing_permissions.user", channel, user.id, userPermissions) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
private suspend fun SlashCommandContext<*, *, *>.sendMissingPermissions( | ||
translation: String, | ||
channel: GuildMessageChannel, | ||
user: Snowflake, | ||
permissions: Permissions | ||
) { | ||
suspend fun Boolean.translate() = translate(if (this) "common.yes" else "common.no") | ||
|
||
respond { | ||
content = translate(translation, arrayOf(channel.mention)) | ||
|
||
components(5.minutes) { | ||
ephemeralButton { | ||
bundle = command.resolvedBundle | ||
style = ButtonStyle.Secondary | ||
label = translate("vote.create.missing_permissions.explainer.label") | ||
|
||
action { | ||
val serverPermissions = channel.guild.getMember(user).getPermissions() | ||
val isAdministrator = | ||
if (Permission.Administrator in serverPermissions) "common.yes" else "common.no" | ||
|
||
val missingPermissions = (requiredPermissions - permissions).values.map { | ||
translate( | ||
"vote.create.missing_permissions.explainer.permission", | ||
arrayOf( | ||
it.translate(this@sendMissingPermissions), | ||
(it in serverPermissions).translate(), | ||
(it in permissions).translate(), | ||
) | ||
) | ||
}.joinToString("\n") | ||
|
||
|
||
respond { | ||
content = translate( | ||
"vote.create.missing_permissions.explainer", | ||
arrayOf(translate(isAdministrator), missingPermissions) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
private suspend fun GuildMessageChannel.getEffectivePermissions(user: Snowflake) = when (this) { | ||
is TopGuildMessageChannel -> getEffectivePermissions(user) | ||
is ThreadChannel -> parent.asChannel().getEffectivePermissions(user) | ||
else -> error("Could not determine permissions for channel type ${this::class.simpleName}") | ||
} | ||
|
||
suspend fun SlashCommandContext<*, *, *>.respond(build: suspend MessageCreateBuilder.() -> Unit): FollowupMessage { | ||
return when (this) { | ||
is EphemeralSlashCommandContext<*, *> -> respond(build) | ||
is PublicSlashCommandContext<*, *> -> respond(build) | ||
else -> error("Unsupported slash command context: $this") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters