1 |
import { GuildBan, GuildMember, Message, MessageEmbed, TextChannel, User } from 'discord.js'; |
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'; |
import DiscordClient from '../client/Client'; |
5 |
|
import Punishment from '../models/Punishment'; |
6 |
import { timeProcess, timeSince } from '../utils/util'; |
import { timeProcess, timeSince } from '../utils/util'; |
7 |
|
|
8 |
class Logger { |
class Logger { |
12 |
this.client = client; |
this.client = client; |
13 |
} |
} |
14 |
|
|
15 |
channel(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
channel(callback: (channel: TextChannel) => any, msg: any) { |
16 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
17 |
let channel = msg.guild!.channels.cache.find(c => c.id === channelID) as TextChannel; |
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
18 |
|
|
19 |
if (channel) { |
if (channel) { |
20 |
return callback(channel); |
return callback(channel); |
21 |
} |
} |
22 |
} |
} |
23 |
|
|
24 |
channelJoinLeft(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) { |
25 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel_join_leave; |
let channelID = this.client.config.props[msg.guild!.id].logging_channel_join_leave; |
26 |
let channel = msg.guild!.channels.cache.find(c => c.id === channelID) as TextChannel; |
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
27 |
|
|
28 |
if (channel) { |
if (channel) { |
29 |
return callback(channel); |
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) { |
logEdit(oldMsg: Message, newMsg: Message) { |
47 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
48 |
await channel.send({ |
await channel.send({ |
80 |
text: "Deleted", |
text: "Deleted", |
81 |
}) |
}) |
82 |
.setTimestamp(); |
.setTimestamp(); |
83 |
|
|
84 |
|
const files: FileOptions[] = []; |
85 |
|
|
86 |
if (msg.attachments.size > 0) { |
if (msg.attachments.size > 0) { |
87 |
let str = ''; |
let str = ''; |
88 |
|
|
89 |
msg.attachments.forEach(a => { |
msg.attachments.forEach(a => { |
90 |
str += `**${a.name}** ${a.url}\n`; |
str += `${a.name}\n`; |
91 |
|
files.push({ |
92 |
|
name: a.name!, |
93 |
|
attachment: a.proxyURL |
94 |
|
}); |
95 |
}); |
}); |
96 |
|
|
97 |
embed.addField('Attachments', str); |
embed.addField('Attachments (top)', str); |
98 |
} |
} |
99 |
|
|
100 |
await channel.send({ |
await channel.send({ |
101 |
embeds: [ |
embeds: [ |
102 |
embed |
embed |
103 |
] |
], |
104 |
|
files |
105 |
}); |
}); |
106 |
}, msg); |
}, msg); |
107 |
} |
} |
110 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
111 |
let r = '*No reason provided*'; |
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) { |
if (ban.reason) { |
120 |
r = ban.reason; |
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({ |
await channel.send({ |
132 |
embeds: [ |
embeds: [ |
148 |
}, ban); |
}, 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) { |
logUnbanned(ban: GuildBan) { |
241 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
242 |
await channel.send({ |
await channel.send({ |
285 |
|
|
286 |
logLeft(member: GuildMember) { |
logLeft(member: GuildMember) { |
287 |
this.channelJoinLeft(async (channel) => { |
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({ |
await channel.send({ |
291 |
embeds: [ |
embeds: [ |
292 |
new MessageEmbed() |
new MessageEmbed() |
296 |
name: member.user.tag, |
name: member.user.tag, |
297 |
iconURL: member.user.displayAvatarURL(), |
iconURL: member.user.displayAvatarURL(), |
298 |
}) |
}) |
299 |
|
.setDescription(`**Roles**\n${roles}`) |
300 |
.addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`) |
.addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`) |
301 |
.addField('User ID', member.user.id) |
.addField('User ID', member.user.id) |
302 |
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
.addField('Bot?', member.user.bot === true ? 'Yes' : 'No') |
332 |
}, member); |
}, member); |
333 |
} |
} |
334 |
|
|
335 |
logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User) { |
logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User, hard: boolean = true) { |
336 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
337 |
await channel.send({ |
await channel.send({ |
338 |
embeds: [ |
embeds: [ |
347 |
.addField('Muted by', d.tag) |
.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*") |
.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) |
.addField('User ID', member.user.id) |
350 |
|
.addField('Hardmute', hard ? 'Yes' : 'No') |
351 |
.setFooter({ |
.setFooter({ |
352 |
text: "Muted", |
text: "Muted", |
353 |
}) |
}) |
379 |
}, member); |
}, member); |
380 |
} |
} |
381 |
|
|
382 |
logWarn(msg: Message, member: GuildMember | User, d: User, reason: string, id: number | string) { |
logWarn(msg: Message | CommandInteraction, member: GuildMember | User, d: User, reason: string | undefined, id: number | string) { |
383 |
if ((member as GuildMember).user) |
if ((member as GuildMember).user) |
384 |
member = (member as GuildMember).user; |
member = (member as GuildMember).user; |
385 |
|
|
393 |
name: (member as User).tag, |
name: (member as User).tag, |
394 |
iconURL: member.displayAvatarURL(), |
iconURL: member.displayAvatarURL(), |
395 |
}) |
}) |
396 |
.addField('Reason', reason) |
.addField('Reason', reason ?? '*No reason provided*') |
397 |
.addField('Warned by', d.tag) |
.addField('Warned by', d.tag) |
398 |
.addField('User ID', member.id) |
.addField('User ID', member.id) |
399 |
.addField('Warning ID', id + '') |
.addField('Case ID', id + '') |
400 |
.setFooter({ |
.setFooter({ |
401 |
text: "Warned", |
text: "Warned", |
402 |
}) |
}) |