/[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 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 338 by rakin, Mon Jul 29 17:29:37 2024 UTC
# Line 1  Line 1 
1  import { GuildBan, GuildMember, Message, MessageEmbed, TextChannel, User } from 'discord.js';  import { roleMention } from '@discordjs/builders';
2    import { formatDuration, intervalToDuration } from 'date-fns';
3    import { BanOptions, CommandInteraction, FileOptions, Guild, GuildBan, GuildMember, Message, MessageEmbed, MessageOptions, MessagePayload, TextChannel, User } from 'discord.js';
4    import ms from 'ms';
5  import DiscordClient from '../client/Client';  import DiscordClient from '../client/Client';
6    import Punishment, { IPunishment } from '../models/Punishment';
7  import { timeProcess, timeSince } from '../utils/util';  import { timeProcess, timeSince } from '../utils/util';
8    
9  class Logger {  class Logger {
# Line 9  class Logger { Line 13  class Logger {
13          this.client = client;          this.client = client;
14      }      }
15    
16      channel(callback: (channel: TextChannel) => any, msg: Message | GuildBan) {      channel(callback: (channel: TextChannel) => any, msg: any) {
17          let channelID = this.client.config.props[msg.guild!.id].logging_channel;          let channelID = this.client.config.props[msg.guild!.id].logging_channel;
18          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;
19    
20          if (channel) {          if (channel) {
21              return callback(channel);              return callback(channel);
22          }          }
23      }      }
24    
25      channelJoinLeft(callback: (channel: TextChannel) => any, msg: Message | GuildBan) {      channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) {
26          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;
27          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;
28    
29          if (channel) {          if (channel) {
30              return callback(channel);              return callback(channel);
31          }          }
32      }      }
33    
34        async send(guild: Guild, messageOptions: MessageOptions | MessagePayload | string) {
35            let channelID = this.client.config.props[guild!.id].logging_channel;
36            let channel = guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel;
37    
38            if (channel) {
39                return await channel.send(messageOptions);
40            }
41        }
42    
43        log(guild: Guild, callback: (channel: TextChannel) => any) {
44            this.channel(callback, { guild });
45        }
46    
47      logEdit(oldMsg: Message, newMsg: Message) {      logEdit(oldMsg: Message, newMsg: Message) {
48          this.channel(async (channel) => {          this.channel(async (channel) => {
49              await channel.send({              await channel.send({
# Line 64  class Logger { Line 81  class Logger {
81                      text: "Deleted",                      text: "Deleted",
82                  })                  })
83                  .setTimestamp();                  .setTimestamp();
84                
85                const files: FileOptions[] = [];
86    
87              if (msg.attachments.size > 0) {              if (msg.attachments.size > 0) {
88                  let str = '';                  let str = '';
89    
90                  msg.attachments.forEach(a => {                  msg.attachments.forEach(a => {
91                      str += `**${a.name}** ${a.url}\n`;                      str += `${a.name}\n`;
92                        files.push({
93                            name: a.name!,
94                            attachment: a.proxyURL
95                        });
96                  });                  });
97    
98                  embed.addField('Attachments', str);                  embed.addField('Attachments (top)', str);
99              }              }
100    
101              await channel.send({              await channel.send({
102                  embeds: [                  embeds: [
103                      embed                      embed
104                  ]                  ],
105                    files
106              });              });
107          }, msg);          }, msg);
108      }      }
# Line 87  class Logger { Line 111  class Logger {
111          this.channel(async (channel) => {          this.channel(async (channel) => {
112              let r = '*No reason provided*';              let r = '*No reason provided*';
113    
114                const auditLog = (await ban.guild.fetchAuditLogs({
115                    limit: 1,
116                    type: 'MEMBER_BAN_ADD',
117                })).entries.first();          
118          
119    
120              if (ban.reason) {              if (ban.reason) {
121                  r = ban.reason;                  r = ban.reason;
122              }              }
123                else if (auditLog) {
124                    console.log(auditLog);  
125                    const { target, reason } = await auditLog;
126    
127                    if (target!.id === ban.user.id && reason) {
128                        r = await reason;
129                    }
130                }
131    
132              await channel.send({              await channel.send({
133                  embeds: [                  embeds: [
# Line 111  class Logger { Line 149  class Logger {
149          }, ban);          }, ban);
150      }      }
151    
152        logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) {
153            this.channel(async (channel) => {
154                let r = '*No reason provided*';
155    
156                const auditLog = (await guild.fetchAuditLogs({
157                    limit: 1,
158                    type: 'MEMBER_BAN_ADD',
159                })).entries.first();        
160    
161                if (banOptions.reason) {
162                    r = banOptions.reason;
163                }
164                else if (auditLog) {
165                    console.log(auditLog);  
166                    const { target, reason } = await auditLog;
167    
168                    if (target!.id === user.id && reason) {
169                        r = await reason;
170                    }
171                }
172    
173                await channel.send({
174                    embeds: [
175                        new MessageEmbed()
176                        .setColor('#f14a60')
177                        .setTitle("A user was softbanned")
178                        .setAuthor({
179                            name: user.tag,
180                            iconURL: user.displayAvatarURL(),
181                        })
182                        .addField('Reason', r)
183                        .addField('Softbanned by', model.mod_tag)
184                        .addField('User ID', user.id)
185                        .setFooter({
186                            text: "Softbanned",
187                        })
188                        .setTimestamp()
189                    ]
190                });
191            }, {
192                guild
193            });
194        }
195    
196        logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: IPunishment) {
197            this.channel(async (channel) => {
198                let r = '*No reason provided*';
199    
200                const auditLog = (await guild.fetchAuditLogs({
201                    limit: 1,
202                    type: 'MEMBER_BAN_ADD',
203                })).entries.first();        
204    
205                if (banOptions.reason) {
206                    r = banOptions.reason;
207                }
208                else if (auditLog) {
209                    console.log(auditLog);  
210                    const { target, reason } = await auditLog;
211    
212                    if (target!.id === user.id && reason) {
213                        r = await reason;
214                    }
215                }
216    
217                await channel.send({
218                    embeds: [
219                        new MessageEmbed()
220                        .setColor('#f14a60')
221                        .setTitle("A user was temporarily banned")
222                        .setAuthor({
223                            name: user.tag,
224                            iconURL: user.displayAvatarURL(),
225                        })
226                        .addField('Reason', r)
227                        .addField('Banned by', model.mod_tag)
228                        .addField('User ID', user.id)
229                        .addField('Duration', ms((model.meta as any).time))
230                        .setFooter({
231                            text: "Temporarily banned",
232                        })
233                        .setTimestamp()
234                    ]
235                });
236            }, {
237                guild
238            });
239        }
240    
241      logUnbanned(ban: GuildBan) {      logUnbanned(ban: GuildBan) {
242          this.channel(async (channel) => {          this.channel(async (channel) => {
243              await channel.send({              await channel.send({
# Line 159  class Logger { Line 286  class Logger {
286    
287      logLeft(member: GuildMember) {      logLeft(member: GuildMember) {
288          this.channelJoinLeft(async (channel) => {          this.channelJoinLeft(async (channel) => {
289                const roles = await member.roles.cache.filter(role => role.id !== member.guild.id).reduce((acc, val) => ` ${acc} ${roleMention(val.id)}`, '');
290    
291              await channel.send({              await channel.send({
292                  embeds: [                  embeds: [
293                      new MessageEmbed()                      new MessageEmbed()
# Line 168  class Logger { Line 297  class Logger {
297                          name: member.user.tag,                          name: member.user.tag,
298                          iconURL: member.user.displayAvatarURL(),                          iconURL: member.user.displayAvatarURL(),
299                      })                      })
300                        .setDescription(`**Roles**\n${roles}`)
301                      .addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`)                      .addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`)
302                      .addField('User ID', member.user.id)                      .addField('User ID', member.user.id)
303                      .addField('Bot?', member.user.bot === true ? 'Yes' : 'No')                      .addField('Bot?', member.user.bot === true ? 'Yes' : 'No')
# Line 203  class Logger { Line 333  class Logger {
333          }, member);          }, member);
334      }      }
335    
336      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) {
337          this.channel(async (channel) => {          this.channel(async (channel) => {
338              await channel.send({              await channel.send({
339                  embeds: [                  embeds: [
# Line 216  class Logger { Line 346  class Logger {
346                      })                      })
347                      .addField('Reason', reason)                      .addField('Reason', reason)
348                      .addField('Muted by', d.tag)                      .addField('Muted by', d.tag)
349                      .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*")
350                      .addField('User ID', member.user.id)                      .addField('User ID', member.user.id)
351                        .addField('Hardmute', hard ? 'Yes' : 'No')
352                      .setFooter({                      .setFooter({
353                          text: "Muted",                          text: "Muted",
354                      })                      })
# Line 249  class Logger { Line 380  class Logger {
380          }, member);          }, member);
381      }      }
382    
383      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) {
384          if ((member as GuildMember).user)          if ((member as GuildMember).user)
385              member = (member as GuildMember).user;              member = (member as GuildMember).user;
386    
# Line 263  class Logger { Line 394  class Logger {
394                          name: (member as User).tag,                          name: (member as User).tag,
395                          iconURL: member.displayAvatarURL(),                          iconURL: member.displayAvatarURL(),
396                      })                      })
397                      .addField('Reason', reason)                      .addField('Reason', reason ?? '*No reason provided*')
398                      .addField('Warned by', d.tag)                      .addField('Warned by', d.tag)
399                      .addField('User ID', member.id)                      .addField('User ID', member.id)
400                      .addField('Warning ID', id + '')                      .addField('Case ID', id + '')
401                      .setFooter({                      .setFooter({
402                          text: "Warned",                          text: "Warned",
403                      })                      })

Legend:
Removed from v.51  
changed lines
  Added in v.338

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26