Skip to content

Commit

Permalink
fix command autosyncing
Browse files Browse the repository at this point in the history
  • Loading branch information
advaith1 committed Jan 10, 2021
1 parent 7590b13 commit 3ddd62d
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Routes/autosync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,30 @@ router.get('/bots', async (req, res) => {
if (botExists.scopes?.slashCommands) {
const owner = await userCache.getUser(botExists.owner.id)

if (owner.auth && Date.now() > owner.auth.expires) {
await refresh.requestNewAccessToken('discord', owner.auth.refreshToken, async (err, accessToken, refreshToken, result: RESTPostOAuth2AccessTokenResult) => {
if (!err) {
await global.db.collection("users").updateOne(
{ _id: owner._id },
{
$set: {
auth: {
accessToken,
refreshToken,
expires: Date.now() + result.expires_in*1000
if (owner.auth) {
if (Date.now() > owner.auth.expires) {
await refresh.requestNewAccessToken('discord', owner.auth.refreshToken, async (err, accessToken, refreshToken, result: RESTPostOAuth2AccessTokenResult) => {
if (!err) {
await global.db.collection("users").updateOne(
{ _id: owner._id },
{
$set: {
auth: {
accessToken,
refreshToken,
expires: Date.now() + result.expires_in*1000
}
}
}
}
);
await userCache.updateUser(owner._id)
}
})
);
await userCache.updateUser(owner._id)
}
})
}

const receivedCommands = await (await fetch(`https://discord.com/api/v8/applications/${bot.id}/commands`, {headers: {authorization: `Bearer ${owner.auth.accessToken}`}})).json().catch(() => {}) as APIApplicationCommand[]
if (Array.isArray(receivedCommands)) commands = receivedCommands;
}

const receivedCommands = await (await fetch(`https://discord.com/api/v8/applications/${bot.id}/commands`, {headers: {authorization: `Bearer ${req.user.db.auth.accessToken}`}})).json().catch(() => {}) as APIApplicationCommand[]
if (Array.isArray(receivedCommands)) commands = receivedCommands;
}

await global.db.collection("bots").updateOne(
Expand Down

0 comments on commit 3ddd62d

Please sign in to comment.