Skip to content

Commit

Permalink
Merge pull request #234 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 8f9dc38 + 0a3f694 commit c8d3d75
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
47 changes: 47 additions & 0 deletions src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

class VCCommand {

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)
}

}
25 changes: 4 additions & 21 deletions src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
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

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()
}
}

}
}

0 comments on commit c8d3d75

Please sign in to comment.