/[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 227 by rakin, Mon Jul 29 17:29:07 2024 UTC
# Line 1  Line 1 
1  import { roleMention } from '@discordjs/builders';  import { roleMention } from '@discordjs/builders';
2  import { FileOptions, GuildBan, GuildMember, Message, MessageEmbed, TextChannel, User } from 'discord.js';  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 {
# Line 10  class Logger { Line 12  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({
# Line 95  class Logger { Line 110  class Logger {
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: [
# Line 119  class Logger { Line 148  class Logger {
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({
# Line 214  class Logger { Line 332  class Logger {
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: [
# Line 229  class Logger { Line 347  class Logger {
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                      })                      })
# Line 260  class Logger { Line 379  class Logger {
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    
# Line 274  class Logger { Line 393  class Logger {
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                      })                      })

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26