/[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 124 by rakin, Mon Jul 29 17:28:41 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 95  class Logger { Line 97  class Logger {
97          this.channel(async (channel) => {          this.channel(async (channel) => {
98              let r = '*No reason provided*';              let r = '*No reason provided*';
99    
100                const auditLog = (await ban.guild.fetchAuditLogs({
101                    limit: 1,
102                    type: 'MEMBER_BAN_ADD',
103                })).entries.first();          
104          
105    
106              if (ban.reason) {              if (ban.reason) {
107                  r = ban.reason;                  r = ban.reason;
108              }              }
109                else if (auditLog) {
110                    console.log(auditLog);  
111                    const { target, reason } = await auditLog;
112    
113                    if (target!.id === ban.user.id && reason) {
114                        r = await reason;
115                    }
116                }
117    
118              await channel.send({              await channel.send({
119                  embeds: [                  embeds: [
# Line 119  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 214  class Logger { Line 319  class Logger {
319          }, member);          }, member);
320      }      }
321    
322      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) {
323          this.channel(async (channel) => {          this.channel(async (channel) => {
324              await channel.send({              await channel.send({
325                  embeds: [                  embeds: [
# Line 229  class Logger { Line 334  class Logger {
334                      .addField('Muted by', d.tag)                      .addField('Muted by', d.tag)
335                      .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*")
336                      .addField('User ID', member.user.id)                      .addField('User ID', member.user.id)
337                        .addField('Hardmute', hard ? 'Yes' : 'No')
338                      .setFooter({                      .setFooter({
339                          text: "Muted",                          text: "Muted",
340                      })                      })
# Line 260  class Logger { Line 366  class Logger {
366          }, member);          }, member);
367      }      }
368    
369      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) {
370          if ((member as GuildMember).user)          if ((member as GuildMember).user)
371              member = (member as GuildMember).user;              member = (member as GuildMember).user;
372    
# Line 274  class Logger { Line 380  class Logger {
380                          name: (member as User).tag,                          name: (member as User).tag,
381                          iconURL: member.displayAvatarURL(),                          iconURL: member.displayAvatarURL(),
382                      })                      })
383                      .addField('Reason', reason)                      .addField('Reason', reason ?? '*No reason provided*')
384                      .addField('Warned by', d.tag)                      .addField('Warned by', d.tag)
385                      .addField('User ID', member.id)                      .addField('User ID', member.id)
386                      .addField('Warning ID', id + '')                      .addField('Case ID', id + '')
387                      .setFooter({                      .setFooter({
388                          text: "Warned",                          text: "Warned",
389                      })                      })

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26