This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
197 lines (159 loc) · 7.17 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
190
191
192
193
194
195
196
197
// Import required modules
const Discord = require('discord.js');
const Moment = require('moment');
const Express = require('express');
const fs = require('fs');
// Import custom modules
const index = require('./index');
const Tickets = require('./Schemas/Tickets');
const Infractions = require('./Schemas/Infractions');
// Create an Express router object
const router = Express.Router();
router.use((req, res, next) => {
// Middleware function to validate authorization header
if (!req.headers.authorization) return res.sendStatus(403);
if (req.headers.authorization != `Bearer ${process.env.DISCORD_TOKEN}`)
return res.sendStatus(401);
next(); // Proceed to next middleware
});
router.get('/api/commands', async (req, res) => {
// API route to get a list of commands
const commands = [];
for (var command of index.client.commands.values()) {
// Loop through the commands in the index module
commands.push({
name: command.data.name,
description: command.data.description,
category: command.category,
subcommands:
command.subCommands?.map((subcommand) => {
return {
name: subcommand.data.name,
description: subcommand.data.description,
};
}) || [],
});
}
res.status(200).json(commands); // Send the commands as a JSON response
});
router.get('/api/guilds', async (req, res) => {
// API route to get a list of guilds
const guilds = await index.client.guilds.fetch().catch(() => null);
if (!guilds) return res.sendStatus(500);
res.status(200).json(
guilds.map((guild) => {
return {
id: guild.id,
name: guild.name,
iconURL: guild.iconURL(),
owner: guild.owner,
};
})
); // Send the guilds as a JSON response
});
router.get('/api/guilds/:guild', async (req, res) => {
// API route to get information about a specific guild
if (!req.params.guild) return res.sendStatus(400);
const guild = await index.client.guilds.fetch({ guild: req.params.guild }).catch(() => null);
if (!guild) return res.sendStatus(404); // Error if guild is not found
res.status(200).json({
id: guild.id,
name: guild.name,
iconURL: guild.iconURL({ forceStatic: true }),
owner: guild.ownerId,
premiumTier: guild.premiumTier,
premiumSubscriptionCount: guild.premiumSubscriptionCount,
createdAt: guild.createdTimestamp,
joinedAt: guild.joinedTimestamp,
channels: guild.channels.cache.size,
members: guild.memberCount,
roles: guild.roles.cache.size,
}); // Send the guild information as a JSON response
});
router.get('/api/guilds/:guild/members', async (req, res) => {
// API route to get a list of members in a specific field
if (!req.params.guild) return res.sendStatus(400);
const guild = await index.client.guilds.fetch({ guild: req.params.guild }).catch(() => null);
if (!guild) return res.sendStatus(404);
const members = await guild.members.fetch().catch(() => null);
if (!members) return res.sendStatus(500);
res.status(200).json(
members.map((member) => {
return {
id: member.id,
username: member.user.username,
discriminator: member.user.discriminator,
displayAvatarURL: member.user.displayAvatarURL({ forceStatic: true }),
nickname: member.nickname,
guildDisplayAvatarURL: member.displayAvatarURL({ forceStatic: true }),
createdAt: member.user.createdTimestamp,
joinedAt: member.joinedTimestamp,
};
})
); // Send the members as a JSON response
});
router.get('/api/guilds/:guild/members/:member', async (req, res) => {
// API route to get information about a specific member in a guild
if (!req.params.guild) return res.sendStatus(400);
if (!req.params.member) return res.sendStatus(400);
const guild = await index.client.guilds.fetch({ guild: req.params.guild }).catch(() => null);
if (!guild) return res.sendStatus(404); // Error if Guild is not found
const member = await guild.members.fetch({ user: req.params.member }).catch(() => null);
if (!member) return res.sendStatus(404); // Error if Guild is not found
res.status(200).json({
id: member.id,
name: member.displayName,
discriminator: member.user.discriminator,
displayAvatarURL: member.user.displayAvatarURL({ forceStatic: true }),
nickname: member.nickname,
guildDisplayAvatarURL: member.displayAvatarURL({ forceStatic: true }),
createdAt: member.user.createdTimestamp,
joinedAt: member.joinedTimestamp,
owner: guild.ownerId == member.id,
administrator: member.permissions.has('Administrator'),
}); // Send the member information as a JSON response
});
router.get('/infractions/:id/inactive', async (req, res) => {
// API route to handle inactive instructions
if (!req.params.id) return res.sendStatus(400);
Infractions.findById(req.params.id)
.then(async (infraction) => {
if (!infraction) return res.sendStatus(404);
if (infraction.type == 'ban') {
const guild = await index.client.guilds
.fetch({ guild: infraction.guild })
.catch(() => null);
if (!guild) return res.sendStatus(500);
guild.members
.unban(infraction.user, 'Temporary ban expired')
.then(() => {
index.client.expiringDocumentsManager.infractions.removeDocument(
infraction
); // Remove the infraction from the documents manager
res.sendStatus(200); // Send success status
})
.catch(() => res.sendStatus(500)); // Error if unbanning fails
}
if (infraction.type == 'timeout') {
const guild = await index.client.guilds
.fetch({ guild: infraction.guild })
.catch(() => null);
if (!guild) return res.sendStatus(500);
const member = await guild.members
.fetch({ user: infraction.user })
.catch(() => null);
if (!member) return res.sendStatus(500);
member
.timeout(null) // Remove the timeout for the member
.then(() => {
index.client.expiringDocumentsManager.infractions.removeDocument(
infraction
); // Remove the infraction from the documents manager
res.sendStatus(200); // Send success status
})
.catch(() => res.sendStatus(500)); // Error if removing timeout fails
}
})
.catch(() => res.sendStatus(400)); // Error if Guild is not found
});
module.exports = router; // Export the router for use in other modules