Skip to content

Commit 5df7a48

Browse files
committed
Finally got somehere... still lots more to add overtime!
1 parent 07a78e0 commit 5df7a48

7 files changed

Lines changed: 218 additions & 54 deletions

File tree

src/main/java/org/mangorage/mangobotplugin/commands/music/GuildMusicManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public GuildMusicManager(long guildId, LavalinkClient lavalink) {
1717
this.guildId = guildId;
1818
}
1919

20+
public void next() {
21+
if (this.scheduler.queue.isEmpty()) return;
22+
final var track = this.scheduler.queue.poll();
23+
getPlayer().ifPresent(player -> {
24+
player.setPaused(false).setTrack(track).subscribe();
25+
});
26+
}
27+
2028
public void stop() {
2129
this.scheduler.queue.clear();
2230

src/main/java/org/mangorage/mangobotplugin/commands/music/impl/MusicCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public MusicCommand(String name, LavalinkClient client) {
2626
addSubCommand(new JoinCommand(this));
2727
addSubCommand(new PlayCommand(this));
2828
addSubCommand(new PauseCommand(this));
29+
addSubCommand(new QueueCommand(this));
2930
addSubCommand(new StopCommand(this));
31+
addSubCommand(new SkipCommand(this));
32+
addSubCommand(new StatusCommand(this));
3033
}
3134

3235
@Override

src/main/java/org/mangorage/mangobotplugin/commands/music/impl/PlayCommand.java

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@
55
import net.dv8tion.jda.api.entities.Member;
66
import net.dv8tion.jda.api.entities.Message;
77
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
8-
import org.mangorage.mangobotcore.api.command.v1.argument.OptionalArg;
9-
import org.mangorage.mangobotcore.api.command.v1.argument.RequiredArg;
10-
import org.mangorage.mangobotcore.api.command.v1.argument.types.StringArgumentType;
118
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
129
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
1310
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandType;
14-
import org.mangorage.mangobotplugin.commands.music.AudioLoader;
1511
import org.mangorage.mangobotplugin.commands.music.IMusicManager;
1612

17-
import java.util.concurrent.TimeUnit;
18-
1913
public final class PlayCommand extends AbstractJDACommand {
2014

21-
private final OptionalArg<String> identifierArg = new OptionalArg<>("identifier", "Identifier", StringArgumentType.single(), null);
2215
private final IMusicManager manager;
2316

2417
public PlayCommand(IMusicManager manager) {
25-
super("play", "Play a song!");
18+
super("play", "Resume the current track!");
2619
this.manager = manager;
2720
}
2821

@@ -35,62 +28,36 @@ public JDACommandType getCommandType() {
3528
public JDACommandResult run(CommandContext<Message> commandContext) throws Throwable {
3629
final var context = commandContext.getContextObject();
3730
final var guild = context.getGuild();
38-
final var identifier = commandContext.getArgumentOrElse(identifierArg);
39-
4031
final var member = context.getMember();
4132

4233
if (!member.getVoiceState().inAudioChannel()) {
43-
context.reply("Must be in a voice chat!").queue();
34+
context.reply("You need to be in a voice channel.").queue();
4435
return JDACommandResult.PASS;
4536
}
4637

4738
final var guildManager = manager.getOrCreate(guild.getIdLong());
48-
final var player = guildManager.getPlayer();
4939

50-
if (identifier == null) {
51-
if (player.isPresent())
52-
player.get()
53-
.setPaused(false)
54-
.subscribe(plr -> {
55-
context.reply("Unpaused music!").queue();
56-
});
57-
} else {
58-
joinHelper(member, member.getJDA());
40+
// Make sure bot is in the same VC
41+
joinHelper(member, member.getJDA());
5942

60-
if (guildManager.getPlayer().isPresent()) {
61-
guildManager
62-
.getLink()
63-
.ifPresent(link -> {
64-
context.reply("Playing Track Soon!").queue();
65-
link.loadItem(identifier).subscribe(new AudioLoader(guildManager, context));
66-
});
67-
} else {
68-
69-
context.reply("Playing Track Soon!")
70-
.queueAfter(2, TimeUnit.SECONDS, m -> {
71-
guildManager
72-
.getLink()
73-
.ifPresent(link -> {
74-
link.loadItem(identifier).subscribe(new AudioLoader(guildManager, context));
75-
});
76-
});
77-
}
78-
}
43+
guildManager.getPlayer().ifPresentOrElse(player ->
44+
player.setPaused(false).subscribe(plr ->
45+
context.reply("Resumed music!").queue()
46+
),
47+
() -> context.reply("Nothing is playing.").queue()
48+
);
7949

8050
return JDACommandResult.PASS;
8151
}
8252

83-
// Makes sure that the bot is in a voice channel!
8453
private boolean joinHelper(Member member, JDA jda) {
85-
manager.getOrCreate(member.getGuild().getIdLong());
86-
87-
final GuildVoiceState memberVoiceState = member.getVoiceState();
54+
final GuildVoiceState voiceState = member.getVoiceState();
8855

89-
if (memberVoiceState.inAudioChannel()) {
90-
jda.getDirectAudioController().connect(memberVoiceState.getChannel());
56+
if (voiceState.inAudioChannel()) {
57+
jda.getDirectAudioController().connect(voiceState.getChannel());
9158
return true;
9259
}
9360

9461
return false;
9562
}
96-
}
63+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.mangorage.mangobotplugin.commands.music.impl;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.entities.GuildVoiceState;
5+
import net.dv8tion.jda.api.entities.Member;
6+
import net.dv8tion.jda.api.entities.Message;
7+
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
8+
import org.mangorage.mangobotcore.api.command.v1.argument.OptionalArg;
9+
import org.mangorage.mangobotcore.api.command.v1.argument.types.StringArgumentType;
10+
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
11+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
12+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandType;
13+
import org.mangorage.mangobotplugin.commands.music.AudioLoader;
14+
import org.mangorage.mangobotplugin.commands.music.IMusicManager;
15+
16+
import java.util.concurrent.TimeUnit;
17+
18+
public final class QueueCommand extends AbstractJDACommand {
19+
20+
private final OptionalArg<String> identifierArg =
21+
new OptionalArg<>("identifier", "Identifier", StringArgumentType.single(), null);
22+
23+
private final IMusicManager manager;
24+
25+
public QueueCommand(IMusicManager manager) {
26+
super("queue", "Queue a song!");
27+
this.manager = manager;
28+
}
29+
30+
@Override
31+
public JDACommandType getCommandType() {
32+
return JDACommandType.GUILD;
33+
}
34+
35+
@Override
36+
public JDACommandResult run(CommandContext<Message> commandContext) throws Throwable {
37+
final var context = commandContext.getContextObject();
38+
final var guild = context.getGuild();
39+
final var member = context.getMember();
40+
final var identifier = commandContext.getArgumentOrElse(identifierArg);
41+
42+
if (identifier == null) {
43+
context.reply("You need to provide something to queue.").queue();
44+
return JDACommandResult.PASS;
45+
}
46+
47+
if (!member.getVoiceState().inAudioChannel()) {
48+
context.reply("Must be in a voice chat!").queue();
49+
return JDACommandResult.PASS;
50+
}
51+
52+
final var guildManager = manager.getOrCreate(guild.getIdLong());
53+
54+
// Ensure bot joins
55+
joinHelper(member, member.getJDA());
56+
57+
context.reply("Processing...").queueAfter(2, TimeUnit.SECONDS, m -> {
58+
guildManager.getLink().ifPresent(link -> {
59+
link.loadItem(identifier).subscribe(new AudioLoader(guildManager, context));
60+
});
61+
});
62+
63+
64+
65+
return JDACommandResult.PASS;
66+
}
67+
68+
// same helper as your play command
69+
private boolean joinHelper(Member member, JDA jda) {
70+
manager.getOrCreate(member.getGuild().getIdLong());
71+
72+
final GuildVoiceState memberVoiceState = member.getVoiceState();
73+
74+
if (memberVoiceState.inAudioChannel()) {
75+
jda.getDirectAudioController().connect(memberVoiceState.getChannel());
76+
return true;
77+
}
78+
79+
return false;
80+
}
81+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.mangorage.mangobotplugin.commands.music.impl;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
5+
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
6+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
7+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandType;
8+
import org.mangorage.mangobotplugin.commands.music.IMusicManager;
9+
10+
public final class SkipCommand extends AbstractJDACommand {
11+
12+
private final IMusicManager manager;
13+
14+
public SkipCommand(IMusicManager manager) {
15+
super("skip", "Skip the current track!");
16+
this.manager = manager;
17+
}
18+
19+
@Override
20+
public JDACommandType getCommandType() {
21+
return JDACommandType.GUILD;
22+
}
23+
24+
@Override
25+
public JDACommandResult run(CommandContext<Message> commandContext) throws Throwable {
26+
final var context = commandContext.getContextObject();
27+
final var guild = context.getGuild();
28+
29+
final var guildManager = manager.getOrCreate(guild.getIdLong());
30+
31+
var playerOpt = guildManager.getPlayer();
32+
33+
if (playerOpt.isEmpty()) {
34+
context.reply("Nothing is playing.").queue();
35+
return JDACommandResult.PASS;
36+
}
37+
38+
39+
guildManager.next();
40+
41+
context.reply("Skipped track! ⏭").queue();
42+
43+
return JDACommandResult.PASS;
44+
}
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.mangorage.mangobotplugin.commands.music.impl;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
5+
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
6+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
7+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandType;
8+
import org.mangorage.mangobotplugin.commands.music.IMusicManager;
9+
10+
public final class StatusCommand extends AbstractJDACommand {
11+
12+
private final IMusicManager manager;
13+
14+
public StatusCommand(IMusicManager manager) {
15+
super("status", "Check music status!");
16+
this.manager = manager;
17+
}
18+
19+
@Override
20+
public JDACommandType getCommandType() {
21+
return JDACommandType.GUILD;
22+
}
23+
24+
@Override
25+
public JDACommandResult run(CommandContext<Message> commandContext) throws Throwable {
26+
final var context = commandContext.getContextObject();
27+
final var guild = context.getGuild();
28+
29+
final var guildManager = manager.getOrCreate(guild.getIdLong());
30+
31+
var playerOpt = guildManager.getPlayer();
32+
33+
if (playerOpt.isEmpty()) {
34+
context.reply("Nothing is playing.").queue();
35+
return JDACommandResult.PASS;
36+
}
37+
38+
var player = playerOpt.get();
39+
40+
String status;
41+
42+
if (player.getPaused()) {
43+
status = "Paused ⏸";
44+
} else {
45+
status = "Playing ▶";
46+
}
47+
48+
// Try to get track info if available (depends on your player implementation)
49+
String trackInfo = "Unknown track";
50+
51+
try {
52+
var track = player.getTrack(); // adjust if your API differs
53+
if (track != null && track.getInfo() != null) {
54+
trackInfo = track.getInfo().getTitle();
55+
}
56+
} catch (Exception ignored) {
57+
// If your API doesn’t support it, just ignore
58+
}
59+
60+
context.reply("**Status:** " + status + "\n**Track:** " + trackInfo).queue();
61+
62+
return JDACommandResult.PASS;
63+
}
64+
}

src/main/java/org/mangorage/mangobotplugin/commands/music/impl/StopCommand.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.mangorage.mangobotcore.api.command.v1.CommandContext;
55
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
66
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
7-
import org.mangorage.mangobotplugin.commands.music.AudioLoader;
87
import org.mangorage.mangobotplugin.commands.music.IMusicManager;
98

109
public final class StopCommand extends AbstractJDACommand {
@@ -21,13 +20,10 @@ public JDACommandResult run(CommandContext<Message> commandContext) throws Throw
2120
final var guild = commandContext.getContextObject().getGuild();
2221

2322
final var guildManager = manager.getOrCreate(guild.getIdLong());
24-
final var player = guildManager.getPlayer();
2523

26-
player.ifPresent(lp -> {
27-
lp.stopTrack().subscribe(plr -> {
28-
context.reply("Stopped Current Music Track!").queue();
29-
});
30-
});
24+
guildManager.stop();
25+
26+
context.reply("Stopped Current Music Track!").queue();
3127

3228
return JDACommandResult.PASS;
3329
}

0 commit comments

Comments
 (0)