From eccd673db8c9f03ed317ea610eb9b239f61e0e18 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 03:50:38 +0100 Subject: [PATCH 1/7] spotify debug --- .../spotify/profile/SpotifyProfile.java | 5 +++++ .../bat/service/SpotifyService.java | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/spotify/profile/SpotifyProfile.java b/src/main/java/cc/fascinated/bat/features/spotify/profile/SpotifyProfile.java index 03e3e0f..eebb3d8 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/profile/SpotifyProfile.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/profile/SpotifyProfile.java @@ -19,6 +19,11 @@ public class SpotifyProfile extends Profile { */ private String refreshToken; + /** + * When the access token expires + */ + private Long expiresAt; + public SpotifyProfile() { super("spotify"); } diff --git a/src/main/java/cc/fascinated/bat/service/SpotifyService.java b/src/main/java/cc/fascinated/bat/service/SpotifyService.java index 0d0bfbd..9998263 100644 --- a/src/main/java/cc/fascinated/bat/service/SpotifyService.java +++ b/src/main/java/cc/fascinated/bat/service/SpotifyService.java @@ -91,6 +91,7 @@ public class SpotifyService { try { return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute(); } catch (Exception e) { + e.printStackTrace(); return null; } } @@ -178,11 +179,26 @@ public class SpotifyService { * * @return the Spotify API */ + @SneakyThrows public SpotifyApi getSpotifyApi(BatUser user) { SpotifyProfile profile = user.getProfile(SpotifyProfile.class); - return new SpotifyApi.Builder() - .setAccessToken(profile.getAccessToken()) + SpotifyApi api = new SpotifyApi.Builder() + .setClientId(clientId) .setClientSecret(clientSecret) + .setAccessToken(profile.getAccessToken()) + .setRefreshToken(profile.getRefreshToken()) .build(); + + // Refresh the access token if it's expired + if (profile.getExpiresAt() == null || profile.getExpiresAt() < System.currentTimeMillis()) { + AuthorizationCodeCredentials credentials = api.authorizationCodeRefresh().build().execute(); + profile.setAccessToken(credentials.getAccessToken()); + profile.setRefreshToken(credentials.getRefreshToken()); + profile.setExpiresAt(System.currentTimeMillis() + (credentials.getExpiresIn() * 1000)); + api.setAccessToken(credentials.getAccessToken()); + api.setRefreshToken(credentials.getRefreshToken()); + userService.saveUser(user); + } + return api; } } From e892dade1c77fcf8eed77ce12081046902f02c1c Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 03:52:29 +0100 Subject: [PATCH 2/7] add log for token refresh in spotify --- src/main/java/cc/fascinated/bat/service/SpotifyService.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/cc/fascinated/bat/service/SpotifyService.java b/src/main/java/cc/fascinated/bat/service/SpotifyService.java index 9998263..661e191 100644 --- a/src/main/java/cc/fascinated/bat/service/SpotifyService.java +++ b/src/main/java/cc/fascinated/bat/service/SpotifyService.java @@ -7,6 +7,8 @@ import lombok.Getter; import lombok.NonNull; import lombok.SneakyThrows; import net.jodah.expiringmap.ExpiringMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import se.michaelthelin.spotify.SpotifyApi; @@ -24,6 +26,7 @@ import java.util.concurrent.TimeUnit; @Service @Getter public class SpotifyService { + private static final Logger log = LoggerFactory.getLogger(SpotifyService.class); /** * The access token map. */ @@ -198,6 +201,7 @@ public class SpotifyService { api.setAccessToken(credentials.getAccessToken()); api.setRefreshToken(credentials.getRefreshToken()); userService.saveUser(user); + log.info("Refreshed spotify access token for user {}", user.getName()); } return api; } From 31e81363ac6cbda247beee856d08dbc1ee02ee2f Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 03:56:34 +0100 Subject: [PATCH 3/7] pls --- .../java/cc/fascinated/bat/service/SpotifyService.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/service/SpotifyService.java b/src/main/java/cc/fascinated/bat/service/SpotifyService.java index 661e191..de8fea3 100644 --- a/src/main/java/cc/fascinated/bat/service/SpotifyService.java +++ b/src/main/java/cc/fascinated/bat/service/SpotifyService.java @@ -72,15 +72,7 @@ public class SpotifyService { .build(); this.authorizationUrl = spotifyApi.authorizationCodeUri() .response_type("code") - .scope( - AuthorizationScope.APP_REMOTE_CONTROL, - AuthorizationScope.USER_READ_PLAYBACK_POSITION, - AuthorizationScope.USER_READ_PLAYBACK_STATE, - AuthorizationScope.USER_MODIFY_PLAYBACK_STATE, - AuthorizationScope.USER_READ_CURRENTLY_PLAYING, - AuthorizationScope.APP_REMOTE_CONTROL, - AuthorizationScope.STREAMING - ) + .scope(AuthorizationScope.values()) .build().execute().toString(); } From 6af78c7da1241f004be36eb6eb382757997c2166 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 04:00:19 +0100 Subject: [PATCH 4/7] pls --- .../spotify/command/CurrentSubCommand.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java b/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java index d54bead..7420b0e 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java @@ -42,13 +42,13 @@ public class CurrentSubCommand extends BatSubCommand { return; } - if (!spotifyService.hasTrackPlaying(user)) { - interaction.replyEmbeds(EmbedUtils.genericEmbed() - .setDescription("You are not currently playing a track.") - .build()) - .queue(); - return; - } +// if (!spotifyService.hasTrackPlaying(user)) { +// interaction.replyEmbeds(EmbedUtils.genericEmbed() +// .setDescription("You are not currently playing a track.") +// .build()) +// .queue(); +// return; +// } CurrentlyPlaying currentlyPlaying = spotifyService.getCurrentlyPlayingTrack(user); Track track = (Track) currentlyPlaying.getItem(); AlbumSimplified album = track.getAlbum(); From bf554933cc9711cf9be56e0f4eee2654e8779a2f Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 04:01:30 +0100 Subject: [PATCH 5/7] pls --- .../features/spotify/command/PauseSubCommand.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java b/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java index 77ef74b..f6fb55a 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java @@ -36,13 +36,13 @@ public class PauseSubCommand extends BatSubCommand { return; } - if (!spotifyService.hasTrackPlaying(user)) { - interaction.replyEmbeds(EmbedUtils.genericEmbed() - .setDescription("You need to be playing a track to pause it.") - .build()) - .queue(); - return; - } +// if (!spotifyService.hasTrackPlaying(user)) { +// interaction.replyEmbeds(EmbedUtils.genericEmbed() +// .setDescription("You need to be playing a track to pause it.") +// .build()) +// .queue(); +// return; +// } boolean didPause = spotifyService.pausePlayback(user); interaction.replyEmbeds(EmbedUtils.genericEmbed() From d77c9378185b3381c980f1f832cfd1c52d86875b Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 04:04:50 +0100 Subject: [PATCH 6/7] spotify is v silly! --- .../spotify/command/CurrentSubCommand.java | 14 +++++++------- .../features/spotify/command/PauseSubCommand.java | 14 +++++++------- .../cc/fascinated/bat/service/SpotifyService.java | 3 +-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java b/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java index 7420b0e..d54bead 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/command/CurrentSubCommand.java @@ -42,13 +42,13 @@ public class CurrentSubCommand extends BatSubCommand { return; } -// if (!spotifyService.hasTrackPlaying(user)) { -// interaction.replyEmbeds(EmbedUtils.genericEmbed() -// .setDescription("You are not currently playing a track.") -// .build()) -// .queue(); -// return; -// } + if (!spotifyService.hasTrackPlaying(user)) { + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setDescription("You are not currently playing a track.") + .build()) + .queue(); + return; + } CurrentlyPlaying currentlyPlaying = spotifyService.getCurrentlyPlayingTrack(user); Track track = (Track) currentlyPlaying.getItem(); AlbumSimplified album = track.getAlbum(); diff --git a/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java b/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java index f6fb55a..77ef74b 100644 --- a/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/spotify/command/PauseSubCommand.java @@ -36,13 +36,13 @@ public class PauseSubCommand extends BatSubCommand { return; } -// if (!spotifyService.hasTrackPlaying(user)) { -// interaction.replyEmbeds(EmbedUtils.genericEmbed() -// .setDescription("You need to be playing a track to pause it.") -// .build()) -// .queue(); -// return; -// } + if (!spotifyService.hasTrackPlaying(user)) { + interaction.replyEmbeds(EmbedUtils.genericEmbed() + .setDescription("You need to be playing a track to pause it.") + .build()) + .queue(); + return; + } boolean didPause = spotifyService.pausePlayback(user); interaction.replyEmbeds(EmbedUtils.genericEmbed() diff --git a/src/main/java/cc/fascinated/bat/service/SpotifyService.java b/src/main/java/cc/fascinated/bat/service/SpotifyService.java index de8fea3..6d5361e 100644 --- a/src/main/java/cc/fascinated/bat/service/SpotifyService.java +++ b/src/main/java/cc/fascinated/bat/service/SpotifyService.java @@ -86,8 +86,7 @@ public class SpotifyService { try { return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute(); } catch (Exception e) { - e.printStackTrace(); - return null; + throw new RuntimeException("Failed to get currently playing track", e); } } From 88b88633a7aba2b062f7357497b985c660732285 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 28 Jun 2024 04:05:41 +0100 Subject: [PATCH 7/7] fix botstats command --- .../java/cc/fascinated/bat/command/impl/BotStatsCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/command/impl/BotStatsCommand.java b/src/main/java/cc/fascinated/bat/command/impl/BotStatsCommand.java index e4061fb..360e970 100644 --- a/src/main/java/cc/fascinated/bat/command/impl/BotStatsCommand.java +++ b/src/main/java/cc/fascinated/bat/command/impl/BotStatsCommand.java @@ -42,8 +42,8 @@ public class BotStatsCommand extends BatCommand { interaction.replyEmbeds(EmbedUtils.genericEmbed().setDescription( "**Bot Statistics**\n" + - "➜ Guilds: **%s\n".formatted(jda.getGuilds().size()) + - "➜ Users: **%s\n".formatted(jda.getUsers().size()) + + "➜ Guilds: **%s**\n".formatted(jda.getGuilds().size()) + + "➜ Users: **%s**\n".formatted(jda.getUsers().size()) + "➜ Gateway Ping: **%sms**\n".formatted(jda.getGatewayPing()) + "\n" + "**Bat Statistics**\n" +