From d73b03b796cd1a13d0bb63d57820ac7ba83578b2 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sat, 10 Jun 2023 15:23:37 -0700 Subject: Bump JDA from v4 to V5 Changed all methods from JDA v4 to V5 --- src/main/java/Main.java | 2 +- src/main/java/audio/Music.java | 66 ++++++++++++++++-------------- src/main/java/commands/CommandManager.java | 9 ++-- src/main/java/commands/UIPusher.java | 18 +++----- src/main/java/utility/StatusHandler.java | 65 +++++++++++++++++------------ 5 files changed, 85 insertions(+), 75 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 4fe9473..fbb066a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,5 +1,5 @@ import audio.Music; -import net.dv8tion.jda.api.events.ReadyEvent; +import net.dv8tion.jda.api.events.session.ReadyEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; diff --git a/src/main/java/audio/Music.java b/src/main/java/audio/Music.java index 3f1b97b..986c9fc 100644 --- a/src/main/java/audio/Music.java +++ b/src/main/java/audio/Music.java @@ -13,19 +13,22 @@ import commands.UIPusher; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.MessageBuilder; + import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.entities.VoiceChannel; -import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; -import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.components.selections.SelectOption; -import net.dv8tion.jda.api.interactions.components.selections.SelectionMenu; + +import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; import net.dv8tion.jda.api.managers.AudioManager; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; import utility.*; import javax.security.auth.login.LoginException; import java.awt.*; @@ -67,7 +70,7 @@ public class Music extends ListenerAdapter { statusHandler = new StatusHandler(jda); statusHandler.setSlashCommands(); System.out.println("Bot Started"); - } catch (LoginException e) { + } catch (Exception e) { throw new RuntimeException(e); } @@ -130,7 +133,7 @@ public class Music extends ListenerAdapter { } s.close(); } - public void queueTrackFromLoadedList(SlashCommandEvent event, int songsToQueue,String url){ + public void queueTrackFromLoadedList(SlashCommandInteractionEvent event, int songsToQueue, String url){ fillLoadedPlaylist(url,"songdb.txt"); Collections.shuffle(currentlyLoadedPlaylist); for (int i = 0;i queue = getGuildAudioPlayer(event.getGuild()).scheduler.queue; @@ -171,7 +174,7 @@ public class Music extends ListenerAdapter { trackCount++; } } - SelectionMenu menu = SelectionMenu.create("menu:class") + StringSelectMenu menu = StringSelectMenu.create("menu:class") .setPlaceholder("-Select a track-") // shows the placeholder indicating what this menu is for .setRequiredRange(1,1)// only one can be selected .addOptions(trackMenuOptions) @@ -184,10 +187,10 @@ public class Music extends ListenerAdapter { } } } - public void showControls(SlashCommandEvent event){ + public void showControls(SlashCommandInteractionEvent event){ uiPusher.showControls(event); } - public void shuffleQueue(SlashCommandEvent event){ + public void shuffleQueue(SlashCommandInteractionEvent event){ Guild guild = event.getGuild(); GuildMusicManager mng = getGuildAudioPlayer(guild); Queue queue = getGuildAudioPlayer(event.getGuild()).scheduler.queue; @@ -209,7 +212,7 @@ public class Music extends ListenerAdapter { event.reply("The queue has been shuffled!").queue(); } - public void playMusic(SlashCommandEvent event){ + public void playMusic(SlashCommandInteractionEvent event){ final YouTubeAPI youtubeAPI = new YouTubeAPI(ytapiKey); try { String userQuery = event.getOption("term").getAsString(); @@ -248,7 +251,7 @@ public class Music extends ListenerAdapter { } } - public void setVolume(SlashCommandEvent event, String command){ + public void setVolume(SlashCommandInteractionEvent event, String command){ Guild guild = event.getGuild(); assert guild != null; GuildMusicManager mng = getGuildAudioPlayer(guild); @@ -273,7 +276,7 @@ public class Music extends ListenerAdapter { } } - public void stopPlayer(SlashCommandEvent event){ + public void stopPlayer(SlashCommandInteractionEvent event){ Guild guild = event.getGuild(); GuildMusicManager mng = getGuildAudioPlayer(guild); AudioPlayer player = mng.player; @@ -284,7 +287,7 @@ public class Music extends ListenerAdapter { event.reply("Playback has been completely stopped and the queue has been cleared.").queue(); } - public void pausePlayer(SlashCommandEvent event){ + public void pausePlayer(SlashCommandInteractionEvent event){ Guild guild = event.getGuild(); GuildMusicManager mng = getGuildAudioPlayer(guild); AudioPlayer player = mng.player; @@ -300,7 +303,7 @@ public class Music extends ListenerAdapter { event.reply("The player has resumed playing.").queue(); } - public void showNowPlaying(SlashCommandEvent event){ + public void showNowPlaying(SlashCommandInteractionEvent event){ Guild guild = event.getGuild(); GuildMusicManager mng = getGuildAudioPlayer(guild); AudioPlayer player = mng.player; @@ -316,14 +319,14 @@ public class Music extends ListenerAdapter { EmbedBuilder embed = embedMaker.makeNowPlayingEmbed(currentTrack,position,duration, "https://img.youtube.com/vi/" + currentTrack.getIdentifier() + "/hqdefault.jpg", "https://www.youtube.com/watch?v=" + currentTrack.getIdentifier(),new Color(0xFD0001)); - MessageBuilder messageBuilder = (MessageBuilder) new MessageBuilder().setEmbeds(embed.build()); + MessageCreateBuilder messageBuilder = new MessageCreateBuilder().setEmbeds(embed.build()); event.reply(messageBuilder.build()).queue(); } else if(currentTrackUrlType=="snd") { //SOUNDCLOUD EMBED EmbedBuilder embed = embedMaker.makeNowPlayingEmbed(currentTrack,position,duration, "https://1000logos.net/wp-content/uploads/2021/04/Soundcloud-logo.png", currentTrack.getInfo().uri,new Color(0xFD5401)); - MessageBuilder messageBuilder = (MessageBuilder) new MessageBuilder().setEmbeds(embed.build()); + MessageCreateBuilder messageBuilder = new MessageCreateBuilder().setEmbeds(embed.build()); event.reply(messageBuilder.build()).queue(); } @@ -333,7 +336,7 @@ public class Music extends ListenerAdapter { currentTrack.getIdentifier().replaceAll("https://www.twitch.tv/","") + "-440x248.jpg", currentTrack.getIdentifier(), new Color(0xA86FFE)); - MessageBuilder messageBuilder = (MessageBuilder) new MessageBuilder().setEmbeds(embed.build()); + MessageCreateBuilder messageBuilder = new MessageCreateBuilder().setEmbeds(embed.build()); event.reply(messageBuilder.build()).queue(); } } @@ -342,7 +345,7 @@ public class Music extends ListenerAdapter { } } - public void showQueue(SlashCommandEvent event){ + public void showQueue(SlashCommandInteractionEvent event){ Queue queue = getGuildAudioPlayer(event.getGuild()).scheduler.queue; synchronized (queue) { @@ -375,7 +378,7 @@ public class Music extends ListenerAdapter { @Override - public void onSelectionMenu(SelectionMenuEvent event){ + public void onStringSelectInteraction(StringSelectInteractionEvent event){ if(event.getValues().get(0).contains("remove-queue")) { boolean deletedSong = false; Queue queue = getGuildAudioPlayer(event.getGuild()).scheduler.queue; @@ -410,7 +413,7 @@ public class Music extends ListenerAdapter { } @Override - public void onButtonClick(ButtonClickEvent event){ + public void onButtonInteraction(ButtonInteractionEvent event){ Guild guild = event.getGuild(); GuildMusicManager mng = getGuildAudioPlayer(guild); AudioPlayer player = mng.player; @@ -455,7 +458,7 @@ public class Music extends ListenerAdapter { } - public void recursiveQueue(SlashCommandEvent event, String playlistUrl,int amount){ + public void recursiveQueue(SlashCommandInteractionEvent event, String playlistUrl,int amount){ final YouTubeAPI youtubeAPI = new YouTubeAPI(ytapiKey); System.out.println(urlCheck.getURLType(playlistUrl)); if(urlCheck.isURL(playlistUrl) && urlCheck.getURLType(playlistUrl).equals("spotify-playlist")){ @@ -524,9 +527,10 @@ public class Music extends ListenerAdapter { private void play(Guild guild, GuildMusicManager musicManager, AudioTrack track) { connectToFirstVoiceChannel(guild.getAudioManager()); musicManager.scheduler.queue(track); + System.out.println("Playing " + track.getInfo().title); } - public void skipTrack(SlashCommandEvent event) { + public void skipTrack(SlashCommandInteractionEvent event) { GuildMusicManager musicManager = getGuildAudioPlayer(event.getGuild()); musicManager.scheduler.nextTrack(); event.reply("Skipped to next track.").queue(); @@ -534,8 +538,10 @@ public class Music extends ListenerAdapter { private static void connectToFirstVoiceChannel(AudioManager audioManager) { System.out.println("Connecting to voice channel"); - if (!audioManager.isConnected() && !audioManager.isAttemptingToConnect()) { + if (!audioManager.isConnected()) { + for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) { + System.out.println("Attempting to connect to " + voiceChannel.getName()); audioManager.openAudioConnection(voiceChannel); break; } diff --git a/src/main/java/commands/CommandManager.java b/src/main/java/commands/CommandManager.java index 0dccf7a..39ca243 100644 --- a/src/main/java/commands/CommandManager.java +++ b/src/main/java/commands/CommandManager.java @@ -1,19 +1,20 @@ package commands; import audio.Music; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import java.util.Objects; public class CommandManager extends ListenerAdapter { Music music; - String songDatabase = "https://pinapelz.github.io/vTuberDiscordBot/forsen.txt"; + String songDatabase = "YOUR_LIST_OF_YOUTUBE_LINKS_OR_AUDIO_URLS_HERE"; public CommandManager(Music music){ this.music = music; } @Override - public void onSlashCommand(SlashCommandEvent event) { + public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { String command = event.getName(); switch (command) { case "play": @@ -70,7 +71,7 @@ public class CommandManager extends ListenerAdapter { music.showQueueMenu(event, "inspect-queue", "Select a track to inspect below"); break; } - super.onSlashCommand(event); + super.onSlashCommandInteraction(event); } } diff --git a/src/main/java/commands/UIPusher.java b/src/main/java/commands/UIPusher.java index 91f5dbf..43d98a3 100644 --- a/src/main/java/commands/UIPusher.java +++ b/src/main/java/commands/UIPusher.java @@ -1,22 +1,14 @@ package commands; -import audio.GuildMusicManager; -import com.sedmelluq.discord.lavaplayer.track.AudioTrack; -import net.dv8tion.jda.api.entities.Emoji; -import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; -import net.dv8tion.jda.api.interactions.components.Button; -import net.dv8tion.jda.api.interactions.components.selections.SelectOption; -import net.dv8tion.jda.api.interactions.components.selections.SelectionMenu; -import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; -import java.util.List; -import java.util.Queue; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.components.buttons.Button; + public class UIPusher { - public void showControls(SlashCommandEvent event){ + public void showControls(SlashCommandInteractionEvent event){ event.reply("Controls for the player:") .addActionRow( Button.primary("action-volumedown", Emoji.fromUnicode("U+1F509")), diff --git a/src/main/java/utility/StatusHandler.java b/src/main/java/utility/StatusHandler.java index 43a34cf..dbd058c 100644 --- a/src/main/java/utility/StatusHandler.java +++ b/src/main/java/utility/StatusHandler.java @@ -1,40 +1,51 @@ package utility; + import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.*; public class StatusHandler { static JDA jda; - public StatusHandler(JDA jda) - { + + public StatusHandler(JDA jda) { this.jda = jda; } - public static void setSlashCommands(){ - // jda.updateCommands().queue(); - jda.upsertCommand(new CommandData("play","Adds a song to the queue with a URL or search terms"). - addOption(OptionType.STRING,"term","The link or search terms of the music to queue") - ).queue(); - jda.upsertCommand(new CommandData("queue-recursive","Adds a set amount of random songs from a playlist"). - addOption(OptionType.STRING,"url","The link of the playlist") - .addOption(OptionType.INTEGER,"amount","The amount of songs to queue") - ).queue(); - jda.upsertCommand(new CommandData("leave","Clears the queue and disconnects the bot from voice channel")).queue(); - jda.upsertCommand(new CommandData("showqueue","Shows the current queue")).queue(); - jda.upsertCommand(new CommandData("pause","Pauses the player")).queue(); - jda.upsertCommand(new CommandData("controls","Show an interface to control the player")).queue(); - jda.upsertCommand(new CommandData("skip","Skips the current song")).queue(); - jda.upsertCommand(new CommandData("nowplaying","Shows a detailed view of the current song playing")).queue(); - jda.upsertCommand(new CommandData("stop","Stops the player and clears the queue")).queue(); - jda.upsertCommand(new CommandData("remove","Remove a track in queue")).queue(); - jda.upsertCommand(new CommandData("shuffle","Shuffle the current queue")).queue(); - jda.upsertCommand(new CommandData("vtmusic","Queues a set number of random VTuber songs and covers"). - addOption(OptionType.INTEGER,"number","Number of songs to queue") - ).queue(); - jda.upsertCommand(new CommandData("volume","Set the volume or leave blank to check current volume"). - addOption(OptionType.INTEGER,"volume","Volume from 0-100") - ).queue(); + public static void setSlashCommands() { + jda.updateCommands() + .addCommands( + Commands.slash("play", "Adds a song to the queue with a URL or search terms"). + addOption(OptionType.STRING, "term", "The link or search terms of the music to queue", true)) + .addCommands( + Commands.slash("queue-recursive", "dds a set amount of random songs from a playlist"). + addOption(OptionType.STRING, "url", "The link of the playlist", true) + .addOption(OptionType.INTEGER, "amount", "The amount of songs to queue", true)) + .addCommands( + Commands.slash("leave", "Clears the queue and disconnects the bot from voice channel")) + .addCommands( + Commands.slash("showqueue", "Shows the current queue")) + .addCommands( + Commands.slash("pause", "Pauses the player")) + .addCommands( + Commands.slash("controls", "Show an interface to control the player")) + .addCommands( + Commands.slash("skip", "Skips the current song")) + .addCommands( + Commands.slash("nowplaying", "Shows a detailed view of the current song playing")) + .addCommands( + Commands.slash("stop", "Stops the player and clears the queue")) + .addCommands( + Commands.slash("remove", "Removes a song from the queue")) + .addCommands( + Commands.slash("shuffle", "Shuffles the queue")) + .addCommands( + Commands.slash("vtmusic", "Queues a set number of random VTuber songs and covers"). + addOption(OptionType.INTEGER, "number", "Number of songs to queue", true)) + .addCommands( + Commands.slash("volume", "Set the volume or leave blank to check current volume"). + addOption(OptionType.INTEGER, "volume", "Volume from 0-100")) + .queue(); } -- cgit v1.2.3