From 1e6f91d238884839ab13c09030904511b98ef062 Mon Sep 17 00:00:00 2001 From: Donald Shan Date: Mon, 26 Dec 2022 16:01:18 -0800 Subject: Added changing Discord Status feature Discord status of the bot will now change based on what is playing --- src/main/java/utility/SpotifyAPI.java | 31 ++++++++++++------------ src/main/java/utility/StatusHandler.java | 41 ++++++++++++++++++++++++++++++++ src/main/java/utility/YouTubeAPI.java | 6 +++-- 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 src/main/java/utility/StatusHandler.java (limited to 'src/main/java/utility') diff --git a/src/main/java/utility/SpotifyAPI.java b/src/main/java/utility/SpotifyAPI.java index b4bcac1..4eb4d6e 100644 --- a/src/main/java/utility/SpotifyAPI.java +++ b/src/main/java/utility/SpotifyAPI.java @@ -1,5 +1,4 @@ package utility; - import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import se.michaelthelin.spotify.SpotifyApi; @@ -16,7 +15,6 @@ import java.time.Instant; public class SpotifyAPI { private static final String clientId = readSetting("spotifyClientID"); private static final String clientSecret = readSetting("spotifyClientSecret"); - public static String spotifyapiKey = ""; public static long lastRefresh = 0; private static final SpotifyApi spotifyApi = new SpotifyApi.Builder() @@ -25,26 +23,26 @@ public class SpotifyAPI { .build(); private static final ClientCredentialsRequest clientCredentialsRequest = spotifyApi.clientCredentials() .build(); - - public SpotifyAPI(){ this.spotifyapiKey = readSetting("spotifyApi"); } public static String getSearchTerm_sync(String trackid) { checkRefreshToken(); String searchQuery = ""; - try { - GetTrackRequest getTrackRequest = spotifyApi.getTrack(trackid) - .build(); - final Track track = getTrackRequest.execute(); - searchQuery = track.getName(); - ArtistSimplified[] artists = track.getArtists(); - for (int i = 0;i< artists.length;i++){ - searchQuery = searchQuery + " "+artists[i].getName(); - } - System.out.println(searchQuery); - } catch (Exception e) { - System.out.println("Error: " + e.getMessage()); + try { + GetTrackRequest getTrackRequest = spotifyApi.getTrack(trackid) + .build(); + final Track track = getTrackRequest.execute(); + searchQuery = track.getName(); + ArtistSimplified[] artists = track.getArtists(); + for (int i = 0; i < artists.length; i++) { + searchQuery = searchQuery + " " + artists[i].getName(); + } + System.out.println(searchQuery); + } catch (Exception e) { + System.out.println("Error with getting name: " + e.getMessage() +"Retrying..."); + return null; + } return searchQuery; @@ -86,6 +84,7 @@ public class SpotifyAPI { } public static String readSetting(String parameter){ Object obj = null; + try { obj = new JSONParser().parse(new FileReader("settings//config.json")); } catch (Exception e) { diff --git a/src/main/java/utility/StatusHandler.java b/src/main/java/utility/StatusHandler.java new file mode 100644 index 0000000..43a34cf --- /dev/null +++ b/src/main/java/utility/StatusHandler.java @@ -0,0 +1,41 @@ +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; + +public class StatusHandler { + static 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(); + + + } + +} diff --git a/src/main/java/utility/YouTubeAPI.java b/src/main/java/utility/YouTubeAPI.java index 61b48ed..4acecf5 100644 --- a/src/main/java/utility/YouTubeAPI.java +++ b/src/main/java/utility/YouTubeAPI.java @@ -5,6 +5,8 @@ import org.json.JSONObject; import org.jsoup.Jsoup; import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; public class YouTubeAPI { @@ -13,8 +15,8 @@ public class YouTubeAPI { this.ytapiKey = ytapiKey; } public String returnTopVideoURL(String keyword )throws IOException { - String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q="+keyword+"&type=video&key="+ytapiKey; - url = url.replaceAll(" ", "%20"); + String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q="+ URLEncoder.encode(keyword, StandardCharsets.UTF_8)+"&type=video&key="+ytapiKey; + System.out.println(url); String data = Jsoup.connect(url).ignoreContentType(true).execute().body(); JSONObject obj = new JSONObject(data); JSONArray arr = obj.getJSONArray("items"); -- cgit v1.2.3