Fixed a bug in the discord.ts controller
This commit is contained in:
parent
ebff0f3914
commit
8128167347
@ -1,4 +1,5 @@
|
||||
import DiscordOauth2 from 'discord-oauth2';
|
||||
import {User} from "../models/User";
|
||||
|
||||
const oauth = new DiscordOauth2();
|
||||
|
||||
@ -37,8 +38,12 @@ export const generateToken = (refreshToken: string) => oauth.tokenRequest({
|
||||
*/
|
||||
export const getGuilds = async (refreshDate: Date, refreshToken: string, accessToken: string) => {
|
||||
let token = accessToken;
|
||||
if (refreshDate.getDate() > Date.now())
|
||||
token = (await generateToken(refreshToken)).access_token;
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user