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