-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.ts
54 lines (50 loc) · 1.96 KB
/
spotify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { params, data } from '@serverless/cloud'
import SpotifyWebApi from 'spotify-web-api-node'
export const parseSpotifyUrl = (url: string): string | undefined => {
if (url.includes('open.spotify.com/track') || url.includes('open.spotify.com/playlist')) {
const id = url.split('/')[4]
if (id.includes('?')) {
return id.split('?')[0]
}
return id
}
return undefined
}
export const createSpotifyAuthUrl = async (guildId: string) => {
const scopes = ['playlist-modify-private', 'playlist-modify-public', 'playlist-read-private']
const state = guildId
const spotify = new SpotifyWebApi({
clientId: params.SPOTIFY_DISCORD_CLIENT_ID,
clientSecret: params.SPOTIFY_DISCORD_CLIENT_SECRET,
redirectUri: params.CLOUD_URL + `/redirect`
})
const existingAuth = await data.get(`guild:${guildId}:spotify`)
if (!existingAuth) {
return spotify.createAuthorizeURL(scopes, state)
}
return undefined
}
export const authSpotifyForGuild = async (authCode: string, guildId: string) => {
const spotify = new SpotifyWebApi({
clientId: params.SPOTIFY_DISCORD_CLIENT_ID,
clientSecret: params.SPOTIFY_DISCORD_CLIENT_SECRET,
redirectUri: params.CLOUD_URL + `/redirect`
})
const { body } = await spotify.authorizationCodeGrant(authCode)
await data.set(`guild:${guildId}:spotify`, { refreshToken: body.refresh_token })
}
export const refreshGuildSpotifySession = async (guildId: string): Promise<SpotifyWebApi> => {
const spotify = new SpotifyWebApi({
clientId: params.SPOTIFY_DISCORD_CLIENT_ID,
clientSecret: params.SPOTIFY_DISCORD_CLIENT_SECRET,
redirectUri: params.CLOUD_URL + `/redirect`
})
const refreshTokenData = await data.get<any>(`guild:${guildId}:spotify`)
if (!refreshTokenData) {
throw new Error('Missing Refresh Token')
}
spotify.setRefreshToken(refreshTokenData.refreshToken)
const refreshData = await spotify.refreshAccessToken()
spotify.setAccessToken(refreshData.body.access_token)
return spotify
}