forked from rockartcuuvvu/op-botlist-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
812 lines (769 loc) · 41.5 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
const url = require("url");
const path = require("path");
const express = require("express");
const passport = require("passport");
const session = require("express-session");
const Strategy = require("passport-discord").Strategy;
const config = require("./settings.json");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const Discord = require("discord.js");
const settingsc = require("./settings.json");
const roles = require("./roles.json");
const channels = require("./channels.json");
const uptimeSchema = require("./models/uptime.js");
const banSchema = require("./models/site-ban.js");
const maintenceSchema = require('./models/maintenance.js');
const app = express();
const MemoryStore = require("memorystore")(session);
const botsdata = require("./models/botlist/bots.js");
const fetch = require("node-fetch");
const cookieParser = require('cookie-parser');
const referrerPolicy = require('referrer-policy')
app.use(referrerPolicy({ policy: "strict-origin" }))
module.exports = async (client) => {
app.get("/robots.txt", function(req,res) {
res.set('Content-Type', 'text/plain');
res.send(`Sitemap: https://op.xyz/sitemap.xml`);
});
app.get("/sitemap.xml", async function(req,res) {
let link = "<url><loc>https://op.xyz/</loc></url>";
let botdataforxml = await botsdata.find()
botdataforxml.forEach(bot => {
link += "\n<url><loc>https://op.xyz/bot/"+bot.botID+"</loc></url>";
})
res.set('Content-Type', 'text/xml');
res.send(`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="https://www.google.com/schemas/sitemap-image/1.1">${link}</urlset>`);
});
const templateDir = path.resolve(`${process.cwd()}${path.sep}www`);
app.use("/css", express.static(path.resolve(`${templateDir}${path.sep}assets/css`)));
app.use("/js", express.static(path.resolve(`${templateDir}${path.sep}assets/js`)));
app.use("/img", express.static(path.resolve(`${templateDir}${path.sep}assets/img`)));
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((obj, done) => done(null, obj));
passport.use(new Strategy({
clientID: config.clientid,
clientSecret: config.clientsecret,
callbackURL: config.clientcallback,
scope: ["identify", "guilds"]
},
(accessToken, refreshToken, profile, done) => {
process.nextTick(() => done(null, profile));
}));
app.use(session({
store: new MemoryStore({ checkPeriod: 86400000 }),
secret: "#@%#&^$^$%@$^$&%#$%@#$%$^%&$%^#$%@#$%#E%#%@$FEErfgr3g#%GT%536c53cc6%5%tv%4y4hrgrggrgrgf4n",
resave: false,
saveUninitialized: false,
}));
app.use(passport.initialize());
app.use(passport.session());
app.engine("html", ejs.renderFile);
app.set("view engine", "html");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const renderTemplate = (res, req, template, data = {}) => {
const baseData = {
bot: client,
path: req.path,
user: req.isAuthenticated() ? req.user : null
};
res.render(path.resolve(`${templateDir}${path.sep}${template}`), Object.assign(baseData, data));
};
const checkAuth = (req, res, next) => {
if (req.isAuthenticated()) return next();
req.session.backURL = req.url;
res.redirect("/login");
}
const checkMaintence = async (req, res, next) => {
const d = await maintenceSchema.findOne({server: settingsc.serverID });
if(d) {
if(req.isAuthenticated()) {
let uyevarmikiaq = client.guilds.cache.get(settingsc.serverID).members.cache.get(req.user.id);
if(uyevarmikiaq) {
if(uyevarmikiaq.roles.cache.get(roles.yonetici)) {
next();
} else {
res.redirect('/error?code=200&message=Our website is temporarily unavailable.')
}
} else {
res.redirect('/error?code=200&message=Our website is temporarily unavailable.')
}
} else {
res.redirect('/error?code=200&message=Our website is temporarily unavailable.')
}
} else {
next();
}
}
app.get("/login", (req, res, next) => {
if (req.session.backURL) {
req.session.backURL = req.session.backURL;
} else if (req.headers.referer) {
const parsed = url.parse(req.headers.referer);
if (parsed.hostname === app.locals.domain) {
req.session.backURL = parsed.path;
}
} else {
req.session.backURL = "/";
}
next();
},
passport.authenticate("discord"));
app.get("/callback", passport.authenticate("discord", { failureRedirect: "/error?code=999&message=We encountered an error while connecting." }), async (req, res) => {
let banned = await banSchema.findOne({user: req.user.id})
if(banned) {
client.users.fetch(req.user.id).then(async a => {
client.channels.cache.get(channels.login).send(new Discord.MessageEmbed().setAuthor(a.username, a.avatarURL({dynamic: true})).setThumbnail(a.avatarURL({dynamic: true})).setColor("RED").setDescription(`[**${a.username}**#${a.discriminator}](https://op.xyz/user/${a.id}) user ** tried to login to the site ** but could not login because it was blocked from the site.`).addField("Username", a.username).addField("User ID", a.id).addField("User Discriminator", a.discriminator))
})
req.session.destroy(() => {
res.json({ login: false, message: "You have been blocked from OP.", reason: `${banned.sebep}` })
req.logout();
});
} else {
res.redirect(req.session.backURL || '/')
client.users.fetch(req.user.id).then(async a => {
client.channels.cache.get(channels.login).send(new Discord.MessageEmbed().setAuthor(a.username, a.avatarURL({dynamic: true})).setThumbnail(a.avatarURL({dynamic: true})).setColor("GREEN").setDescription(`[**${a.username}**#${a.discriminator}](https://op.xyz/user/${a.id}) user ** to the site** logged in.`).addField("Username", a.username).addField("User ID", a.id).addField("User Discriminator", a.discriminator))
})
}
});
app.get("/logout", function (req, res) {
req.session.destroy(() => {
req.logout();
res.redirect("/");
});
});
const http = require('http').createServer(app);
const io = require('socket.io')(http);
io.on('connection', socket => {
io.emit("userCount", io.engine.clientsCount);
});
http.listen(3000);
//------------------- EXTRA -------------------//
app.get("/", checkMaintence, async (req, res) => {
const botdata = await botsdata.find();
renderTemplate(res, req, "index.ejs", {config, roles, botdata, getuser});
});
app.get("/dc", (req, res) => {
res.redirect('https://discord.gg/gn24nq4RGp');
});
app.get("/discord", (req, res) => {
res.redirect('https://discord.gg/gn24nq4RGp');
});
app.get("/error", (req, res) => {
renderTemplate(res, req, "pages/error.ejs", {req, config, res, roles, channels});
});
app.get("/team", checkMaintence, (req, res) => {
renderTemplate(res, req, "team.ejs", {req, roles, config});
});
app.get("/bot-rules", checkMaintence, (req, res) => {
renderTemplate(res, req, "/botlist/bot-rules.ejs", {config,roles});
});
//------------------- UPTİME -------------------//
const uptimedata = require("./models/uptime.js");
app.get("/uptime/add", checkMaintence, checkAuth, async (req,res) => {
renderTemplate(res, req, "uptime/add.ejs", {req, roles, config});
})
app.post("/uptime/add", checkMaintence, checkAuth, async (req,res) => {
const rBody = req.body;
if(!rBody['link']) {
res.redirect('?error=true&message=Write a any link.')
} else {
if(!rBody['link'].match('https')) return res.redirect('?error=true&message=You must enter a valid link.')
const updcode = makeidd(5);
const dde = await uptimedata.findOne({link: rBody['link']});
const dd = await uptimedata.find({userID: req.user.id});
if(dd.length > 9) res.redirect('?error=true&message=Your uptime limit has reached.')
if(dde) return res.redirect('?error=true&message=This link already exists in the system.')
client.users.fetch(req.user.id).then(a => {
client.channels.cache.get(channels.uptimelog).send(new Discord.MessageEmbed()
.setAuthor(a.username, a.avatarURL({dynamic: true}))
.setDescription("New link added uptime system.")
.setThumbnail(client.user.avatarURL)
.setColor("GREEN")
.addField("User;", `${a.tag} \`(${a.id})\``, true)
.addField("Uptime Code;", updcode, true)
.addField("Uptime Limit;", `${dd.length+1}/10`, true)
)
new uptimedata({server: config.serverID, userName: a.username, userID: req.user.id, link: rBody['link'], code: updcode}).save();
})
res.redirect('?success=true&message=Your link has been successfully added to the uptime system.');
}
})
app.get("/uptime/links", checkMaintence, checkAuth, async (req,res) => {
let uptimes = await uptimedata.find({ userID: req.user.id })
renderTemplate(res, req, "uptime/mylinks.ejs", {req, roles, config, uptimes});
})
app.get("/uptime/:code/delete", checkMaintence, checkAuth, async (req,res) => {
const dde = await uptimedata.findOne({code: req.params.code});
if(!dde) return res.redirect('/uptime/links?error=true&message=There is no such site in the system.')
uptimedata.findOne({ 'code': req.params.code }, async function (err, docs) {
if(docs.userID != req.user.id) return res.redirect('/uptime/links?error=true&message=The link you tried to delete does not belong to you.');
res.redirect('/uptime/links?success=true&message=The link has been successfully deleted from the system.');
await uptimedata.deleteOne({ code: req.params.code });
})
})
//------------------- UPTİME -------------------//
//------------------- BOT LİST -------------------//
app.get("/bots", checkMaintence, async (req,res) => {
let page = req.query.page || 1;
let data = await botsdata.find() || await botsdata.find().filter(b => b.status === "Approved")
if(page < 1) return res.redirect(`/bots`);
if(data.length <= 0) return res.redirect("/");
if((page > Math.ceil(data.length / 8)))return res.redirect(`/bots`);
if (Math.ceil(data.length / 8) < 1) {
page = 1;
};
renderTemplate(res, req, "botlist/bots.ejs", {
req,
roles,
config,
data,
page: page
});
})
app.get("/search", checkMaintence, async (req,res) => {
let page = req.query.page || 1;
let x = await botsdata.find()
let data = x.filter(a => a.status == "Approved" && a.username.includes(req.query.q) || a.shortDesc.includes(req.query.q))
if(page < 1) return res.redirect(`/bots`);
if(data.length <= 0) return res.redirect("/");
if((page > Math.ceil(data.length / 8)))return res.redirect(`/bots`);
if (Math.ceil(data.length / 8) < 1) {
page = 1;
};
renderTemplate(res, req, "botlist/search.ejs", {
req,
roles,
config,
data,
page: page
});
})
app.get("/tags", checkMaintence, async (req,res) => {
renderTemplate(res, req, "botlist/tags.ejs", {
req,
roles,
config
});
})
app.get("/tag/:tag", checkMaintence, async (req,res) => {
let page = req.query.page || 1;
let x = await botsdata.find()
let data = x.filter(a => a.status == "Approved" && a.tags.includes(req.params.tag))
if(page < 1) return res.redirect(`/tag/`+req.params.tag);
if(data.length <= 0) return res.redirect("/");
if((page > Math.ceil(data.length / 8)))return res.redirect(`/tag/`+req.params.tag);
if (Math.ceil(data.length / 8) < 1) {
page = 1;
};
renderTemplate(res, req, "botlist/tag.ejs", {
req,
roles,
config,
data,
page: page
});
})
app.get("/addbot", checkMaintence, checkAuth, async (req,res) => {
if(!client.guilds.cache.get(settingsc.serverID).members.cache.get(req.user.id)) return res.redirect("/error?code=403&message=To do this, you have to join our discord server.");
renderTemplate(res, req, "botlist/addbot.ejs", {req, roles, config});
})
app.get("/bot/@:privateURL/vote", checkMaintence, async (req,res) => {
let botdata = await botsdata.findOne({ privateURL: req.params.privateURL });
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
if(req.user) {
if(!req.user.id === botdata.ownerID || req.user.id.includes(botdata.coowners)) {
if(botdata.status != "Approved") return res.redirect("/error?code=404&message=You entered an invalid bot id.");
}
}
renderTemplate(res, req, "botlist/vote.ejs", {req, roles, config, botdata});
})
app.post("/bot/@:privateURL/vote", checkMaintence, checkAuth, async (req,res) => {
const votes = require("./models/botlist/vote.js");
let botdata = await botsdata.findOne({ privateURL: req.params.privateURL });
let x = await votes.findOne({user: req.user.id,bot: botdata.botID })
if(x) return res.redirect("/error?code=400&message=You can vote every 12 hours.");
await votes.findOneAndUpdate({bot: botdata.botID, user: req.user.id }, {$set: {Date: Date.now(), ms: 43200000 }}, {upsert: true})
await botsdata.findOneAndUpdate({botID: botdata.botID}, {$inc: {votes: 1}})
client.channels.cache.get(channels.votes).send(`**${req.user.username}** voted **${botdata.username}** **\`(${botdata.votes + 1} votes)\`**`)
return res.redirect(`/bot/@${req.params.privateURL}/vote?success=true&message=You voted successfully. You can vote again after 12 hours.`);
renderTemplate(res, req, "botlist/vote.ejs", {req, roles, config, botdata});
})
app.get("/bot/:botID/vote", checkMaintence, async (req,res) => {
let botdata = await botsdata.findOne({ botID: req.params.botID });
if(botdata.privateURL) return res.redirect('/bot/@'+botdata.privateURL+'/vote');
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
if(req.user) {
if(!req.user.id === botdata.ownerID || req.user.id.includes(botdata.coowners)) {
if(botdata.status != "Approved") return res.redirect("/error?code=404&message=You entered an invalid bot id.");
}
}
renderTemplate(res, req, "botlist/vote.ejs", {req, roles, config, botdata});
})
app.post("/bot/:botID/vote", checkMaintence, checkAuth, async (req,res) => {
const votes = require("./models/botlist/vote.js");
let botdata = await botsdata.findOne({ botID: req.params.botID });
let x = await votes.findOne({user: req.user.id,bot: req.params.botID})
if(x) return res.redirect("/error?code=400&message=You can vote every 12 hours.");
await votes.findOneAndUpdate({bot: req.params.botID, user: req.user.id }, {$set: {Date: Date.now(), ms: 43200000 }}, {upsert: true})
await botsdata.findOneAndUpdate({botID: req.params.botID}, {$inc: {votes: 1}})
client.channels.cache.get(channels.votes).send(`**${req.user.username}** voted **${botdata.username}** **\`(${botdata.votes + 1} votes)\`**`)
return res.redirect(`/bot/${req.params.botID}/vote?success=true&message=You voted successfully. You can vote again after 12 hours.`);
renderTemplate(res, req, "botlist/vote.ejs", {req, roles, config, botdata});
})
app.post("/addbot", checkMaintence, checkAuth, async (req,res) => {
let rBody = req.body;
let botvarmi = await botsdata.findOne({botID: rBody['botID']});
if(botvarmi) return res.redirect('/error?code=404&message=The bot you are trying to add exists in the system.');
client.users.fetch(req.body.botID).then(async a => {
if(!a.bot) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
if(!a) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
if(rBody['coowners']) {
if(String(rBody['coowners']).split(',').length > 3) return res.redirect("?error=true&message=You can add up to 3 CO-Owners..")
if(String(rBody['coowners']).split(',').includes(req.user.id)) return res.redirect("?error=true&message=You cannot add yourself to other CO-Owners.");
}
await new botsdata({
botID: rBody['botID'],
ownerID: req.user.id,
ownerName: req.user.usename,
username: a.username,
discrim: a.discriminator,
avatar: a.avatarURL(),
prefix: rBody['prefix'],
longDesc: rBody['longDesc'],
shortDesc: rBody['shortDesc'],
status: "UnApproved",
tags: rBody['tags'],
certificate: "None"
}).save()
if(rBody['github']) {
await botsdata.findOneAndUpdate({botID: rBody['botID']},{$set: {github: rBody['github']}}, function (err,docs) {})
}
if(rBody['website']) {
await botsdata.findOneAndUpdate({botID: rBody['botID']},{$set: {website: rBody['website']}}, function (err,docs) {})
}
if(rBody['support']) {
await botsdata.findOneAndUpdate({botID: rBody['botID']},{$set: {support: rBody['support']}}, function (err,docs) {})
}
if(rBody['coowners']) {
if(String(rBody['coowners']).split(',').length > 3) return res.redirect("?error=true&message=You can add up to 3 CO-Owners..")
if(String(rBody['coowners']).split(',').includes(req.user.id)) return res.redirect("?error=true&message=You cannot add yourself to other CO-Owners.");
await botsdata.findOneAndUpdate({botID: rBody['botID']},{$set: { coowners: String(rBody['coowners']).split(',') }}, function (err,docs) {})
}
})
client.users.fetch(rBody['botID']).then(a => {
client.channels.cache.get(channels.botlog).send(`<@${req.user.id}> added **${a.tag}**`)
res.redirect(`?success=true&message=Your bot has been successfully added to the system.&botID=${rBody['botID']}`)
})
})
app.get("/bot/@:privateURL", checkMaintence, async (req,res,next) => {
let botdata = await botsdata.findOne({ privateURL: req.params.privateURL });
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
let coowner = new Array()
botdata.coowners.map(a => {
client.users.fetch(a).then(b => coowner.push(b))
})
client.users.fetch(botdata.ownerID).then(aowner => {
client.users.fetch(botdata.botID).then(abot => {
renderTemplate(res, req, "botlist/bot.ejs", { req, abot, config, botdata, coowner, aowner, roles});
});
});
});
app.get("/bot/:botID", checkMaintence, async (req,res,next) => {
let botdata = await botsdata.findOne({botID: req.params.botID});
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
if(botdata.status != "Approved") {
if(req.user.id == botdata.ownerID || botdata.coowners.includes(req.user.id)) {
let coowner = new Array()
botdata.coowners.map(a => {
client.users.fetch(a).then(b => coowner.push(b))
})
client.users.fetch(botdata.ownerID).then(aowner => {
client.users.fetch(req.params.botID).then(abot => {
renderTemplate(res, req, "botlist/bot.ejs", { req, abot, config, botdata, coowner, aowner, roles});
});
});
} else {
res.redirect("/error?code=404&message=To edit this bot, you must be one of its owners.");
}
} else {
let coowner = new Array()
botdata.coowners.map(a => {
client.users.fetch(a).then(b => coowner.push(b))
})
client.users.fetch(botdata.ownerID).then(aowner => {
client.users.fetch(req.params.botID).then(abot => {
renderTemplate(res, req, "botlist/bot.ejs", { req, abot, config, botdata, coowner, aowner, roles});
});
});
}
});
app.post("/bot/@:privateURL", checkMaintence, async (req,res,next) => {
let botdata = await botsdata.findOne({ privateURL: req.params.privateURL});
client.users.fetch(botdata.botID).then(async bot => {
client.users.fetch(botdata.ownerID).then(async owner => {
if(bot) {
await botsdata.findOneAndUpdate({ botID: botdata.botID },{$set: { ownerName: owner.username, username: bot.username, discrim: bot.discriminator, avatar: bot.avatarURL() }})
} else {
await botsdata.findOneAndDelete({ botID: botdata.botID })
}
})
})
return res.redirect('/bot/@'+req.params.privateURL);
});
app.post("/bot/:botID", async (req,res) => {
let botdata = await botsdata.findOne({botID: req.params.botID});
client.users.fetch(botdata.botID).then(async bot => {
client.users.fetch(botdata.ownerID).then(async owner => {
if(bot) {
await botsdata.findOneAndUpdate({ botID: botdata.botID },{$set: { ownerName: owner.username, username: bot.username, discrim: bot.discriminator, avatar: bot.avatarURL() }})
} else {
await botsdata.findOneAndDelete({ botID: botdata.botID })
}
})
})
return res.redirect('/bot/'+req.params.botID);
})
app.get("/bot/:botID/edit", checkMaintence, checkAuth, async (req,res) => {
let botdata = await botsdata.findOne({botID: req.params.botID});
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.")
if(req.user.id == botdata.ownerID || botdata.coowners.includes(req.user.id)) {
renderTemplate(res, req, "botlist/bot-edit.ejs", { req, config, botdata, roles});
} else {
res.redirect("/error?code=404&message=To edit this bot, you must be one of its owners.");
}
});
app.post("/bot/:botID/edit", checkMaintence, checkAuth, async (req,res) => {
let rBody = req.body;
let botdata = await botsdata.findOne({ botID: req.params.botID })
if(String(rBody['coowners']).split(',').length > 3) return res.redirect("?error=true&message=You can add up to 3 CO-Owners..")
if(String(rBody['coowners']).split(',').includes(req.user.id)) return res.redirect("?error=true&message=You cannot add yourself to other CO-Owners.");
await botsdata.findOneAndUpdate({botID: req.params.botID},{$set: {
botID: req.params.botID,
ownerID: botdata.ownerID,
prefix: rBody['prefix'],
longDesc: rBody['longDesc'],
shortDesc: rBody['shortDesc'],
tags: rBody['tags'],
github: rBody['github'],
website: rBody['website'],
support: rBody['support'],
privateURL: rBody['privateURL'],
coowners: String(rBody['coowners']).split(','),
}
}, function (err,docs) {})
client.users.fetch(req.params.botID).then(a => {
client.channels.cache.get(channels.botlog).send(`<@${req.user.id}> edited **${a.tag}**`)
res.redirect(`?success=true&message=Your bot has been successfully edited.&botID=${req.params.botID}`)
})
})
//------------------- BOT LİST -------------------//
//---------------- ADMIN ---------------\\
const appsdata = require("./models/botlist/certificate-apps.js");
// CERTIFICATE APP
app.get("/certification", checkMaintence, checkAuth, async (req, res) => {
renderTemplate(res, req, "/botlist/apps/certification.ejs", {req, roles, config})
});
app.get("/certification/apply", checkMaintence, checkAuth, async (req, res) => {
const userbots = await botsdata.find({ ownerID: req.user.id })
renderTemplate(res, req, "/botlist/apps/certificate-app.ejs", {req, roles, config, userbots})
});
app.post("/certification/apply", checkMaintence, checkAuth, async (req, res) => {
const rBody = req.body;
new appsdata({botID: rBody['bot'], future: rBody['future']}).save();
res.redirect("/bots?success=true&message=Certificate application applied.&botID="+rBody['bot'])
let botdata = await botsdata.findOne({ botID: rBody['bot'] })
client.channels.cache.get(channels.botlog).send(`User **${botdata.ownerName}** requested a certificate for bot **${botdata.username}**.`)
});
//
const checkAdmin = async (req, res, next) => {
if (req.isAuthenticated()) {
if(client.guilds.cache.get(config.serverID).members.cache.get(req.user.id).roles.cache.get(roles.yonetici) || client.guilds.cache.get(config.serverID).members.cache.get(req.user.id).roles.cache.get(roles.moderator)) {
next();
} else {
res.redirect("/error?code=403&message=You is not competent to do this.")
}
} else {
req.session.backURL = req.url;
res.redirect("/login");
}
}
app.get("/admin", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.find()
const udata = await uptimedata.find()
const alexarank = require("alexa-rank-nodejs");
renderTemplate(res, req, "admin/index.ejs", {req, roles, config, botdata, udata, alexarank})
});
// MINI PAGES
app.get("/admin/unapproved", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.find()
renderTemplate(res, req, "admin/unapproved.ejs", {req, roles, config, botdata})
});
app.get("/admin/approved", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.find()
renderTemplate(res, req, "admin/approved.ejs", {req, roles, config, botdata})
});
app.get("/admin/certificate-apps", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.find()
const apps = await appsdata.find()
renderTemplate(res, req, "admin/certificate-apps.ejs", {req, roles, config, apps,botdata})
});
// SYSTEMS PAGES
// CONFIRM BOT
app.get("/admin/confirm/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.findOne({ botID: req.params.botID })
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
await botsdata.findOneAndUpdate({botID: req.params.botID},{$set: {
status: "Approved",
Date: Date.now(),
}
}, function (err,docs) {})
client.users.fetch(req.params.botID).then(bot => {
client.channels.cache.get(channels.botlog).send(`<@${botdata.ownerID}> bot **${bot.tag}** has been approved. `)
client.users.cache.get(botdata.ownerID).send(`Your bot **${bot.tag}** has been approved.`)
});
let guild = client.guilds.cache.get(settingsc.serverID)
guild.members.cache.get(botdata.botID).roles.add(roles.bot);
guild.members.cache.get(botdata.ownerID).roles.add(roles.developer);
if(botdata.coowners) {
botdata.coowners.map(a => {
guild.members.cache.get(a).roles.add(roles.developer);
})
}
return res.redirect(`/admin/unapproved?success=true&message=Bot approved.`)
});
// DELETE BOT
app.get("/admin/delete/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.findOne({ botID: req.params.botID })
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
let guild = client.guilds.cache.get(settingsc.serverID)
guild.members.cache.get(botdata.botID).roles.remove(roles.bot);
await guild.members.cache.get(botdata.botID).kick();
await botsdata.deleteOne({ botID: req.params.botID, ownerID: botdata.ownerID })
return res.redirect(`/admin/approved?success=true&message=Bot deleted.`)
});
// DECLINE BOT
app.get("/admin/decline/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.findOne({ botID: req.params.botID })
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
renderTemplate(res, req, "admin/decline.ejs", {req, roles, config, botdata})
});
app.post("/admin/decline/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
let rBody = req.body;
let botdata = await botsdata.findOne({ botID: req.params.botID });
client.users.fetch(botdata.ownerID).then(sahip => {
client.channels.cache.get(channels.botlog).send(`<@${botdata.ownerID}> bot **${botdata.username}** has been declined. `)
client.users.cache.get(botdata.ownerID).send(`Your bot **${botdata.username}** has been declined.\nReason: **${rBody['reason']}**\nAuthorized: **${req.user.username}**`)
})
await botsdata.deleteOne({ botID: req.params.botID, ownerID: botdata.ownerID })
return res.redirect(`/admin/unapproved?success=true&message=Bot declined.`)
});
// CERTIFICATE
app.get("/admin/certified-bots", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.find();
renderTemplate(res, req, "admin/certified-bots.ejs", {req, roles, config, botdata})
});
app.get("/admin/certificate/give/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
await botsdata.findOneAndUpdate({botID: req.params.botID},{$set: {
certificate: "Certified",
}
}, function (err,docs) {})
let botdata = await botsdata.findOne({ botID: req.params.botID });
client.users.fetch(botdata.botID).then(bot => {
client.channels.cache.get(channels.botlog).send(`<@${botdata.ownerID}> bot **${bot.tag}** has been granted a certificate.`)
client.users.cache.get(botdata.ownerID).send(`Your bot **${bot.tag}** has been certified.`)
});
await appsdata.deleteOne({ botID: req.params.botID })
let guild = client.guilds.cache.get(settingsc.serverID)
guild.members.cache.get(botdata.botID).roles.add(roles.certified_bot);
guild.members.cache.get(botdata.ownerID).roles.add(roles.certified_developer);
if(botdata.coowners) {
botdata.coowners.map(a => {
if(guild.members.cache.get(a)) {
guild.members.cache.get(a).roles.add(roles.certified_developer);
}
})
}
return res.redirect(`/admin/certificate-apps?success=true&message=Certificate gived.&botID=${req.params.botID}`)
});
app.get("/admin/certificate/delete/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const botdata = await botsdata.findOne({ botID: req.params.botID })
if(!botdata) return res.redirect("/error?code=404&message=You entered an invalid bot id.");
renderTemplate(res, req, "admin/certificate-delete.ejs", {req, roles, config, botdata})
});
app.post("/admin/certificate/delete/:botID", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
let rBody = req.body;
await botsdata.findOneAndUpdate({botID: req.params.botID},{$set: {
certificate: "None",
}
}, function (err,docs) {})
let botdata = await botsdata.findOne({ botID: req.params.botID });
client.users.fetch(botdata.botID).then(bot => {
client.channels.cache.get(channels.botlog).send(`<@${botdata.ownerID}> bot **${bot.tag}** has not been granted a certificate.`)
client.users.cache.get(botdata.ownerID).send(`Your bot **${bot.tag}** certificate application has been declined.\nReason: **${rBody['reason']}**`)
});
await appsdata.deleteOne({ botID: req.params.botID })
let guild = client.guilds.cache.get(settingsc.serverID)
guild.members.cache.get(botdata.botID).roles.remove(roles.certified_bot);
guild.members.cache.get(botdata.ownerID).roles.remove(roles.certified_developer);
return res.redirect(`/admin/certificate-apps?success=true&message=Certificate deleted.`)
});
// UPTIME
// UPTIMES
app.get("/admin/uptimes", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
let updata = await uptimeSchema.find();
renderTemplate(res, req, "admin/uptimes.ejs", {req, roles, config, updata})
});
app.get("/admin/deleteuptime/:code", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
await uptimeSchema.deleteOne({ code: req.params.code })
return res.redirect('../admin/uptimes?error=true&message=Link deleted.');
});
app.get("/admin/maintence", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
let bandata = await banSchema.find();
renderTemplate(res, req, "/admin/administrator/maintence.ejs", { req, roles, config, bandata })
});
app.post("/admin/maintence", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
let bakimdata = await maintenceSchema.findOne({ server: settingsc.serverID });
if(bakimdata) return res.redirect('../admin/maintence?error=true&message=Maintenance mode has already been activated for this site.');
client.channels.cache.get(channels.webstatus).send(`<a:maintence:833375738547535913> OP has been switched to __maintance__ due to **${req.body.reason}**`).then(a => {
new maintenceSchema({server: settingsc.serverID, reason: req.body.reason, bakimmsg: a.id}).save();
})
return res.redirect('../admin/maintence?success=true&message=Maintence opened.');
});
app.post("/admin/unmaintence", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
const dc = require("discord.js");
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
let bakimdata = await maintenceSchema.findOne({ server: settingsc.serverID });
if(!bakimdata) return res.redirect('../admin/maintence?error=true&message=The website is not in maintenance mode anyway.');
const bakimsonaerdikardesvcodes = new dc.MessageEmbed()
.setAuthor("OP.xyz", client.user.avatarURL())
.setThumbnail(client.user.avatarURL())
.setColor("GREEN")
.setDescription(`<a:online:833375738785824788> OP are **active** again!\n[Click to redirect website](https://OP.xyz)`)
.setFooter("OP © All rights reserved.");
await client.channels.cache.get(channels.webstatus).messages.fetch(bakimdata.bakimmsg).then(a => { a.edit(`~~ <a:maintence:833375738547535913> OP has been switched to __maintance__ due to **${bakimdata.reason}** ~~`, bakimsonaerdikardesvcodes) } )
client.channels.cache.get(channels.webstatus).send(".").then(b => { b.delete({ timeout: 500 })})
await maintenceSchema.deleteOne({server: settingsc.serverID}, function (error, server) {
if(error) console.log(error)
});
return res.redirect('../admin/maintence?success=true&message=Maintenance mode has been shut down successfully.');
});
app.get("/admin/userban", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
let bandata = await banSchema.find();
renderTemplate(res, req, "/admin/administrator/user-ban.ejs", { req, roles, config, bandata })
});
app.post("/admin/userban", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
new banSchema({ user: req.body.userID, sebep: req.body.reason, yetkili: req.user.id }).save()
return res.redirect('../admin/userban?success=true&message=User banned.');
});
app.post("/admin/userunban", checkMaintence, checkAdmin, checkAuth, async (req, res) => {
if(!settingsc.owner.includes(req.user.id)) return res.redirect('../admin');
banSchema.deleteOne({ user: req.body.userID }, function (error, user) {
if(error) console.log(error)
})
return res.redirect('../admin/userban?success=true&message=User ban removed.');
});
//---------------- ADMIN ---------------\\
//------------------- PROFILE -------------------//
const profiledata = require("./models/profile.js");
app.get("/user/:userID", checkMaintence, async (req, res) => {
client.users.fetch(req.params.userID).then(async a => {
const pdata = await profiledata.findOne({userID: a.id});
const botdata = await botsdata.find()
const member = a;
const uptimecount = await uptimedata.find({userID: a.id});
renderTemplate(res, req, "profile/profile.ejs", {member, req, roles, config, uptimecount, pdata, botdata});
});
});
app.get("/user/:userID/edit", checkMaintence, checkAuth, async (req, res) => {
client.users.fetch(req.user.id).then(async member => {
const pdata = await profiledata.findOne({userID: member.id});
renderTemplate(res, req, "profile/profile-edit.ejs", {member, req, roles, config, pdata, member});
});
});
app.post("/user/:userID/edit", checkMaintence, checkAuth, async (req, res) => {
let rBody = req.body;
await profiledata.findOneAndUpdate({userID: req.user.id}, {$set: {biography: rBody['biography']}}, {upsert:true})
await profiledata.findOneAndUpdate({userID: req.user.id}, {$set: {website: rBody['website']}}, {upsert:true})
await profiledata.findOneAndUpdate({userID: req.user.id}, {$set: {github: rBody['github']}}, {upsert:true})
await profiledata.findOneAndUpdate({userID: req.user.id}, {$set: {twitter: rBody['twitter']}}, {upsert:true})
await profiledata.findOneAndUpdate({userID: req.user.id}, {$set: {instagram: rBody['instagram']}}, {upsert:true})
return res.redirect('?success=true&message=Your profile has been successfully edited.');
});
//------------------- PROFILE -------------------//
app.set('json spaces', 2)
//------------------- API -------------------//
app.get("/api/:botID", async (req, res) => {
const botdata = await botsdata.findOne({ botID: req.params.botID })
if(!botdata) res.json({ error: true, message: "This bot id invalid.", errorcode: 404})
res.header("Content-Type",'application/json');
res.json(botdata);
});
app.get("/api/:botID/voted/:userID", async (req, res) => {
const votedatas = require("./models/botlist/vote.js");
const bots = await botsdata.findOne({ botID: req.params.botID })
const botdata = await votedatas.findOne({ bot: req.params.botID, user: req.params.userID })
if(!bots) return res.json({ error: true, message: "This bot id invalid.", errorcode: 404});
res.header("Content-Type",'application/json');
if(botdata) {
res.json({user: req.params.userID, hasvote: true});
} else {
res.json({user: req.params.userID, hasvote: false});
}
});
app.get("/api/search/:search", async (req, res) => {
const votedatas = require("./models/botlist/vote.js");
const botdata = await botsdata.find()
res.header("Content-Type",'application/json');
botdata.map(a => {
if(a.username.toLowerCase().includes(req.params.search.toLocaleLowerCase()) || a.shortDesc.toLowerCase().includes(req.params.search.toLowerCase())) {
res.json({ botID: a.botID, votes: a.votes, owner: a.ownerName, ownerID: a.ownerID, coowners: a.coowners})
} else {
res.json({ error: true, message: "Search result not founded.", errorcode: 404})
}
})
app.post("/api/search", async (req,res) =>{
const botdata = await botsdata.find()
let key = req.body.key;
if (key.length <=0) return res.json({status: true, data: []});
let data = botdata.filter(d => d.status == "Approved" && d.username.toLowerCase().includes(key.toLowerCase())).sort(function (a,b) { return b - a.votes; });
res.json({status: true, data: data});
})
});
//------------------- API -------------------//
app.use((req, res) => {
res.status(404).redirect("/")
});
};
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function makeidd(length) {
var result = '';
var characters = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function getuser(id) {
try {
return client.users.fetch(id)
} catch (error) {
return undefined
}
}