Skip to content

Commit 4a46490

Browse files
committed
Turn voice kick into a slash command
1 parent 5f094f1 commit 4a46490

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Skybot, a multipurpose discord bot
3+
* Copyright (C) 2017 Duncan "duncte123" Sterken & Ramid "ramidzkh" Khan & Maurice R S "Sanduhr32"
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published
7+
* by the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package me.duncte123.skybot.objects.command;
20+
21+
import me.duncte123.skybot.Variables;
22+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
23+
import net.dv8tion.jda.api.interactions.commands.build.Commands;
24+
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
25+
26+
import javax.annotation.Nonnull;
27+
28+
public interface ISlashCommand {
29+
String getName();
30+
String getDescription();
31+
32+
default SlashCommandData getSlashData() {
33+
return Commands.slash(
34+
getName(),
35+
getDescription()
36+
)
37+
.setGuildOnly(true);
38+
}
39+
40+
void handleSlashEvent(@Nonnull SlashCommandInteractionEvent event, @Nonnull Variables variables);
41+
}

bot/src/main/kotlin/me/duncte123/skybot/commands/mod/VoiceKickCommand.kt

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ package me.duncte123.skybot.commands.mod
2020

2121
import me.duncte123.botcommons.messaging.MessageUtils.sendMsg
2222
import me.duncte123.botcommons.messaging.MessageUtils.sendSuccess
23+
import me.duncte123.skybot.Variables
2324
import me.duncte123.skybot.commands.guild.mod.ModBaseCommand
2425
import me.duncte123.skybot.objects.command.CommandContext
26+
import me.duncte123.skybot.objects.command.ISlashCommand
2527
import net.dv8tion.jda.api.Permission
28+
import net.dv8tion.jda.api.entities.channel.ChannelType
29+
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
30+
import net.dv8tion.jda.api.interactions.commands.OptionType
31+
import net.dv8tion.jda.api.interactions.commands.build.OptionData
32+
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData
2633

27-
class VoiceKickCommand : ModBaseCommand() {
34+
class VoiceKickCommand : ModBaseCommand(), ISlashCommand {
2835
init {
2936
this.requiresArgs = true
3037
this.name = "voicekick"
@@ -68,4 +75,70 @@ class VoiceKickCommand : ModBaseCommand() {
6875

6976
sendMsg(ctx, "I could not find any Voice Channel or member to kick from voice")
7077
}
78+
79+
override fun getDescription(): String = this.help
80+
81+
override fun getSlashData(): SlashCommandData {
82+
return super.getSlashData()
83+
.addOptions(
84+
OptionData(
85+
OptionType.CHANNEL,
86+
"voice_channel",
87+
"The voice channel to kick ALL users from.",
88+
false
89+
),
90+
OptionData(
91+
OptionType.USER,
92+
"user",
93+
"The user to kick from the voice channel they are currently in.",
94+
false
95+
),
96+
)
97+
}
98+
99+
override fun handleSlashEvent(event: SlashCommandInteractionEvent, variables: Variables) {
100+
val vc = event.getOption("voice_channel")?.asChannel
101+
102+
if (vc != null) {
103+
if (vc.type == ChannelType.VOICE) {
104+
event.reply("Kicking all users from `${vc.name}`, please wait...")
105+
.setEphemeral(false)
106+
.queue()
107+
108+
vc.asVoiceChannel().createCopy().queue {
109+
vc.delete().reason("Kicking all users from voice (${event.user.asTag})").queue()
110+
111+
event.hook.editOriginal("Kicked all users from `${vc.name}`").queue()
112+
}
113+
return
114+
}
115+
116+
event.reply("Can't voice kick from a text channel, sorry")
117+
.setEphemeral(true)
118+
.queue()
119+
return
120+
}
121+
122+
val member = event.getOption("user")?.asMember
123+
124+
if (member != null) {
125+
val memberChannel = member.voiceState?.channel
126+
127+
// We have to "== true" here because the return type is a nullable boolean
128+
if (member.voiceState?.inAudioChannel() == true) {
129+
event.guild!!.kickVoiceMember(member).queue()
130+
event.reply("Kicked ${member.user.asTag} from voice").setEphemeral(false).queue()
131+
return
132+
}
133+
134+
event.reply("That user is not in a voice channel")
135+
.setEphemeral(true)
136+
.queue()
137+
138+
return
139+
}
140+
141+
event.reply("Please either specify a voice channel or a user to kick from the voice channel.")
142+
.queue()
143+
}
71144
}

0 commit comments

Comments
 (0)