Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
Implement jagrosh#1556
Browse files Browse the repository at this point in the history
Hopefully would result in better performance in different regions.
  • Loading branch information
uncreativeCultist committed Jul 11, 2024
1 parent 2599d93 commit b4bcbc0
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
evalEngine;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots, autoNowPlaying;
private long owner, maxSeconds, aloneTimeUntilStop;
Expand Down Expand Up @@ -82,6 +82,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");
Expand Down Expand Up @@ -274,6 +275,11 @@ public String getSearching()
return searchingEmoji;
}

public String getPiped()
{
return "NONE".equalsIgnoreCase(pipedURL) ? null : pipedURL;
}

public Activity getGame()
{
return game;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private static CommandClient createCommandClient(BotConfig config, SettingsManag
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017 John Grosh <john.a.grosh@gmail.com>.
*
* 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 <john.a.grosh@gmail.com>
*/
public class Setpipedcmd extends OwnerCommand
{
public SetpipedCmd(Bot bot)
{
this.name = "setpiped";
this.help = "sets the Piped instance's URL";
this.arguments = "<url>";
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();
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.")
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,41 @@ public static String getUnsupportedBotReason(JDA jda)
ApplicationInfo info = jda.retrieveApplicationInfo().complete();
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;
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ songinstatus=false

altprefix = "NONE"

// If you set this, the bot will play streams from this Piped instance
// Depending on your server location, this may perform better than default youtube.
// For more information on Piped, check out the project page here: https://github.com/TeamPiped/Piped
// For a list of Piped instances and their URLs, check the wiki: https://github.com/TeamPiped/Piped/wiki/Instances

pipedURL = "NONE"


// If you set these, it will change the various emojis

Expand Down

0 comments on commit b4bcbc0

Please # to comment.