This repository has been archived by the owner on May 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (77 loc) · 2.08 KB
/
index.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
const express = require('express');
const cookieparser = require("cookie-parser")
const app = express();
const axios = require('axios');
const port = 8080;
// set the view engine to ejs
app.set('view engine', 'ejs');
// Styles
app.use("/assets", express.static(__dirname + "/assets"));
app.use(cookieparser())
// index page
app.get('/', function (req, res) {
res.render('index');
});
// about page
app.get('/about', function (req, res) {
res.render('about');
});
// Facts from api
app.get('/facts/cats', async function (req, res) {
const fact = (await axios("https://catfact.ninja/fact")).data.fact
res.render('facts/cats', {
fact: fact
})
})
// SLash commands manager
app.get("/manage",function (req,res) {
if (req.cookies.token) {
return res.redirect("/managecommands")
} else {
res.render('give_token')
}
})
app.get("/setcookie",function(req,res){
res.cookie("token",req.query.token).send("OK")
})
app.get("/deletecookie",function(req,res){
res.clearCookie("token")
res.send("OK")
})
// Manage slash commands
app.get('/managecommands', async function (req, res) {
const token = req.cookies?req.cookies.token:""
if (!token) return res.redirect("/manage")
//get the bot id by using the token
let id;
try {
id = (await axios({
url: "https://discord.com/api/v9/oauth2/applications/@me",
headers: {
"Authorization": `Bot ${token}`,
"Content-Type": "application/json"
}
})).data.id
} catch (e) {
console.log(e)
return res.render('errors/404')
}
//get the bot's slash commands
const slash_commands = (await axios({
url: `https://discordapp.com/api/v9/applications/${id}/commands`,
headers: {
"Authorization": `Bot ${token}`,
"Content-Type": "application/json"
}
})).data
return res.render('manage', {
id: id,
token: token,
commands: slash_commands
})
})
app.get('*', function (req, res) {
res.status(404).render('errors/404')
});
app.listen(port);
console.log(`Server is running on port ${port}\nhttp://localhost:${port}`);