Added stop and skip command

For sync purposes
This commit is contained in:
Anthony committed 2018-03-06 06:51:34 -05:00
1 parent 5131611bbb
commit ae5e4176bf
6 files changed
+161 -9

No files matched your search

@@ -48,7 +48,6 @@ public class JoinCommand extends AbstractCommand {
public String execute(MessageReceivedEvent event, String args) {
Member member = event.getMember();
MusicManager player = MusicManager.get(event.getGuild());
if (member.getVoiceState().inVoiceChannel()) {
VoiceChannel vChannel = member.getVoiceState().getChannel();
@@ -57,9 +57,10 @@ public class PlayCommand extends AbstractCommand {
player.play(args.split(" ")[0], event.getTextChannel(), false);
} else
event.getTextChannel().sendMessage("Not in your voice channel").queue();
} else
event.getTextChannel().sendMessage("Not in a voice channel").queue();
} else {
player.connect(vChannel);
player.play(args.split(" ")[0], event.getTextChannel(), false);
}
} else
event.getTextChannel().sendMessage("You are not in a voice channel").queue();
@@ -0,0 +1,67 @@
package netneutrality.command.music;
/*
* Copyright (c) anthorob
*
* This file is closed-source. It should not be distributed or copied in any way or form
*/
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import netneutrality.command.AbstractCommand;
import netneutrality.command.Visibility;
import netneutrality.music.MusicManager;
public class SkipCommand extends AbstractCommand {
@Override
public boolean needAdmin() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getCommand() {
return "skip";
}
@Override
public String[] getAlias() {
return new String[0];
}
@Override
public String getUsage() {
return null;
}
@Override
public Visibility getVisibility() {
return Visibility.CHANNEL;
}
@Override
public String execute(MessageReceivedEvent event, String args) {
Member member = event.getMember();
MusicManager player = MusicManager.get(event.getGuild());
if (member.getVoiceState().inVoiceChannel()) {
VoiceChannel vChannel = member.getVoiceState().getChannel();
if (player.isConnected()) {
if (player.isConnectedTo(vChannel)) {
player.skip();
} else
event.getTextChannel().sendMessage("Not in your voice channel").queue();
} else
event.getTextChannel().sendMessage("Not in a voice channel").queue();
} else
event.getTextChannel().sendMessage("You are not in a voice channel").queue();
return null;
}
}
@@ -6,5 +6,63 @@ package netneutrality.command.music;
* This file is closed-source. It should not be distributed or copied in any way or form
*/
public class StopCommand {
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import netneutrality.command.AbstractCommand;
import netneutrality.command.Visibility;
import netneutrality.music.MusicManager;
public class StopCommand extends AbstractCommand {
@Override
public boolean needAdmin() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getCommand() {
return "stop";
}
@Override
public String[] getAlias() {
return new String[0];
}
@Override
public String getUsage() {
return null;
}
@Override
public Visibility getVisibility() {
return Visibility.CHANNEL;
}
@Override
public String execute(MessageReceivedEvent event, String args) {
Member member = event.getMember();
MusicManager player = MusicManager.get(event.getGuild());
if (member.getVoiceState().inVoiceChannel()) {
VoiceChannel vChannel = member.getVoiceState().getChannel();
if (player.isConnected()) {
if (player.isConnectedTo(vChannel)) {
player.stopMusic();
} else
event.getTextChannel().sendMessage("Not in your voice channel").queue();
} else
event.getTextChannel().sendMessage("Not in a voice channel").queue();
} else
event.getTextChannel().sendMessage("You are not in a voice channel").queue();
return null;
}
}
@@ -87,6 +87,7 @@ public class MusicManager {
}
public synchronized void stopMusic() {
scheduler.getTextChannel().getManager().setTopic("").queue();
player.destroy();
}
@@ -100,12 +101,14 @@ public class MusicManager {
String youtubeCode = YoutubeClass.getCodeFromUrl(url);
System.out.println(youtubeCode);
scheduler.setTextChannel(channel);
if (!youtubeCode.isEmpty()) {
playerManager.loadItem(youtubeCode, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack audioTrack) {
if (!silent)
channel.sendMessage("Starting track [" + audioTrack.getInfo().title + "]").queue();
channel.sendMessage("Adding track to queue : " + audioTrack.getInfo().title).queue();
scheduler.queue(audioTrack);
}
@@ -132,6 +135,13 @@ public class MusicManager {
}
}
public synchronized void skip() {
if (!scheduler.queue.isEmpty()) {
scheduler.nextTrack();
} else
stopMusic();
}
public void setVolume(int volume) {
player.setVolume(volume);
}
@@ -11,6 +11,7 @@ import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import net.dv8tion.jda.core.entities.TextChannel;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -18,7 +19,8 @@ import java.util.concurrent.LinkedBlockingQueue;
public class TrackScheduler extends AudioEventAdapter {
private final AudioPlayer player;
private final BlockingQueue<AudioTrack> queue;
private TextChannel channel;
public final BlockingQueue<AudioTrack> queue;
public TrackScheduler(AudioPlayer player) {
this.player = player;
@@ -29,9 +31,19 @@ public class TrackScheduler extends AudioEventAdapter {
// Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
// something is playing, it returns false and does nothing. In that case the player was already playing so this
// track goes to the queue instead.
if (!player.startTrack(track, true)) {
boolean start = player.startTrack(track, true);
if (!start) {
queue.offer(track);
} else
channel.getManager().setTopic(":speaker: Now playing : " + track.getInfo().title).queue();
}
public void setTextChannel(TextChannel channel) {
this.channel = channel;
}
public TextChannel getTextChannel() {
return channel;
}
/**
@@ -40,7 +52,12 @@ public class TrackScheduler extends AudioEventAdapter {
public void nextTrack() {
// Start the next track, regardless of if something is already playing or not. In case queue was empty, we are
// giving null to startTrack, which is a valid argument and will simply stop the player.
player.startTrack(queue.poll(), false);
AudioTrack track = queue.poll();
if (track != null)
channel.getManager().setTopic(":speaker: Now playing : " + track.getInfo().title).queue();
else
channel.getManager().setTopic("").queue();
player.startTrack(track, false);
}
@Override