Automatically refresh Spotify token

This commit is contained in:
Max Isom 2020-10-31 10:42:06 -04:00
parent 60a7658293
commit e7bf2a4b47
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880

View file

@ -9,11 +9,19 @@ import {sequelize} from './utils/db';
let bot = container.get<Bot>(TYPES.Bot);
const spotify = container.get<Spotify>(TYPES.Lib.Spotify);
(async () => {
const refreshSpotifyToken = async () => {
const auth = await spotify.clientCredentialsGrant();
spotify.setAccessToken(auth.body.access_token);
return auth.body.expires_in;
};
(async () => {
const spotifyRefreshIntervalSeconds = await refreshSpotifyToken();
setInterval(async () => refreshSpotifyToken(), (spotifyRefreshIntervalSeconds / 2) * 1000);
// Create data directories if necessary
await makeDir(container.get(TYPES.Config.DATA_DIR));
await makeDir(container.get(TYPES.Config.CACHE_DIR));