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
|
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.
|
* Gets the guilds from the access token.
|
||||||
* This function also automatically renews the access token if it expires
|
* 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
|
* @param accessToken The token to access the discord api
|
||||||
*/
|
*/
|
||||||
export const getGuilds = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
export const getGuilds = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
||||||
let token = accessToken;
|
return await oauth.getUserGuilds(await updateToken(refreshDate, refreshToken, accessToken));
|
||||||
if (refreshDate.valueOf() < Date.now()) {
|
}
|
||||||
const newToken = await generateToken(refreshToken);
|
|
||||||
token = newToken.access_token;
|
export const canEditGuild = async (refreshDate: Date, refreshToken: string, accessToken: string, guildId: string) => {
|
||||||
User.update({accessToken: token, refreshDate: new Date(Date.now() + newToken.expires_in),
|
const guilds = await getGuilds(refreshDate, refreshToken, accessToken);
|
||||||
refreshToken: newToken.refresh_token}, {where: {refreshToken: refreshToken}});
|
return guilds.some(guild => guild.id === guildId && (guild.owner || (guild.permissions && (guild.permissions & 0x8))));
|
||||||
}
|
|
||||||
return await oauth.getUserGuilds(token);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user