Skip to content

Commit 5c926a2

Browse files
Add pagination to /queue embeds
1 parent ce8b66a commit 5c926a2

1 file changed

Lines changed: 45 additions & 29 deletions

File tree

src/main/java/technobot/commands/music/QueueCommand.java

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
import net.dv8tion.jda.api.EmbedBuilder;
66
import net.dv8tion.jda.api.entities.MessageEmbed;
77
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
8+
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
89
import org.jetbrains.annotations.NotNull;
910
import technobot.TechnoBot;
1011
import technobot.commands.Category;
1112
import technobot.commands.Command;
1213
import technobot.data.GuildData;
1314
import technobot.handlers.MusicHandler;
15+
import technobot.listeners.ButtonListener;
1416
import technobot.listeners.MusicListener;
1517
import technobot.util.embeds.EmbedColor;
1618
import technobot.util.embeds.EmbedUtils;
1719

18-
import java.util.ListIterator;
20+
import java.util.ArrayList;
21+
import java.util.LinkedList;
22+
import java.util.List;
1923

2024
/**
2125
* Command that displays an embed to showcases the music queue.
@@ -24,6 +28,8 @@
2428
*/
2529
public class QueueCommand extends Command {
2630

31+
public static final int SONGS_PER_PAGE = 7;
32+
2733
public QueueCommand(TechnoBot bot) {
2834
super(bot);
2935
this.name = "queue";
@@ -41,28 +47,43 @@ public void execute(SlashCommandInteractionEvent event) {
4147
event.replyEmbeds(EmbedUtils.createDefault(text)).queue();
4248
return;
4349
}
44-
// Create embed and send to channel
45-
MessageEmbed embed = buildQueueEmbed(music.getQueue().listIterator(), music.getQueue().size());
46-
event.replyEmbeds(embed).queue();
50+
// Create embeds and send to channel
51+
List<MessageEmbed> embeds = buildQueueEmbeds(music.getQueue(), music.getQueue().size());
52+
ReplyCallbackAction action = event.replyEmbeds(embeds.get(0));
53+
if (embeds.size() > 1) {
54+
ButtonListener.sendPaginatedMenu(event.getUser().getId(), action, embeds);
55+
} else {
56+
action.queue();
57+
}
4758
}
4859

4960
/**
50-
* Builds a beautiful embed out of a music queue.
61+
* Builds a beautiful paginated embed out of the music queue.
5162
*
5263
* @param queue Iterator of the music queue.
5364
* @param queueSize Number of elements in queue.
5465
* @return MessageEmbed of the music queue.
5566
*/
56-
private @NotNull MessageEmbed buildQueueEmbed(@NotNull ListIterator<AudioTrack> queue, int queueSize) {
67+
private @NotNull List<MessageEmbed> buildQueueEmbeds(@NotNull LinkedList<AudioTrack> queue, int queueSize) {
5768
int count = 0;
58-
long queueTime = 0;
5969
StringBuilder description = new StringBuilder();
70+
List<MessageEmbed> embeds = new ArrayList<>();
71+
EmbedBuilder embed = new EmbedBuilder()
72+
.setColor(EmbedColor.DEFAULT.color)
73+
.setTitle("Music Queue :musical_note:");
6074

61-
while (queue.hasNext()) {
62-
AudioTrack track = queue.next();
63-
AudioTrackInfo trackInfo = track.getInfo();
64-
queueTime += trackInfo.length;
75+
// Calculate total playlist length
76+
long queueTime = 0;
77+
for (AudioTrack track : queue) {
78+
queueTime += track.getInfo().length;
79+
}
80+
String total = MusicListener.formatTrackLength(queueTime);
81+
String song = "Song";
82+
if (queueSize >= 3) { song += "s";}
83+
String footer = queueSize > 1 ? String.format("**%s %s in Queue | %s Total Length**", queueSize - 1, song, total) : "";
6584

85+
for (AudioTrack track : queue) {
86+
AudioTrackInfo trackInfo = track.getInfo();
6687
if (count == 0) { //Current playing track
6788
description.append("__Now Playing:__\n");
6889
description.append(String.format("[%s](%s) | ", trackInfo.title, trackInfo.uri));
@@ -75,25 +96,20 @@ public void execute(SlashCommandInteractionEvent event) {
7596
//Rest of the queue
7697
description.append(String.format("`%s.` [%s](%s) | ", count, trackInfo.title, trackInfo.uri));
7798
description.append(String.format("`%s`\n\n", MusicListener.formatTrackLength(trackInfo.length)));
78-
79-
//Footer information
80-
if (count == 10 || count + 1 == queueSize) {
81-
String total = MusicListener.formatTrackLength(queueTime);
82-
String song = "Song";
83-
if (queueSize >= 3) { // Make "Song" plural if necessary
84-
song += "s";
85-
}
86-
description.append(String.format("**%s %s in Queue | %s Total Length**", queueSize - 1, song, total));
87-
break;
88-
}
8999
count++;
100+
if (count % SONGS_PER_PAGE == 0) {
101+
// Add embed as new page
102+
description.append(footer);
103+
embed.setDescription(description.toString());
104+
embeds.add(embed.build());
105+
description = new StringBuilder();
106+
}
90107
}
91-
92-
//Add to embed and send message
93-
EmbedBuilder embed = new EmbedBuilder()
94-
.setColor(EmbedColor.DEFAULT.color)
95-
.setTitle("Music Queue :musical_note:")
96-
.setDescription(description.toString());
97-
return embed.build();
108+
if (count % SONGS_PER_PAGE != 0) {
109+
description.append(footer);
110+
embed.setDescription(description.toString());
111+
embeds.add(embed.build());
112+
}
113+
return embeds;
98114
}
99115
}

0 commit comments

Comments
 (0)