-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
/test/ | ||
*.json | ||
*.txt | ||
nb*.xml | ||
nb*.xml | ||
.classpath | ||
.project | ||
.settings/ | ||
.circleci |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
@@ -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."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) | ||
|
@@ -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; | ||
} | ||
} |
There was a problem hiding this comment.
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