-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (61 loc) · 1.97 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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const path = require("path");
const PORT = 3000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// Define routes
app.get("/", (req, res) => {
res.render("index", { messages: [] });
});
app.post("/execute", async (req, res) => {
const startAt = Date.now();
const messages = [];
try {
const token = req.body.token;
const guilds = await getGuilds(token);
for (const guild of guilds) {
await makeRequest(
"PATCH",
`https://discordapp.com/api/v8/users/@me/guilds/${guild.id}/settings`,
{
message_notifications: 2,
},
token,
);
messages.push({
type: "success",
content: `Notifications disabled for '${guild.name}' (ID: ${guild.id}).`,
});
}
const successMessage = `Done. Notifications updated for ${guilds.length} servers in ${(Date.now() - startAt) / 1000}s.`;
res.render("index", { messages: [{ type: "success", content: successMessage }, ...messages] });
} catch (error) {
console.error(`Error: ${error.message}`);
res.render("index", { messages: [{ type: "error", content: error.message }] });
}
});
const makeRequest = async (method, url, body, token) => {
const fetchModule = await import("node-fetch");
const fetch = fetchModule.default;
const res = await fetch(url, {
headers: {
authorization: token,
"content-type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
method,
});
const json = await res.json();
return json;
};
const getGuilds = async (token) => {
const res = await makeRequest("GET", "https://discordapp.com/api/v8/users/@me/guilds", null, token);
return res;
};
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});