-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from flytegg/feat/vcs
Feat/vcs
- Loading branch information
Showing
2 changed files
with
51 additions
and
21 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/learnspigot/bot/voicechat/VCCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/kotlin/com/learnspigot/bot/voicechat/VCListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
|
||
} | ||
} |