From 580594adaf382fe334d2a3ccd76a6d34fd646da4 Mon Sep 17 00:00:00 2001 From: ZtereoHYPE <57519662+ZtereoHYPE@users.noreply.github.com> Date: Wed, 3 Nov 2021 23:59:27 +0100 Subject: [PATCH] new: skip command --- .../codes/ztereohype/ztereomusic/Bot.java | 16 +++-- .../ztereomusic/command/commands/Skip.java | 59 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/main/java/codes/ztereohype/ztereomusic/command/commands/Skip.java diff --git a/src/main/java/codes/ztereohype/ztereomusic/Bot.java b/src/main/java/codes/ztereohype/ztereomusic/Bot.java index c90d501..28a1b7a 100644 --- a/src/main/java/codes/ztereohype/ztereomusic/Bot.java +++ b/src/main/java/codes/ztereohype/ztereomusic/Bot.java @@ -1,9 +1,10 @@ package codes.ztereohype.ztereomusic; -import codes.ztereohype.ztereomusic.audio.TrackScheduer; +import codes.ztereohype.ztereomusic.audio.TrackManager; import codes.ztereohype.ztereomusic.command.Command; import codes.ztereohype.ztereomusic.command.commands.Ping; -import codes.ztereohype.ztereomusic.command.commands.Playtest; +import codes.ztereohype.ztereomusic.command.commands.Play; +import codes.ztereohype.ztereomusic.command.commands.Skip; import codes.ztereohype.ztereomusic.database.Config; import codes.ztereohype.ztereomusic.listeners.CommandListener; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; @@ -25,7 +26,7 @@ public class Bot { private static @Getter final Map commandAliases = new HashMap<>(); public static AudioPlayerManager playerManager; - public static Map trackScheduerMap = new HashMap<>(); + public static Map trackScheduerMap = new HashMap<>(); public static void main(String[] args) throws Exception { config = new Config("./config.json5"); @@ -40,11 +41,16 @@ public class Bot { Ping ping = new Ping(); commandMap.put(ping.getMeta().getName(), ping); - Playtest playtest = new Playtest(); - commandMap.put(playtest.getMeta().getName(), playtest); + Play play = new Play(); + commandMap.put(play.getMeta().getName(), play); + + Skip skip = new Skip(); + commandMap.put(skip.getMeta().getName(), skip); for (String commandName : commandMap.keySet()) { + System.out.println("loading aliases from: " + commandName); for (String aliasName : commandMap.get(commandName).getMeta().getAliases()) { + System.out.println("loaded " + aliasName + " from command " + commandName); commandAliases.put(aliasName, commandName); } } diff --git a/src/main/java/codes/ztereohype/ztereomusic/command/commands/Skip.java b/src/main/java/codes/ztereohype/ztereomusic/command/commands/Skip.java new file mode 100644 index 0000000..dd1101a --- /dev/null +++ b/src/main/java/codes/ztereohype/ztereomusic/command/commands/Skip.java @@ -0,0 +1,59 @@ +package codes.ztereohype.ztereomusic.command.commands; + +import codes.ztereohype.ztereomusic.Bot; +import codes.ztereohype.ztereomusic.audio.TrackManager; +import codes.ztereohype.ztereomusic.command.Command; +import codes.ztereohype.ztereomusic.command.CommandMeta; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.VoiceChannel; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.managers.AudioManager; + +import java.util.Objects; + +public class Skip implements Command { + CommandMeta meta = new CommandMeta("skip", "Skip the current track!", new String[]{"next"}, false, false); + + @Override + public CommandMeta getMeta() { + return this.meta; + } + + public void execute(MessageReceivedEvent messageEvent, String[] args) { + Member author = Objects.requireNonNull(messageEvent.getMember()); + + if (author.getVoiceState() == null) { + messageEvent.getChannel().sendMessage("I was unable to access your information... strange...").queue(); + return; + } + + if (!author.getVoiceState().inVoiceChannel()) { + messageEvent.getMessage().reply("You are not in a voice channel!").queue(); + return; + } + + Guild guild = messageEvent.getGuild(); + VoiceChannel voiceChannel = author.getVoiceState().getChannel(); + AudioManager manager = guild.getAudioManager(); + + boolean isInVC = manager.isConnected(); + boolean isInSameVC = isInVC && Objects.equals(manager.getConnectedChannel(), voiceChannel); + + if (isInSameVC && Bot.trackScheduerMap.containsKey(voiceChannel)) { + TrackManager trackManager = Bot.trackScheduerMap.get(voiceChannel); + + if (trackManager.getPlayer().getPlayingTrack() == null) { + messageEvent.getMessage().reply("No track is playing.").queue(); + return; + } + + trackManager.skip(); + + } else { + messageEvent.getMessage().reply("No track is playing...").queue(); + } + + } + +}