1 |
import { CommandInteraction, Message, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
import getUser from '../../utils/getUser'; |
8 |
import Punishment from '../../models/Punishment'; |
9 |
import { fetchEmoji } from '../../utils/Emoji'; |
10 |
import PunishmentType from '../../types/PunishmentType'; |
11 |
|
12 |
export default class WarningCommand extends BaseCommand { |
13 |
supportsInteractions: boolean = true; |
14 |
|
15 |
constructor() { |
16 |
super('warning', 'moderation', []); |
17 |
} |
18 |
|
19 |
async list(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
20 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
21 |
await msg.reply({ |
22 |
embeds: [ |
23 |
new MessageEmbed() |
24 |
.setColor('#f14a60') |
25 |
.setDescription(`This command requires an argument.`) |
26 |
] |
27 |
}); |
28 |
|
29 |
return; |
30 |
} |
31 |
|
32 |
let user: User; |
33 |
|
34 |
try { |
35 |
user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!; |
36 |
|
37 |
if (!user) |
38 |
throw new Error(); |
39 |
} |
40 |
catch (e) { |
41 |
await msg.reply({ |
42 |
embeds: [ |
43 |
new MessageEmbed() |
44 |
.setColor('#f14a60') |
45 |
.setDescription(`Invalid user given.`) |
46 |
] |
47 |
}); |
48 |
|
49 |
return; |
50 |
} |
51 |
|
52 |
const warnings = await Punishment.find({ |
53 |
guild_id: msg.guild!.id, |
54 |
user_id: user.id, |
55 |
type: PunishmentType.WARNING |
56 |
}).sort({ createdAt: -1 }); |
57 |
|
58 |
if (warnings.length < 1) { |
59 |
await msg.reply({ |
60 |
embeds: [ |
61 |
new MessageEmbed() |
62 |
.setColor('#f14a60') |
63 |
.setDescription(`No warnings found for that user.`) |
64 |
] |
65 |
}); |
66 |
|
67 |
return; |
68 |
} |
69 |
|
70 |
let str = ''; |
71 |
|
72 |
for await (const warning of warnings) { |
73 |
str += `ID: ${warning.id}\n`; |
74 |
str += `Reason: ${warning.reason ?? '*No reason provided*'}\n`; |
75 |
|
76 |
try { |
77 |
str += `Warned by: ${(await client.users.fetch(warning.mod_id)).tag}\n`; |
78 |
} |
79 |
catch (e) { |
80 |
str += `Warned by: ${warning.mod_id}\n`; |
81 |
} |
82 |
|
83 |
str += `Date: ${warning.createdAt}\n\n`; |
84 |
} |
85 |
|
86 |
await msg.reply({ |
87 |
embeds: [ |
88 |
new MessageEmbed() |
89 |
.setAuthor({ |
90 |
name: user.tag, |
91 |
iconURL: user.displayAvatarURL() |
92 |
}) |
93 |
.setDescription(`**All warnings**\n\n${str}`) |
94 |
] |
95 |
}); |
96 |
} |
97 |
|
98 |
async clear(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
99 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
100 |
await msg.reply({ |
101 |
embeds: [ |
102 |
new MessageEmbed() |
103 |
.setColor('#f14a60') |
104 |
.setDescription(`This command requires an argument.`) |
105 |
] |
106 |
}); |
107 |
|
108 |
return; |
109 |
} |
110 |
|
111 |
let user: User; |
112 |
|
113 |
try { |
114 |
user = await (options.isInteraction ? options.options.getUser('user') : (await getUser(client, msg as Message, options)))!; |
115 |
|
116 |
if (!user) |
117 |
throw new Error(); |
118 |
} |
119 |
catch (e) { |
120 |
await msg.reply({ |
121 |
embeds: [ |
122 |
new MessageEmbed() |
123 |
.setColor('#f14a60') |
124 |
.setDescription(`Invalid user given.`) |
125 |
] |
126 |
}); |
127 |
|
128 |
return; |
129 |
} |
130 |
|
131 |
const warning = await Punishment.deleteOne({ |
132 |
guild_id: msg.guild!.id, |
133 |
user_id: user.id, |
134 |
type: PunishmentType.WARNING |
135 |
}); |
136 |
|
137 |
if (warning.deletedCount < 1) { |
138 |
await msg.reply({ |
139 |
embeds: [ |
140 |
new MessageEmbed() |
141 |
.setColor('#f14a60') |
142 |
.setDescription(`No warnings found for that user.`) |
143 |
] |
144 |
}); |
145 |
|
146 |
return; |
147 |
} |
148 |
|
149 |
await msg.reply({ |
150 |
embeds: [ |
151 |
new MessageEmbed() |
152 |
.setColor('GREEN') |
153 |
.setDescription(`${(await fetchEmoji('check'))?.toString()} Cleared ${warning} warnings for ${user.tag}`) |
154 |
] |
155 |
}); |
156 |
} |
157 |
|
158 |
async remove(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
159 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
160 |
await msg.reply({ |
161 |
embeds: [ |
162 |
new MessageEmbed() |
163 |
.setColor('#f14a60') |
164 |
.setDescription(`This command requires an argument.`) |
165 |
] |
166 |
}); |
167 |
|
168 |
return; |
169 |
} |
170 |
|
171 |
const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]); |
172 |
|
173 |
const warning = await Punishment.findOne({ |
174 |
id, |
175 |
guild_id: msg.guild!.id, |
176 |
type: PunishmentType.WARNING |
177 |
}); |
178 |
|
179 |
if (!warning) { |
180 |
await msg.reply({ |
181 |
embeds: [ |
182 |
new MessageEmbed() |
183 |
.setColor('#f14a60') |
184 |
.setDescription(`Invalid warning ID given.`) |
185 |
] |
186 |
}); |
187 |
|
188 |
return; |
189 |
} |
190 |
|
191 |
await warning.delete(); |
192 |
|
193 |
await msg.reply({ |
194 |
embeds: [ |
195 |
new MessageEmbed() |
196 |
.setColor('GREEN') |
197 |
.setDescription(`${(await fetchEmoji('check'))?.toString()} Warning removed successfully!`) |
198 |
] |
199 |
}); |
200 |
} |
201 |
|
202 |
async view(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
203 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
204 |
await msg.reply({ |
205 |
embeds: [ |
206 |
new MessageEmbed() |
207 |
.setColor('#f14a60') |
208 |
.setDescription(`This command requires an argument.`) |
209 |
] |
210 |
}); |
211 |
|
212 |
return; |
213 |
} |
214 |
|
215 |
const id = options.isInteraction ? options.options.getString('id') : parseInt(options.args[0]); |
216 |
|
217 |
const warning = await Punishment.findOne({ |
218 |
id, |
219 |
guild_id: msg.guild!.id, |
220 |
type: PunishmentType.WARNING |
221 |
}); |
222 |
|
223 |
if (!warning) { |
224 |
await msg.reply({ |
225 |
embeds: [ |
226 |
new MessageEmbed() |
227 |
.setColor('#f14a60') |
228 |
.setDescription(`Invalid warning ID given.`) |
229 |
] |
230 |
}); |
231 |
|
232 |
return; |
233 |
} |
234 |
|
235 |
let mod: string = warning.get('mod_id') as string; |
236 |
|
237 |
try { |
238 |
const m = await client.users.fetch(mod); |
239 |
mod = m.tag; |
240 |
} |
241 |
catch (e) { |
242 |
|
243 |
} |
244 |
|
245 |
let user: User | string = warning.get('user_id') as string; |
246 |
|
247 |
try { |
248 |
user = await client.users.fetch(user); |
249 |
} |
250 |
catch (e) { |
251 |
|
252 |
} |
253 |
|
254 |
const fields = [ |
255 |
{ |
256 |
name: 'Warning ID', |
257 |
value: (warning.get('id') as number) + '' |
258 |
}, |
259 |
{ |
260 |
name: 'Reason', |
261 |
value: warning.get('reason') as string|null ?? '*No reason provided*' |
262 |
}, |
263 |
{ |
264 |
name: 'Warned by', |
265 |
value: mod |
266 |
}, |
267 |
]; |
268 |
|
269 |
console.log(fields); |
270 |
|
271 |
|
272 |
await msg.reply({ |
273 |
embeds: [ |
274 |
new MessageEmbed({ |
275 |
author: { |
276 |
name: typeof user === 'string' ? user : user.tag, |
277 |
iconURL: typeof user === 'string' ? undefined : user.displayAvatarURL() |
278 |
}, |
279 |
fields, |
280 |
}) |
281 |
.setTimestamp(warning.get('createdAt') as Date) |
282 |
] |
283 |
}); |
284 |
} |
285 |
|
286 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
287 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
288 |
await msg.reply({ |
289 |
embeds: [ |
290 |
new MessageEmbed() |
291 |
.setColor('#f14a60') |
292 |
.setDescription(`This command requires a subcommand.`) |
293 |
] |
294 |
}); |
295 |
|
296 |
return; |
297 |
} |
298 |
|
299 |
const subcmd = options.isInteraction ? options.options.getSubcommand(true) : options.args[0]; |
300 |
|
301 |
if (!['view', 'remove', 'clear', 'list'].includes(subcmd)) { |
302 |
await msg.reply({ |
303 |
embeds: [ |
304 |
new MessageEmbed() |
305 |
.setColor('#f14a60') |
306 |
.setDescription(`Invalid subcommand given.`) |
307 |
] |
308 |
}); |
309 |
|
310 |
return; |
311 |
} |
312 |
|
313 |
if (!options.isInteraction) |
314 |
options.args.shift(); |
315 |
|
316 |
await (this as any)[subcmd](client, msg, options); |
317 |
} |
318 |
} |