Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Piped links #1556

Open
wants to merge 4 commits into
base: master
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/test/
*.json
*.txt
nb*.xml
nb*.xml
.DS_Store
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;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots;
private long owner, maxSeconds, aloneTimeUntilStop;
private double skipratio;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -268,6 +269,11 @@ public String getSearching()
{
return searchingEmoji;
}

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

public Activity getGame()
{
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ private static CommandClient createCommandClient(BotConfig config, SettingsManag
RECOMMENDED_PERMS);
aboutCommand.setIsAuthor(false);
aboutCommand.setReplacementCharacter("\uD83C\uDFB6"); // 🎶

// set up the command client
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 @@ -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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 @@ -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;
}
}
}
5 changes: 5 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "🎶"
Expand Down