Updated the discord.ts controller
This commit is contained in:
parent
3d4dcdd7c8
commit
174dcc9223
@ -29,6 +29,26 @@ export const generateToken = (refreshToken: string) => oauth.tokenRequest({
|
||||
refreshToken
|
||||
});
|
||||
|
||||
/**
|
||||
* Updates a user's access token if it is expired
|
||||
* @param refreshDate The date of when to renew the provided access token
|
||||
* @param refreshToken The token to refresh the access token
|
||||
* @param accessToken The token to access the discord api
|
||||
*/
|
||||
export const updateToken = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
||||
let token = accessToken;
|
||||
if (refreshDate.valueOf() < Date.now()) {
|
||||
const newToken = await generateToken(refreshToken);
|
||||
token = newToken.access_token;
|
||||
User.update({
|
||||
accessToken: token, refreshDate: new Date(Date.now() + newToken.expires_in),
|
||||
refreshToken: newToken.refresh_token
|
||||
}, {where: {refreshToken: refreshToken}});
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the guilds from the access token.
|
||||
* This function also automatically renews the access token if it expires
|
||||
@ -37,14 +57,12 @@ export const generateToken = (refreshToken: string) => oauth.tokenRequest({
|
||||
* @param accessToken The token to access the discord api
|
||||
*/
|
||||
export const getGuilds = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
||||
let token = accessToken;
|
||||
if (refreshDate.valueOf() < Date.now()) {
|
||||
const newToken = await generateToken(refreshToken);
|
||||
token = newToken.access_token;
|
||||
User.update({accessToken: token, refreshDate: new Date(Date.now() + newToken.expires_in),
|
||||
refreshToken: newToken.refresh_token}, {where: {refreshToken: refreshToken}});
|
||||
}
|
||||
return await oauth.getUserGuilds(token);
|
||||
return await oauth.getUserGuilds(await updateToken(refreshDate, refreshToken, accessToken));
|
||||
}
|
||||
|
||||
export const canEditGuild = async (refreshDate: Date, refreshToken: string, accessToken: string, guildId: string) => {
|
||||
const guilds = await getGuilds(refreshDate, refreshToken, accessToken);
|
||||
return guilds.some(guild => guild.id === guildId && (guild.owner || (guild.permissions && (guild.permissions & 0x8))));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user