Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Feature/clear on empty #589

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()))
}

Expand Down
10 changes: 10 additions & 0 deletions FredBoat/src/main/java/fredboat/db/transfer/GuildConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class GuildConfig implements TransferObject<String> {
private String guildId = "";
private boolean trackAnnounce = false;
private boolean autoResume = false;
private boolean clearOnEmpty = true;
private String lang = "en_US";

@Override
Expand Down Expand Up @@ -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;
}
Expand Down
45 changes: 30 additions & 15 deletions FredBoat/src/main/java/fredboat/event/AudioEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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

Expand All @@ -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()
}
}

}
1 change: 1 addition & 0 deletions FredBoat/src/main/resources/lang/en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down