Skip to content

Commit a81da55

Browse files
committed
Configure bot permission checks for slash commands
1 parent 1600048 commit a81da55

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

bot/src/main/java/me/duncte123/skybot/CommandManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ public void executeSlashCommand(SlashCommandInteractionEvent event) {
762762
final SlashSupport command = (SlashSupport) this.getCommand(event.getName());
763763

764764
if (command != null) {
765-
command.handleEvent(event, variables);
765+
command.executeEventWithChecks(event, variables);
766766
}
767767
}
768768
catch (Exception e) {

bot/src/main/kotlin/me/duncte123/skybot/objects/SlashSupport.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package me.duncte123.skybot.objects
2020

2121
import me.duncte123.skybot.Variables
2222
import me.duncte123.skybot.objects.command.CommandCategory
23+
import me.duncte123.skybot.utils.AirUtils
2324
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
2425
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions
2526
import net.dv8tion.jda.api.interactions.commands.build.CommandData
@@ -49,5 +50,29 @@ abstract class SlashSupport : SkyCommand() {
4950
return base
5051
}
5152

53+
fun executeEventWithChecks(event: SlashCommandInteractionEvent, variables: Variables) {
54+
if (event.isFromGuild) {
55+
val self = event.guild!!.selfMember
56+
57+
if (this.botPermissions.isNotEmpty() && !self.hasPermission(this.botPermissions.toList())) {
58+
val permissionsWord = "permission${if (this.botPermissions.size > 1) "s" else ""}"
59+
val permsList = AirUtils.parsePerms(this.botPermissions)
60+
61+
event.reply(
62+
"I need the `$permsList` $permissionsWord for this command to work\n" +
63+
"Please contact your server administrator about this."
64+
)
65+
.setEphemeral(false)
66+
.queue()
67+
}
68+
69+
// TODO: cooldowns
70+
71+
handleEvent(event, variables)
72+
} else {
73+
handleEvent(event, variables)
74+
}
75+
}
76+
5277
abstract fun handleEvent(event: SlashCommandInteractionEvent, variables: Variables)
5378
}

0 commit comments

Comments
 (0)