diff --git a/FredBoat/src/main/java/fredboat/command/config/ConfigCommand.kt b/FredBoat/src/main/java/fredboat/command/config/ConfigCommand.kt index 373627e14..c099ba1a0 100644 --- a/FredBoat/src/main/java/fredboat/command/config/ConfigCommand.kt +++ b/FredBoat/src/main/java/fredboat/command/config/ConfigCommand.kt @@ -58,6 +58,7 @@ class ConfigCommand(name: String, vararg aliases: String) : Command(name, *alias .append(context.i18nFormat("configNoArgs", context.guild.name)).append("\n") .append("track_announce = ${gc.isTrackAnnounce}\n") .append("auto_resume = ${gc.isAutoResume}\n") + .append("clear_on_empty = ${gc.isClearOnEmpty}\n") .append("```") //opening ``` is part of the configNoArgs language string context.reply(mb.build()) @@ -94,6 +95,13 @@ class ConfigCommand(name: String, vararg aliases: String) : Command(name, *alias } else { context.reply(context.i18nFormat("configMustBeBoolean", invoker.effectiveName.escapeAndDefuse())) } + } else if (key == "clear_on_empty") { + if (`val`.equals("true", ignoreCase = true) or `val`.equals("false", ignoreCase = true)) { + Launcher.botController.guildConfigService.transformGuildConfig(context.guild.id) { + gc -> gc.setClearOnEmpty(java.lang.Boolean.parseBoolean(`val`)) + } + context.replyWithName("`clear_on_empty`" + context.i18nFormat("configSetTo", `val`)) + } } else context.reply(context.i18nFormat("configUnknownKey", invoker.effectiveName.escapeAndDefuse())) } diff --git a/FredBoat/src/main/java/fredboat/db/transfer/GuildConfig.java b/FredBoat/src/main/java/fredboat/db/transfer/GuildConfig.java index 1eb4b2c70..4434d2444 100644 --- a/FredBoat/src/main/java/fredboat/db/transfer/GuildConfig.java +++ b/FredBoat/src/main/java/fredboat/db/transfer/GuildConfig.java @@ -35,6 +35,7 @@ public class GuildConfig implements TransferObject { private String guildId = ""; private boolean trackAnnounce = false; private boolean autoResume = false; + private boolean clearOnEmpty = true; private String lang = "en_US"; @Override @@ -65,6 +66,15 @@ public GuildConfig setAutoResume(boolean autoplay) { return this; } + public boolean isClearOnEmpty() { + return clearOnEmpty; + } + + public GuildConfig setClearOnEmpty(boolean clearOnEmpty) { + this.clearOnEmpty = clearOnEmpty; + return this; + } + public String getLang() { return lang; } diff --git a/FredBoat/src/main/java/fredboat/event/AudioEventHandler.kt b/FredBoat/src/main/java/fredboat/event/AudioEventHandler.kt index bc6a13675..b7e4d1acd 100644 --- a/FredBoat/src/main/java/fredboat/event/AudioEventHandler.kt +++ b/FredBoat/src/main/java/fredboat/event/AudioEventHandler.kt @@ -25,14 +25,18 @@ class AudioEventHandler( } override fun onVoiceLeave(channel: VoiceChannel, member: Member) { + checkForAutoStop(channel) checkForAutoPause(channel) + if (!member.isUs) return getLink(channel).onDisconnected() } override fun onVoiceMove(oldChannel: VoiceChannel, newChannel: VoiceChannel, member: Member) { checkForAutoResume(newChannel, member) + checkForAutoStop(newChannel) checkForAutoPause(oldChannel) + if (!member.isUs) return getLink(newChannel).setChannel(newChannel.id.toString()) } @@ -42,6 +46,24 @@ class AudioEventHandler( private fun getLink(channel: VoiceChannel) = lavalink.getLink(channel.guild.idString) + private fun checkForAutoResume(joinedChannel: VoiceChannel, joined: Member) { + val guild = joinedChannel.guild + val player = playerRegistry.getExisting(guild) ?: return + + //ignore bot users that aren't us joining / moving + if (joined.isBot && !joined.isUs) + return + + if (player.isPaused + && player.playingTrack != null + && joinedChannel.members.contains(guild.selfMember) + && player.humanUsersInCurrentVC.isNotEmpty() + && guildConfigService.fetchGuildConfig(guild.id).isAutoResume) { + player.setPause(false) + player.activeTextChannel?.send(I18n.get(guild).getString("eventAutoResumed"))?.subscribe() + } + } + private fun checkForAutoPause(channelLeft: VoiceChannel) { if (appConfig.continuePlayback) return @@ -59,22 +81,15 @@ class AudioEventHandler( } } - private fun checkForAutoResume(joinedChannel: VoiceChannel, joined: Member) { - val guild = joinedChannel.guild - val player = playerRegistry.getExisting(guild) ?: return - - //ignore bot users that aren't us joining / moving - if (joined.isBot && !joined.isUs) - return + private fun checkForAutoStop(channel: VoiceChannel) { + val player = playerRegistry.getExisting(channel.guild) ?: return - if (player.isPaused - && player.playingTrack != null - && joinedChannel.members.contains(guild.selfMember) - && player.humanUsersInCurrentVC.isNotEmpty() - && guildConfigService.fetchGuildConfig(guild.id).isAutoResume) { - player.setPause(false) - player.activeTextChannel?.send(I18n.get(guild).getString("eventAutoResumed"))?.subscribe() + if (player.isPlaying + && !player.isQueueEmpty + && player.humanUsersInCurrentVC.isEmpty() + && guildConfigService.fetchGuildConfig(channel.guild.id).isClearOnEmpty) { + player.stop() + player.activeTextChannel?.send(I18n.get(channel.guild).getString("eventUsersLeftVCStop"))?.subscribe() } } - } \ No newline at end of file diff --git a/FredBoat/src/main/resources/lang/en_US.properties b/FredBoat/src/main/resources/lang/en_US.properties index 40871fa6f..bf6053d84 100644 --- a/FredBoat/src/main/resources/lang/en_US.properties +++ b/FredBoat/src/main/resources/lang/en_US.properties @@ -210,6 +210,7 @@ userinfoCreationTime=Creation Date\: userinfoBlacklisted=Blacklisted\: skipDeniedTooManyTracks=You can't skip someone else's tracks if you are not a DJ.\nConsider using the Voteskip command. eventUsersLeftVC=All users have left the voice channel. The player has been paused. +eventUsersLeftVCStop=All users have left the voice channel. The player has been cleared. eventAutoResumed=User presence detected, automatically resuming the player. commandsFun=Fun commandsMemes=Memes