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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 124 - (show annotations)
Mon Jul 29 17:28:41 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 15004 byte(s)
Added hardmuting support
1 import { roleMention } from '@discordjs/builders';
2 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';
5 import Punishment from '../models/Punishment';
6 import { timeProcess, timeSince } from '../utils/util';
7
8 class Logger {
9 client: DiscordClient;
10
11 constructor(client: DiscordClient) {
12 this.client = client;
13 }
14
15 channel(callback: (channel: TextChannel) => any, msg: any) {
16 let channelID = this.client.config.props[msg.guild!.id].logging_channel;
17 let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel;
18
19 if (channel) {
20 return callback(channel);
21 }
22 }
23
24 channelJoinLeft(callback: (channel: TextChannel) => any, msg: any) {
25 let channelID = this.client.config.props[msg.guild!.id].logging_channel_join_leave;
26 let channel = msg.guild!.channels.cache.find((c: any) => c.id === channelID) as TextChannel;
27
28 if (channel) {
29 return callback(channel);
30 }
31 }
32
33 logEdit(oldMsg: Message, newMsg: Message) {
34 this.channel(async (channel) => {
35 await channel.send({
36 embeds: [
37 new MessageEmbed()
38 .setColor('#007bff')
39 .setTitle('Message Edited in #' + (newMsg.channel as TextChannel).name + " (" + newMsg.channel.id + ")")
40 .setDescription('**-+-+Before**\n' + oldMsg.content + '\n\n**-+-+After**\n' + newMsg.content)
41 .addField('ID', newMsg.id)
42 .setAuthor({
43 name: newMsg.author.tag,
44 iconURL: newMsg.author.displayAvatarURL(),
45 })
46 .setFooter({
47 text: "Edited",
48 })
49 .setTimestamp()
50 ]
51 });
52 }, newMsg);
53 }
54
55 logDelete(msg: Message) {
56 this.channel(async (channel) => {
57 const embed = new MessageEmbed()
58 .setColor('#f14a60')
59 .setTitle('Message Deleted in #' + (msg.channel as TextChannel).name + " (" + msg.channel.id + ")")
60 .setDescription(msg.content)
61 .setAuthor({
62 name: msg.author.tag,
63 iconURL: msg.author.displayAvatarURL(),
64 })
65 .addField('ID', msg.id)
66 .setFooter({
67 text: "Deleted",
68 })
69 .setTimestamp();
70
71 const files: FileOptions[] = [];
72
73 if (msg.attachments.size > 0) {
74 let str = '';
75
76 msg.attachments.forEach(a => {
77 str += `${a.name}\n`;
78 files.push({
79 name: a.name!,
80 attachment: a.proxyURL
81 });
82 });
83
84 embed.addField('Attachments (top)', str);
85 }
86
87 await channel.send({
88 embeds: [
89 embed
90 ],
91 files
92 });
93 }, msg);
94 }
95
96 logBanned(ban: GuildBan) {
97 this.channel(async (channel) => {
98 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) {
107 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({
119 embeds: [
120 new MessageEmbed()
121 .setColor('#f14a60')
122 .setTitle("A user was banned")
123 .setAuthor({
124 name: ban.user.tag,
125 iconURL: ban.user.displayAvatarURL(),
126 })
127 .addField('Reason', r)
128 .addField('User ID', ban.user.id)
129 .setFooter({
130 text: "Banned",
131 })
132 .setTimestamp()
133 ]
134 });
135 }, 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) {
228 this.channel(async (channel) => {
229 await channel.send({
230 embeds: [
231 new MessageEmbed()
232 .setColor('#f14a60')
233 .setTitle("A user was unbanned")
234 .setAuthor({
235 name: ban.user.tag,
236 iconURL: ban.user.displayAvatarURL(),
237 })
238 .addField('User ID', ban.user.id)
239 .setFooter({
240 text: "Unbanned",
241 })
242 .setTimestamp()
243 ]
244 });
245 }, ban);
246 }
247
248 logJoined(member: GuildMember) {
249 this.channelJoinLeft(async (channel) => {
250 await channel.send({
251 embeds: [
252 new MessageEmbed()
253 .setColor('#007bff')
254 .setTitle("New member joined")
255 .setAuthor({
256 name: member.user.tag,
257 iconURL: member.user.displayAvatarURL(),
258 })
259 .setDescription(`<@${member.user.id}> just joined the server!`)
260 .addField('Account Created', `${member.user.createdAt.toLocaleString()} (${timeSince(member.user.createdAt.getTime())})`)
261 .addField('New Account?', (new Date().getTime() - member.user.createdAt.getTime()) <= 3 * 24 * 60 * 60 * 1000 ? ":warning: Yes :warning:" : "No")
262 .addField('Bot?', member.user.bot === true ? 'Yes' : 'No')
263 .addField('User ID', member.user.id)
264 .setFooter({
265 text: "Joined",
266 })
267 .setTimestamp()
268 ]
269 });
270 }, member);
271 }
272
273 logLeft(member: GuildMember) {
274 this.channelJoinLeft(async (channel) => {
275 const roles = await member.roles.cache.filter(role => role.id !== member.guild.id).reduce((acc, val) => ` ${acc} ${roleMention(val.id)}`, '');
276
277 await channel.send({
278 embeds: [
279 new MessageEmbed()
280 .setColor('#f14a60')
281 .setTitle("Member left")
282 .setAuthor({
283 name: member.user.tag,
284 iconURL: member.user.displayAvatarURL(),
285 })
286 .setDescription(`**Roles**\n${roles}`)
287 .addField('Joined at', `${member.joinedAt!.toLocaleString()} (${timeSince(member.joinedAt!.getTime())})`)
288 .addField('User ID', member.user.id)
289 .addField('Bot?', member.user.bot === true ? 'Yes' : 'No')
290 .setFooter({
291 text: "Left",
292 })
293 .setTimestamp()
294 ]
295 });
296 }, member);
297 }
298
299 logBeaned(member: GuildMember, r: string, d: User) {
300 this.channel(async (channel) => {
301 await channel.send({
302 embeds: [
303 new MessageEmbed()
304 .setColor('#007bff')
305 .setTitle("Member beaned")
306 .setAuthor({
307 name: member.user.tag,
308 iconURL: member.user.displayAvatarURL(),
309 })
310 .addField('Reason', r)
311 .addField('Beaned by', d.tag)
312 .addField('User ID', member.user.id)
313 .setFooter({
314 text: "Beaned",
315 })
316 .setTimestamp()
317 ]
318 });
319 }, member);
320 }
321
322 logMute(member: GuildMember, reason: string, timeMs: number | null | undefined, d: User, hard: boolean = true) {
323 this.channel(async (channel) => {
324 await channel.send({
325 embeds: [
326 new MessageEmbed()
327 .setColor('#f14a60')
328 .setTitle("Member muted")
329 .setAuthor({
330 name: member.user.tag,
331 iconURL: member.user.displayAvatarURL(),
332 })
333 .addField('Reason', reason)
334 .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*")
336 .addField('User ID', member.user.id)
337 .addField('Hardmute', hard ? 'Yes' : 'No')
338 .setFooter({
339 text: "Muted",
340 })
341 .setTimestamp()
342 ]
343 });
344 }, member);
345 }
346
347 logUnmute(member: GuildMember, d: User) {
348 this.channel(async (channel) => {
349 await channel.send({
350 embeds: [
351 new MessageEmbed()
352 .setColor('#007bff')
353 .setTitle("Member unmuted")
354 .setAuthor({
355 name: member.user.tag,
356 iconURL: member.user.displayAvatarURL(),
357 })
358 .addField('Unmuted by', d.tag)
359 .addField('User ID', member.user.id)
360 .setFooter({
361 text: "Unmuted",
362 })
363 .setTimestamp()
364 ]
365 });
366 }, member);
367 }
368
369 logWarn(msg: Message | CommandInteraction, member: GuildMember | User, d: User, reason: string | undefined, id: number | string) {
370 if ((member as GuildMember).user)
371 member = (member as GuildMember).user;
372
373 this.channel(async (channel) => {
374 await channel.send({
375 embeds: [
376 new MessageEmbed()
377 .setColor('GOLD')
378 .setTitle("Member warned")
379 .setAuthor({
380 name: (member as User).tag,
381 iconURL: member.displayAvatarURL(),
382 })
383 .addField('Reason', reason ?? '*No reason provided*')
384 .addField('Warned by', d.tag)
385 .addField('User ID', member.id)
386 .addField('Case ID', id + '')
387 .setFooter({
388 text: "Warned",
389 })
390 .setTimestamp()
391 ]
392 });
393 }, msg);
394 }
395
396 logWarndel(msg: Message, member: GuildMember, warn: any, d: User) {
397 this.channel(async (channel) => {
398 await channel.send({
399 embeds: [
400 new MessageEmbed()
401 .setColor('GOLD')
402 .setTitle("Warning deleted")
403 .setAuthor({
404 name: member.user.tag,
405 iconURL: member.user.displayAvatarURL(),
406 })
407 .addField('Warned by', d.tag + '')
408 .addField('Warning ID', warn.id + '')
409 .addField('User ID', member.user.id)
410 .setFooter({
411 text: "Warning Deleted",
412 })
413 .setTimestamp()
414 ]
415 });
416 }, msg);
417 }
418 }
419
420 export default Logger;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26