|
| 1 | +/* |
| 2 | + * Copyright 2018 John Grosh <[email protected]>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.jagrosh.jmusicbot.commands.music; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import com.jagrosh.jdautilities.command.CommandEvent; |
| 22 | +import com.jagrosh.jdautilities.menu.Paginator; |
| 23 | +import com.jagrosh.jmusicbot.Bot; |
| 24 | +import com.jagrosh.jmusicbot.audio.AudioHandler; |
| 25 | +import com.jagrosh.jmusicbot.audio.QueuedTrack; |
| 26 | +import com.jagrosh.jmusicbot.audio.StatusHandler; |
| 27 | +import com.jagrosh.jmusicbot.commands.MusicCommand; |
| 28 | +import com.jagrosh.jmusicbot.settings.QueueType; |
| 29 | +import com.jagrosh.jmusicbot.settings.RepeatMode; |
| 30 | +import com.jagrosh.jmusicbot.settings.Settings; |
| 31 | +import com.jagrosh.jmusicbot.utils.FormatUtil; |
| 32 | +import com.jagrosh.jmusicbot.utils.TimeUtil; |
| 33 | + |
| 34 | +import net.dv8tion.jda.api.MessageBuilder; |
| 35 | +import net.dv8tion.jda.api.Permission; |
| 36 | +import net.dv8tion.jda.api.entities.Message; |
| 37 | +import net.dv8tion.jda.api.exceptions.PermissionException; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + * @author John Grosh <[email protected]> |
| 42 | + */ |
| 43 | +public class QueueCmd extends MusicCommand |
| 44 | +{ |
| 45 | + private final Paginator.Builder builder; |
| 46 | + |
| 47 | + public QueueCmd(Bot bot) |
| 48 | + { |
| 49 | + super(bot); |
| 50 | + this.name = "queue"; |
| 51 | + this.help = "shows the current queue"; |
| 52 | + this.arguments = "[pagenum]"; |
| 53 | + this.aliases = bot.getConfig().getAliases(this.name); |
| 54 | + this.bePlaying = true; |
| 55 | + this.botPermissions = new Permission[]{Permission.MESSAGE_ADD_REACTION,Permission.MESSAGE_EMBED_LINKS}; |
| 56 | + builder = new Paginator.Builder() |
| 57 | + .setColumns(1) |
| 58 | + .setFinalAction(m -> {try{m.clearReactions().queue();}catch(PermissionException ignore){}}) |
| 59 | + .setItemsPerPage(10) |
| 60 | + .waitOnSinglePage(false) |
| 61 | + .useNumberedItems(true) |
| 62 | + .showPageNumbers(true) |
| 63 | + .wrapPageEnds(true) |
| 64 | + .setEventWaiter(bot.getWaiter()) |
| 65 | + .setTimeout(1, TimeUnit.MINUTES); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void doCommand(CommandEvent event) |
| 70 | + { |
| 71 | + int pagenum = 1; |
| 72 | + try |
| 73 | + { |
| 74 | + pagenum = Integer.parseInt(event.getArgs()); |
| 75 | + } |
| 76 | + catch(NumberFormatException ignore){} |
| 77 | + AudioHandler ah = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler(); |
| 78 | + StatusHandler sh = (StatusHandler)event.getGuild().getAudioManager().getSendingHandler(); |
| 79 | + List<QueuedTrack> list = ah.getQueue().getList(); |
| 80 | + if(list.isEmpty()) |
| 81 | + { |
| 82 | + Message nowp = sh.getNowPlaying(event.getJDA()); |
| 83 | + Message nonowp = sh.getNoMusicPlaying(event.getJDA()); |
| 84 | + Message built = new MessageBuilder() |
| 85 | + .setContent(event.getClient().getWarning() + " There is no music in the queue!") |
| 86 | + .setEmbeds((nowp==null ? nonowp : nowp).getEmbeds().get(0)).build(); |
| 87 | + event.reply(built, m -> |
| 88 | + { |
| 89 | + if(nowp!=null) |
| 90 | + bot.getNowplayingHandler().setLastNPMessage(m); |
| 91 | + }); |
| 92 | + return; |
| 93 | + } |
| 94 | + String[] songs = new String[list.size()]; |
| 95 | + long total = 0; |
| 96 | + for(int i=0; i<list.size(); i++) |
| 97 | + { |
| 98 | + total += list.get(i).getTrack().getDuration(); |
| 99 | + songs[i] = list.get(i).toString(); |
| 100 | + } |
| 101 | + Settings settings = event.getClient().getSettingsFor(event.getGuild()); |
| 102 | + long fintotal = total; |
| 103 | + builder.setText((i1,i2) -> getQueueTitle(ah, event.getClient().getSuccess(), songs.length, fintotal, settings.getRepeatMode(), settings.getQueueType())) |
| 104 | + .setItems(songs) |
| 105 | + .setUsers(event.getAuthor()) |
| 106 | + .setColor(event.getSelfMember().getColor()) |
| 107 | + ; |
| 108 | + builder.build().paginate(event.getChannel(), pagenum); |
| 109 | + } |
| 110 | + |
| 111 | + private String getQueueTitle(AudioHandler ah, String success, int songslength, long total, RepeatMode repeatmode, QueueType queueType) |
| 112 | + { |
| 113 | + StringBuilder sb = new StringBuilder(); |
| 114 | + if(ah.getPlayer().getPlayingTrack()!=null) |
| 115 | + { |
| 116 | + sb.append(ah.getStatusEmoji()).append(" **") |
| 117 | + .append(ah.getPlayer().getPlayingTrack().getInfo().title).append("**\n"); |
| 118 | + } |
| 119 | + return FormatUtil.filter(sb.append(success).append(" Current Queue | ").append(songslength) |
| 120 | + .append(" entries | `").append(TimeUtil.formatTime(total)).append("` ") |
| 121 | + .append("| ").append(queueType.getEmoji()).append(" `").append(queueType.getUserFriendlyName()).append('`') |
| 122 | + .append(repeatmode.getEmoji() != null ? " | "+repeatmode.getEmoji() : "").toString()); |
| 123 | + } |
| 124 | +} |
0 commit comments