From ea4c17ec6114b3238aed5c4a3d66eda7b589538d Mon Sep 17 00:00:00 2001 From: Duncan Sterken Date: Tue, 13 Feb 2018 08:53:00 +0100 Subject: [PATCH] [REQUEST] make option to disable track announcements And some other things --- build.gradle | 2 +- .../skybot/audio/GuildMusicManager.java | 5 +++++ .../skybot/audio/TrackScheduler.java | 10 ++++++++-- .../commands/guild/owner/SettingsCommand.java | 17 +++++++++++++--- .../database/MySQLConnectionManager.java | 1 + .../SQLiteDatabaseConnectionManager.java | 1 + .../spotify/SpotifyAudioSourceManager.java | 12 ++++++++--- .../skybot/objects/guild/GuildSettings.java | 20 +++++++++++++++++++ .../skybot/utils/GuildSettingsUtils.java | 8 +++++++- .../commands/essentials/eval/EvalCommand.kt | 6 +++++- .../skybot/commands/music/RepeatCommand.kt | 2 +- 11 files changed, 72 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 75dab7408..e330c68e6 100644 --- a/build.gradle +++ b/build.gradle @@ -39,7 +39,7 @@ plugins { group = 'ml.duncte123.skybot' -version '3.61.5' +version '3.61.6' sourceCompatibility = 1.9 diff --git a/src/main/java/ml/duncte123/skybot/audio/GuildMusicManager.java b/src/main/java/ml/duncte123/skybot/audio/GuildMusicManager.java index 88825b80c..8aec49990 100644 --- a/src/main/java/ml/duncte123/skybot/audio/GuildMusicManager.java +++ b/src/main/java/ml/duncte123/skybot/audio/GuildMusicManager.java @@ -20,6 +20,8 @@ import fredboat.audio.player.LavalinkManager; import lavalink.client.player.IPlayer; +import ml.duncte123.skybot.objects.guild.GuildSettings; +import ml.duncte123.skybot.utils.GuildSettingsUtils; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.TextChannel; @@ -30,6 +32,8 @@ public class GuildMusicManager { */ public TextChannel latestChannel = null; + final GuildSettings guildSettings; + /** * This is our player */ @@ -55,6 +59,7 @@ public GuildMusicManager(Guild g) { scheduler = new TrackScheduler(player, this); sendHandler = new AudioPlayerSenderHandler(player); player.addListener(scheduler); + this.guildSettings = GuildSettingsUtils.getGuild(g); } /** diff --git a/src/main/java/ml/duncte123/skybot/audio/TrackScheduler.java b/src/main/java/ml/duncte123/skybot/audio/TrackScheduler.java index 321f1fb23..232e54c35 100644 --- a/src/main/java/ml/duncte123/skybot/audio/TrackScheduler.java +++ b/src/main/java/ml/duncte123/skybot/audio/TrackScheduler.java @@ -98,7 +98,7 @@ public void nextTrack() { AudioTrack nextTrack = queue.poll(); if(nextTrack != null) { player.playTrack(nextTrack); - MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + nextTrack.getInfo().title); + announceNextTrack(nextTrack); } } else if(player.getPlayingTrack() != null) player.seekTo(player.getPlayingTrack().getDuration()); @@ -121,10 +121,11 @@ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason logger.debug("repeating"); if (!repeatPlayList) { this.player.playTrack(lastTrack.makeClone()); - MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + lastTrack.getInfo().title); + announceNextTrack(lastTrack); } else { logger.debug("a playlist....."); queue(lastTrack.makeClone()); + nextTrack(); } } else { logger.debug("starting next track"); @@ -176,4 +177,9 @@ public void shuffle() { Collections.shuffle((List) queue); } + private void announceNextTrack(AudioTrack track) { + if(guildMusicManager.guildSettings.isAnnounceTracks()) + MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + track.getInfo().title); + } + } diff --git a/src/main/java/ml/duncte123/skybot/commands/guild/owner/SettingsCommand.java b/src/main/java/ml/duncte123/skybot/commands/guild/owner/SettingsCommand.java index 094cab927..fa863e0ca 100644 --- a/src/main/java/ml/duncte123/skybot/commands/guild/owner/SettingsCommand.java +++ b/src/main/java/ml/duncte123/skybot/commands/guild/owner/SettingsCommand.java @@ -62,9 +62,11 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve MessageEmbed message = EmbedUtils.embedMessage("Here are the settings from this guild.\n" + "**Show join/leave messages:** " + (settings.isEnableJoinMessage() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" + "**Swearword filter:** " + (settings.isEnableSwearFilter() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" + + "**Announce next track:** " + (settings.isAnnounceTracks() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" + "**Join message:** " + settings.getCustomJoinMessage() + "\n" + "**Leave message:** " + settings.getCustomLeaveMessage() + "\n" + - "**AutoRole:** " + (settings.getAutoroleRole() == null || settings.getAutoroleRole().equals("") ? "Not Set": event.getGuild().getRoleById(settings.getAutoroleRole()).getAsMention() )+ "\n" + + "**AutoRole:** " + (settings.getAutoroleRole() == null || settings.getAutoroleRole().equals("") + ? "Not Set": event.getGuild().getRoleById(settings.getAutoroleRole()).getAsMention() )+ "\n" + "**Current prefix:** " + settings.getCustomPrefix() + "\n" + "**Modlog Channel:** " + (logChan !=null ? logChan.getAsMention(): "none") + "\n" + "**Welcome/Leave channel:** " + (welcomeLeaveChannel != null ? welcomeLeaveChannel.getAsMention() : "none") @@ -222,6 +224,13 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve GuildSettingsUtils.updateGuildSettings(event.getGuild(), settings.setServerDesc(description) ); sendMsg(event, "Description has been updated, check `" + PREFIX + "guildinfo` to see your description"); break; + + case "toggleannouncetracks": + boolean shouldAnnounceTracks = !settings.isAnnounceTracks(); + GuildSettingsUtils.updateGuildSettings(event.getGuild(), settings.setAnnounceTracks(shouldAnnounceTracks) ); + sendMsg(event, "Announcing the next track has been **" + + ( shouldAnnounceTracks ? "enabled" : "disabled" ) + "**" ); + break; } } @@ -237,7 +246,8 @@ public String help() { "`"+PREFIX+"setLogChannel ` => Sets the channel to log messages in\n" + "`"+PREFIX+"setWelcomeChannel ` => Sets the channel that displays the welcome and leave messages\n" + "`"+PREFIX+"autorole ` => Gives members a role when they join\n" + - "`"+PREFIX+"setdescription ` => Set a custom description in " + PREFIX + "guildinfo" + "`"+PREFIX+"setdescription ` => Set a custom description in " + PREFIX + "guildinfo\n" + + "`"+PREFIX+"toggleannouncetracks` => Toggles if the player should announce the next playing track" ; } @@ -264,7 +274,8 @@ public String[] getAliases() { "setleavechannel", "setleavemessage", "autorole", - "setdescription" + "setdescription", + "toggleannouncetracks" }; } } diff --git a/src/main/java/ml/duncte123/skybot/connections/database/MySQLConnectionManager.java b/src/main/java/ml/duncte123/skybot/connections/database/MySQLConnectionManager.java index c59293d9f..bd1ecfd69 100644 --- a/src/main/java/ml/duncte123/skybot/connections/database/MySQLConnectionManager.java +++ b/src/main/java/ml/duncte123/skybot/connections/database/MySQLConnectionManager.java @@ -150,6 +150,7 @@ private void innitDB(Connection connection) { " `autoRole` varchar(255) DEFAULT NULL,\n" + " `enableJoinMessage` tinyint(1) NOT NULL DEFAULT '0',\n" + " `enableSwearFilter` tinyint(1) NOT NULL DEFAULT '0',\n" + + " `announceNextTrack` tinyint(1) NOT NULL DEFAULT '1',\n" + " `customWelcomeMessage` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,\n" + " `customLeaveMessage` text DEFAULT NULL,\n" + " `serverDesc` text DEFAULT NULL,\n" + diff --git a/src/main/java/ml/duncte123/skybot/connections/database/SQLiteDatabaseConnectionManager.java b/src/main/java/ml/duncte123/skybot/connections/database/SQLiteDatabaseConnectionManager.java index 8216265a5..1185e6b1e 100644 --- a/src/main/java/ml/duncte123/skybot/connections/database/SQLiteDatabaseConnectionManager.java +++ b/src/main/java/ml/duncte123/skybot/connections/database/SQLiteDatabaseConnectionManager.java @@ -137,6 +137,7 @@ private void innitDB(Connection connection) { "autoRole VARCHAR(255) NULL," + "enableJoinMessage tinyint(1) NOT NULL DEFAULT '0'," + "enableSwearFilter tinyint(1) NOT NULL DEFAULT '0'," + + "announceNextTrack tinyint(1) NOT NULL DEFAULT '1'," + "customWelcomeMessage TEXT NOT NULL," + "serverDesc TEXT NULL," + "customLeaveMessage TEXT NOT NULL);"); diff --git a/src/main/java/ml/duncte123/skybot/objects/audioManagers/spotify/SpotifyAudioSourceManager.java b/src/main/java/ml/duncte123/skybot/objects/audioManagers/spotify/SpotifyAudioSourceManager.java index 750219948..9ff15d4e2 100644 --- a/src/main/java/ml/duncte123/skybot/objects/audioManagers/spotify/SpotifyAudioSourceManager.java +++ b/src/main/java/ml/duncte123/skybot/objects/audioManagers/spotify/SpotifyAudioSourceManager.java @@ -78,8 +78,9 @@ public class SpotifyAudioSourceManager implements AudioSourceManager, HttpConfig private final YoutubeSearchProvider youtubeSearchProvider; private final YoutubeAudioSourceManager youtubeAudioSourceManager; private final SoundCloudAudioSourceManager manager = null; + private final ScheduledExecutorService service; -// public SpotifyAudioSourceManager(SoundCloudAudioSourceManager manager) { + // public SpotifyAudioSourceManager(SoundCloudAudioSourceManager manager) { public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceManager) { String defaultValue = "To use Spotify search, please create an app over at https://developer.spotify.com/web-api/"; String clientId = AirUtils.config.getString("apis.spotify.clientId", defaultValue); @@ -89,6 +90,7 @@ public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceMan this.api = null; //this.manager = null; youtubeSearchProvider = null; + this.service = null; this.youtubeAudioSourceManager = null; } else { //this.manager = manager; @@ -98,7 +100,7 @@ public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceMan .clientId(clientId) .clientSecret(clientSecret) .build(); - final ScheduledExecutorService service = Executors.newScheduledThreadPool(1, r -> new Thread(r, "Spotify-Token-Update-Thread")); + this.service = Executors.newScheduledThreadPool(1, r -> new Thread(r, "Spotify-Token-Update-Thread")); service.scheduleAtFixedRate(this::updateAccessToken, 0, 1, TimeUnit.HOURS); } } @@ -245,7 +247,11 @@ public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) { @Override public void shutdown() { - //youtubeAudioSourceManager.shutdown(); + if(this.youtubeAudioSourceManager != null) + this.youtubeAudioSourceManager.shutdown(); + if(this.service != null) + this.service.shutdown(); + } @Override diff --git a/src/main/java/ml/duncte123/skybot/objects/guild/GuildSettings.java b/src/main/java/ml/duncte123/skybot/objects/guild/GuildSettings.java index 08d44a12e..83180c9fa 100644 --- a/src/main/java/ml/duncte123/skybot/objects/guild/GuildSettings.java +++ b/src/main/java/ml/duncte123/skybot/objects/guild/GuildSettings.java @@ -75,6 +75,8 @@ public class GuildSettings { */ private String serverDesc = null; + private boolean announceTracks = false; + /** * This will init everything * @@ -263,6 +265,24 @@ public String getServerDesc() { return serverDesc; } + /** + * Sets if the audio player should announce the tracks + * @param announceTracks true to announce tracks + * @return the current {@link GuildSettings} + */ + public GuildSettings setAnnounceTracks(boolean announceTracks) { + this.announceTracks = announceTracks; + return this; + } + + /** + * Returns if we should announce the next track + * @return if we should announce the next track + */ + public boolean isAnnounceTracks() { + return announceTracks; + } + /** * {@inheritDoc} */ diff --git a/src/main/java/ml/duncte123/skybot/utils/GuildSettingsUtils.java b/src/main/java/ml/duncte123/skybot/utils/GuildSettingsUtils.java index 154d1e1b3..bf59c8362 100644 --- a/src/main/java/ml/duncte123/skybot/utils/GuildSettingsUtils.java +++ b/src/main/java/ml/duncte123/skybot/utils/GuildSettingsUtils.java @@ -96,6 +96,7 @@ public static void loadGuildSettings() { String leaveMessage = replaceNewLines(resSettings.getString("customLeaveMessage")); String autoroleId = resSettings.getString("autoRole"); String serverDesc = replaceNewLines(resSettings.getString("serverDesc")); + boolean announceNextTrack = resSettings.getBoolean("announceNextTrack"); AirUtils.guildSettings.put(guildId, new GuildSettings(guildId) .setEnableJoinMessage(enableJoinMsg) @@ -107,6 +108,7 @@ public static void loadGuildSettings() { .setCustomLeaveMessage(leaveMessage) .setAutoroleRole(autoroleId) .setServerDesc(serverDesc) + .setAnnounceTracks(announceNextTrack) ); } @@ -160,6 +162,8 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) { String chanId = settings.getLogChannel(); String welcomeLeaveChannel = settings.getWelcomeLeaveChannel(); String serverDesc = settings.getServerDesc(); + boolean announceNextTrack = settings.isAnnounceTracks(); + String dbName = AirUtils.db.getName(); Connection database = AirUtils.db.getConnManager().getConnection(); @@ -173,7 +177,8 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) { "logChannelId= ? ," + "welcomeLeaveChannel= ? ," + "customLeaveMessage = ? ," + - "serverDesc = ? " + + "serverDesc = ? ," + + "announceNextTrack = ? " + "WHERE guildId='" + guildId + "'"); preparedStatement.setBoolean(1, enableJoinMessage); preparedStatement.setBoolean(2, enableSwearFilter); @@ -184,6 +189,7 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) { preparedStatement.setString(7, welcomeLeaveChannel); preparedStatement.setString(8, replaceUnicode(customLeaveMessage)); preparedStatement.setString(9, replaceUnicode(serverDesc)); + preparedStatement.setBoolean(10, announceNextTrack); preparedStatement.executeUpdate(); } catch (SQLException e1) { diff --git a/src/main/kotlin/ml/duncte123/skybot/commands/essentials/eval/EvalCommand.kt b/src/main/kotlin/ml/duncte123/skybot/commands/essentials/eval/EvalCommand.kt index 98b11b420..1e5d7b4ef 100644 --- a/src/main/kotlin/ml/duncte123/skybot/commands/essentials/eval/EvalCommand.kt +++ b/src/main/kotlin/ml/duncte123/skybot/commands/essentials/eval/EvalCommand.kt @@ -206,7 +206,7 @@ class EvalCommand : Command() { } } when (out) { - null, (out as String).isBlank() -> { + null -> { coroutine.coroutineContext.cancel() MessageUtils.sendSuccess(event.message) } @@ -237,6 +237,10 @@ class EvalCommand : Command() { } } else -> { + if(out.toString().isEmpty() || out.toString().isBlank() || out.toString() == "") { + MessageUtils.sendSuccess(event.message) + return + } if (isRanByBotOwner) { MessageBuilder() .append(out.toString()) diff --git a/src/main/kotlin/ml/duncte123/skybot/commands/music/RepeatCommand.kt b/src/main/kotlin/ml/duncte123/skybot/commands/music/RepeatCommand.kt index 55ff19035..97381162b 100644 --- a/src/main/kotlin/ml/duncte123/skybot/commands/music/RepeatCommand.kt +++ b/src/main/kotlin/ml/duncte123/skybot/commands/music/RepeatCommand.kt @@ -41,7 +41,7 @@ class RepeatCommand : MusicCommand() { } scheduler.isRepeating = !scheduler.isRepeating - MessageUtils.sendMsg(event, "Player was set to: **${if (scheduler.isRepeating) " " else "not"} repeating**") + MessageUtils.sendMsg(event, "Player was set to: **${if (scheduler.isRepeating) "" else "not "} repeating**") } override fun help(): String = "Makes the player repeat the currently playing song"