0

So basically I were creating a whitelist system in node.js to check if the user is whitelisted by checking the role. If user in guild and if the user has the role It will response as ACCEPTED but If he has no role then will response as DECLINED and I want you guys to add if the user is not in the GUILD then it will response as NONMEMBER if it has a error then response as ERROR. I don't even know why it is I've annoyed myself for like three hours and made me to ask you. so can you guys able to help me i hope you guys can thank you for reading and helping me!

const express = require('express'); const axios = require('axios'); const querystring = require('querystring'); const app = express(); const PORT = 3000; const CLIENT_ID = '1332081657993367582'; const REQUIRED_ROLE_ID = "1325501603201028171" const GUILD_ID = "1325501050471448647" const CLIENT_SECRET = process.env.CLIENT_SECRET; const REDIRECT_URI = `https://dour-repeated-gasoline.glitch.me/validate`; const SCOPE = 'identify'; app.get('/login', (req, res) => { const discordOAuthURL = `https://discord.com/oauth2/authorize?` + `client_id=${CLIENT_ID}` + `&response_type=token` + `&redirect_uri=${encodeURIComponent(REDIRECT_URI)}` + `&scope=identify`; res.redirect(discordOAuthURL); }); app.get('/validate', async (req, res) => { const { code } = req.query; if (!code) { return res.status(400).send(''); } try { const tokenResponse = await axios.post('https://discord.com/api/oauth2/token', null, { params: { client_id: CLIENT_ID, client_secret: CLIENT_SECRET, grant_type: 'authorization_code', code, redirect_uri: REDIRECT_URI, }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }); const accessToken = tokenResponse.data.access_token; const userGuilds = await axios.get('https://discord.com/api/v10/users/@me/guilds', { headers: { Authorization: "Bearer " + code }, }); const isRolePresent = userGuilds.data.some((guild) => guild.roles?.includes(REQUIRED_ROLE_ID)); if (isRolePresent) { res.send("ACCEPTED"); } else { res.send("DECLINED"); } } catch (error) { console.error('Error during validation:', error.response?.data || error.message); res.status(500).send('Something went wrong try again later'); } }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });``` 
2
  • I don't fully understand your question. Based on the title, it sounds like you are getting some kind of permission or authentication error. I imagine that's because you are mixing the OAuth2 auth code grant with the implicit grant, but it's difficult to tell for sure. Can you provide error messages and stack traces to clarify where your code is failing? Commented Jan 26 at 12:50
  • Thank you for your response! My goal is to check if a user is in a specific Discord guild and has a certain role, responding with ACCEPTED, DECLINED, NONMEMBER, or ERROR accordingly. I suspect I’m mixing OAuth2 flows (auth code and implicit grant), and the API doesn’t seem to return role data or indicate if the user is in the guild. Could you clarify if my approach is correct, and how I can properly implement this check? Let me know if you need specific error logs—thanks! Commented Jan 26 at 13:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.