Compare commits

..

No commits in common. "087ab99b448b05d850e3b416bdef1a48972262cc" and "7f58a84acf7df87aff8ae7ad1fba33acec8ea53b" have entirely different histories.

3 changed files with 14 additions and 30 deletions

View File

@ -42,8 +42,8 @@ public class BotStatsCommand extends BatCommand {
interaction.replyEmbeds(EmbedUtils.genericEmbed().setDescription( interaction.replyEmbeds(EmbedUtils.genericEmbed().setDescription(
"**Bot Statistics**\n" + "**Bot Statistics**\n" +
"➜ Guilds: **%s**\n".formatted(jda.getGuilds().size()) + "➜ Guilds: **%s\n".formatted(jda.getGuilds().size()) +
"➜ Users: **%s**\n".formatted(jda.getUsers().size()) + "➜ Users: **%s\n".formatted(jda.getUsers().size()) +
"➜ Gateway Ping: **%sms**\n".formatted(jda.getGatewayPing()) + "➜ Gateway Ping: **%sms**\n".formatted(jda.getGatewayPing()) +
"\n" + "\n" +
"**Bat Statistics**\n" + "**Bat Statistics**\n" +

View File

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

View File

@ -7,8 +7,6 @@ import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import net.jodah.expiringmap.ExpiringMap; import net.jodah.expiringmap.ExpiringMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import se.michaelthelin.spotify.SpotifyApi; import se.michaelthelin.spotify.SpotifyApi;
@ -26,7 +24,6 @@ import java.util.concurrent.TimeUnit;
@Service @Service
@Getter @Getter
public class SpotifyService { public class SpotifyService {
private static final Logger log = LoggerFactory.getLogger(SpotifyService.class);
/** /**
* The access token map. * The access token map.
*/ */
@ -72,7 +69,15 @@ public class SpotifyService {
.build(); .build();
this.authorizationUrl = spotifyApi.authorizationCodeUri() this.authorizationUrl = spotifyApi.authorizationCodeUri()
.response_type("code") .response_type("code")
.scope(AuthorizationScope.values()) .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
)
.build().execute().toString(); .build().execute().toString();
} }
@ -86,7 +91,7 @@ public class SpotifyService {
try { try {
return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute(); return getSpotifyApi(user).getUsersCurrentlyPlayingTrack().build().execute();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Failed to get currently playing track", e); return null;
} }
} }
@ -173,27 +178,11 @@ public class SpotifyService {
* *
* @return the Spotify API * @return the Spotify API
*/ */
@SneakyThrows
public SpotifyApi getSpotifyApi(BatUser user) { public SpotifyApi getSpotifyApi(BatUser user) {
SpotifyProfile profile = user.getProfile(SpotifyProfile.class); SpotifyProfile profile = user.getProfile(SpotifyProfile.class);
SpotifyApi api = new SpotifyApi.Builder() return new SpotifyApi.Builder()
.setClientId(clientId)
.setClientSecret(clientSecret)
.setAccessToken(profile.getAccessToken()) .setAccessToken(profile.getAccessToken())
.setRefreshToken(profile.getRefreshToken()) .setClientSecret(clientSecret)
.build(); .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;
} }
} }