/[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 61 by rakin, Mon Jul 29 17:28:26 2024 UTC revision 106 by rakin, Mon Jul 29 17:28:37 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, 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);
# Line 133  class Logger { Line 135  class Logger {
135          }, ban);          }, ban);
136      }      }
137    
138        logSoftBan(banOptions: BanOptions, guild: Guild, user: User, model: Punishment) {
139            this.channel(async (channel) => {
140                let r = '*No reason provided*';
141    
142                const auditLog = (await guild.fetchAuditLogs({
143                    limit: 1,
144                    type: 'MEMBER_BAN_ADD',
145                })).entries.first();        
146    
147                if (banOptions.reason) {
148                    r = banOptions.reason;
149                }
150                else if (auditLog) {
151                    console.log(auditLog);  
152                    const { target, reason } = await auditLog;
153    
154                    if (target!.id === user.id && reason) {
155                        r = await reason;
156                    }
157                }
158    
159                await channel.send({
160                    embeds: [
161                        new MessageEmbed()
162                        .setColor('#f14a60')
163                        .setTitle("A user was softbanned")
164                        .setAuthor({
165                            name: user.tag,
166                            iconURL: user.displayAvatarURL(),
167                        })
168                        .addField('Reason', r)
169                        .addField('Softbanned by', model.get().mod_tag)
170                        .addField('User ID', user.id)
171                        .setFooter({
172                            text: "Softbanned",
173                        })
174                        .setTimestamp()
175                    ]
176                });
177            }, {
178                guild
179            });
180        }
181    
182        logTempBan(banOptions: BanOptions, guild: Guild, user: User, model: Punishment) {
183            this.channel(async (channel) => {
184                let r = '*No reason provided*';
185    
186                const auditLog = (await guild.fetchAuditLogs({
187                    limit: 1,
188                    type: 'MEMBER_BAN_ADD',
189                })).entries.first();        
190    
191                if (banOptions.reason) {
192                    r = banOptions.reason;
193                }
194                else if (auditLog) {
195                    console.log(auditLog);  
196                    const { target, reason } = await auditLog;
197    
198                    if (target!.id === user.id && reason) {
199                        r = await reason;
200                    }
201                }
202    
203                await channel.send({
204                    embeds: [
205                        new MessageEmbed()
206                        .setColor('#f14a60')
207                        .setTitle("A user was temporarily banned")
208                        .setAuthor({
209                            name: user.tag,
210                            iconURL: user.displayAvatarURL(),
211                        })
212                        .addField('Reason', r)
213                        .addField('Banned by', model.get().mod_tag)
214                        .addField('User ID', user.id)
215                        .addField('Duration', ms(model.get().meta?.time))
216                        .setFooter({
217                            text: "Temporarily banned",
218                        })
219                        .setTimestamp()
220                    ]
221                });
222            }, {
223                guild
224            });
225        }
226    
227      logUnbanned(ban: GuildBan) {      logUnbanned(ban: GuildBan) {
228          this.channel(async (channel) => {          this.channel(async (channel) => {
229              await channel.send({              await channel.send({
# Line 274  class Logger { Line 365  class Logger {
365          }, member);          }, member);
366      }      }
367    
368      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) {
369          if ((member as GuildMember).user)          if ((member as GuildMember).user)
370              member = (member as GuildMember).user;              member = (member as GuildMember).user;
371    
# Line 288  class Logger { Line 379  class Logger {
379                          name: (member as User).tag,                          name: (member as User).tag,
380                          iconURL: member.displayAvatarURL(),                          iconURL: member.displayAvatarURL(),
381                      })                      })
382                      .addField('Reason', reason)                      .addField('Reason', reason ?? '*No reason provided*')
383                      .addField('Warned by', d.tag)                      .addField('Warned by', d.tag)
384                      .addField('User ID', member.id)                      .addField('User ID', member.id)
385                      .addField('Warning ID', id + '')                      .addField('Case ID', id + '')
386                      .setFooter({                      .setFooter({
387                          text: "Warned",                          text: "Warned",
388                      })                      })

Legend:
Removed from v.61  
changed lines
  Added in v.106

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26