From 9ac52739bfd2a4fa7e6b96d42ae99de87f88d499 Mon Sep 17 00:00:00 2001 From: Fernando Date: Sat, 27 Apr 2024 16:40:07 -0300 Subject: [PATCH] fix --- config/web-config.js | 3 +- index.js | 77 +++++++++++++++++++++++++++++++++--------- public/js/dashboard.js | 9 +++++ views/dashboard.ejs | 9 ++--- views/index.ejs | 8 ++++- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/config/web-config.js b/config/web-config.js index 995afca..6f19950 100644 --- a/config/web-config.js +++ b/config/web-config.js @@ -18,5 +18,6 @@ module.exports = { host:process.env.HOST, product1: require('./product.json').product1, product2:require('./product.json').product2, - product3:require('./product.json').product3 + product3:require('./product.json').product3, + loginURL: 'https://discord.com/oauth2/authorize?client_id=1210894508028338197&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fauth%2Fcallback&scope=identify+guilds+email+guilds.join' } diff --git a/index.js b/index.js index 58bde6f..7443130 100644 --- a/index.js +++ b/index.js @@ -122,20 +122,36 @@ app.get('/dashboard', async (req, res) => { if (req.session.uid) { let user = await db.findOne({ colecao: 'users', doc: req.session.uid }) let server = await functions.reqServerByTime(user, functions.findServers) - if (server.error) { - res.redirect('/') - return - } let servidoresEnd = [] - for (let i = 0; i < server.length; i++) { - let element = server[i] - - let Findserver = await db.findOne({ colecao: 'servers', doc: element.id }) - if (Findserver.error == false) { - servidoresEnd.push(Findserver) - } else { - servidoresEnd.push(element) + if (server.error) { + if (user.lastServers) { + for (let index = 0; index < user.lastServers.length; index++) { + const element = user.lastServers[index]; + let Findserver = await db.findOne({ colecao: 'servers', doc: element }) + if (Findserver.error == false) { + servidoresEnd.push(Findserver) + } + } + }else{ + res.redirect('/') } + }else{ + let lastServers = [] + for (let i = 0; i < server.length; i++) { + let element = server[i] + + let Findserver = await db.findOne({ colecao: 'servers', doc: element.id }) + if (Findserver.error == false) { + servidoresEnd.push(Findserver) + lastServers.push(Findserver.id) + } else { + servidoresEnd.push(element) + } + + } + db.update('users',user.id,{ + lastServers:lastServers + }) } res.render('dashboard', { host: `${webConfig.host}`, user: user, servers: servidoresEnd }) @@ -147,6 +163,34 @@ app.get('/dashboard', async (req, res) => { +app.get('/auth/verify/:acesstoken', async (req, res) => { + let param = req.params.acesstoken + if (param) { + try { + const headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Accept-Encoding': 'application/x-www-form-urlencoded' + }; + let userResponse = await axios.get('https://discord.com/api/users/@me', { + headers: { + Authorization: `Bearer ${param}`, + ...headers + } + }).then((res) => { return res.data }) + if (userResponse) { + req.session.uid = userResponse.id + res.redirect('/dashboard') + }else{ + res.redirect(webConfig.loginURL) + } + } catch (error) { + res.redirect(webConfig.loginURL) + } + } else { + res.redirect(webConfig.loginURL) + } +}) + app.get('/auth/callback', async (req, res) => { try { if (req.session.uid) { @@ -166,8 +210,9 @@ app.get('/auth/callback', async (req, res) => { '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)}) + 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 @@ -212,7 +257,7 @@ app.get('/logout', async (req, res) => { res.redirect('/') } }) - }else{ + } else { res.redirect('/') } } catch (error) { @@ -421,7 +466,7 @@ app.get('/server/config/:id', functions.subscriptionStatus, async (req, res) => -app.get('/help',(req,res)=>{ +app.get('/help', (req, res) => { res.redirect('https://discord.com/channels/1210714138838568960/1210927599669485648') }) diff --git a/public/js/dashboard.js b/public/js/dashboard.js index e69de29..a693944 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -0,0 +1,9 @@ +const botedit = new URLSearchParams(new URL(location.href).search).get('botedit', 0) +if (botedit == 'false') { + errorNotify('Você não tem permissão para editar o bot!') + const novaURL = window.location.protocol + '//' + window.location.host + window.location.pathname + window.history.pushState({ path: novaURL }, '', novaURL); +} + + + diff --git a/views/dashboard.ejs b/views/dashboard.ejs index 6ce8d93..0c445db 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -58,13 +58,8 @@ <%- include('./reusable/script.ejs') %> \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index 2a79d62..09be107 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -212,7 +212,13 @@ <%- include('./reusable/script.ejs') %>