-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
189 lines (173 loc) · 4.76 KB
/
server.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
// all copyrights gose to def --> http://discord.com/users/933856726770413578
const express = require("express");
const app = express();
const db = require("quick.db");
const path = require("path");
const passport = require("passport");
const bodyparser = require("body-parser");
const Strategy = require("passport-discord").Strategy;
const url = require("url");
const session = require("express-session");
const memorystore = require("memorystore")(session);
const settings = require("./config").dashboard;
const discord = require("discord.js");
const client = new discord.Client({
intents: new discord.Intents(32767),
});
client.login(settings.token);
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((obj, done) => done(null, obj));
passport.use(
new Strategy(
{
clientID: settings.id,
clientSecret: settings.secret,
callbackURL: settings.callback,
scope: ["identify", "guilds", "guilds.join"],
},
(accessToken, refreshToken, profile, done) => {
process.nextTick(() => done(null, profile));
}
)
);
app.use(
session({
store: new memorystore({ checkPeriod: 86400000 }),
secret: `!@#$d8932%^ni#(!@bn9312)`,
resave: false,
saveUninitialized: false,
})
);
app.set("views", __dirname + "/views");
app.set("view engine", "pug");
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyparser.json());
app.use(
bodyparser.urlencoded({
extended: true,
})
);
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
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", { prompt: "none" })
);
app.get("/logout", (req, res) => {
req.session.destroy(() => {
req.logout();
res.redirect("/");
});
});
app.get(
"/callback",
passport.authenticate("discord", { fallureRedirect: "/" }),
async (req, res) => {
res.redirect("/thanks");
}
);
app.get("/thanks", (req, res) => {
res.render("thanks", {
title: "Thanks",
client: client,
req: req,
user: req.isAuthenticated() ? req.user : null,
});
})
app.get("/", (req, res) => {
res.render("index", {
title: "Home",
client: client,
req: req,
user: req.isAuthenticated() ? req.user : null,
});
});
app.get("/dashboard", (req, res) => {
res.render("servers", {
title: "Servers",
client: client,
req: req,
user: req.isAuthenticated() ? req.user : null,
permissions: discord.Permissions,
domain: settings.domain,
});
});
app.get("/dashboard/:guildId", async (req, res) => {
let guildId = req.params.guildId;
if (!guildId) return res.redirect("/");
let guild = client.guilds.cache.find((g) => g.id == guildId);
if (!guild) return res.redirect("/");
let userGG = req.user;
if (!userGG) return res.redirect("/");
let user = guild.members.cache.find((u) => u.id == req.user.id);
if (!user) {
user = await guild.members.fetch(req.user.id).then((u) => {
if (!u) return res.redirect("/");
});
}
if (!user.permissions.has(discord.Permissions.FLAGS.ADMINISTRATOR))
return res.redirect("/");
res.render("server", {
title: guild.name,
client: client,
req: req,
user: req.isAuthenticated() ? req.user : null,
guild: guild,
});
});
app.post("/dashboard/:guildId", async (req, res) => {
let guildId = req.params.guildId;
if (!guildId) return res.redirect("/");
let guild = client.guilds.cache.find((g) => g.id == guildId);
if (!guild) return res.redirect("/");
let userGG = req.user;
if (!userGG) return res.redirect("/");
let user = guild.members.cache.find((u) => u.id == req.user.id);
if (!user) {
user = await guild.members.fetch(req.user.id).then((u) => {
if (!u) return res.redirect("/");
});
}
if (!user.permissions.has(discord.Permissions.FLAGS.ADMINISTRATOR))
return res.redirect("/");
let body = req.body;
if (body?.language) {
let language = body.language;
db.set(`Lang_${guildId}`, language);
} else if (body?.prefix) {
let prefix = body.prefix;
db.set(`Prefix_${guildId}`, prefix);
}
res.render("server", {
title: guild.name,
client: client,
req: req,
user: req.isAuthenticated() ? req.user : null,
guild: guild,
});
});
app.use(express.static(`${__dirname}/assets`));
app.locals.basedir = `${__dirname}/assets`;
app.listen(process.env.PORT || settings.port, () =>
console.log("dashboard in load..")
);
let discordF = require("./discord");
discordF(discord, client, db)