Compare commits

...

8 Commits

Author SHA1 Message Date
087ab99b44 Merge branch 'master' into master 2024-06-28 09:26:34 +00:00
88b88633a7 fix botstats command 2024-06-28 04:05:41 +01:00
d77c937818 spotify is v silly! 2024-06-28 04:04:50 +01:00
bf554933cc pls 2024-06-28 04:01:30 +01:00
6af78c7da1 pls 2024-06-28 04:00:19 +01:00
31e81363ac pls 2024-06-28 03:56:34 +01:00
e892dade1c add log for token refresh in spotify 2024-06-28 03:52:29 +01:00
eccd673db8 spotify debug 2024-06-28 03:50:38 +01:00
3 changed files with 30 additions and 14 deletions

View File

@ -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" +

View File

@ -19,6 +19,11 @@ public class SpotifyProfile extends Profile {
*/
private String refreshToken;
/**
* When the access token expires
*/
private Long expiresAt;
public SpotifyProfile() {
super("spotify");
}

View File

@ -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.
*/
@ -69,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();
}
@ -91,7 +86,7 @@ public class SpotifyService {
try {
return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
} catch (Exception e) {
return null;
throw new RuntimeException("Failed to get currently playing track", e);
}
}
@ -178,11 +173,27 @@ 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);
log.info("Refreshed spotify access token for user {}", user.getName());
}
return api;
}
}