Skip to content

Commit

Permalink
Merge pull request #236 from flytegg/feat/vcs
Browse files Browse the repository at this point in the history
Feat/vcs
  • Loading branch information
joshbker authored Feb 17, 2024
2 parents c8d3d75 + 2755bcb commit ddd51cb
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 net.dv8tion.jda.api.exceptions.ContextException
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
Expand All @@ -24,23 +25,36 @@ class VCCommand {
@Description("Max user limit") @Optional limit: Int?,
) {
val guild = event.guild ?: return
val member = event.member ?: return

if (guild.getVoiceChannelsByName("${member.effectiveName}'s channel", true).isNotEmpty()) {
event.reply("You already have a voice channel!").setEphemeral(true).queue()
return
}

if (limit != null && limit < 1) {
event.reply("The max user limit must be 1 or higher.").setEphemeral(true).queue()
return
}

val newChannel = guild.createVoiceChannel(
"${event.member!!.effectiveName}'s channel",
event.guild!!.getCategoryById(Environment.get("CHAT_CATEGORY"))
"${member.effectiveName}'s channel",
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()
if (member.voiceState?.inAudioChannel() == true)
guild.moveVoiceMember(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()
try {
if (newChannel.members.isEmpty()) newChannel.delete().queue()
} catch (_: ContextException) {}
}, 5, TimeUnit.MINUTES)
}

Expand Down

0 comments on commit ddd51cb

Please sign in to comment.