Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to fetch user's guild #40

Open
AshuTheDoughnut opened this issue Jun 21, 2021 · 6 comments
Open

Failed to fetch user's guild #40

AshuTheDoughnut opened this issue Jun 21, 2021 · 6 comments

Comments

@AshuTheDoughnut
Copy link

Is this occurring cause if rate limits or something?

seems to happen randomly.

image

@c43721
Copy link

c43721 commented Jun 27, 2021

This happens when you hit the rate limit supposedly, but to reproduce:

  1. Go to oauth login page
  2. Double click on "Authorize"
  3. Find error

I don't know what causes this, but I will try to look into it.

@AshuTheDoughnut
Copy link
Author

Thanks....please do

@Lusiny
Copy link

Lusiny commented Aug 16, 2021

I'm having the same problem if someone can tell me some way around the multiple clicks on the Authorize button.

@Lusiny
Copy link

Lusiny commented Aug 18, 2021

After several hours thinking of various solutions to avoid this error I came to this code:

const express = require("express");
const app = express();

const passport = require("passport");
const DiscordStrategy = require("passport-discord").Strategy;

const Strategy = new DiscordStrategy({ 
    clientID: "", // your client id here
    clientSecret: "", // your client secret here
    callbackURL: "", // your callback url here 
    scope: ["guilds"] // your scopes here
}, async (accessToken, refreshToken, profile, cb) => {
    await process.nextTick(async () => {
        if (profile.guilds == undefined) return cb(null, false); // When there is an error fetching the user's guilds, the .guilds property returns undefined, so we must point out in the callback that all the information hasn't arrived with cb(null, false)
        
        return cb(null, profile);
    });
});

passport.use(Strategy);
passport.serializeUser((user, done) => {
    if (!user) return; // In serialization, if the value received is false, as we saw above, it means that the information did not arrive in its entirety, that is, it is a repeated request and therefore it did not get all the information, so we must return to avoid serialization errors
    return done(null, user);
});
passport.deserializeUser((obj, done) => done(null, obj));

app
// Put whatever you are using in your application
.use(passport.initialize())
.use(passport.session())
.get("/callback", function (req, res, next) { return passport.authenticate("discord", { failureRedirect: "/" }, async function (err, user, info) {
    if (err) {
        console.error(err); // In case of an error, it must always be registered the choice is yours
        return res.redirect('/');  // This part is very important, even if it gives an error, unless it is a 429 (rate limit), the client will have all the information requested from the user, so here you must put the url you want it to be sent to (you can use the req.session.backURL, for example)
    }

    await req.login(user, function (e) {
        if (e) return next(e); // As we are careful with user serialization here it will not return anything so this part is not really necessary
        return next();
    });
})(req, res, next); }, async function (req, res) {
    return res.redirect('/'); // Then we must redirect the user to wherever you want
})

IMPORTANT

  • I've been looking for the answer for a few days and I've come to this conclusion if you have any better conclusions, please reply to this message.
  • If something was not clear I apologize, and sorry also for my English I used google translator a lot.

@c43721
Copy link

c43721 commented Aug 18, 2021

What we should do is see if this happens on any scope, or just the guilds scope.

I don't have time to look into this myself, but I know how to reproduce this, but I am unsure if this is a library bug or a Discord bug (and I'm leaning towards both).

@Catdontpat
Copy link

HEY! Sorry for the late response, if you're getting this error it's most likely caused by the actual `

Strategy.prototype.checkScope
part, all you have to do is simply change the url in
this._oauth2.get('https://discord.com/api/users/@me' + scope, accessToken, function(err, body, res)

to

this._oauth2.get('https://discord.com/api/users/@me/' + scope, accessToken, function(err, body, res)

What it's trying to do is do a function to pull the guilds from the api in the wrong way and putting guilds right next to @me rather than making a slash

Hopefully this should help ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants