Migrate music to slash command

This commit is contained in:
SebClem 2022-05-14 19:26:08 +02:00
parent 9d50e4c95f
commit 4118116ee9
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
16 changed files with 196 additions and 228 deletions

View File

@ -1,182 +0,0 @@
package net.Broken.Commands;
import net.Broken.Commande;
import net.Broken.MainBot;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut;
import net.Broken.audio.AudioM;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
/**
* Music commands
*/
public class Music implements Commande {
private Logger logger = LogManager.getLogger();
@Override
public void action(String[] args, MessageReceivedEvent event) {
AudioM audio = AudioM.getInstance(event.getGuild());
if(args.length >= 1){
switch (args[0]){
case "play":
event.getTextChannel().sendTyping().queue();
if(args.length>=2){
if(event.getMember().getVoiceState().inVoiceChannel()){
VoiceChannel voiceChanel = event.getMember().getVoiceState().getChannel();
logger.info("Connecting to "+voiceChanel.getName()+"...");
if(args.length ==2){
audio.loadAndPlay(event,voiceChanel,args[1],30,false);
}
else if(args.length == 3){
try{
int limit = Integer.parseInt(args[2]);
audio.loadAndPlay(event,voiceChanel,args[1],limit,false);
}catch (NumberFormatException e){
audio.loadAndPlay(event,voiceChanel,args[1],30,false);
}
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("You are not in a voice channel !")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Missing argument!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
break;
case "pause":
audio.pause(event);
break;
case "resume":
audio.resume(event);
break;
case "next":
audio.skipTrack(event);
break;
case "stop":
audio.stop(event);
break;
case "info":
audio.info(event);
break;
case "flush":
audio.flush(event);
break;
case "list":
audio.list(event);
break;
case "add":
event.getTextChannel().sendTyping().queue();
if(args.length ==2){
audio.add(event,args[1],30,false);
}
else if(args.length == 3){
try{
int limit = Integer.parseInt(args[2]);
audio.add(event,args[1],limit,false);
}catch (NumberFormatException e){
audio.add(event,args[1],30,false);
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Missing argument!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
break;
case "addNext":
event.getTextChannel().sendTyping().queue();
if(args.length >=2){
audio.add(event,args[1],1,true);
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Missing argument!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
break;
case "disconnect":
audio.stop();
List<Message> messages = new ArrayList<Message>(){{
add(event.getMessage());
}};
new MessageTimeOut(messages, 0).start();
break;
default:
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Unknown argument!")).complete();
List<Message> messagess = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messagess, MainBot.messageTimeOut).start();
break;
}
}
else{
Message message = event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Missing argument!")).complete();
List<Message> messages = new ArrayList<Message>(){{
add(message);
add(event.getMessage());
}};
new MessageTimeOut(messages, MainBot.messageTimeOut).start();
}
}
@Override
public boolean isPrivateUsable() {
return false;
}
@Override
public boolean isAdminCmd() {
return false;
}
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override
public boolean isNSFW() {
return false;
}
}

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi;
import net.Broken.Commands.Music;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;
import net.dv8tion.jda.api.entities.Guild;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,7 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.MainBot;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,6 +1,5 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;

View File

@ -1,13 +1,11 @@
package net.Broken.RestApi.Commands;
import net.Broken.Commands.Music;
import net.Broken.RestApi.CommandInterface;
import net.Broken.RestApi.Data.CommandPostData;
import net.Broken.RestApi.Data.CommandResponseData;
import net.Broken.audio.AudioM;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

View File

@ -2,6 +2,7 @@ package net.Broken;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import java.util.List;
@ -20,6 +21,8 @@ public interface SlashCommand {
List<OptionData> getOptions();
List<SubcommandData> getSubcommands();
/**
* Determines if the command is usable only by bot level admin user
* @return boolean

View File

@ -11,6 +11,7 @@ import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
@ -63,6 +64,11 @@ public class Cat implements SlashCommand {
return new ArrayList<>();
}
@Override
public List<SubcommandData> getSubcommands() {
return null;
}
/**
* Determines if the command is usable only by bot level admin user

View File

@ -0,0 +1,142 @@
package net.Broken.SlashCommands;
import net.Broken.MainBot;
import net.Broken.SlashCommand;
import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut;
import net.Broken.audio.AudioM;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* Command that return a random picture of cat.
*/
public class Music implements SlashCommand {
private Logger logger = LogManager.getLogger();
;
@Override
public void action(SlashCommandEvent event) {
AudioM audio = AudioM.getInstance(event.getGuild());
String action = event.getSubcommandName();
event.deferReply().queue();
switch (action) {
case "play":
if (event.getMember().getVoiceState().inVoiceChannel()) {
VoiceChannel voiceChanel = event.getMember().getVoiceState().getChannel();
logger.info("Connecting to " + voiceChanel.getName() + "...");
OptionMapping url = event.getOption("url");
if (event.getOption("playlist-limit") == null) {
audio.loadAndPlay(event, voiceChanel, url.getAsString(), 30, false);
} else {
long limit = event.getOption("playlist-limit").getAsLong();
audio.loadAndPlay(event, voiceChanel, url.getAsString(), (int) limit, false);
}
} else {
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicError("You are not in a voice channel !")).build();
event.getHook().sendMessage(message).queue();
}
break;
case "add":
OptionMapping url = event.getOption("url");
boolean next = false;
if(event.getOption("next") != null){
next = event.getOption("next").getAsBoolean();
}
if(event.getOption("playlist-limit") == null){
audio.add(event, url.getAsString(),30, next);
}
else{
long limit = event.getOption("playlist-limit").getAsLong();
audio.add(event, url.getAsString(), (int) limit, next);
}
break;
case "pause":
audio.pause(event);
break;
case "resume":
audio.resume(event);
break;
case "next":
audio.skipTrack(event);
break;
case "stop":
case "disconnect":
audio.stop(event);
break;
case "info":
audio.info(event);
break;
case "flush":
audio.flush(event);
break;
case "list":
audio.list(event);
break;
}
}
@Override
public String getDescription() {
return "Music Bot !";
}
@Override
public List<OptionData> getOptions() {
return null;
}
@Override
public List<SubcommandData> getSubcommands() {
ArrayList<SubcommandData> subCommandList = new ArrayList<>();
subCommandList.add(new SubcommandData("play", "Play music")
.addOption(OptionType.STRING, "url", "The URL of the video to play", true)
.addOption(OptionType.INTEGER, "playlist-limit", "If a playlist is loaded, enter the max number of loaded tracks (default to 30)"));
subCommandList.add(new SubcommandData("add", "Add track to queue")
.addOption(OptionType.STRING, "url", " The URL of the video to play", true)
.addOption(OptionType.BOOLEAN, "next", "If true, track will be added on top of the playlist and will be the next to play")
.addOption(OptionType.INTEGER, "playlist-limit", "If a playlist is loaded, enter the max number of loaded tracks (default to 30)"));
subCommandList.add(new SubcommandData("pause", "Pause playback"));
subCommandList.add(new SubcommandData("resume", "Resume playback"));
subCommandList.add(new SubcommandData("stop", "Stop playback"));
subCommandList.add(new SubcommandData("next", "Next music"));
subCommandList.add(new SubcommandData("info", "Get currently playing info"));
subCommandList.add(new SubcommandData("flush", "Flush queue"));
subCommandList.add(new SubcommandData("list", "Get queue"));
return subCommandList;
}
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override
public boolean isNSFW() {
return false;
}
}

View File

@ -63,7 +63,13 @@ public class SlashCommandLoader {
public static void registerSlashCommands(CommandListUpdateAction commandListUpdateAction){
MainBot.slashCommands.forEach((k,v)->{
commandListUpdateAction.addCommands(new CommandData(k, v.getDescription()).addOptions(v.getOptions()));
CommandData command = new CommandData(k, v.getDescription());
if(v.getOptions() != null)
command.addOptions(v.getOptions());
if(v.getSubcommands() != null){
command.addSubcommands(v.getSubcommands());
}
commandListUpdateAction.addCommands(command);
});
commandListUpdateAction.queue();
}

View File

@ -11,9 +11,12 @@ import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import net.Broken.MainBot;
import net.Broken.RestApi.Data.UserAudioTrackData;
import net.Broken.Tools.EmbedMessageUtils;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -77,7 +80,7 @@ public class AudioM {
* @param playlistLimit Limit of playlist
* @param onHead True for adding audio track on top of playlist
*/
public void loadAndPlay(MessageReceivedEvent event, VoiceChannel voiceChannel, final String trackUrl, int playlistLimit, boolean onHead) {
public void loadAndPlay(SlashCommandEvent event, VoiceChannel voiceChannel, final String trackUrl, int playlistLimit, boolean onHead) {
GuildMusicManager musicManager = getGuildAudioPlayer();
playedChanel = voiceChannel;
@ -85,9 +88,9 @@ public class AudioM {
@Override
public void trackLoaded(AudioTrack track) {
logger.info("[" + guild + "] Single Track detected!");
UserAudioTrack uat = new UserAudioTrack(event.getAuthor(), track);
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Add " + track.getInfo().title + " to playlist")).queue();
UserAudioTrack uat = new UserAudioTrack(event.getUser(), track);
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Add " + track.getInfo().title + " to playlist")).build();
event.getHook().sendMessage(message).queue();
play(guild, voiceChannel, musicManager, uat, onHead);
}
@ -95,27 +98,24 @@ public class AudioM {
public void playlistLoaded(AudioPlaylist playlist) {
logger.info("[" + guild + "] Playlist detected! Limit: " + playlistLimit);
AudioTrack firstTrack = playlist.getSelectedTrack();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Add " + firstTrack.getInfo().title + " and 30 first videos to playlist !")).queue();
playListLoader(playlist, playlistLimit, event.getAuthor(), onHead);
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Add " + firstTrack.getInfo().title + " and 30 first videos to playlist !")).build();
event.getHook().sendMessage(message).queue();
playListLoader(playlist, playlistLimit, event.getUser(), onHead);
}
@Override
public void noMatches() {
logger.warn("[" + guild + "] Cant find media!");
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Video not found !")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicError("Video not found !")).build();
event.getHook().sendMessage(message).queue();
}
@Override
public void loadFailed(FriendlyException exception) {
logger.error("[" + guild + "] Can't load media!");
logger.error(exception.getMessage());
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Playback error !")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicError("Playback error !")).build();
event.getHook().sendMessage(message).queue();
}
});
}
@ -208,10 +208,11 @@ public class AudioM {
*
* @param event
*/
public void skipTrack(MessageReceivedEvent event) {
public void skipTrack(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.scheduler.nextTrack();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Next music!")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Next music !")).build();
event.getHook().sendMessage(message).queue();
}
/**
@ -219,11 +220,11 @@ public class AudioM {
*
* @param event
*/
public void pause(MessageReceivedEvent event) {
public void pause(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.scheduler.pause();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Music paused")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Playback paused")).build();
event.getHook().sendMessage(message).queue();
}
@ -233,11 +234,11 @@ public class AudioM {
*
* @param event
*/
public void resume(MessageReceivedEvent event) {
public void resume(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.scheduler.resume();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Music resumed")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Playback resumed")).build();
event.getHook().sendMessage(message).queue();
}
/**
@ -245,18 +246,19 @@ public class AudioM {
*
* @param event
*/
public void info(MessageReceivedEvent event) {
public void info(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
AudioTrackInfo info = musicManager.scheduler.getInfo();
UserAudioTrack userAudioTrack = musicManager.scheduler.getCurrentPlayingTrack();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk(info.title + "\n" + info.uri + "\nSubmitted by: " + userAudioTrack.getSubmittedUser().getName())).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk(info.title + "\n" + info.uri + "\nSubmitted by: " + userAudioTrack.getSubmittedUser().getName())).build();
event.getHook().sendMessage(message).queue();
}
public void flush(MessageReceivedEvent event) {
public void flush(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.scheduler.flush();
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Flush playlist!")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Flush playlist!")).build();
event.getHook().sendMessage(message).queue();
}
/**
@ -264,7 +266,7 @@ public class AudioM {
*
* @param event
*/
public void list(MessageReceivedEvent event) {
public void list(SlashCommandEvent event) {
GuildMusicManager musicManager = getGuildAudioPlayer();
List<UserAudioTrackData> list = musicManager.scheduler.getList();
StringBuilder resp = new StringBuilder();
@ -277,7 +279,8 @@ public class AudioM {
resp.append("\n");
}
}
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Playlist:\n\n" + resp.toString())).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Playlist:\n\n" + resp.toString())).build();
event.getHook().sendMessage(message).queue();
}
/**
@ -288,11 +291,12 @@ public class AudioM {
* @param playListLimit Limit of playlist
* @param onHead True for adding audio track on top of playlist
*/
public void add(MessageReceivedEvent event, String url, int playListLimit, boolean onHead) {
public void add(SlashCommandEvent event, String url, int playListLimit, boolean onHead) {
if (playedChanel != null) {
loadAndPlay(event, playedChanel, url, playListLimit, onHead);
} else {
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicError("Not connected to vocal chanel !")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicError("Not connected to vocal chanel !")).build();
event.getHook().sendMessage(message).queue();
}
}
@ -301,12 +305,13 @@ public class AudioM {
*
* @param event
*/
public void stop(MessageReceivedEvent event) {
public void stop(SlashCommandEvent event) {
musicManager.scheduler.stop();
musicManager.scheduler.flush();
if (event != null) {
event.getTextChannel().sendMessage(EmbedMessageUtils.getMusicOk("Music stopped")).queue();
Message message = new MessageBuilder().setEmbeds(EmbedMessageUtils.getMusicOk("Music stopped")).build();
event.getHook().sendMessage(message).queue();
}
}