/[sudobot]/trunk/src/automod/Logger.ts
ViewVC logotype

Diff of /trunk/src/automod/Logger.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 58 by rakin, Mon Jul 29 17:28:25 2024 UTC revision 428 by rakin, Mon Jul 29 17:30:11 2024 UTC
# Line 1  Line 1 
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;
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: Message | GuildBan) {      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 => c.id === channelID) as TextChannel;          let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel;
48    
49          if (channel) {          if (channel) {
50              return callback(channel);              return callback(channel);
51          }          }
52      }      }
53    
54      channelJoinLeft(callback: (channel: TextChannel) => any, msg: Message | GuildBan) {      channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) {
55          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;
56          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;
57    
58          if (channel) {          if (channel) {
59              return callback(channel);              return callback(channel);
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({
# Line 95  class Logger { Line 140  class Logger {
140          this.channel(async (channel) => {          this.channel(async (channel) => {
141              let r = '*No reason provided*';              let r = '*No reason provided*';
142    
143                const auditLog = (await ban.guild.fetchAuditLogs({
144                    limit: 1,
145                    type: 'MEMBER_BAN_ADD',
146                })).entries.first();          
147          
148              if (ban.reason) {              if (ban.reason) {
149                  r = ban.reason;                  r = ban.reason;
150              }              }
151                else if (auditLog) {
152                    console.log(auditLog);  
153                    const { target, reason } = await auditLog;
154    
155                    if (target!.id === ban.user.id && reason) {
156                        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    
164              await channel.send({              await channel.send({
165                  embeds: [                  embeds: [
# Line 119  class Logger { Line 181  class Logger {
181          }, ban);          }, ban);
182      }      }
183    
184        logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) {
185            this.channel(async (channel) => {
186                let r = '*No reason provided*';
187    
188                const auditLog = (await guild.fetchAuditLogs({
189                    limit: 1,
190                    type: 'MEMBER_BAN_ADD',
191                })).entries.first();        
192    
193                if (banOptions.reason) {
194                    r = banOptions.reason;
195                }
196                else if (auditLog) {
197                    console.log(auditLog);  
198                    const { target, reason } = await auditLog;
199    
200                    if (target!.id === user.id && reason) {
201                        r = reason.replace(/^\[SOFTBAN\]/, '');
202                    }
203                }
204    
205                await channel.send({
206                    embeds: [
207                        new MessageEmbed()
208                        .setColor('#f14a60')
209                        .setTitle("A user was softbanned")
210                        .setAuthor({
211                            name: user.tag,
212                            iconURL: user.displayAvatarURL(),
213                        })
214                        .addField('Reason', r)
215                        .addField('Softbanned by', model.mod_tag)
216                        .addField('User ID', user.id)
217                        .setFooter({
218                            text: "Softbanned",
219                        })
220                        .setTimestamp()
221                    ]
222                });
223            }, {
224                guild
225            });
226        }
227    
228        logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) {
229            this.channel(async (channel) => {
230                let r = '*No reason provided*';
231    
232                const auditLog = (await guild.fetchAuditLogs({
233                    limit: 1,
234                    type: 'MEMBER_BAN_ADD',
235                })).entries.first();        
236    
237                if (banOptions.reason) {
238                    r = banOptions.reason;
239                }
240                else if (auditLog) {
241                    console.log(auditLog);  
242                    const { target, reason } = await auditLog;
243    
244                    if (target!.id === user.id && reason) {
245                        r = reason.replace(/^\[TEMPBAN\]/, '');
246                    }
247                }
248    
249                await channel.send({
250                    embeds: [
251                        new MessageEmbed()
252                        .setColor('#f14a60')
253                        .setTitle("A user was temporarily banned")
254                        .setAuthor({
255                            name: user.tag,
256                            iconURL: user.displayAvatarURL(),
257                        })
258                        .addField('Reason', r)
259                        .addField('Banned by', model.mod_tag)
260                        .addField('User ID', user.id)
261                        .addField('Duration', ms((model.meta as any).time))
262                        .setFooter({
263                            text: "Temporarily banned",
264                        })
265                        .setTimestamp()
266                    ]
267                });
268            }, {
269                guild
270            });
271        }
272    
273      logUnbanned(ban: GuildBan) {      logUnbanned(ban: GuildBan) {
274          this.channel(async (channel) => {          this.channel(async (channel) => {
275              await channel.send({              await channel.send({
# Line 214  class Logger { Line 365  class Logger {
365          }, member);          }, member);
366      }      }
367    
368      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) {
369          this.channel(async (channel) => {          this.channel(async (channel) => {
370              await channel.send({              await channel.send({
371                  embeds: [                  embeds: [
# Line 227  class Logger { Line 378  class Logger {
378                      })                      })
379                      .addField('Reason', reason)                      .addField('Reason', reason)
380                      .addField('Muted by', d.tag)                      .addField('Muted by', d.tag)
381                      .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*")
382                      .addField('User ID', member.user.id)                      .addField('User ID', member.user.id)
383                        .addField('Hardmute', hard ? 'Yes' : 'No')
384                      .setFooter({                      .setFooter({
385                          text: "Muted",                          text: "Muted",
386                      })                      })
# Line 260  class Logger { Line 412  class Logger {
412          }, member);          }, member);
413      }      }
414    
415      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) {
416          if ((member as GuildMember).user)          if ((member as GuildMember).user)
417              member = (member as GuildMember).user;              member = (member as GuildMember).user;
418    
# Line 274  class Logger { Line 426  class Logger {
426                          name: (member as User).tag,                          name: (member as User).tag,
427                          iconURL: member.displayAvatarURL(),                          iconURL: member.displayAvatarURL(),
428                      })                      })
429                      .addField('Reason', reason)                      .addField('Reason', reason ?? '*No reason provided*')
430                      .addField('Warned by', d.tag)                      .addField('Warned by', d.tag)
431                      .addField('User ID', member.id)                      .addField('User ID', member.id)
432                      .addField('Warning ID', id + '')                      .addField('Case ID', id + '')
433                      .setFooter({                      .setFooter({
434                          text: "Warned",                          text: "Warned",
435                      })                      })

Legend:
Removed from v.58  
changed lines
  Added in v.428

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26