From 61d48df8c13dfd5bb1b89c9a30bb65eea9f8be30 Mon Sep 17 00:00:00 2001 From: Ben Yohros <68928883+Yohr011@users.noreply.github.com> Date: Wed, 3 Apr 2024 22:56:50 -0400 Subject: [PATCH 1/4] Update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4ed319bec..9866a878c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /test/ *.json *.txt -nb*.xml \ No newline at end of file +nb*.xml +.DS_Store \ No newline at end of file From 419f5969a8719eef8235a1e1e0fec8ca3ebc3c41 Mon Sep 17 00:00:00 2001 From: Ben Yohros <68928883+Yohr011@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:47:19 -0400 Subject: [PATCH 2/4] Created setpiped command Added a command to set the Piped instance URL. Also added Piped to the config file. --- .../java/com/jagrosh/jmusicbot/BotConfig.java | 8 ++- .../java/com/jagrosh/jmusicbot/JMusicBot.java | 4 +- .../jmusicbot/commands/owner/DebugCmd.java | 1 + .../jmusicbot/commands/owner/SetpipedCmd.java | 61 +++++++++++++++++++ .../jagrosh/jmusicbot/utils/OtherUtil.java | 37 +++++++++++ src/main/resources/reference.conf | 5 ++ 6 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java diff --git a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java index 9a57beff9..27d34bc9d 100644 --- a/src/main/java/com/jagrosh/jmusicbot/BotConfig.java +++ b/src/main/java/com/jagrosh/jmusicbot/BotConfig.java @@ -40,7 +40,7 @@ public class BotConfig private Path path = null; private String token, prefix, altprefix, helpWord, playlistsFolder, logLevel, - successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji; + successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji, pipedURL; private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots; private long owner, maxSeconds, aloneTimeUntilStop; private double skipratio; @@ -80,6 +80,7 @@ public void load() errorEmoji = config.getString("error"); loadingEmoji = config.getString("loading"); searchingEmoji = config.getString("searching"); + pipedURL = config.getString("piped"); game = OtherUtil.parseGame(config.getString("game")); status = OtherUtil.parseStatus(config.getString("status")); stayInChannel = config.getBoolean("stayinchannel"); @@ -268,6 +269,11 @@ public String getSearching() { return searchingEmoji; } + + public String getPiped() + { + return "NONE".equalsIgnoreCase(pipedURL) ? null : pipedURL; + } public Activity getGame() { diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index 66071a1fa..2b77ad253 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -177,11 +177,13 @@ private static CommandClient createCommandClient(BotConfig config, SettingsManag RECOMMENDED_PERMS); aboutCommand.setIsAuthor(false); aboutCommand.setReplacementCharacter("\uD83C\uDFB6"); // 🎶 - + // set up the command client + //TODO: Add piped command CommandClientBuilder cb = new CommandClientBuilder() .setPrefix(config.getPrefix()) .setAlternativePrefix(config.getAltPrefix()) + // .setPipedURL(config.getPiped()) .setOwnerId(Long.toString(config.getOwnerId())) .setEmojis(config.getSuccess(), config.getWarning(), config.getError()) .setHelpWord(config.getHelp()) diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/owner/DebugCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/owner/DebugCmd.java index 829955d9f..bf31385f8 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/owner/DebugCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/owner/DebugCmd.java @@ -57,6 +57,7 @@ protected void execute(CommandEvent event) .append("\n Owner = ").append(bot.getConfig().getOwnerId()) .append("\n Prefix = ").append(bot.getConfig().getPrefix()) .append("\n AltPrefix = ").append(bot.getConfig().getAltPrefix()) + .append("\n PipedURL = ").append(bot.getConfig().getPiped()) .append("\n MaxSeconds = ").append(bot.getConfig().getMaxSeconds()) .append("\n NPImages = ").append(bot.getConfig().useNPImages()) .append("\n SongInStatus = ").append(bot.getConfig().getSongInStatus()) diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java new file mode 100644 index 000000000..b838fbf15 --- /dev/null +++ b/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java @@ -0,0 +1,61 @@ +/* + * Copyright 2017 John Grosh . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jagrosh.jmusicbot.commands.owner; + +import java.io.IOException; +import java.io.InputStream; +import com.jagrosh.jdautilities.command.CommandEvent; +import com.jagrosh.jmusicbot.Bot; +import com.jagrosh.jmusicbot.commands.OwnerCommand; +import com.jagrosh.jmusicbot.utils.OtherUtil; +import net.dv8tion.jda.api.entities.Icon; + +/** + * + * @author John Grosh + */ +public class Setpipedcmd extends OwnerCommand +{ + public SetpipedCmd(Bot bot) + { + this.name = "setpiped"; + this.help = "sets the Piped instance's URL"; + this.arguments = ""; + this.aliases = bot.getConfig().getAliases(this.name); + this.guildOnly = false; + } + + @Override + protected void execute(CommandEvent event) + { + String url; + if(event.getArgs().isEmpty()) + url = null; + else + url = event.getArgs(); + //TODO: Add util to fetch Piped URL + boolean isPiped = OtherUtil.isPipedInstance(url); + if(!isPiped) + { + event.reply(event.getClient().getError()+" Invalid Piped URL"); + } + else + { + pipedURL = url; + event.reply(event.getClient().getSuccess()+" Successfully changed Piped URL.") + } + } +} diff --git a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java index a22175d02..46687d57f 100644 --- a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java +++ b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java @@ -228,4 +228,41 @@ public static String getUnsupportedBotReason(JDA jda) return null; } + + public static boolean isPipedInstance(String url) + { + try + { + Response response = new OkHttpClient.Builder().build() + .newCall(new Request.Builder().get().url(url + "/trending").build()) + .execute(); + ResponseBody body = response.body(); + if(body != null) + { + try(Reader reader = body.charStream()) + { + JSONObject obj = new JSONObject(new JSONTokener(reader)); + try { + if (obj.getString("error") == "region is a required parameter") { + return true; + } + return false; + } + catch (Exception e) { + return false; + } + } + finally + { + response.close(); + } + } + else + return false; + } + catch(IOException | JSONException | NullPointerException ex) + { + return false; + } + } } diff --git a/src/main/resources/reference.conf b/src/main/resources/reference.conf index 19206d3ab..276dd1769 100644 --- a/src/main/resources/reference.conf +++ b/src/main/resources/reference.conf @@ -61,6 +61,11 @@ songinstatus=false altprefix = "NONE" +// If you set this, the bot will play streams from this Piped instance + +pipedURL = "NONE" + + // If you set these, it will change the various emojis success = "🎶" From 375ccf01f3ec0901b7931fec605e9f06ace9d351 Mon Sep 17 00:00:00 2001 From: Ben Yohros <68928883+Yohr011@users.noreply.github.com> Date: Fri, 26 Apr 2024 14:16:28 -0300 Subject: [PATCH 3/4] Accept Piped commands Bot will accept music commands using the user's Piped instance links. --- .../java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java | 3 +++ .../java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java | 4 ++++ 2 files changed, 7 insertions(+) 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..b062b6641 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/dj/PlaynextCmd.java @@ -58,6 +58,9 @@ public void doCommand(CommandEvent event) String args = event.getArgs().startsWith("<") && event.getArgs().endsWith(">") ? event.getArgs().substring(1,event.getArgs().length()-1) : event.getArgs().isEmpty() ? event.getMessage().getAttachments().get(0).getUrl() : event.getArgs(); + if (args == event.getMessage().getAttachments().get(0).getUrl() && bot.getConfig().getPiped() != null && args.contains(bot.getConfig().getPiped())) { + args.replace(bot.getConfig().getPiped(), "youtube.com"); + } event.reply(loadingEmoji+" Loading... `["+args+"]`", m -> bot.getPlayerManager().loadItemOrdered(event.getGuild(), args, new ResultHandler(m,event,false))); } 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..a9f9d14c6 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java @@ -84,9 +84,13 @@ public void doCommand(CommandEvent event) event.reply(builder.toString()); return; } + //TODO: Reformat this to if statements, if URL matches piped URL, replace with youtube. Repeat for all other play/queue commands String args = event.getArgs().startsWith("<") && event.getArgs().endsWith(">") ? event.getArgs().substring(1,event.getArgs().length()-1) : event.getArgs().isEmpty() ? event.getMessage().getAttachments().get(0).getUrl() : event.getArgs(); + if (args == event.getMessage().getAttachments().get(0).getUrl() && bot.getConfig().getPiped() != null && args.contains(bot.getConfig().getPiped())) { + args.replace(bot.getConfig().getPiped(), "youtube.com"); + } event.reply(loadingEmoji+" Loading... `["+args+"]`", m -> bot.getPlayerManager().loadItemOrdered(event.getGuild(), args, new ResultHandler(m,event,false))); } From dcc9ec065bf2d23b5274007be41bf70c9b80516c Mon Sep 17 00:00:00 2001 From: Ben Yohros <68928883+Yohr011@users.noreply.github.com> Date: Wed, 1 May 2024 16:27:44 -0300 Subject: [PATCH 4/4] Remove comments --- src/main/java/com/jagrosh/jmusicbot/JMusicBot.java | 1 - src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java | 1 - .../java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java | 1 - 3 files changed, 3 deletions(-) diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index 2b77ad253..42af34333 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -179,7 +179,6 @@ private static CommandClient createCommandClient(BotConfig config, SettingsManag aboutCommand.setReplacementCharacter("\uD83C\uDFB6"); // 🎶 // set up the command client - //TODO: Add piped command CommandClientBuilder cb = new CommandClientBuilder() .setPrefix(config.getPrefix()) .setAlternativePrefix(config.getAltPrefix()) 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 a9f9d14c6..6e00f1db0 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java @@ -84,7 +84,6 @@ public void doCommand(CommandEvent event) event.reply(builder.toString()); return; } - //TODO: Reformat this to if statements, if URL matches piped URL, replace with youtube. Repeat for all other play/queue commands String args = event.getArgs().startsWith("<") && event.getArgs().endsWith(">") ? event.getArgs().substring(1,event.getArgs().length()-1) : event.getArgs().isEmpty() ? event.getMessage().getAttachments().get(0).getUrl() : event.getArgs(); diff --git a/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java b/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java index b838fbf15..93808b4b5 100644 --- a/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java +++ b/src/main/java/com/jagrosh/jmusicbot/commands/owner/SetpipedCmd.java @@ -46,7 +46,6 @@ protected void execute(CommandEvent event) url = null; else url = event.getArgs(); - //TODO: Add util to fetch Piped URL boolean isPiped = OtherUtil.isPipedInstance(url); if(!isPiped) {