From 3139305519347d2a3b4d9a07db5008af77868304 Mon Sep 17 00:00:00 2001 From: Panav Bindal Date: Sat, 17 Feb 2024 19:12:38 +0000 Subject: [PATCH 1/2] New changes --- .../learnspigot/bot/voicechat/VCCommand.kt | 35 +++++++++++++++++++ .../learnspigot/bot/voicechat/VCListener.kt | 25 +++---------- 2 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt diff --git a/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt b/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt new file mode 100644 index 0000000..5aedc85 --- /dev/null +++ b/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt @@ -0,0 +1,35 @@ +package com.learnspigot.bot.voicechat + +import com.learnspigot.bot.Environment +import gg.flyte.neptune.annotation.Command +import gg.flyte.neptune.annotation.Description +import gg.flyte.neptune.annotation.Optional +import net.dv8tion.jda.api.Permission +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent + +class VCCommand { + + @Command( + name = "createvoice", + description = "Create a temporary voice channel!", + permissions = [Permission.CREATE_PUBLIC_THREADS]) + fun onCreateVoiceCommand( + event: SlashCommandInteractionEvent, + @Description("Max user limit") @Optional limit: Int?, + ) { + if (event.member!!.voiceState?.inAudioChannel() != true){ + event.reply("You must be in a voice channel to use this command").setEphemeral(true).queue() + return + } + + val guild = event.guild ?: return + val newChannel = guild.createVoiceChannel("${event.member!!.effectiveName}'s channel", event.guild!!.getCategoryById(Environment.get("CHAT_CATEGORY"))).complete() + + if (limit != null) { + newChannel.manager.setUserLimit(limit).queue() + } + + guild.moveVoiceMember(event.member!!, newChannel).queue() + event.reply("VC created, enjoy!").setEphemeral(true).queue() + } +} diff --git a/src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt b/src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt index 3b17c64..e054a48 100644 --- a/src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt +++ b/src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt @@ -1,8 +1,7 @@ package com.learnspigot.bot.voicechat import com.learnspigot.bot.Environment -import io.github.cdimascio.dotenv.dotenv -import net.dv8tion.jda.api.entities.Widget.VoiceChannel +import net.dv8tion.jda.api.entities.channel.concrete.StageChannel import net.dv8tion.jda.api.events.guild.voice.GuildVoiceUpdateEvent import net.dv8tion.jda.api.hooks.ListenerAdapter @@ -10,27 +9,11 @@ class VCListener : ListenerAdapter() { override fun onGuildVoiceUpdate(event: GuildVoiceUpdateEvent){ val guild = event.guild val voiceChannel = guild.getVoiceChannelById(Environment.get("VOICE_CHANNEL_ID")) - val joinedChannel = event.channelJoined val leftChannel = event.channelLeft - val oldChannel = event.oldValue - if (joinedChannel !is VoiceChannel || joinedChannel.parentCategoryId != Environment.get("CHAT_CATEGORY")) return - if (joinedChannel == voiceChannel){ - - if ((oldChannel != null) && oldChannel.members.isEmpty()){ - oldChannel.delete().queue() - } - - val newChannel = guild.createVoiceChannel("${event.member.effectiveName}'s channel", joinedChannel.parentCategory!!).complete() - guild.moveVoiceMember(event.member, newChannel).queue() - return + if (leftChannel != null && leftChannel.members.isEmpty()) { + if ((leftChannel == voiceChannel) || (leftChannel is StageChannel) || (leftChannel.parentCategoryId != Environment.get("CHAT_CATEGORY"))) return + leftChannel.delete().queue() } - - if (leftChannel != null){ - if (leftChannel.members.isEmpty() && (leftChannel != voiceChannel)){ - leftChannel.delete().queue() - } - } - } } \ No newline at end of file From 0a3f69467aceeb3916de67390266269a39a2d519 Mon Sep 17 00:00:00 2001 From: Josh <43449531+joshbker@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:05:36 +0000 Subject: [PATCH 2/2] feat: create vc and reply with mention, move if they are already in a vc, delete the vc after 5 minutes if it's empty --- .../learnspigot/bot/voicechat/VCCommand.kt | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt b/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt index 5aedc85..fcdf8d5 100644 --- a/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt +++ b/src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt @@ -6,30 +6,42 @@ import gg.flyte.neptune.annotation.Description import gg.flyte.neptune.annotation.Optional import net.dv8tion.jda.api.Permission import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService +import java.util.concurrent.TimeUnit class VCCommand { - @Command( - name = "createvoice", - description = "Create a temporary voice channel!", - permissions = [Permission.CREATE_PUBLIC_THREADS]) - fun onCreateVoiceCommand( - event: SlashCommandInteractionEvent, - @Description("Max user limit") @Optional limit: Int?, - ) { - if (event.member!!.voiceState?.inAudioChannel() != true){ - event.reply("You must be in a voice channel to use this command").setEphemeral(true).queue() - return - } - - val guild = event.guild ?: return - val newChannel = guild.createVoiceChannel("${event.member!!.effectiveName}'s channel", event.guild!!.getCategoryById(Environment.get("CHAT_CATEGORY"))).complete() - - if (limit != null) { - newChannel.manager.setUserLimit(limit).queue() - } - - guild.moveVoiceMember(event.member!!, newChannel).queue() - event.reply("VC created, enjoy!").setEphemeral(true).queue() - } + private val scheduledExecutor: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor() + + @Command( + name = "createvoice", + description = "Create a temporary voice channel!", + permissions = [Permission.CREATE_PUBLIC_THREADS] + ) + fun onCreateVoiceCommand( + event: SlashCommandInteractionEvent, + @Description("Max user limit") @Optional limit: Int?, + ) { + val guild = event.guild ?: return + val newChannel = guild.createVoiceChannel( + "${event.member!!.effectiveName}'s channel", + event.guild!!.getCategoryById(Environment.get("CHAT_CATEGORY")) + ).complete() + + if (limit != null) { + newChannel.manager.setUserLimit(limit).queue() + } + + if (event.member!!.voiceState?.inAudioChannel() == true) + guild.moveVoiceMember(event.member!!, newChannel).queue() + + event.reply("Your voice channel has been created - ${newChannel.asMention}").setEphemeral(true).queue() + + scheduledExecutor.schedule({ + if (newChannel == null) return@schedule + if (newChannel.members.isEmpty()) newChannel.delete().queue() + }, 5, TimeUnit.MINUTES) + } + }