Compare commits

..
2 Commits
Author SHA1 Message Date
John Grosh c88979a6dd fixed npe during autoplaylists
added playnext command (simple version)
setting dj role to everyone works now
fixed massively wide joptionpanes
version number bump
2018-11-21 23:34:18 -05:00
John Grosh a6f7c9b3ef dependency update 2018-11-21 19:07:04 -05:00
12 changed files with 179 additions and 23 deletions

No files matched your search

+2 -2
View File
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.jagrosh</groupId>
<artifactId>JMusicBot</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>
<packaging>jar</packaging>
<repositories>
@@ -18,7 +18,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.8.1_446</version>
<version>3.8.1_447</version>
</dependency>
<dependency>
<groupId>com.sedmelluq</groupId>
+1 -1
View File
@@ -130,7 +130,7 @@ public class Bot
{
ah.stopAndClear();
ah.getPlayer().destroy();
nowplaying.updateTopic(g.getIdLong(), ah);
nowplaying.updateTopic(g.getIdLong(), ah, true);
}
});
jda.shutdown();
@@ -167,15 +167,9 @@ public class BotConfig
// if we get through the whole config, it's good to go
valid = true;
}
catch (ConfigException.Missing | ConfigException.WrongType ex)
catch (ConfigException ex)
{
prompt.alert(Prompt.Level.ERROR, CONTEXT, ex.getMessage());
ex.printStackTrace();
}
catch (ConfigException ex)
{
prompt.alert(Prompt.Level.WARNING, CONTEXT, "'"+getConfigLocation()+"' was not found!");
ex.printStackTrace();
prompt.alert(Prompt.Level.ERROR, CONTEXT, ex + ": " + ex.getMessage());
}
}
@@ -114,6 +114,7 @@ public class JMusicBot
new ForceskipCmd(bot),
new PauseCmd(bot),
new PlaynextCmd(bot, config.getLoading()),
new RepeatCmd(bot),
new SkiptoCmd(bot),
new StopCmd(bot),
@@ -123,7 +124,6 @@ public class JMusicBot
new SettcCmd(),
new SetvcCmd(),
//new GuildlistCommand(waiter),
new AutoplaylistCmd(bot),
new PlaylistCmd(bot),
new SetavatarCmd(),
@@ -32,7 +32,6 @@ import com.jagrosh.jmusicbot.utils.FormatUtil;
import static com.jagrosh.jmusicbot.utils.FormatUtil.formatTime;
import static com.jagrosh.jmusicbot.utils.FormatUtil.volumeIcon;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.MessageBuilder;
@@ -64,6 +63,20 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
this.guildId = guild.getIdLong();
}
public int addTrackToFront(QueuedTrack qtrack)
{
if(audioPlayer.getPlayingTrack()==null)
{
audioPlayer.playTrack(qtrack.getTrack());
return -1;
}
else
{
queue.addAt(0, qtrack);
return 0;
}
}
public int addTrack(QueuedTrack qtrack)
{
if(audioPlayer.getPlayingTrack()==null)
@@ -105,7 +118,7 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
public long getRequester()
{
if(audioPlayer.getPlayingTrack()==null)
if(audioPlayer.getPlayingTrack()==null || audioPlayer.getPlayingTrack().getUserData(Long.class)==null)
return 0;
return audioPlayer.getPlayingTrack().getUserData(Long.class);
}
@@ -145,7 +158,7 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
// if the track ended normally, and we're in repeat mode, re-add it to the queue
if(endReason==AudioTrackEndReason.FINISHED && manager.getBot().getSettingsManager().getSettings(guildId).getRepeatMode())
{
queue.add(new QueuedTrack(track.makeClone(), track.getUserData(Long.class)));
queue.add(new QueuedTrack(track.makeClone(), track.getUserData(Long.class)==null ? 0L : track.getUserData(Long.class)));
}
if(queue.isEmpty())
@@ -99,7 +99,7 @@ public class NowplayingHandler
toRemove.forEach(id -> lastNP.remove(id));
}
public void updateTopic(long guildId, AudioHandler handler)
public void updateTopic(long guildId, AudioHandler handler, boolean wait)
{
Guild guild = bot.getJDA().getGuildById(guildId);
if(guild==null)
@@ -120,7 +120,10 @@ public class NowplayingHandler
{
try
{
tchan.getManager().setTopic(text).queue();
if(wait)
tchan.getManager().setTopic(text).complete();
else
tchan.getManager().setTopic(text).queue();
}
catch(PermissionException e) {}
}
@@ -140,7 +143,7 @@ public class NowplayingHandler
}
// update channel topic if applicable
updateTopic(guildId, handler);
updateTopic(guildId, handler, false);
}
public void onMessageDelete(Guild guild, long messageId)
@@ -39,7 +39,7 @@ public abstract class DJCommand extends MusicCommand
return true;
Settings settings = event.getClient().getSettingsFor(event.getGuild());
Role dj = settings.getRole(event.getGuild());
return dj!=null && event.getMember().getRoles().contains(dj);
return dj!=null && (event.getMember().getRoles().contains(dj) || dj.getIdLong()==event.getGuild().getIdLong());
});
}
}
@@ -0,0 +1,135 @@
/*
* Copyright 2018 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.dj;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.menu.ButtonMenu;
import com.jagrosh.jmusicbot.Bot;
import com.jagrosh.jmusicbot.audio.AudioHandler;
import com.jagrosh.jmusicbot.audio.QueuedTrack;
import com.jagrosh.jmusicbot.commands.DJCommand;
import com.jagrosh.jmusicbot.commands.music.PlayCmd;
import static com.jagrosh.jmusicbot.commands.music.PlayCmd.CANCEL;
import static com.jagrosh.jmusicbot.commands.music.PlayCmd.LOAD;
import com.jagrosh.jmusicbot.utils.FormatUtil;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.exceptions.PermissionException;
/**
*
* @author John Grosh (john.a.grosh@gmail.com)
*/
public class PlaynextCmd extends DJCommand
{
private final String loadingEmoji;
public PlaynextCmd(Bot bot, String loadingEmoji)
{
super(bot);
this.loadingEmoji = loadingEmoji;
this.name = "playnext";
this.arguments = "<title|URL>";
this.help = "plays a single song next";
this.beListening = true;
this.bePlaying = false;
}
@Override
public void doCommand(CommandEvent event)
{
if(event.getArgs().isEmpty() && event.getMessage().getAttachments().isEmpty())
{
event.replyWarning("Please include a song title or URL!");
return;
}
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();
event.reply(loadingEmoji+" Loading... `["+args+"]`", m -> bot.getPlayerManager().loadItemOrdered(event.getGuild(), args, new ResultHandler(m,event,false)));
}
private class ResultHandler implements AudioLoadResultHandler
{
final Message m;
final CommandEvent event;
final boolean ytsearch;
private ResultHandler(Message m, CommandEvent event, boolean ytsearch)
{
this.m = m;
this.event = event;
this.ytsearch = ytsearch;
}
private void loadSingle(AudioTrack track)
{
if(bot.getConfig().isTooLong(track))
{
m.editMessage(FormatUtil.filter(event.getClient().getWarning()+" This track (**"+track.getInfo().title+"**) is longer than the allowed maximum: `"
+FormatUtil.formatTime(track.getDuration())+"` > `"+FormatUtil.formatTime(bot.getConfig().getMaxSeconds()*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
+"** (`"+FormatUtil.formatTime(track.getDuration())+"`) "+(pos==0?"to begin playing":" to the queue at position "+pos));
m.editMessage(addMsg).queue();
}
@Override
public void trackLoaded(AudioTrack track)
{
loadSingle(track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist)
{
AudioTrack single;
if(playlist.getTracks().size()==1 || playlist.isSearchResult())
single = playlist.getSelectedTrack()==null ? playlist.getTracks().get(0) : playlist.getSelectedTrack();
else if (playlist.getSelectedTrack()!=null)
single = playlist.getSelectedTrack();
else
single = playlist.getTracks().get(0);
loadSingle(single);
}
@Override
public void noMatches()
{
if(ytsearch)
m.editMessage(FormatUtil.filter(event.getClient().getWarning()+" No results found for `"+event.getArgs()+"`.")).queue();
else
bot.getPlayerManager().loadItemOrdered(event.getGuild(), "ytsearch:"+event.getArgs(), new ResultHandler(m,event,true));
}
@Override
public void loadFailed(FriendlyException throwable)
{
if(throwable.severity==FriendlyException.Severity.COMMON)
m.editMessage(event.getClient().getError()+" Error loading: "+throwable.getMessage()).queue();
else
m.editMessage(event.getClient().getError()+" Error loading track.").queue();
}
}
}
@@ -77,7 +77,7 @@ public class Prompt
case WARNING: option = JOptionPane.WARNING_MESSAGE; break;
case ERROR: option = JOptionPane.ERROR_MESSAGE; break;
}
JOptionPane.showMessageDialog(null, message, title, option);
JOptionPane.showMessageDialog(null, "<html><body><p style='width: 400px;'>"+message, title, option);
}
catch(Exception e)
{
@@ -173,6 +173,7 @@ public class PlaylistLoader
errors.add(new PlaylistLoadError(index, items.get(index), "This track is longer than the allowed maximum"));
else
{
at.setUserData(0L);
tracks.add(at);
consumer.accept(at);
}
@@ -189,6 +190,7 @@ public class PlaylistLoader
errors.add(new PlaylistLoadError(index, items.get(index), "This track is longer than the allowed maximum"));
else
{
ap.getTracks().get(0).setUserData(0L);
tracks.add(ap.getTracks().get(0));
consumer.accept(ap.getTracks().get(0));
}
@@ -199,6 +201,7 @@ public class PlaylistLoader
errors.add(new PlaylistLoadError(index, items.get(index), "This track is longer than the allowed maximum"));
else
{
ap.getSelectedTrack().setUserData(0L);
tracks.add(ap.getSelectedTrack());
consumer.accept(ap.getSelectedTrack());
}
@@ -207,7 +210,6 @@ public class PlaylistLoader
{
List<AudioTrack> loaded = new ArrayList<>(ap.getTracks());
if(shuffle)
for(int first =0; first<loaded.size(); first++)
{
int second = (int)(Math.random()*loaded.size());
@@ -216,6 +218,7 @@ public class PlaylistLoader
loaded.set(second, tmp);
}
loaded.removeIf(track -> config.isTooLong(track));
loaded.forEach(at -> at.setUserData(0L));
tracks.addAll(loaded);
loaded.forEach(at -> consumer.accept(at));
}
@@ -47,6 +47,14 @@ public class FairQueue<T extends Queueable> {
return lastIndex;
}
public void addAt(int index, T item)
{
if(index >= list.size())
list.add(item);
else
list.add(index, item);
}
public int size()
{
return list.size();
+2 -2
View File
@@ -39,7 +39,7 @@ prefix="@mention"
// where X is the title. If you don't include an action, it will use the
// default of "Playing"
game=DEFAULT
game="DEFAULT"
// If you set this, it will modify the default status of bot
@@ -58,7 +58,7 @@ songinstatus=false
// If you set this, the bot will also use this prefix in addition to
// the one provided above
altprefix=NONE
altprefix="NONE"
// If you set these, it will change the various emojis