diff --git a/HTML/joinedGuild.html b/HTML/joinedGuild.html new file mode 100644 index 0000000..70b73b4 --- /dev/null +++ b/HTML/joinedGuild.html @@ -0,0 +1,232 @@ + + + + + + + Selmer Bot Web Dashboard + + + + + + +
+
+
G
+
N
+
I
+
D
+
A
+
O
+
L
+
+
+ +
Animation by Dissimulate https://codepen.io/dissimulate + + + \ No newline at end of file diff --git a/HTML/myGuilds.html b/HTML/myGuilds.html index 3cf0039..a1bb8a6 100644 --- a/HTML/myGuilds.html +++ b/HTML/myGuilds.html @@ -58,7 +58,8 @@ function getSessionData() { return new Promise((resolve, reject) => { var xhrsess = new XMLHttpRequest(); - xhrsess.open('post', 'https://www.selmerbot.com/getSessionInfo/', true); + // xhrsess.open('post', 'https://www.selmerbot.com/getSessionInfo/', true); + xhrsess.open('post', 'http://localhost:53134/getSessionInfo/', true); xhrsess.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); xhrsess.setRequestHeader('session', window.localStorage.getItem('sessionId')); @@ -131,7 +132,14 @@ // imagem.classList.add('serverImgOut'); imagem.className = 'serverImgOut'; imagem.onclick = function() { - window.open('https://discord.com/oauth2/authorize?client_id=926551095352901632&scope=applications.commands+bot&permissions=549755289087'); + var url; + //Open a new window with the invite. Once the invite is processed, send request to the server to update the db (toggle inServer to true?) + if (window.location.href.indexOf('localhost') != -1) { + url = 'https://discord.com/api/oauth2/authorize?client_id=944046902415093760&permissions=549755289087&redirect_uri=http%3A%2F%2Flocalhost%3A53134%2FjoinedGuild&response_type=code&scope=rpc%20bot%20applications.commands' + } else { + url = 'https://discord.com/api/oauth2/authorize?client_id=944046902415093760&permissions=549755289087&redirect_uri=https%3A%2F%2Fwww.selmerbot.com%2FjoinedGuild&scope=bot%20applications.commands' + } + window.open(url); }//OPEN A NEW WINDOW WITH SERVER INVITE } diff --git a/main.js b/main.js index d938c2e..4305a50 100644 --- a/main.js +++ b/main.js @@ -52,10 +52,69 @@ async function getJSONResponse(body) { return JSON.parse(fullBody); } + +async function setCurrentServer(sid, id) { + return new Promise((resolve, reject) => { + try { + if (id) { + const obj = {}; + const arr = {text: [], voice: []}; + const guild = bot.guilds.cache.get(id); + + const roles = []; + guild.roles.cache.filter((role) => { return(!role.managed) }).forEach((role) => { + let newObj = {}; + newObj.Id = role.id; + newObj.name = role.name; + newObj.color = role.hexColor; + + roles.push(newObj); + }); + + const channels = guild.channels.cache; + channels.forEach((channel) => { + const type = channel.type; + if (type == 'GUILD_TEXT') { + arr.text.push({ name: channel.name, id: channel.id }); + } else if (type == 'GUILD_VOICE') { + arr.voice.push({ name: channel.name, id: channel.id }); + } + }); + + connection.then((client) => { + const dbo = client.db(id).collection('SETUP'); + dbo.find().toArray(async (err, docs) => { + const m = new Map(); + m.set('Id', id); + + await Promise.all(docs.map(async (doc) => { + m.set(doc._id, doc); + })).then(() => { + obj['Id'] = id; + obj['roles'] = JSON.stringify(roles); + obj['channels'] = JSON.stringify(arr); + obj['serverSettings'] = JSON.stringify(Object.fromEntries(m)); + + const dbo = client.db('main').collection('sessions'); + dbo.updateOne({ sessionId: sid }, {$set: { currentServer: JSON.stringify(obj) }}); + resolve(200); + }); + }) + }); + } + } catch (err) { + reject(err); + } + }); +} + + + const app = express(); // app.use(express.json()); app.use(express.static('/assets')); app.use('/CSS', express.static('./CSS')); +app.use('/scripts', express.static('./scripts')); app.use(bodyParser.urlencoded({ extended: true })); @@ -160,59 +219,12 @@ app.get('/getChannels', async (req, res) => { //Headers: servernumber, sessionid app.post('/setCurrentServer', async (req, res) => { - try { - const id = req.headers.servernumber; - - if (id) { - const obj = {}; - const arr = {text: [], voice: []}; - const guild = bot.guilds.cache.get(id); - - const roles = []; - guild.roles.cache.filter((role) => { return(!role.managed) }).forEach((role) => { - let newObj = {}; - newObj.Id = role.id; - newObj.name = role.name; - newObj.color = role.hexColor; - - roles.push(newObj); - }); - - const channels = guild.channels.cache; - channels.forEach((channel) => { - const type = channel.type; - if (type == 'GUILD_TEXT') { - arr.text.push({ name: channel.name, id: channel.id }); - } else if (type == 'GUILD_VOICE') { - arr.voice.push({ name: channel.name, id: channel.id }); - } - }); - - connection.then((client) => { - const dbo = client.db(id).collection('SETUP'); - dbo.find().toArray(async (err, docs) => { - const m = new Map(); - m.set('Id', id); - - await Promise.all(docs.map(async (doc) => { - m.set(doc._id, doc); - })).then(() => { - obj['Id'] = id; - obj['roles'] = JSON.stringify(roles); - obj['channels'] = JSON.stringify(arr); - obj['serverSettings'] = JSON.stringify(Object.fromEntries(m)); - - const dbo = client.db('main').collection('sessions'); - dbo.updateOne({ sessionId: req.headers.sessionid }, {$set: { currentServer: JSON.stringify(obj) }}); - res.sendStatus(200); - }); - }) - }); - } - } catch (err) { + setCurrentServer().then((code) => { + res.sendStatus(code); + }).catch((err) => { console.error(err); res.sendStatus(500); - } + }); }); @@ -293,7 +305,37 @@ app.get('/calendar', async (req, res) => { app.get('/team', async (req, res) => { return res.sendFile("team.html", { root: 'HTML' }); -}) +}); + +app.get('/joinedGuild', async (req, res) => { + res.sendFile('joinedGuild.html', { root: 'HTML' }); +}); + +app.post('/joinedGuild', async (req, res) => { + // res.sendFile('joinedGuild.html', { root: 'HTML' }); + const { headers } = req; + const { servernumber, sessionid } = headers; + + connection.then((client) => { + const dbo = client.db('main').collection('sessions'); + dbo.find().toArray((err, docs) => { + const guilds = JSON.parse(docs[0].guilds); + + try { + for (let i = 0; i < guilds.length; i++) { + if (guilds[i].id == servernumber) { + guilds[i].inServer = true; + setCurrentServer(sessionid, servernumber); + return res.sendStatus(200); + } + } + } catch (err) { + console.log(err); + res.sendStatus(500); + } + }); + }); +}); //#endregion diff --git a/scripts/scripts.js b/scripts/scripts.js index e69de29..ef1730f 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -0,0 +1,2 @@ +// I'm not sure this will contain any of the html scripts +// This is because every file's functions (even sendData) are unique to that file \ No newline at end of file