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

Convert queueing from reactions to Discord buttons. #1006

Open
wants to merge 3 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
52 changes: 0 additions & 52 deletions .circleci/config.yml

This file was deleted.

6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
/test/
*.json
*.txt
nb*.xml
nb*.xml
.classpath
.project
.settings/
.circleci
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 @@ -23,6 +23,7 @@
import com.jagrosh.jmusicbot.commands.general.*;
import com.jagrosh.jmusicbot.commands.music.*;
import com.jagrosh.jmusicbot.commands.owner.*;
import com.jagrosh.jmusicbot.queue.QueueButtonListener;
import com.jagrosh.jmusicbot.entities.Prompt;
import com.jagrosh.jmusicbot.gui.GUI;
import com.jagrosh.jmusicbot.settings.SettingsManager;
Expand Down Expand Up @@ -177,7 +178,7 @@ else if(config.getGame().getName().equalsIgnoreCase("none"))
.setActivity(nogame ? null : Activity.playing("loading..."))
.setStatus(config.getStatus()==OnlineStatus.INVISIBLE || config.getStatus()==OnlineStatus.OFFLINE
? OnlineStatus.INVISIBLE : OnlineStatus.DO_NOT_DISTURB)
.addEventListeners(cb.build(), waiter, new Listener(bot))
.addEventListeners(cb.build(), waiter, new Listener(bot), new QueueButtonListener(settings, cb.build()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not add another listener, it should re-use the EventWaiter

.setBulkDeleteSplittingEnabled(true)
.build();
bot.setJDA(jda);
Expand Down
250 changes: 123 additions & 127 deletions src/main/java/com/jagrosh/jmusicbot/commands/music/PlayCmd.java

Large diffs are not rendered by default.

123 changes: 86 additions & 37 deletions src/main/java/com/jagrosh/jmusicbot/commands/music/QueueCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.jagrosh.jmusicbot.commands.music;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.jagrosh.jdautilities.command.CommandEvent;
Expand All @@ -27,51 +28,54 @@
import com.jagrosh.jmusicbot.settings.RepeatMode;
import com.jagrosh.jmusicbot.settings.Settings;
import com.jagrosh.jmusicbot.utils.FormatUtil;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.exceptions.PermissionException;
import net.dv8tion.jda.api.interactions.components.Button;
import net.dv8tion.jda.api.interactions.components.Component;

/**
*
* @author John Grosh <john.a.grosh@gmail.com>
*/
public class QueueCmd extends MusicCommand
{
public class QueueCmd extends MusicCommand {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first instance of many where you have converted next-line braces to same-line braces. JMusicBot uses next-line.

void someMethod()
{
    // content
}

private final Paginator.Builder builder;

public QueueCmd(Bot bot)
{

public QueueCmd(Bot bot) {
super(bot);
this.name = "queue";
this.help = "shows the current queue";
this.arguments = "[pagenum]";
this.aliases = bot.getConfig().getAliases(this.name);
this.bePlaying = true;
this.botPermissions = new Permission[]{Permission.MESSAGE_ADD_REACTION,Permission.MESSAGE_EMBED_LINKS};
builder = new Paginator.Builder()
.setColumns(1)
.setFinalAction(m -> {try{m.clearReactions().queue();}catch(PermissionException ignore){}})
.setItemsPerPage(10)
.waitOnSinglePage(false)
.useNumberedItems(true)
.showPageNumbers(true)
.wrapPageEnds(true)
.setEventWaiter(bot.getWaiter())
.setTimeout(1, TimeUnit.MINUTES);
this.botPermissions = new Permission[] { Permission.MESSAGE_ADD_REACTION, Permission.MESSAGE_EMBED_LINKS };
builder = new Paginator.Builder().setColumns(1).setFinalAction(m -> {
try {
m.clearReactions().queue();
} catch (PermissionException ignore) {
}
}).setItemsPerPage(10).waitOnSinglePage(false).useNumberedItems(true).showPageNumbers(true).wrapPageEnds(true)
.setEventWaiter(bot.getWaiter()).setTimeout(1, TimeUnit.MINUTES);
// bot.
}

@Override
public void doCommand(CommandEvent event)
{
public void doCommand(CommandEvent event) {
int pagenum = 1;
try
{
try {
pagenum = Integer.parseInt(event.getArgs());
} catch (NumberFormatException ignore) {
}
catch(NumberFormatException ignore){}

AudioHandler ah = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler();
List<QueuedTrack> list = ah.getQueue().getList();

if(list.isEmpty())
{
Message nowp = ah.getNowPlaying(event.getJDA());
Expand All @@ -86,6 +90,32 @@ public void doCommand(CommandEvent event)
});
return;
}

if(pagenum > ah.getQueue().getNumberOfPages()){
event.reply("That page does not exist. The maximum number of pages on the queue is " + String.valueOf(ah.getQueue().getNumberOfPages()) + ". Showing last page.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be sending an extraneous message when we're not changing the behavior and the changed behavior is already visible (in the different page number).

pagenum = ah.getQueue().getNumberOfPages();
}

Settings settings = event.getClient().getSettingsFor(event.getGuild());
event.getChannel().sendMessage(getQueueEmbed(pagenum, event, ah, list))
.setActionRow(QueueCmd.getButtonsForQueue(pagenum, ah.getQueue().getNumberOfPages(), settings.getRepeatMode(), event.getClient().getSuccess())).queue((msg) -> {
ah.getQueue().setLastMessage(msg);
});

}

private static String getQueueTitle(AudioHandler ah, String success, int songslength, long total, RepeatMode repeatmode) {
StringBuilder sb = new StringBuilder();
if (ah.getPlayer().getPlayingTrack() != null) {
sb.append(ah.getPlayer().isPaused() ? JMusicBot.PAUSE_EMOJI : JMusicBot.PLAY_EMOJI).append(" **")
.append(ah.getPlayer().getPlayingTrack().getInfo().title).append("**\n");
}
return FormatUtil.filter(sb.append(success).append(" Current Queue | ").append(songslength)
.append(" entries | `").append(FormatUtil.formatTime(total)).append("` ")
.append(repeatmode.getEmoji() != null ? "| " + repeatmode.getEmoji() : "").toString());
}

private static MessageEmbed getQueueEmbed(int pagenum, CommandEvent event, AudioHandler ah, List<QueuedTrack> list){
String[] songs = new String[list.size()];
long total = 0;
for(int i=0; i<list.size(); i++)
Expand All @@ -94,25 +124,44 @@ public void doCommand(CommandEvent event)
songs[i] = list.get(i).toString();
}
Settings settings = event.getClient().getSettingsFor(event.getGuild());
long fintotal = total;
builder.setText((i1,i2) -> getQueueTitle(ah, event.getClient().getSuccess(), songs.length, fintotal, settings.getRepeatMode()))
.setItems(songs)
.setUsers(event.getAuthor())
.setColor(event.getSelfMember().getColor())
;
builder.build().paginate(event.getChannel(), pagenum);
int page_deficit = pagenum == ah.getQueue().getNumberOfPages() ? list.size() % 10 : 10;
String[] this_page = Arrays.copyOfRange(songs, (10*(pagenum-1)), ((10*(pagenum-1)) + page_deficit));
for(int i = 1; i < this_page.length + 1; i++) this_page[i-1] = String.valueOf(i + (10*(pagenum-1))) + ") " + this_page[i-1];
return new EmbedBuilder()
.setTitle(QueueCmd.getQueueTitle(ah, event.getClient().getSuccess(), songs.length, total, settings.getRepeatMode()))
.setDescription(String.join("\n", this_page))
.build();
}


private String getQueueTitle(AudioHandler ah, String success, int songslength, long total, RepeatMode repeatmode)
{
StringBuilder sb = new StringBuilder();
if(ah.getPlayer().getPlayingTrack()!=null)
public static MessageEmbed getQueueEmbed(int pagenum, ButtonClickEvent event, AudioHandler ah, List<QueuedTrack> list, RepeatMode rm, String success_emoji){
//Don't waste time by reacquiring them if this is being called from the queue command rather than the button.
if(ah == null) ah = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler();
if(list == null) list = ah.getQueue().getList();
if(rm == null) rm = RepeatMode.OFF;

//Return nullptr if there's nothing on the queue. This might happen if the queue is cleared whilst someone is browsing it.
if(list.isEmpty()) return null;
String[] songs = new String[list.size()];
long total = 0;
for(int i=0; i<list.size(); i++)
{
sb.append(ah.getPlayer().isPaused() ? JMusicBot.PAUSE_EMOJI : JMusicBot.PLAY_EMOJI).append(" **")
.append(ah.getPlayer().getPlayingTrack().getInfo().title).append("**\n");
total += list.get(i).getTrack().getDuration();
songs[i] = list.get(i).toString();
}
return FormatUtil.filter(sb.append(success).append(" Current Queue | ").append(songslength)
.append(" entries | `").append(FormatUtil.formatTime(total)).append("` ")
.append(repeatmode.getEmoji() != null ? "| "+repeatmode.getEmoji() : "").toString());
int page_deficit = pagenum == ah.getQueue().getNumberOfPages() ? list.size() % 10 : 10;
String[] this_page = Arrays.copyOfRange(songs, (10*(pagenum-1)), ((10*(pagenum-1)) + page_deficit));
for(int i = 1; i < this_page.length + 1; i++) this_page[i-1] = String.valueOf(i + (10*(pagenum-1))) + ") " + this_page[i-1];
return new EmbedBuilder()
.setTitle(QueueCmd.getQueueTitle(ah, success_emoji, songs.length, total, rm))
.setDescription(String.join("\n", this_page))
.build();
}

public static Component[] getButtonsForQueue(int page_num, int max_queue_pages, RepeatMode rm, String success_emoji){
Component[] btns = {null, null};
btns[0] = page_num == 1 ? Button.secondary("QUEUE_PREV:DISABLED", "Previous").asDisabled() : Button.secondary("QUEUE_PREV:" + page_num, "Previous").withEmoji(Emoji.fromUnicode("⬅️"));
btns[1] = page_num == max_queue_pages ? Button.secondary("QUEUE_NEXT:DISABLED", "Next").asDisabled() : Button.secondary("QUEUE_NEXT:" + page_num, "Next").withEmoji(Emoji.fromUnicode("➡️"));
return btns;
}
}
Loading