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