Ajout de la commande /nowplaying, /pause, /volume

This commit is contained in:
Melaine Gérard 2024-12-26 22:19:28 +01:00
parent e2d61d644a
commit c7b07a5843
5 changed files with 179 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -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();
}
}

View File

@ -39,7 +39,7 @@ public class PlayCommand implements ISlashCommand {
Member member = event.getMember(); Member member = event.getMember();
GuildVoiceState voiceState = member.getVoiceState(); 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 !") event.getHook().editOriginal("Vous devez être connecté à un salon vocal pour utiliser cette commande !")
.queue(); .queue();
return; return;
@ -64,8 +64,11 @@ public class PlayCommand implements ISlashCommand {
} }
}); });
if (!audioManager.isConnected()) {
audioManager.openAudioConnection(voiceState.getChannel()); audioManager.openAudioConnection(voiceState.getChannel());
PlayerManager.getInstance().getMusicManager(event.getGuild()).audioPlayer.setVolume(25); PlayerManager.getInstance().getMusicManager(event.getGuild()).audioPlayer.setVolume(25);
}
PlayerManager.getInstance().loadAndPlay(event.getChannel().asGuildMessageChannel(), url); PlayerManager.getInstance().loadAndPlay(event.getChannel().asGuildMessageChannel(), url);
event.getHook().editOriginal("Chargement du fichier audio en cours...").queue(); event.getHook().editOriginal("Chargement du fichier audio en cours...").queue();
} }

View File

@ -29,7 +29,7 @@ public class QueueCommand implements ISlashCommand {
@Override @Override
public String getDescription() { public String getDescription() {
return "Permet de passer à la musique suivante"; return "Permet de voir la file d'attente";
} }
@Override @Override
@ -62,7 +62,6 @@ public class QueueCommand implements ISlashCommand {
// On passe aux musiques suivantes // On passe aux musiques suivantes
GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild()); GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(event.getGuild());
Queue<AudioTrack> queue = musicManager.scheduler.getQueue(); Queue<AudioTrack> queue = musicManager.scheduler.getQueue();
AudioTrack currentTrack = musicManager.audioPlayer.getPlayingTrack();
if (queue.isEmpty()) { if (queue.isEmpty()) {
event.getHook().editOriginal("La file d'attente est vide.").queue(); event.getHook().editOriginal("La file d'attente est vide.").queue();

View File

@ -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<OptionData> 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();
}
}