From 99bb04159d12ccf31bdef43ad2e2029fa1db1f87 Mon Sep 17 00:00:00 2001 From: Petteri <26197131+PetteriM1@users.noreply.github.com> Date: Wed, 26 Jul 2023 00:49:59 +0300 Subject: [PATCH] Add mintime similar to maxtime --- .gitignore | 15 +++++++------- .../java/com/jagrosh/jmusicbot/BotConfig.java | 20 ++++++++++++++++++- .../jmusicbot/commands/dj/PlaynextCmd.java | 6 ++++++ .../jmusicbot/commands/music/PlayCmd.java | 8 +++++++- .../jmusicbot/commands/music/SearchCmd.java | 12 +++++++++++ .../jmusicbot/playlist/PlaylistLoader.java | 4 +++- src/main/resources/reference.conf | 7 +++++++ 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 4ed319bec..a650691c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ -/nbproject/ -/target/ -/Playlists/ -/test/ -*.json -*.txt -nb*.xml \ No newline at end of file +/nbproject/ +/target/ +/Playlists/ +/test/ +*.json +*.txt +nb*.xml +.idea diff --git a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java index 1e1b6c100..834ff525b 100644 --- a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java +++ b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java @@ -42,7 +42,7 @@ public class BotConfig private String token, prefix, altprefix, helpWord, playlistsFolder, successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji; private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots; - private long owner, maxSeconds, aloneTimeUntilStop; + private long owner, maxSeconds, minSeconds, aloneTimeUntilStop; private OnlineStatus status; private Activity game; private Config aliases, transforms; @@ -87,6 +87,7 @@ public void load() updatealerts = config.getBoolean("updatealerts"); useEval = config.getBoolean("eval"); maxSeconds = config.getLong("maxtime"); + minSeconds = config.getLong("mintime"); aloneTimeUntilStop = config.getLong("alonetimeuntilstop"); playlistsFolder = config.getString("playlistsfolder"); aliases = config.getConfig("aliases"); @@ -315,12 +316,22 @@ public long getMaxSeconds() { return maxSeconds; } + + public long getMinSeconds() + { + return minSeconds; + } public String getMaxTime() { return FormatUtil.formatTime(maxSeconds * 1000); } + public String getMinTime() + { + return FormatUtil.formatTime(minSeconds * 1000); + } + public long getAloneTimeUntilStop() { return aloneTimeUntilStop; @@ -333,6 +344,13 @@ public boolean isTooLong(AudioTrack track) return Math.round(track.getDuration()/1000.0) > maxSeconds; } + public boolean isTooShort(AudioTrack track) + { + if(minSeconds<=0) + return false; + return Math.round(track.getDuration()/1000.0) < minSeconds; + } + public String[] getAliases(String command) { try diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java index 91d79be14..8b4652f48 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java @@ -82,6 +82,12 @@ private void loadSingle(AudioTrack track) +FormatUtil.formatTime(track.getDuration())+"` > `"+FormatUtil.formatTime(bot.getConfig().getMaxSeconds()*1000)+"`")).queue(); return; } + if(bot.getConfig().isTooShort(track)) + { + m.editMessage(FormatUtil.filter(event.getClient().getWarning()+" This track (**"+track.getInfo().title+"**) is shorter than the allowed minimum: `" + +FormatUtil.formatTime(track.getDuration())+"` < `"+FormatUtil.formatTime(bot.getConfig().getMinSeconds()*1000)+"`")).queue(); + return; + } AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); int pos = handler.addTrackToFront(new QueuedTrack(track, event.getAuthor()))+1; String addMsg = FormatUtil.filter(event.getClient().getSuccess()+" Added **"+track.getInfo().title diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java index 6f9a3d297..bc0b3298c 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java @@ -111,6 +111,12 @@ private void loadSingle(AudioTrack track, AudioPlaylist playlist) +FormatUtil.formatTime(track.getDuration())+"` > `"+FormatUtil.formatTime(bot.getConfig().getMaxSeconds()*1000)+"`")).queue(); return; } + if(bot.getConfig().isTooShort(track)) + { + m.editMessage(FormatUtil.filter(event.getClient().getWarning()+" This track (**"+track.getInfo().title+"**) is shorter than the allowed minimum: `" + +FormatUtil.formatTime(track.getDuration())+"` < `"+FormatUtil.formatTime(bot.getConfig().getMinSeconds()*1000)+"`")).queue(); + return; + } AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); int pos = handler.addTrack(new QueuedTrack(track, event.getAuthor()))+1; String addMsg = FormatUtil.filter(event.getClient().getSuccess()+" Added **"+track.getInfo().title @@ -141,7 +147,7 @@ private int loadPlaylist(AudioPlaylist playlist, AudioTrack exclude) { int[] count = {0}; playlist.getTracks().stream().forEach((track) -> { - if(!bot.getConfig().isTooLong(track) && !track.equals(exclude)) + if(!bot.getConfig().isTooLong(track) && !bot.getConfig().isTooShort(track) && !track.equals(exclude)) { AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); handler.addTrack(new QueuedTrack(track, event.getAuthor())); diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/music/SearchCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/music/SearchCmd.java index 9358698aa..e2aa6d87f 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/music/SearchCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/music/SearchCmd.java @@ -91,6 +91,12 @@ public void trackLoaded(AudioTrack track) +FormatUtil.formatTime(track.getDuration())+"` > `"+bot.getConfig().getMaxTime()+"`")).queue(); return; } + if(bot.getConfig().isTooShort(track)) + { + m.editMessage(FormatUtil.filter(event.getClient().getWarning()+" This track (**"+track.getInfo().title+"**) is shorter than the allowed minimum: `" + +FormatUtil.formatTime(track.getDuration())+"` < `"+bot.getConfig().getMinTime()+"`")).queue(); + return; + } AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); int pos = handler.addTrack(new QueuedTrack(track, event.getAuthor()))+1; m.editMessage(FormatUtil.filter(event.getClient().getSuccess()+" Added **"+track.getInfo().title @@ -113,6 +119,12 @@ public void playlistLoaded(AudioPlaylist playlist) +FormatUtil.formatTime(track.getDuration())+"` > `"+bot.getConfig().getMaxTime()+"`"); return; } + if(bot.getConfig().isTooShort(track)) + { + event.replyWarning("This track (**"+track.getInfo().title+"**) is shorter than the allowed minimum: `" + +FormatUtil.formatTime(track.getDuration())+"` < `"+bot.getConfig().getMinTime()+"`"); + return; + } AudioHandler handler = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); int pos = handler.addTrack(new QueuedTrack(track, event.getAuthor()))+1; event.replySuccess("Added **" + FormatUtil.filter(track.getInfo().title) diff --git a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java index 602235fcf..ddec70d35 100644 --- a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java +++ b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java @@ -182,6 +182,8 @@ public void trackLoaded(AudioTrack at) { if(config.isTooLong(at)) errors.add(new PlaylistLoadError(index, items.get(index), "This track is longer than the allowed maximum")); + else if(config.isTooShort(at)) + errors.add(new PlaylistLoadError(index, items.get(index), "This track is shorter than the allowed minimum")); else { at.setUserData(0L); @@ -213,7 +215,7 @@ else if(ap.getSelectedTrack()!=null) loaded.set(first, loaded.get(second)); loaded.set(second, tmp); } - loaded.removeIf(track -> config.isTooLong(track)); + loaded.removeIf(track -> config.isTooLong(track) || config.isTooShort(track)); loaded.forEach(at -> at.setUserData(0L)); tracks.addAll(loaded); loaded.forEach(at -> consumer.accept(at)); diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index b74103b3e..8821e01e9 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -99,6 +99,13 @@ stayinchannel = false maxtime = 0 +// This sets the minimum amount of seconds any track loaded can be. If not set or set +// to any number less than or equal to zero, there is no minimum time length. This time +// restriction applies to songs loaded from any source. + +mintime = 0 + + // This sets the amount of seconds the bot will stay alone on a voice channel until it // automatically leaves the voice channel and clears the queue. If not set or set // to any number less than or equal to zero, the bot won't leave when alone.