blob: 81b23377fda7e11ddfbd0a6295167b97df2769db [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { AuditLogEvent, GuildAuditLogsEntry, GuildMember } from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00002import type { NucleusClient } from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01003
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01005
PineaFan752af462022-12-31 21:59:38 +00006export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) {
TheCodedProf6ec331b2023-02-20 12:13:06 -05007 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
8 if (!await isLogging(after.guild.id, "memberUpdate")) return;
PineaFanc4d6c3f2023-01-19 12:17:25 +00009 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.EmojiCreate))
10 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
11 if (!auditLog) return;
12 if (auditLog.executor!.id === client.user!.id) return;
13 if (before.nickname !== after.nickname) {
14 await client.database.history.create(
15 "nickname",
16 after.guild.id,
17 after.user,
18 auditLog.executor,
19 null,
20 before.nickname ?? before.user.username,
21 after.nickname ?? after.user.username
22 );
23 const data = {
24 meta: {
25 type: "memberUpdate",
26 displayName: "Nickname Changed",
27 calculateType: "guildMemberUpdate",
28 color: NucleusColors.yellow,
29 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050030 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000031 },
32 list: {
33 memberId: entry(after.id, `\`${after.id}\``),
34 name: entry(after.user.id, renderUser(after.user)),
35 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
36 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -050037 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +000038 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
39 },
40 hidden: {
41 guild: after.guild.id
42 }
43 };
44 log(data);
45 } else if (
TheCodedProf6ec331b2023-02-20 12:13:06 -050046 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
47 (after.communicationDisabledUntil ?? 0) > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000048 ) {
49 await client.database.history.create(
50 "mute",
51 after.guild.id,
52 after.user,
53 auditLog.executor,
54 auditLog.reason,
55 null,
56 null,
57 null
58 );
59 const data = {
60 meta: {
61 type: "memberMute",
62 displayName: "Muted",
63 calculateType: "guildMemberPunish",
64 color: NucleusColors.yellow,
65 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050066 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000067 },
68 list: {
69 memberId: entry(after.id, `\`${after.id}\``),
70 name: entry(after.user.id, renderUser(after.user)),
71 mutedUntil: entry(
72 after.communicationDisabledUntilTimestamp,
73 renderDelta(after.communicationDisabledUntilTimestamp!)
74 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -050075 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +000076 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
77 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
78 },
79 hidden: {
80 guild: after.guild.id
81 }
82 };
83 log(data);
84 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
85 guild: after.guild.id,
86 user: after.id,
87 expires: after.communicationDisabledUntilTimestamp
88 });
89 } else if (
90 after.communicationDisabledUntil === null &&
91 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -050092 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +000093 ) {
94 await client.database.history.create(
95 "unmute",
96 after.guild.id,
97 after.user,
98 auditLog.executor,
99 null,
100 null,
101 null,
102 null
103 );
104 const data = {
105 meta: {
106 type: "memberUnmute",
107 displayName: "Unmuted",
108 calculateType: "guildMemberPunish",
109 color: NucleusColors.green,
110 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500111 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000112 },
113 list: {
114 memberId: entry(after.id, `\`${after.id}\``),
115 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500116 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000117 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
118 },
119 hidden: {
120 guild: after.guild.id
121 }
122 };
123 log(data);
124 client.database.eventScheduler.cancel("naturalUnmute", {
125 guild: after.guild.id,
126 user: after.id,
127 expires: before.communicationDisabledUntilTimestamp
128 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 }
pineafane625d782022-05-09 18:04:32 +0100130}