use of de.notecho.spotify.bot.modules.BaseModule in project spotifybot by NotEchoDE.
the class BotInstance method updateTokens.
private void updateTokens() {
if (user.spotifyTokens() == null) {
context.getBean(BotInstanceManagementService.class).stopInstance(user);
return;
}
updateTwitchToken(user.twitchTokens());
if (user.chatAccountTokens() != null)
updateTwitchToken(user.chatAccountTokens());
try {
this.spotifyApi.setRefreshToken(user.spotifyTokens().getRefreshToken());
AuthorizationCodeRefreshRequest authorizationCodeRefreshRequest = spotifyApi.authorizationCodeRefresh().build();
final AuthorizationCodeCredentials authorizationCodeCredentials = authorizationCodeRefreshRequest.execute();
spotifyApi.setAccessToken(authorizationCodeCredentials.getAccessToken());
spotifyApi.setRefreshToken(authorizationCodeCredentials.getRefreshToken());
user.spotifyTokens().setAccessToken(authorizationCodeCredentials.getAccessToken());
Logger.log(LogType.DEBUG, "[" + user.getId() + "] Got new Spotify Token for " + login + ", expires in: " + authorizationCodeCredentials.getExpiresIn() + " | " + authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...", "Spotify", login, String.valueOf(authorizationCodeCredentials.getExpiresIn()), authorizationCodeCredentials.getAccessToken().substring(0, 10) + "...");
} catch (BadRequestException e) {
TokenPair spotifyTokens = user.spotifyTokens();
user.getTokenPairs().remove(spotifyTokens);
context.getBean(UserRepository.class).saveAndFlush(user);
context.getBean(TokenPairRepository.class).delete(spotifyTokens);
Logger.log(LogType.INFO, "[" + user.getId() + "] " + login + " revoked his access token so it was removed from the database.", login, "revoked", "database");
for (BaseModule module : this.modules) module.unregister(client);
client.getChat().leaveChannel(this.login);
context.getBean(BotInstanceManagementService.class).stopInstance(user);
return;
} catch (IOException | SpotifyWebApiException | ParseException e) {
e.printStackTrace();
}
context.getBean(UserRepository.class).saveAndFlush(user);
nextCheck = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(50);
}
Aggregations