blob: 242b10ad7c024aa2339b69f739d356b1f813fd35 [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";
TheCodedProfe35c4592023-02-20 12:47:12 -05003import type { LoggerOptions } from "../utils/log.js";
TheCodedProf1807fb32023-02-20 14:33:48 -05004import { generalException } from "../utils/createTemporaryStorage.js";
pineafan1e462ab2023-03-07 21:34:06 +00005import { doMemberChecks } from "../reflex/scanners.js";
pineafan0f5cc782022-08-12 21:55:42 +01006
pineafan63fc5e22022-08-04 22:04:10 +01007export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01008
PineaFan752af462022-12-31 21:59:38 +00009export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) {
TheCodedProfe35c4592023-02-20 12:47:12 -050010 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000011 if (before.guild.id === "684492926528651336") {
12 await client.database.premium.checkAllPremium(after);
TheCodedProf94ff6de2023-02-22 17:47:26 -050013 }
TheCodedProfe35c4592023-02-20 12:47:12 -050014
Skyler Greye9c3ef62023-03-09 14:09:00 +000015 if (before.displayAvatarURL({forceStatic: true}) !== after.displayAvatarURL({forceStatic: true})) await doMemberChecks(after);
Skyler Greyda16adf2023-03-05 10:22:12 +000016 if (!before.roles.cache.equals(after.roles.cache)) {
17 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberRoleUpdate)).filter(
18 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id
19 )[0];
TheCodedProf1807fb32023-02-20 14:33:48 -050020 if (!auditLog) return;
21 if (client.noLog.includes(`${after.guild.id}${after.id}${auditLog.id}`)) return;
22 generalException(`${after.guild.id}${after.id}${auditLog.id}`);
23 if (auditLog.executor!.id !== client.user!.id) {
Skyler Greyda16adf2023-03-05 10:22:12 +000024 const rolesAdded = after.roles.cache.filter((role) => !before.roles.cache.has(role.id));
25 const rolesRemoved = before.roles.cache.filter((role) => !after.roles.cache.has(role.id));
TheCodedProf1807fb32023-02-20 14:33:48 -050026 let displayName = "Roles Removed";
27 let color = NucleusColors.red;
28 let emoji = "GUILD.ROLES.DELETE";
Skyler Greyda16adf2023-03-05 10:22:12 +000029 if (rolesAdded.size > 0 && rolesRemoved.size > 0) {
30 displayName = "Roles Changed";
31 color = NucleusColors.yellow;
32 emoji = "GUILD.ROLES.EDIT";
33 } else if (rolesAdded.size > 0) {
34 displayName = "Roles Added";
35 color = NucleusColors.green;
36 emoji = "GUILD.ROLES.CREATE";
37 }
38 const removedEntry = rolesRemoved.map((role) => role.id);
39 const addedEntry = rolesAdded.map((role) => role.id);
TheCodedProf1807fb32023-02-20 14:33:48 -050040
41 let list = {
42 memberId: entry(after.id, `\`${after.id}\``),
Skyler Greyda16adf2023-03-05 10:22:12 +000043 name: entry(after.user.id, renderUser(after.user))
TheCodedProf1807fb32023-02-20 14:33:48 -050044 };
45
46 if (rolesAdded.size > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +000047 list = Object.assign(list, {
48 rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", "))
49 });
TheCodedProf1807fb32023-02-20 14:33:48 -050050 }
51 if (rolesRemoved.size > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +000052 list = Object.assign(list, {
53 rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", "))
54 });
TheCodedProf1807fb32023-02-20 14:33:48 -050055 }
56
57 list = Object.assign(list, {
58 changed: entry(Date.now(), renderDelta(Date.now())),
59 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
60 });
61
62 let data: LoggerOptions = {
63 meta: {
64 type: "memberUpdate",
65 displayName: displayName,
66 calculateType: "guildMemberUpdate",
67 color: color,
68 emoji: emoji,
69 timestamp: Date.now()
70 },
71 list: {},
72 hidden: {
73 guild: after.guild.id
74 }
75 };
76
Skyler Greyda16adf2023-03-05 10:22:12 +000077 if (rolesAdded.size > 0) {
78 list = Object.assign(list, {
79 rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", "))
80 });
TheCodedProf1807fb32023-02-20 14:33:48 -050081 }
Skyler Greyda16adf2023-03-05 10:22:12 +000082 if (rolesRemoved.size > 0) {
83 list = Object.assign(list, {
84 rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", "))
85 });
TheCodedProf1807fb32023-02-20 14:33:48 -050086 }
Skyler Greyda16adf2023-03-05 10:22:12 +000087 data = Object.assign(data, { list: list });
Skyler Greyf4f21c42023-03-08 14:36:29 +000088 await log(data);
TheCodedProf1807fb32023-02-20 14:33:48 -050089 }
90 }
Skyler Greyda16adf2023-03-05 10:22:12 +000091 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate)).filter(
92 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id
93 )[0];
PineaFanc4d6c3f2023-01-19 12:17:25 +000094 if (!auditLog) return;
95 if (auditLog.executor!.id === client.user!.id) return;
96 if (before.nickname !== after.nickname) {
Skyler Grey0d885222023-03-08 21:46:37 +000097 await doMemberChecks(after);
PineaFanc4d6c3f2023-01-19 12:17:25 +000098 await client.database.history.create(
99 "nickname",
100 after.guild.id,
101 after.user,
102 auditLog.executor,
103 null,
104 before.nickname ?? before.user.username,
105 after.nickname ?? after.user.username
106 );
107 const data = {
108 meta: {
109 type: "memberUpdate",
110 displayName: "Nickname Changed",
111 calculateType: "guildMemberUpdate",
112 color: NucleusColors.yellow,
113 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500114 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000115 },
116 list: {
PineaFanc4d6c3f2023-01-19 12:17:25 +0000117 name: entry(after.user.id, renderUser(after.user)),
118 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
119 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500120 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000121 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
122 },
123 hidden: {
124 guild: after.guild.id
125 }
126 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000127 await log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -0500128 }
129 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -0500130 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
pineafan96228bd2023-02-21 14:22:55 +0000131 new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000132 ) {
133 await client.database.history.create(
134 "mute",
135 after.guild.id,
136 after.user,
137 auditLog.executor,
138 auditLog.reason,
139 null,
140 null,
141 null
142 );
143 const data = {
144 meta: {
145 type: "memberMute",
146 displayName: "Muted",
147 calculateType: "guildMemberPunish",
148 color: NucleusColors.yellow,
149 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500150 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000151 },
152 list: {
153 memberId: entry(after.id, `\`${after.id}\``),
154 name: entry(after.user.id, renderUser(after.user)),
155 mutedUntil: entry(
156 after.communicationDisabledUntilTimestamp,
157 renderDelta(after.communicationDisabledUntilTimestamp!)
158 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500159 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000160 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
161 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
162 },
163 hidden: {
164 guild: after.guild.id
165 }
166 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000167 await log(data);
Skyler Greyc6a93662023-03-08 16:13:39 +0000168 await client.database.eventScheduler.schedule(
169 "naturalUnmute",
170 after.communicationDisabledUntil?.toISOString()!,
171 {
172 guild: after.guild.id,
173 user: after.id,
174 expires: after.communicationDisabledUntilTimestamp
175 }
176 );
TheCodedProfe35c4592023-02-20 12:47:12 -0500177 }
178 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000179 after.communicationDisabledUntil === null &&
180 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500181 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000182 ) {
183 await client.database.history.create(
184 "unmute",
185 after.guild.id,
186 after.user,
187 auditLog.executor,
188 null,
189 null,
190 null,
191 null
192 );
193 const data = {
194 meta: {
195 type: "memberUnmute",
196 displayName: "Unmuted",
197 calculateType: "guildMemberPunish",
198 color: NucleusColors.green,
199 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500200 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000201 },
202 list: {
203 memberId: entry(after.id, `\`${after.id}\``),
204 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500205 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000206 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
207 },
208 hidden: {
209 guild: after.guild.id
210 }
211 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000212 await log(data);
213 await client.database.eventScheduler.cancel("naturalUnmute", {
PineaFanc4d6c3f2023-01-19 12:17:25 +0000214 guild: after.guild.id,
215 user: after.id,
216 expires: before.communicationDisabledUntilTimestamp
217 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100218 }
pineafane625d782022-05-09 18:04:32 +0100219}