1 |
|
/** |
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'; |
import { roleMention } from '@discordjs/builders'; |
21 |
import { FileOptions, GuildBan, GuildMember, Message, MessageEmbed, TextChannel, User } from 'discord.js'; |
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'; |
import DiscordClient from '../client/Client'; |
25 |
import { timeProcess, timeSince } from '../utils/util'; |
import { IPunishment } from '../models/Punishment'; |
26 |
|
import { timeSince } from '../utils/util'; |
27 |
|
|
28 |
class Logger { |
class Logger { |
29 |
client: DiscordClient; |
client: DiscordClient; |
32 |
this.client = client; |
this.client = client; |
33 |
} |
} |
34 |
|
|
35 |
channel(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
channel(callback: (channel: TextChannel) => any, msg: any) { |
36 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
37 |
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; |
38 |
|
|
39 |
if (channel) { |
if (channel) { |
40 |
return callback(channel); |
return callback(channel); |
41 |
} |
} |
42 |
} |
} |
43 |
|
|
44 |
channelJoinLeft(callback: (channel: TextChannel) => any, msg: Message | GuildBan) { |
channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) { |
45 |
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; |
46 |
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; |
47 |
|
|
48 |
if (channel) { |
if (channel) { |
49 |
return callback(channel); |
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) { |
logEdit(oldMsg: Message, newMsg: Message) { |
67 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
68 |
await channel.send({ |
await channel.send({ |
130 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
131 |
let r = '*No reason provided*'; |
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) { |
if (ban.reason) { |
140 |
r = ban.reason; |
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({ |
await channel.send({ |
152 |
embeds: [ |
embeds: [ |
168 |
}, ban); |
}, 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) { |
logUnbanned(ban: GuildBan) { |
261 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
262 |
await channel.send({ |
await channel.send({ |
352 |
}, member); |
}, member); |
353 |
} |
} |
354 |
|
|
355 |
logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User) { |
logMute(member: GuildMember, reason: string, duration: number | null | undefined, d: User, hard: boolean = true) { |
356 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
357 |
await channel.send({ |
await channel.send({ |
358 |
embeds: [ |
embeds: [ |
365 |
}) |
}) |
366 |
.addField('Reason', reason) |
.addField('Reason', reason) |
367 |
.addField('Muted by', d.tag) |
.addField('Muted by', d.tag) |
368 |
.addField('Duration Until', typeof timeMs === 'number' ? `${new Date((timeMs / 1000) + Date.now()).toLocaleString()} (${timeProcess(timeMs / 1000)})` : "*No duration set*") |
.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) |
.addField('User ID', member.user.id) |
370 |
|
.addField('Hardmute', hard ? 'Yes' : 'No') |
371 |
.setFooter({ |
.setFooter({ |
372 |
text: "Muted", |
text: "Muted", |
373 |
}) |
}) |
399 |
}, member); |
}, member); |
400 |
} |
} |
401 |
|
|
402 |
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) { |
403 |
if ((member as GuildMember).user) |
if ((member as GuildMember).user) |
404 |
member = (member as GuildMember).user; |
member = (member as GuildMember).user; |
405 |
|
|
413 |
name: (member as User).tag, |
name: (member as User).tag, |
414 |
iconURL: member.displayAvatarURL(), |
iconURL: member.displayAvatarURL(), |
415 |
}) |
}) |
416 |
.addField('Reason', reason) |
.addField('Reason', reason ?? '*No reason provided*') |
417 |
.addField('Warned by', d.tag) |
.addField('Warned by', d.tag) |
418 |
.addField('User ID', member.id) |
.addField('User ID', member.id) |
419 |
.addField('Warning ID', id + '') |
.addField('Case ID', id + '') |
420 |
.setFooter({ |
.setFooter({ |
421 |
text: "Warned", |
text: "Warned", |
422 |
}) |
}) |