Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-Santana-j committed Apr 27, 2024
1 parent d0dff2d commit cb9480f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 63 deletions.
1 change: 0 additions & 1 deletion config/web-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module.exports = {
iban:process.env.IBAN,
stripeAccount:process.env.STRIPEACCOUNT,
host:process.env.HOST,
discordGuildUrl: "https://discord.com/oauth2/authorize?client_id=1210894508028338197&permissions=8&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fauth%2Fcallback%2Fguild&scope=bot+applications.commands+guilds.members.read+applications.commands.permissions.update",
product1: require('./product.json').product1,
product2:require('./product.json').product2,
product3:require('./product.json').product3
Expand Down
106 changes: 44 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,73 +149,47 @@ app.get('/dashboard', async (req, res) => {

app.get('/auth/callback', async (req, res) => {
try {
if (!req.query.code) {
res.redirect('/logout')
if (req.session.uid) {
res.redirect('/dashboard')
} else {
let param = new URLSearchParams({
client_id: webConfig.clientId,
client_secret: webConfig.secret,
grant_type: 'authorization_code',
code: req.query.code,
redirect_uri: webConfig.redirect
})
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'application/x-www-form-urlencoded'
};
const response = await axios.post('https://discord.com/api/oauth2/token', param, { headers }).then((res) => { return res }).catch((err) => console.error(err))
if (!response) {
if (!req.query.code) {
res.redirect('/logout')
return
}
let userResponse = await axios.get('https://discord.com/api/users/@me', {
headers: {
Authorization: `Bearer ${response.data.access_token}`,
...headers
} else {
let param = new URLSearchParams({
client_id: webConfig.clientId,
client_secret: webConfig.secret,
grant_type: 'authorization_code',
code: req.query.code,
redirect_uri: webConfig.redirect
})
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'application/x-www-form-urlencoded'
};
const response = await axios.post('https://discord.com/api/oauth2/token', param, { headers }).then((res) => { return res }).catch((err) => console.error(err))
if (!response) {
res.redirect('/logout')
return
}
}).then((res) => { return res.data }).catch((err) => console.error(err));
await db.create('users', userResponse.id, {
id: userResponse.id,
username: userResponse.username,
profile_pic: userResponse.avatar ? `https://cdn.discordapp.com/avatars/${userResponse.id}/${userResponse.avatar}.png` : 'https://res.cloudinary.com/dgcnfudya/image/upload/v1709143898/gs7ylxx370phif3usyuf.png',
displayName: userResponse.global_name,
email: userResponse.email,
access_token: response.data.access_token
})

req.session.uid = userResponse.id
let userResponse = await axios.get('https://discord.com/api/users/@me', {
headers: {
Authorization: `Bearer ${response.data.access_token}`,
...headers
}
}).then((res) => { return res.data }).catch((err) => console.error(err));
await db.create('users', userResponse.id, {
id: userResponse.id,
username: userResponse.username,
profile_pic: userResponse.avatar ? `https://cdn.discordapp.com/avatars/${userResponse.id}/${userResponse.avatar}.png` : 'https://res.cloudinary.com/dgcnfudya/image/upload/v1709143898/gs7ylxx370phif3usyuf.png',
displayName: userResponse.global_name,
email: userResponse.email,
access_token: response.data.access_token
})

req.session.uid = userResponse.id

res.redirect('/dashboard')
}
} catch (error) {
res.redirect('/logout')
}
})
app.get('/auth/callback/guild', async (req, res) => {
try {
if (!req.query.code) {
res.redirect('/dashboard')
} else {
let param = new URLSearchParams({
client_id: webConfig.clientId,
client_secret: webConfig.secret,
grant_type: 'authorization_code',
code: req.query.code,
redirect_uri: webConfig.redirect + '/guild'
})
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'application/x-www-form-urlencoded'
};
const response = await axios.post('https://discord.com/api/oauth2/token', param, { headers }).then((res) => { return res }).catch((err) => console.error(err))
if (!response) {
res.redirect('/dashboard')
return
}
await db.update('servers', req.query.guild_id, {
access_token: response.data.access_token
})
res.redirect(`/server/${req.query.guild_id}`)
}
} catch (error) {
res.redirect('/logout')
Expand Down Expand Up @@ -333,10 +307,18 @@ app.get('/addbot/:serverID', (req, res) => {
res.redirect(`/`)
return
}
res.redirect(`${webConfig.discordGuildUrl}&guild_id=${req.params.serverID}&disable_guild_select=true`)
const guilds = client.guilds.cache;
const isBotInServer = guilds.has(req.params.serverID);
if (isBotInServer) {
res.redirect(`/server/${req.body.serverID}`)
} else {
res.redirect(`https://discord.com/oauth2/authorize?client_id=1210894508028338197&permissions=8&response_type=code&scope=bot+applications.commands+guilds.members.read+applications.commands.permissions.update&redirect_uri=${process.env.DISCORDURI}&guild_id=${req.params.serverID}&disable_guild_select=true`)
}

})



// host:`${webConfig.host}`,


Expand Down

0 comments on commit cb9480f

Please sign in to comment.