const cheerio = require('cheerio'); const {defaultPermissions} = require('../util/default.json'); const {db, settingsData, sendMsg, createNotice, hasPerm} = require('./util.js'); const DiscordOauth2 = require('discord-oauth2'); const oauth = new DiscordOauth2( { clientId: process.env.bot, clientSecret: process.env.secret, redirectUri: process.env.dashboard } ); const file = require('fs').readFileSync('./dashboard/index.html'); /** * Let a user change settings * @param {import('http').ServerResponse} res - The server response * @param {String} state - The user state * @param {URL} reqURL - The used url */ function dashboard_guilds(res, state, reqURL) { var arguments = reqURL.pathname.split('/'); var settings = settingsData.get(state); var $ = cheerio.load(file); let notice = ''; if ( process.env.READONLY ) { notice = createNotice($, { title: 'Read-only database!', text: 'You can currently only view your settings but not change them.' }); } $('replace#notice').replaceWith(notice); $('.navbar #logout img').attr('src', settings.user.avatar); $('.navbar #logout span').text(`${settings.user.username} #${settings.user.discriminator}`); $('.guild#invite a').attr('href', oauth.generateAuthUrl( { scope: ['identify', 'guilds', 'bot'], permissions: defaultPermissions, state } )); $('.guild#refresh a').attr('href', '/refresh?return=' + reqURL.pathname); let guilds = $('
'); if ( settings.guilds.isMember.size ) { $('
').append( $('
') ).appendTo(guilds); settings.guilds.isMember.forEach( guild => { $('
').attr('id', guild.id).append( $('
'), $('').attr('href', `/guild/${guild.id}`).attr('alt', guild.name).append( ( guild.icon ? $('').attr('src', guild.icon).attr('alt', guild.name) : $('
').text(guild.acronym) ) ) ).appendTo(guilds); } ); } if ( settings.guilds.notMember.size ) { $('
').append( $('
') ).appendTo(guilds); settings.guilds.notMember.forEach( guild => { $('