This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
162 lines (127 loc) · 4.52 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const token = require("./token.json");
const fs = require("fs");
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
const sql = require('sqlite');
sql.open('./bot.sqlite');
const express = require('express');
const app = express();
const port = 80;
const bodyParser = require('body-parser');
const urlencodedParser = bodyParser.urlencoded({ extended: true });
const url = require('url');
const WebSocket = require('ws');
function WebSocketSetup() {
wss.on('connection', function(ws) {
ws.on('message', async function(message) {
let msg = JSON.stringify(message);
const arguments = ['id', 'username', 'discriminator'];
for (let i = 0; i < arguments.length; i++) {
if (msg[arguments[i]] === undefined) return;
}
let user = await client.users.find(u => u.username === msg['username'] && u.discriminator === msg['discriminator']);
if (!user) return;
await sql.run(`INSERT INTO whmcs VALUES (?, ?)`, [msg['id'], user.id]);
});
});
}
function expressSetup(guild, data) {
let clientId;
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/api/discord/', (req, res) => {
clientId = req.query.id;
res.redirect('/link');
});
app.get('/link/', (req, res) => {
res.render('link', {clientId: clientId})
});
app.get('/success', (req, res) => {
res.render('success', {
clientId: clientId,
username: req.query.username,
discriminator: req.query.discriminator
});
});
app.get('/fail', (req, res) => {
res.render('fail', {clientId: clientId});
});
app.get('/api/link/', async (req, res) => {
//let isFail = true;
let discriminator = req.query.discriminator;
console.log(discriminator);
let username = decodeURI(req.query.username);
let id = req.query.id;
console.log(id);
let clientId = req.query.clientId;
console.log(clientId);
let user = client.users.find(u => u.username === username && u.discriminator === discriminator);
let guild = '346715007469355009';
new Discord.GuildMember(client, data, guild);
const crole = '354348234413441027';
await Discord.id.addRole(crole);
//if (user) {
// if (!isNaN(discriminator) && discriminator.toString().length === 4) isFail = false;
//}
//if (isFail === false) {
// let isCatch = false;
await sql.run(`INSERT INTO whmcs VALUES (?, ?)`, [clientId, id]).catch((err) => {
work();
isCatch = true;
});
if (isCatch === true) return;
res.redirect(url.format({
pathname: "/success",
query: req.query
}));
// } else {
// fail();
//}
function work() {
res.redirect(url.format({
pathname: "/success"
}));
}
});
app.listen(port, () => {});
}
client.elevation = async msg => {
let permlvl = 0;
config.staffRoles.map(r => {
let role = msg.guild.roles.find(role => role.name === r);
if (role) {
if (msg.member.roles.has(role.id)) permlvl = 1;
}
});
if (msg.author.id === msg.guild.ownerID) permlvl = 2;
return permlvl;
};
fs.readdir('./commands', (err, files) => {
if (err) console.error(err);
files.forEach(f => {
let props = require(`./commands/${f}`);
console.log(`Loading Command: ${props.help.name}.`);
client.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
});
console.log(`Loading a total of ${files.length} commands.`);
});
fs.readdir('./events/', (err, files) => {
if (err) console.error(err);
console.log(`Loading a total of ${files.length} events.`);
files.forEach(file => {
const eventName = file.split(".")[0];
const event = require(`./events/${file}`);
if (eventName === "messageUpdate") client.on(eventName, event.bind(null));
else client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
});
});
//expressSetup(); call if you want to use the webserver anyways.
expressSetup();
client.login(token.token);