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 { BanOptions, CommandInteraction, FileOptions, Guild, 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'; |
import ms from 'ms'; |
24 |
import DiscordClient from '../client/Client'; |
import DiscordClient from '../client/Client'; |
25 |
import Punishment from '../models/Punishment'; |
import { IPunishment } from '../models/Punishment'; |
26 |
import { timeProcess, timeSince } from '../utils/util'; |
import { timeSince } from '../utils/util'; |
27 |
|
|
28 |
class Logger { |
class Logger { |
29 |
client: DiscordClient; |
client: DiscordClient; |
30 |
|
pause = false; |
31 |
|
pauseBan = false; |
32 |
|
|
33 |
|
pauseAll(p = true) { |
34 |
|
this.pause = p; |
35 |
|
} |
36 |
|
|
37 |
constructor(client: DiscordClient) { |
constructor(client: DiscordClient) { |
38 |
this.client = client; |
this.client = client; |
39 |
} |
} |
40 |
|
|
41 |
channel(callback: (channel: TextChannel) => any, msg: any) { |
channel(callback: (channel: TextChannel) => any, msg: any) { |
42 |
|
if (this.pause) { |
43 |
|
return; |
44 |
|
} |
45 |
|
|
46 |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
let channelID = this.client.config.props[msg.guild!.id].logging_channel; |
47 |
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
48 |
|
|
60 |
} |
} |
61 |
} |
} |
62 |
|
|
63 |
|
async send(guild: Guild, messageOptions: MessageOptions | MessagePayload | string) { |
64 |
|
let channelID = this.client.config.props[guild!.id].logging_channel; |
65 |
|
let channel = guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel; |
66 |
|
|
67 |
|
if (channel) { |
68 |
|
return await channel.send(messageOptions); |
69 |
|
} |
70 |
|
} |
71 |
|
|
72 |
|
log(guild: Guild, callback: (channel: TextChannel) => any) { |
73 |
|
this.channel(callback, { guild }); |
74 |
|
} |
75 |
|
|
76 |
logEdit(oldMsg: Message, newMsg: Message) { |
logEdit(oldMsg: Message, newMsg: Message) { |
77 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
78 |
await channel.send({ |
await channel.send({ |
145 |
type: 'MEMBER_BAN_ADD', |
type: 'MEMBER_BAN_ADD', |
146 |
})).entries.first(); |
})).entries.first(); |
147 |
|
|
|
|
|
148 |
if (ban.reason) { |
if (ban.reason) { |
149 |
r = ban.reason; |
r = ban.reason; |
150 |
} |
} |
153 |
const { target, reason } = await auditLog; |
const { target, reason } = await auditLog; |
154 |
|
|
155 |
if (target!.id === ban.user.id && reason) { |
if (target!.id === ban.user.id && reason) { |
156 |
r = await reason; |
r = reason.replace(/^\[BAN\]/, ''); |
157 |
|
} |
158 |
|
|
159 |
|
if (auditLog.executor?.id === this.client.user!.id || (r.startsWith("[TEMPBAN]") || r.startsWith("[SOFTBAN]"))) { |
160 |
|
return; |
161 |
} |
} |
162 |
} |
} |
163 |
|
|
172 |
}) |
}) |
173 |
.addField('Reason', r) |
.addField('Reason', r) |
174 |
.addField('User ID', ban.user.id) |
.addField('User ID', ban.user.id) |
175 |
|
.addFields({ |
176 |
|
name: 'Banned by', |
177 |
|
value: auditLog?.executor ? `${auditLog.executor.tag} (${auditLog.executor.id})` : 'Unknown' |
178 |
|
}) |
179 |
.setFooter({ |
.setFooter({ |
180 |
text: "Banned", |
text: "Banned", |
181 |
}) |
}) |
185 |
}, ban); |
}, ban); |
186 |
} |
} |
187 |
|
|
188 |
logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: Punishment) { |
logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) { |
189 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
190 |
let r = '*No reason provided*'; |
let r = '*No reason provided*'; |
191 |
|
|
202 |
const { target, reason } = await auditLog; |
const { target, reason } = await auditLog; |
203 |
|
|
204 |
if (target!.id === user.id && reason) { |
if (target!.id === user.id && reason) { |
205 |
r = await reason; |
r = reason.replace(/^\[SOFTBAN\]/, ''); |
206 |
} |
} |
207 |
} |
} |
208 |
|
|
216 |
iconURL: user.displayAvatarURL(), |
iconURL: user.displayAvatarURL(), |
217 |
}) |
}) |
218 |
.addField('Reason', r) |
.addField('Reason', r) |
219 |
.addField('Softbanned by', model.get().mod_tag) |
.addField('Softbanned by', model.mod_tag) |
220 |
.addField('User ID', user.id) |
.addField('User ID', user.id) |
221 |
.setFooter({ |
.setFooter({ |
222 |
text: "Softbanned", |
text: "Softbanned", |
229 |
}); |
}); |
230 |
} |
} |
231 |
|
|
232 |
logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: Punishment) { |
logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) { |
233 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
234 |
let r = '*No reason provided*'; |
let r = '*No reason provided*'; |
235 |
|
|
246 |
const { target, reason } = await auditLog; |
const { target, reason } = await auditLog; |
247 |
|
|
248 |
if (target!.id === user.id && reason) { |
if (target!.id === user.id && reason) { |
249 |
r = await reason; |
r = reason.replace(/^\[TEMPBAN\]/, ''); |
250 |
} |
} |
251 |
} |
} |
252 |
|
|
260 |
iconURL: user.displayAvatarURL(), |
iconURL: user.displayAvatarURL(), |
261 |
}) |
}) |
262 |
.addField('Reason', r) |
.addField('Reason', r) |
263 |
.addField('Banned by', model.get().mod_tag) |
.addField('Banned by', model.mod_tag) |
264 |
.addField('User ID', user.id) |
.addField('User ID', user.id) |
265 |
.addField('Duration', ms(model.get().meta?.time)) |
.addField('Duration', ms((model.meta as any).time)) |
266 |
.setFooter({ |
.setFooter({ |
267 |
text: "Temporarily banned", |
text: "Temporarily banned", |
268 |
}) |
}) |
369 |
}, member); |
}, member); |
370 |
} |
} |
371 |
|
|
372 |
logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User, hard: boolean = true) { |
logMute(member: GuildMember, reason: string, duration: number | null | undefined, d: User, hard: boolean = true) { |
373 |
this.channel(async (channel) => { |
this.channel(async (channel) => { |
374 |
await channel.send({ |
await channel.send({ |
375 |
embeds: [ |
embeds: [ |
382 |
}) |
}) |
383 |
.addField('Reason', reason) |
.addField('Reason', reason) |
384 |
.addField('Muted by', d.tag) |
.addField('Muted by', d.tag) |
385 |
.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*") |
386 |
.addField('User ID', member.user.id) |
.addField('User ID', member.user.id) |
387 |
.addField('Hardmute', hard ? 'Yes' : 'No') |
.addField('Hardmute', hard ? 'Yes' : 'No') |
388 |
.setFooter({ |
.setFooter({ |