Skip to content

Commit fcade73

Browse files
Update /np to /playing with new embed
1 parent 4e62f3e commit fcade73

4 files changed

Lines changed: 40 additions & 59 deletions

File tree

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package technobot.commands.music;
22

33
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
4-
import net.dv8tion.jda.api.EmbedBuilder;
54
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
65
import technobot.TechnoBot;
76
import technobot.commands.Category;
87
import technobot.commands.Command;
98
import technobot.data.GuildData;
109
import technobot.handlers.MusicHandler;
11-
import technobot.listeners.MusicListener;
12-
import technobot.util.embeds.EmbedColor;
1310
import technobot.util.embeds.EmbedUtils;
1411

1512
/**
@@ -21,19 +18,18 @@ public class NowPlayingCommand extends Command {
2118

2219
public NowPlayingCommand(TechnoBot bot) {
2320
super(bot);
24-
this.name = "np";
25-
this.description = "Display the current playing song.";
21+
this.name = "playing";
22+
this.description = "Check what song is currently playing.";
2623
this.category = Category.MUSIC;
2724
}
2825

2926
@Override
3027
public void execute(SlashCommandInteractionEvent event) {
31-
MusicHandler music = GuildData.get(event.getGuild()).musicHandler;
32-
3328
// Verify the Music Manager isn't null.
29+
MusicHandler music = GuildData.get(event.getGuild()).musicHandler;
3430
if (music == null) {
3531
String text = ":sound: Not currently playing any music!";
36-
event.replyEmbeds(EmbedUtils.createDefault(text)).queue();
32+
event.replyEmbeds(EmbedUtils.createDefault(text)).setEphemeral(true).queue();
3733
return;
3834
}
3935

@@ -44,29 +40,6 @@ public void execute(SlashCommandInteractionEvent event) {
4440
event.replyEmbeds(EmbedUtils.createDefault(text)).queue();
4541
return;
4642
}
47-
48-
// Create progress bar
49-
int barLength = 17;
50-
String[] progressBarArray = new String[barLength];
51-
for (int i = 0; i < barLength; i++) {
52-
progressBarArray[i] = "⎯";
53-
}
54-
progressBarArray[(int) Math.floor((float) nowPlaying.getPosition() / nowPlaying.getDuration() * barLength)] = "◉";
55-
String progressBar = String.join("", progressBarArray);
56-
57-
long pos = nowPlaying.getPosition();
58-
String trackStart = MusicListener.formatTrackLength(pos);
59-
String trackEnd = MusicListener.formatTrackLength(nowPlaying.getInfo().length);
60-
61-
// Create and send embed message
62-
event.replyEmbeds(
63-
new EmbedBuilder()
64-
.setColor(EmbedColor.DEFAULT.color)
65-
.setTitle("Now Playing :musical_note: ")
66-
.setDescription("[" + nowPlaying.getInfo().title + "](" + nowPlaying.getInfo().uri + ")")
67-
.addField("Position", progressBar, false)
68-
.addField("Progress", "%s / %s".formatted(trackStart, trackEnd), false)
69-
.build()
70-
).queue();
43+
event.replyEmbeds(MusicHandler.displayTrack(nowPlaying, music)).queue();
7144
}
7245
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void execute(SlashCommandInteractionEvent event) {
4949
}
5050

5151
// Find working URL
52+
String userID = event.getUser().getId();
5253
try {
5354
String url;
5455
try {
@@ -58,7 +59,7 @@ public void execute(SlashCommandInteractionEvent event) {
5859
// Else search youtube using args
5960
url = "ytsearch:" + song;
6061
music.setLogChannel(event.getTextChannel());
61-
bot.musicListener.addTrack(event, url);
62+
bot.musicListener.addTrack(event, url, userID);
6263
return;
6364
}
6465
// Search youtube if using a soundcloud link
@@ -68,7 +69,7 @@ public void execute(SlashCommandInteractionEvent event) {
6869
}
6970
// Otherwise add real URL to queue
7071
music.setLogChannel(event.getTextChannel());
71-
bot.musicListener.addTrack(event, url);
72+
bot.musicListener.addTrack(event, url, userID);
7273
} catch (IndexOutOfBoundsException e) {
7374
String text = "Please specify a song a to play.";
7475
event.replyEmbeds(EmbedUtils.createError(text)).setEphemeral(true).queue();

src/main/java/technobot/handlers/MusicHandler.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
77
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
88
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
9-
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
109
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
1110
import net.dv8tion.jda.api.EmbedBuilder;
1211
import net.dv8tion.jda.api.audio.AudioSendHandler;
1312
import net.dv8tion.jda.api.entities.AudioChannel;
14-
import net.dv8tion.jda.api.entities.Guild;
13+
import net.dv8tion.jda.api.entities.MessageEmbed;
1514
import net.dv8tion.jda.api.entities.TextChannel;
1615
import org.jetbrains.annotations.NotNull;
1716
import org.jetbrains.annotations.Nullable;
@@ -233,26 +232,7 @@ public TrackScheduler(MusicHandler handler) {
233232
*/
234233
@Override
235234
public void onTrackStart(AudioPlayer player, @NotNull AudioTrack track) {
236-
//Grab Track Info
237-
String duration = MusicListener.formatTrackLength(track.getInfo().length);
238-
String thumb = getThumbnail(track);
239-
String repeat = (handler.isLoop()) ? "Enabled" : "Disabled";
240-
241-
//Create Embed Message
242-
handler.logChannel.sendMessageEmbeds(
243-
new EmbedBuilder()
244-
.setTitle("Now Playing")
245-
.setDescription("[" + track.getInfo().title + "](" + track.getInfo().uri + ")")
246-
.addField("Duration", "`"+duration+"`", true)
247-
.addField("Queue", "`"+(handler.queue.size()-1)+"`", true)
248-
.addField("Volume", "`"+handler.audioPlayer.getVolume()+"%`", true)
249-
.addField("Requester", "<@!979590525428580363>", true)
250-
.addField("Link", "[`Click Here`]("+track.getInfo().uri+")", true)
251-
.addField("Repeat", "`"+repeat+"`", true)
252-
.setColor(EmbedColor.DEFAULT.color)
253-
.setThumbnail(thumb)
254-
.build()
255-
).queue();
235+
handler.logChannel.sendMessageEmbeds(displayTrack(track, handler)).queue();
256236
}
257237

258238
@Override
@@ -294,11 +274,36 @@ public void onTrackStuck(@NotNull AudioPlayer player, AudioTrack track, long thr
294274
*
295275
* @return a URL to the song video thumbnail.
296276
*/
297-
public static String getThumbnail(AudioTrack track) {
277+
private static String getThumbnail(AudioTrack track) {
298278
String domain = SecurityUtils.getDomain(track.getInfo().uri);
299279
if (domain.equalsIgnoreCase("spotify") || domain.equalsIgnoreCase("apple")) {
300280
return ((ISRCAudioTrack) track).getArtworkURL();
301281
}
302282
return String.format("https://img.youtube.com/vi/%s/0.jpg", track.getInfo().uri.substring(32));
303283
}
284+
285+
/**
286+
* Creates an embed displaying details about a track.
287+
*
288+
* @param track the track to display details about.
289+
* @param handler the music handler instance.
290+
* @return a MessageEmbed displaying track details.
291+
*/
292+
public static MessageEmbed displayTrack(AudioTrack track, MusicHandler handler) {
293+
String duration = MusicListener.formatTrackLength(track.getInfo().length);
294+
String repeat = (handler.isLoop()) ? "Enabled" : "Disabled";
295+
String userMention = "<@!"+track.getUserData(String.class)+">";
296+
return new EmbedBuilder()
297+
.setTitle("Now Playing")
298+
.setDescription("[" + track.getInfo().title + "](" + track.getInfo().uri + ")")
299+
.addField("Duration", "`"+duration+"`", true)
300+
.addField("Queue", "`"+(handler.queue.size()-1)+"`", true)
301+
.addField("Volume", "`"+handler.audioPlayer.getVolume()+"%`", true)
302+
.addField("Requester", userMention, true)
303+
.addField("Link", "[`Click Here`]("+track.getInfo().uri+")", true)
304+
.addField("Repeat", "`"+repeat+"`", true)
305+
.setColor(EmbedColor.DEFAULT.color)
306+
.setThumbnail(getThumbnail(track))
307+
.build();
308+
}
304309
}

src/main/java/technobot/listeners/MusicListener.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ public boolean inChannel(@NotNull Member member) {
151151
*
152152
* @param event A slash command event.
153153
* @param url The track URL.
154+
* @param user The ID of the user that added this track.
154155
*/
155-
public void addTrack(SlashCommandInteractionEvent event, String url) {
156+
public void addTrack(SlashCommandInteractionEvent event, String url, String userID) {
156157
MusicHandler music = GuildData.get(event.getGuild()).musicHandler;
157158
if (music == null) return;
158159

@@ -167,8 +168,9 @@ public void addTrack(SlashCommandInteractionEvent event, String url) {
167168

168169
@Override
169170
public void trackLoaded(@NotNull AudioTrack audioTrack) {
170-
event.reply(":notes: | Added **"+audioTrack.getInfo().title+"** to the queue.").queue();
171+
audioTrack.setUserData(userID);
171172
music.enqueue(audioTrack);
173+
event.reply(":notes: | Added **"+audioTrack.getInfo().title+"** to the queue.").queue();
172174
}
173175

174176
@Override

0 commit comments

Comments
 (0)