1 |
rakinar2 |
577 |
/** |
2 |
|
|
* This file is part of SudoBot. |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
|
* |
6 |
|
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU Affero General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
import { roleMention } from '@discordjs/builders'; |
21 |
|
|
import { formatDuration, intervalToDuration } from 'date-fns'; |
22 |
|
|
import { BanOptions, CommandInteraction, FileOptions, Guild, GuildBan, GuildMember, Message, MessageEmbed, MessageOptions, MessagePayload, TextChannel, User } from 'discord.js'; |
23 |
|
|
import ms from 'ms'; |
24 |
|
|
import DiscordClient from '../client/Client'; |
25 |
|
|
import { IPunishment } from '../models/Punishment'; |
26 |
|
|
import { timeSince } from '../utils/util'; |
27 |
|
|
|
28 |
|
|
class Logger { |
29 |
|
|
client: DiscordClient; |
30 |
|
|
|
31 |
|
|
constructor(client: DiscordClient) { |
32 |
|
|
this.client = client; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
channel(callback: (channel: TextChannel) => any, msg: any) { |
36 |
|
|
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
37 |
|
|
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
38 |
|
|
|
39 |
|
|
if (channel) { |
40 |
|
|
return callback(channel); |
41 |
|
|
} |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) { |
45 |
|
|
let channelID = this.client.config.props[msg.guild!.id].logging_channel_join_leave; |
46 |
|
|
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
47 |
|
|
|
48 |
|
|
if (channel) { |
49 |
|
|
return callback(channel); |
50 |
|
|
} |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
async send(guild: Guild, messageOptions: MessageOptions | MessagePayload | string) { |
54 |
|
|
let channelID = this.client.config.props[guild!.id].logging_channel; |
55 |
|
|
let channel = guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
56 |
|
|
|
57 |
|
|
if (channel) { |
58 |
|
|
return await channel.send(messageOptions); |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
log(guild: Guild, callback: (channel: TextChannel) => any) { |
63 |
|
|
this.channel(callback, { guild }); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
logEdit(oldMsg: Message, newMsg: Message) { |
67 |
|
|
this.channel(async (channel) => { |
68 |
|
|
await channel.send({ |
69 |
|
|
embeds: [ |
70 |
|
|
new MessageEmbed() |
71 |
|
|
.setColor('#007bff') |
72 |
|
|
.setTitle('Message Edited in #' + (newMsg.channel as TextChannel).name + " (" + newMsg.channel.id + ")") |
73 |
|
|
.setDescription('**-+-+Before**\n' + oldMsg.content + '\n\n**-+-+After**\n' + newMsg.content) |
74 |
|
|
.addField('ID', newMsg.id) |
75 |
|
|
.setAuthor({ |
76 |
|
|
name: newMsg.author.tag, |
77 |
|
|
iconURL: newMsg.author.displayAvatarURL(), |
78 |
|
|
}) |
79 |
|
|
.setFooter({ |
80 |
|
|
text: "Edited", |
81 |
|
|
}) |
82 |
|
|
.setTimestamp() |
83 |
|
|
] |
84 |
|
|
}); |
85 |
|
|
}, newMsg); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
logDelete(msg: Message) { |
89 |
|
|
this.channel(async (channel) => { |
90 |
|
|
const embed = new MessageEmbed() |
91 |
|
|
.setColor('#f14a60') |
92 |
|
|
.setTitle('Message Deleted in #' + (msg.channel as TextChannel).name + " (" + msg.channel.id + ")") |
93 |
|
|
.setDescription(msg.content) |
94 |
|
|
.setAuthor({ |
95 |
|
|
name: msg.author.tag, |
96 |
|
|
iconURL: msg.author.displayAvatarURL(), |
97 |
|
|
}) |
98 |
|
|
.addField('ID', msg.id) |
99 |
|
|
.setFooter({ |
100 |
|
|
text: "Deleted", |
101 |
|
|
}) |
102 |
|
|
.setTimestamp(); |
103 |
|
|
|
104 |
|
|
const files: FileOptions[] = []; |
105 |
|
|
|
106 |
|
|
if (msg.attachments.size > 0) { |
107 |
|
|
let str = ''; |
108 |
|
|
|
109 |
|
|
msg.attachments.forEach(a => { |
110 |
|
|
str += `${a.name}\n`; |
111 |
|
|
files.push({ |
112 |
|
|
name: a.name!, |
113 |
|
|
attachment: a.proxyURL |
114 |
|
|
}); |
115 |
|
|
}); |
116 |
|
|
|
117 |
|
|
embed.addField('Attachments (top)', str); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
await channel.send({ |
121 |
|
|
embeds: [ |
122 |
|
|
embed |
123 |
|
|
], |
124 |
|
|
files |
125 |
|
|
}); |
126 |
|
|
}, msg); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
logBanned(ban: GuildBan) { |
130 |
|
|
this.channel(async (channel) => { |
131 |
|
|
let r = '*No reason provided*'; |
132 |
|
|
|
133 |
|
|
const auditLog = (await ban.guild.fetchAuditLogs({ |
134 |
|
|
limit: 1, |
135 |
|
|
type: 'MEMBER_BAN_ADD', |
136 |
|
|
})).entries.first(); |
137 |
|
|
|
138 |
|
|
|
139 |
|
|
if (ban.reason) { |
140 |
|
|
r = ban.reason; |
141 |
|
|
} |
142 |
|
|
else if (auditLog) { |
143 |
|
|
console.log(auditLog); |
144 |
|
|
const { target, reason } = await auditLog; |
145 |
|
|
|
146 |
|
|
if (target!.id === ban.user.id && reason) { |
147 |
|
|
r = await reason; |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
await channel.send({ |
152 |
|
|
embeds: [ |
153 |
|
|
new MessageEmbed() |
154 |
|
|
.setColor('#f14a60') |
155 |
|
|
.setTitle("A user was banned") |
156 |
|
|
.setAuthor({ |
157 |
|
|
name: ban.user.tag, |
158 |
|
|
iconURL: ban.user.displayAvatarURL(), |
159 |
|
|
}) |
160 |
|
|
.addField('Reason', r) |
161 |
|
|
.addField('User ID', ban.user.id) |
162 |
|
|
.setFooter({ |
163 |
|
|
text: "Banned", |
164 |
|
|
}) |
165 |
|
|
.setTimestamp() |
166 |
|
|
] |
167 |
|
|
}); |
168 |
|
|
}, ban); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) { |
172 |
|
|
this.channel(async (channel) => { |
173 |
|
|
let r = '*No reason provided*'; |
174 |
|
|
|
175 |
|
|
const auditLog = (await guild.fetchAuditLogs({ |
176 |
|
|
limit: 1, |
177 |
|
|
type: 'MEMBER_BAN_ADD', |
178 |
|
|
})).entries.first(); |
179 |
|
|
|
180 |
|
|
if (banOptions.reason) { |
181 |
|
|
r = banOptions.reason; |
182 |
|
|
} |
183 |
|
|
else if (auditLog) { |
184 |
|
|
console.log(auditLog); |
185 |
|
|
const { target, reason } = await auditLog; |
186 |
|
|
|
187 |
|
|
if (target!.id === user.id && reason) { |
188 |
|
|
r = await reason; |
189 |
|
|
} |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
await channel.send({ |
193 |
|
|
embeds: [ |
194 |
|
|
new MessageEmbed() |
195 |
|
|
.setColor('#f14a60') |
196 |
|
|
.setTitle("A user was softbanned") |
197 |
|
|
.setAuthor({ |
198 |
|
|
name: user.tag, |
199 |
|
|
iconURL: user.displayAvatarURL(), |
200 |
|
|
}) |
201 |
|
|
.addField('Reason', r) |
202 |
|
|
.addField('Softbanned by', model.mod_tag) |
203 |
|
|
.addField('User ID', user.id) |
204 |
|
|
.setFooter({ |
205 |
|
|
text: "Softbanned", |
206 |
|
|
}) |
207 |
|
|
.setTimestamp() |
208 |
|
|
] |
209 |
|
|
}); |
210 |
|
|
}, { |
211 |
|
|
guild |
212 |
|
|
}); |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) { |
216 |
|
|
this.channel(async (channel) => { |
217 |
|
|
let r = '*No reason provided*'; |
218 |
|
|
|
219 |
|
|
const auditLog = (await guild.fetchAuditLogs({ |
220 |
|
|
limit: 1, |
221 |
|
|
type: 'MEMBER_BAN_ADD', |
222 |
|
|
})).entries.first(); |
223 |
|
|
|
224 |
|
|
if (banOptions.reason) { |
225 |
|
|
r = banOptions.reason; |
226 |
|
|
} |
227 |
|
|
else if (auditLog) { |
228 |
|
|
console.log(auditLog); |
229 |
|
|
const { target, reason } = await auditLog; |
230 |
|
|
|
231 |
|
|
if (target!.id === user.id && reason) { |
232 |
|
|
r = await reason; |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
await channel.send({ |
237 |
|
|
embeds: [ |
238 |
|
|
new MessageEmbed() |
239 |
|
|
.setColor('#f14a60') |
240 |
|
|
.setTitle("A user was temporarily banned") |
241 |
|
|
.setAuthor({ |
242 |
|
|
name: user.tag, |
243 |
|
|
iconURL: user.displayAvatarURL(), |
244 |
|
|
}) |
245 |
|
|
.addField('Reason', r) |
246 |
|
|
.addField('Banned by', model.mod_tag) |
247 |
|
|
.addField('User ID', user.id) |
248 |
|
|
.addField('Duration', ms((model.meta as any).time)) |
249 |
|
|
.setFooter({ |
250 |
|
|
text: "Temporarily banned", |
251 |
|
|
}) |
252 |
|
|
.setTimestamp() |
253 |
|
|
] |
254 |
|
|
}); |
255 |
|
|
}, { |
256 |
|
|
guild |
257 |
|
|
}); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
logUnbanned(ban: GuildBan) { |
261 |
|
|
this.channel(async (channel) => { |
262 |
|
|
await channel.send({ |
263 |
|
|
embeds: [ |
264 |
|
|
new MessageEmbed() |
265 |
|
|
.setColor('#f14a60') |
266 |
|
|
.setTitle("A user was unbanned") |
267 |
|
|
.setAuthor({ |
268 |
|
|
name: ban.user.tag, |
269 |
|
|
iconURL: ban.user.displayAvatarURL(), |
270 |
|
|
}) |
271 |
|
|
.addField('User ID', ban.user.id) |
272 |
|
|
.setFooter({ |
273 |
|
|
text: "Unbanned", |
274 |
|
|
}) |
275 |
|
|
.setTimestamp() |
276 |
|
|
] |
277 |
|
|
}); |
278 |
|
|
}, ban); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
logJoined(member: GuildMember) { |
282 |
|
|
this.channelJoinLeft(async (channel) => { |
283 |
|
|
await channel.send({ |
284 |
|
|
embeds: [ |
285 |
|
|
new MessageEmbed() |
286 |
|
|
.setColor('#007bff') |
287 |
|
|
.setTitle("New member joined") |
288 |
|
|
.setAuthor({ |
289 |
|
|
name: member.user.tag, |
290 |
|
|
iconURL: member.user.displayAvatarURL(), |
291 |
|
|
}) |
292 |
|
|
.setDescription(`<@${member.user.id}> just joined the server!`) |
293 |
|
|
.addField('Account Created', `${member.user.createdAt.toLocaleString()} (${timeSince(member.user.createdAt.getTime())})`) |
294 |
|
|
.addField('New Account?', (new Date().getTime() - member.user.createdAt.getTime()) <= 3 * 24 * 60 * 60 * 1000 ? ":warning: Yes :warning:" : "No") |
295 |
|
|
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
296 |
|
|
.addField('User ID', member.user.id) |
297 |
|
|
.setFooter({ |
298 |
|
|
text: "Joined", |
299 |
|
|
}) |
300 |
|
|
.setTimestamp() |
301 |
|
|
] |
302 |
|
|
}); |
303 |
|
|
}, member); |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
logLeft(member: GuildMember) { |
307 |
|
|
this.channelJoinLeft(async (channel) => { |
308 |
|
|
const roles = await member.roles.cache.filter(role => role.id !== member.guild.id).reduce((acc, val) => ` ${acc} ${roleMention(val.id)}`, ''); |
309 |
|
|
|
310 |
|
|
await channel.send({ |
311 |
|
|
embeds: [ |
312 |
|
|
new MessageEmbed() |
313 |
|
|
.setColor('#f14a60') |
314 |
|
|
.setTitle("Member left") |
315 |
|
|
.setAuthor({ |
316 |
|
|
name: member.user.tag, |
317 |
|
|
iconURL: member.user.displayAvatarURL(), |
318 |
|
|
}) |
319 |
|
|
.setDescription(`**Roles**\n${roles}`) |
320 |
|
|
.addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`) |
321 |
|
|
.addField('User ID', member.user.id) |
322 |
|
|
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
323 |
|
|
.setFooter({ |
324 |
|
|
text: "Left", |
325 |
|
|
}) |
326 |
|
|
.setTimestamp() |
327 |
|
|
] |
328 |
|
|
}); |
329 |
|
|
}, member); |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
logBeaned(member: GuildMember, r: string, d: User) { |
333 |
|
|
this.channel(async (channel) => { |
334 |
|
|
await channel.send({ |
335 |
|
|
embeds: [ |
336 |
|
|
new MessageEmbed() |
337 |
|
|
.setColor('#007bff') |
338 |
|
|
.setTitle("Member beaned") |
339 |
|
|
.setAuthor({ |
340 |
|
|
name: member.user.tag, |
341 |
|
|
iconURL: member.user.displayAvatarURL(), |
342 |
|
|
}) |
343 |
|
|
.addField('Reason', r) |
344 |
|
|
.addField('Beaned by', d.tag) |
345 |
|
|
.addField('User ID', member.user.id) |
346 |
|
|
.setFooter({ |
347 |
|
|
text: "Beaned", |
348 |
|
|
}) |
349 |
|
|
.setTimestamp() |
350 |
|
|
] |
351 |
|
|
}); |
352 |
|
|
}, member); |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
logMute(member: GuildMember, reason: string, duration: number | null | undefined, d: User, hard: boolean = true) { |
356 |
|
|
this.channel(async (channel) => { |
357 |
|
|
await channel.send({ |
358 |
|
|
embeds: [ |
359 |
|
|
new MessageEmbed() |
360 |
|
|
.setColor('#f14a60') |
361 |
|
|
.setTitle("Member muted") |
362 |
|
|
.setAuthor({ |
363 |
|
|
name: member.user.tag, |
364 |
|
|
iconURL: member.user.displayAvatarURL(), |
365 |
|
|
}) |
366 |
|
|
.addField('Reason', reason) |
367 |
|
|
.addField('Muted by', d.tag) |
368 |
|
|
.addField('Duration Until', duration ? `${(new Date(Date.now() + duration)).toLocaleString()} (${formatDuration(intervalToDuration({ start: 0, end: duration }))})` : "*No duration set*") |
369 |
|
|
.addField('User ID', member.user.id) |
370 |
|
|
.addField('Hardmute', hard ? 'Yes' : 'No') |
371 |
|
|
.setFooter({ |
372 |
|
|
text: "Muted", |
373 |
|
|
}) |
374 |
|
|
.setTimestamp() |
375 |
|
|
] |
376 |
|
|
}); |
377 |
|
|
}, member); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
logUnmute(member: GuildMember, d: User) { |
381 |
|
|
this.channel(async (channel) => { |
382 |
|
|
await channel.send({ |
383 |
|
|
embeds: [ |
384 |
|
|
new MessageEmbed() |
385 |
|
|
.setColor('#007bff') |
386 |
|
|
.setTitle("Member unmuted") |
387 |
|
|
.setAuthor({ |
388 |
|
|
name: member.user.tag, |
389 |
|
|
iconURL: member.user.displayAvatarURL(), |
390 |
|
|
}) |
391 |
|
|
.addField('Unmuted by', d.tag) |
392 |
|
|
.addField('User ID', member.user.id) |
393 |
|
|
.setFooter({ |
394 |
|
|
text: "Unmuted", |
395 |
|
|
}) |
396 |
|
|
.setTimestamp() |
397 |
|
|
] |
398 |
|
|
}); |
399 |
|
|
}, member); |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
logWarn(msg: Message | CommandInteraction, member: GuildMember | User, d: User, reason: string | undefined, id: number | string) { |
403 |
|
|
if ((member as GuildMember).user) |
404 |
|
|
member = (member as GuildMember).user; |
405 |
|
|
|
406 |
|
|
this.channel(async (channel) => { |
407 |
|
|
await channel.send({ |
408 |
|
|
embeds: [ |
409 |
|
|
new MessageEmbed() |
410 |
|
|
.setColor('GOLD') |
411 |
|
|
.setTitle("Member warned") |
412 |
|
|
.setAuthor({ |
413 |
|
|
name: (member as User).tag, |
414 |
|
|
iconURL: member.displayAvatarURL(), |
415 |
|
|
}) |
416 |
|
|
.addField('Reason', reason ?? '*No reason provided*') |
417 |
|
|
.addField('Warned by', d.tag) |
418 |
|
|
.addField('User ID', member.id) |
419 |
|
|
.addField('Case ID', id + '') |
420 |
|
|
.setFooter({ |
421 |
|
|
text: "Warned", |
422 |
|
|
}) |
423 |
|
|
.setTimestamp() |
424 |
|
|
] |
425 |
|
|
}); |
426 |
|
|
}, msg); |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
logWarndel(msg: Message, member: GuildMember, warn: any, d: User) { |
430 |
|
|
this.channel(async (channel) => { |
431 |
|
|
await channel.send({ |
432 |
|
|
embeds: [ |
433 |
|
|
new MessageEmbed() |
434 |
|
|
.setColor('GOLD') |
435 |
|
|
.setTitle("Warning deleted") |
436 |
|
|
.setAuthor({ |
437 |
|
|
name: member.user.tag, |
438 |
|
|
iconURL: member.user.displayAvatarURL(), |
439 |
|
|
}) |
440 |
|
|
.addField('Warned by', d.tag + '') |
441 |
|
|
.addField('Warning ID', warn.id + '') |
442 |
|
|
.addField('User ID', member.user.id) |
443 |
|
|
.setFooter({ |
444 |
|
|
text: "Warning Deleted", |
445 |
|
|
}) |
446 |
|
|
.setTimestamp() |
447 |
|
|
] |
448 |
|
|
}); |
449 |
|
|
}, msg); |
450 |
|
|
} |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
export default Logger; |