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