From c7b07a5843c2398b70f8e8a2468c06b7ec5bf1ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Thu, 26 Dec 2024 22:19:28 +0100 Subject: [PATCH] :sparkles: Ajout de la commande /nowplaying, /pause, /volume --- .../commands/audio/NowPlayingCommand.java | 67 +++++++++++++++++++ .../acerola/commands/audio/PauseCommand.java | 54 +++++++++++++++ .../acerola/commands/audio/PlayCommand.java | 9 ++- .../acerola/commands/audio/QueueCommand.java | 3 +- .../acerola/commands/audio/VolumeCommand.java | 51 ++++++++++++++ 5 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/NowPlayingCommand.java create mode 100644 src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PauseCommand.java create mode 100644 src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/VolumeCommand.java diff --git a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/NowPlayingCommand.java b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/NowPlayingCommand.java new file mode 100644 index 0000000..4bcc799 --- /dev/null +++ b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/NowPlayingCommand.java @@ -0,0 +1,67 @@ +package org.camelia.studio.kiss.shot.acerola.commands.audio; + +import org.camelia.studio.kiss.shot.acerola.audio.GuildMusicManager; +import org.camelia.studio.kiss.shot.acerola.audio.PlayerManager; +import org.camelia.studio.kiss.shot.acerola.interfaces.ISlashCommand; + +import com.sedmelluq.discord.lavaplayer.track.AudioTrack; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.managers.AudioManager; + +import java.awt.Color; + +public class NowPlayingCommand implements ISlashCommand { + @Override + public String getName() { + return "nowplaying"; + } + + @Override + public String getDescription() { + return "Permet de voir la musique en cours de lecture"; + } + + @Override + public void execute(SlashCommandInteractionEvent event) { + event.deferReply().queue(); + + AudioManager audioManager = event.getGuild().getAudioManager(); + if (!audioManager.isConnected()) { + event.getHook().editOriginal("Je ne suis pas connecté à un canal vocal !").queue(); + return; + } + + GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild()); + AudioTrack playingTrack = musicManager.audioPlayer.getPlayingTrack(); + + if (playingTrack == null) { + event.getHook().editOriginal("La file d'attente est vide.").queue(); + return; + } + + EmbedBuilder embed = new EmbedBuilder(); + embed.setTitle("En cours de lecture"); + embed.setColor(Color.ORANGE); + embed.addField("Titre", playingTrack.getInfo().title, false); + embed.addField("Durée", formatTime(playingTrack.getDuration()), false); + embed.addField("Auteur", playingTrack.getInfo().author, false); + embed.addField("Lien", playingTrack.getInfo().uri, false); + embed.setImage(playingTrack.getInfo().artworkUrl); + + event.getHook().editOriginalEmbeds(embed.build()).queue(); + } + + private String formatTime(long timeInMillis) { + long hours = timeInMillis / 3600000; + long minutes = (timeInMillis % 3600000) / 60000; + long seconds = (timeInMillis % 60000) / 1000; + + if (hours > 0) { + return String.format("%d:%02d:%02d", hours, minutes, seconds); + } else { + return String.format("%d:%02d", minutes, seconds); + } + } +} diff --git a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PauseCommand.java b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PauseCommand.java new file mode 100644 index 0000000..c99fa62 --- /dev/null +++ b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PauseCommand.java @@ -0,0 +1,54 @@ +package org.camelia.studio.kiss.shot.acerola.commands.audio; + +import net.dv8tion.jda.api.entities.GuildVoiceState; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.managers.AudioManager; + +import org.camelia.studio.kiss.shot.acerola.audio.GuildMusicManager; +import org.camelia.studio.kiss.shot.acerola.audio.PlayerManager; +import org.camelia.studio.kiss.shot.acerola.interfaces.ISlashCommand; + +public class PauseCommand implements ISlashCommand { + @Override + public String getName() { + return "pause"; + } + + @Override + public String getDescription() { + return "Permet de mettre en pause la musique en cours de lecture"; + } + + @Override + public void execute(SlashCommandInteractionEvent event) { + event.deferReply().queue(); + Member member = event.getMember(); + GuildVoiceState voiceState = member.getVoiceState(); + + if (!voiceState.inAudioChannel()) { + event.getHook().editOriginal("Vous devez être connecté à un salon vocal pour utiliser cette commande !") + .queue(); + return; + } + + AudioManager audioManager = event.getGuild().getAudioManager(); + if (!audioManager.isConnected()) { + event.getHook().editOriginal("Je ne suis pas connecté à un canal vocal !").queue(); + return; + } + + GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild()); + + boolean isPaused = musicManager.audioPlayer.isPaused(); + + musicManager.audioPlayer.setPaused(!isPaused); + + if (isPaused) { + event.getHook().editOriginal("La musique a été reprise !").queue(); + return; + } + + event.getHook().editOriginal("La musique est en pause !").queue(); + } +} \ No newline at end of file diff --git a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PlayCommand.java b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PlayCommand.java index ea583e4..78dbd08 100644 --- a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PlayCommand.java +++ b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/PlayCommand.java @@ -39,7 +39,7 @@ public class PlayCommand implements ISlashCommand { Member member = event.getMember(); GuildVoiceState voiceState = member.getVoiceState(); - if (!member.getVoiceState().inAudioChannel()) { + if (!voiceState.inAudioChannel()) { event.getHook().editOriginal("Vous devez être connecté à un salon vocal pour utiliser cette commande !") .queue(); return; @@ -64,8 +64,11 @@ public class PlayCommand implements ISlashCommand { } }); - audioManager.openAudioConnection(voiceState.getChannel()); - PlayerManager.getInstance().getMusicManager(event.getGuild()).audioPlayer.setVolume(25); + if (!audioManager.isConnected()) { + audioManager.openAudioConnection(voiceState.getChannel()); + PlayerManager.getInstance().getMusicManager(event.getGuild()).audioPlayer.setVolume(25); + } + PlayerManager.getInstance().loadAndPlay(event.getChannel().asGuildMessageChannel(), url); event.getHook().editOriginal("Chargement du fichier audio en cours...").queue(); } diff --git a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/QueueCommand.java b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/QueueCommand.java index 671bce7..3b64b86 100644 --- a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/QueueCommand.java +++ b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/QueueCommand.java @@ -29,7 +29,7 @@ public class QueueCommand implements ISlashCommand { @Override public String getDescription() { - return "Permet de passer à la musique suivante"; + return "Permet de voir la file d'attente"; } @Override @@ -62,7 +62,6 @@ public class QueueCommand implements ISlashCommand { // On passe aux musiques suivantes GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild()); Queue queue = musicManager.scheduler.getQueue(); - AudioTrack currentTrack = musicManager.audioPlayer.getPlayingTrack(); if (queue.isEmpty()) { event.getHook().editOriginal("La file d'attente est vide.").queue(); diff --git a/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/VolumeCommand.java b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/VolumeCommand.java new file mode 100644 index 0000000..34dfd73 --- /dev/null +++ b/src/main/java/org/camelia/studio/kiss/shot/acerola/commands/audio/VolumeCommand.java @@ -0,0 +1,51 @@ +package org.camelia.studio.kiss.shot.acerola.commands.audio; + +import org.camelia.studio.kiss.shot.acerola.audio.GuildMusicManager; +import org.camelia.studio.kiss.shot.acerola.audio.PlayerManager; +import org.camelia.studio.kiss.shot.acerola.interfaces.ISlashCommand; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.managers.AudioManager; + +import java.util.List; + +public class VolumeCommand implements ISlashCommand { + + @Override + public String getName() { + return "volume"; + } + + @Override + public String getDescription() { + return "Permet de changer le volume du bot"; + } + + @Override + public List getOptions() { + return List.of( + new OptionData(OptionType.INTEGER, "volume", "Le volume souhaité").setRequired(true).setMinValue(0).setMaxValue(100) + ); + } + + @Override + public void execute(SlashCommandInteractionEvent event) { + event.deferReply().queue(); + + AudioManager audioManager = event.getGuild().getAudioManager(); + if (!audioManager.isConnected()) { + event.getHook().editOriginal("Je ne suis pas connecté à un canal vocal !").queue(); + return; + } + + GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild()); + + int volume = Integer.parseInt(event.getOption("volume").getAsString()); + + musicManager.audioPlayer.setVolume(volume); + + event.getHook().editOriginal("Le volume a été changé à " + volume + "%").queue(); + } +}