This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodewizards.js
459 lines (429 loc) · 13 KB
/
codewizards.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
const axios = require('axios');
const Discord = require('discord.js');
const fs = require('fs');
const express = require('express');
const https = require('https');
const fetch = require('node-fetch');
const CryptoJS = require('crypto-js');
require('dotenv').config();
const client = new Discord.Client();
const app = express();
port = 3750;
const hskey = fs.readFileSync(process.env.HTTPS_KEY);
const hscert = fs.readFileSync(process.env.HTTPS_CERT);
const expressOptions = {
key: hskey,
cert: hscert
};
server = https.createServer(expressOptions, app);
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*'); // update to match the domain you will make the request from
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.use(express.json());
app.get('/', (req, res) => res.send('How have you found this O_O'));
app.post('/authuser', async (req, res) => {
data = req.body;
console.log(data);
if (!data.code) {
res.status(406).send('No code specified');
return;
}
if (!req.headers.timestamp) {
res.status(406).send('No timestamp provided');
return;
} else if (new Date(new Date()).getTime() - new Date(req.headers.timestamp).getTime() > 60000) {
res.status(406).send('Invalid timestamp provided');
return;
}
if (
req.headers.authorization !==
CryptoJS.HmacSHA512(data.code + req.headers.timestamp, process.env.APPLICATION_SECRET).toString(
CryptoJS.enc.Base64
)
) {
res.status(406).send('Authorization failed');
return;
}
console.log('Verifying user with code ' + data.code);
try {
data = {
client_id: process.env.APPLICATION_ID,
client_secret: process.env.APPLICATION_SECRET,
grant_type: 'authorization_code',
code: data.code,
redirect_uri: 'https://elies.codewizardshq.com/Testing_Folder/discord/redirect.html',
scope: 'identify'
};
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
var form_data = new URLSearchParams();
for (var key in data) {
form_data.append(key, data[key]);
}
const tokenResponse = await fetch('https://discordapp.com/api/oauth2/token', {
method: 'POST',
body: form_data,
headers: headers
});
console.log(tokenResponse);
if (!tokenResponse.ok) {
res.status(406).send('Code invalid/expired');
}
const tokenInfo = await tokenResponse.json();
if (!tokenInfo.access_token) {
res.status(407).send('An error occured.');
return;
}
const userResponse = await fetch('https://discordapp.com/api/users/@me', {
headers: { Authorization: 'Bearer ' + tokenInfo.access_token }
});
const userData = await userResponse.json();
res.send(userData);
const user = client.guilds.cache.get(guildId).members.cache.get(userData.id);
// console.log(user);
user.send(createEmbeds(user.user.username).checking).then(function(message) {
let role = client.guilds.cache.get(guildId).roles.cache.find((role) => role.name === giveRoleName);
client.guilds.cache.get(guildId).members.cache.get(user.id).roles.add(role).catch(console.error);
let role2 = client.guilds.cache.get(guildId).roles.cache.find((role) => role.name === removeRoleName);
client.guilds.cache.get(guildId).members.cache.get(user.id).roles.remove(role2).catch(console.error);
message.edit(createEmbeds(user.user.username).verified);
client.guilds.cache
.get(guildId)
.channels.cache.find((val) => val.name === 'verify-log')
.send(createEmbeds(user.user.username).isverified);
});
} catch (error) {
console.log(error);
res.status(407).send('An error occured.');
return;
}
});
server.listen(port, () => console.log(`Discord Bot listening on port ${port}!`));
// get addons -- Loader made by tbranyen
let addons = [];
const normalizedPath = require('path').join(__dirname, 'addons');
require('fs').readdirSync(normalizedPath).forEach(function(file) {
if (file.substr(-3) === '.js') addons.push(require('./addons/' + file));
});
const guildId = process.env.GUILD_ID;
const giveRoleName = process.env.GIVE_ROLE_NAME;
const removeRoleName = process.env.REMOVE_ROLE_NAME;
addons.forEach((addon) => {
if (addon.init) addon.init(client);
});
client.on('ready', () => {
console.log('Logged in as ' + client.user.tag + '!');
client.user.setActivity('uses of !verify', {
type: 'WATCHING'
});
addons.forEach((addon) => {
if (addon.ready) addon.ready();
});
});
function createEmbeds(name, _id) {
if (!_id) _id = null;
return {
checking: {
embed: {
title: '@' + name + ', Checking website...',
description: '',
url: undefined,
color: 16777011,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
toverify: {
embed: {
title: '@' + name + ", Check DM's for more info.",
description: 'Instructions will be posted there.',
url: undefined,
color: 16777011,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
sendfailed: {
embed: {
title: '@' + name + ', Error, cannot send DM.',
description: "Please allow DM's from users in your security settings!",
url: undefined,
color: 16724736,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
isverifying: {
embed: {
title: '@' + name + ' started the verification process.',
url: undefined,
color: 16777011,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
toverifydm: {
embed: {
title: '@' + name + ', Please read the instructions below.',
description:
'To verify that you have a CodeWizardsHQ Account, \n\n1) go to one of your webpages on your **CodeWizards** editor and paste ```<id verificationnum="cw' +
_id +
'"/>``` somewhere on the page \n\n2) Afterwards, paste in the website link below in a DM replying to me.',
url: undefined,
color: 16777011,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
verified: {
embed: {
title: '@' + name + ', you are now Verified.',
description: 'You now have your roles! Now get going and go do coding. \n\n\nShoo!\n',
url: undefined,
color: 63744,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
isverified: {
embed: {
title: '@' + name + ' was verified.',
url: undefined,
color: 63744,
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
},
declined: {
embed: {
title: '@' + name + ', you are still NOT verified!',
description:
'An issue has occured. Please revisit the instructions and try again. You may have mistyped or something like that. Also, the page **HAS TO BE ON CODEWIZARDS**.',
url: undefined,
color: '16724736',
author: {
name: 'Code Wizard',
icon_url:
'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png',
url: undefined
},
timestamp: undefined,
fields: [],
thumbnail: {
url: 'https://cdn.discordapp.com/avatars/623314619154300988/539bf23444c28b765923eb5b40e9f859.png'
},
image: undefined,
footer: {
text: 'Code Wizard Verification Bot made by Razboy20',
icon_url: undefined
},
file: undefined
}
}
};
}
client.on('message', (msg) => {
if (msg.author.id === client.user.id) return;
try {
addons.forEach((addon) => {
if (addon.message) addon.message(client, msg);
});
} catch (err) {
console.log(err);
}
if (msg.channel.type === 'text') {
if (msg.channel.name !== process.env.VERIFY_CHANNEL_NAME) {
return;
}
if (msg.content === '!verify') {
msg.channel.send(createEmbeds(msg.member.displayName).toverify).then(function(message) {
// send dm
msg.author.send(createEmbeds(msg.author.username, msg.author.id).toverifydm).catch(() => {
// if fails, edit message to be 'sendFailed'
message.edit(createEmbeds(msg.member.displayName).sendfailed);
});
message.delete({ timeout: 10000 });
});
msg.guild.channels.cache
.find((val) => val.name === 'verify-log')
.send(createEmbeds(msg.member.displayName).isverifying);
}
if (msg.content === '!clear50') {
msg.channel.bulkDelete(50).catch(console.error);
}
// if (msg.author.id == '250809865767878657') {
// return;
// }
if (msg.author.id !== client.user.id) msg.delete({ timeout: 3000 });
}
//checking if author is a bot
if (msg.author.bot === true) {
return;
}
if (msg.channel.type === 'dm') {
msg.channel.send(createEmbeds(msg.author.username).checking).then(function(message) {
if (!msg.content.includes('codewizardshq.com')) {
message.edit(createEmbeds(msg.author.username).declined);
} else {
axios
.get(msg.content)
.catch(function(error) {
message.edit(createEmbeds(msg.author.username).declined);
console.error('Website does not exist.', error);
})
.then(function(response) {
console.log(response);
if (response.data.includes('cw' + msg.author.id)) {
let role = client.guilds.cache
.get(guildId)
.roles.cache.find((role) => role.name === giveRoleName);
client.guilds.cache
.get(guildId)
.members.cache.get(msg.author.id)
.roles.add(role)
.catch(console.error);
let role2 = client.guilds.cache
.get(guildId)
.roles.cache.find((role) => role.name === removeRoleName);
client.guilds.cache
.get(guildId)
.members.cache.get(msg.author.id)
.roles.remove(role2)
.catch(console.error);
message.edit(createEmbeds(msg.author.username).verified);
client.guilds.cache
.get(guildId)
.channels.cache.find((val) => val.name === 'verify-log')
.send(createEmbeds(msg.author.username).isverified);
} else {
message.edit(createEmbeds(msg.author.username).declined);
}
});
}
});
// msg.delete();
}
});
client.on('guildMemberAdd', (member) => {
member.send(
'Welcome to the **Official CodeWizardsHQ** Discord! To get started, to go the #hall-of-upgrades channel and type in `!verify`.'
);
});
client.on('messageUpdate', (oldmsg, newmsg) => {
if (oldmsg.author.id === client.user.id) return;
try {
addons.forEach((addon) => {
if (addon.messageEdit) addon.messageEdit(client, oldmsg, newmsg);
});
} catch (err) {
console.log(err);
}
});
client.login(process.env.SECRET_TOKEN).catch(() => {
console.error(
'\nERROR: Incorrect login details were provided. Please change the client login token to a valid token.\n'
);
process.exit();
});