blob: a0acd34c52f816cef11e24813a9b04849369b3ff [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";
pineafan0f5cc782022-08-12 21:55:42 +01005
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01007
PineaFan752af462022-12-31 21:59:38 +00008export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) {
TheCodedProfe35c4592023-02-20 12:47:12 -05009 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000010 if (before.guild.id === "684492926528651336") {
11 await client.database.premium.checkAllPremium(after);
TheCodedProf94ff6de2023-02-22 17:47:26 -050012 }
TheCodedProfe35c4592023-02-20 12:47:12 -050013
Skyler Greyda16adf2023-03-05 10:22:12 +000014 if (!before.roles.cache.equals(after.roles.cache)) {
15 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberRoleUpdate)).filter(
16 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id
17 )[0];
TheCodedProf1807fb32023-02-20 14:33:48 -050018 if (!auditLog) return;
19 if (client.noLog.includes(`${after.guild.id}${after.id}${auditLog.id}`)) return;
20 generalException(`${after.guild.id}${after.id}${auditLog.id}`);
21 if (auditLog.executor!.id !== client.user!.id) {
Skyler Greyda16adf2023-03-05 10:22:12 +000022 const rolesAdded = after.roles.cache.filter((role) => !before.roles.cache.has(role.id));
23 const rolesRemoved = before.roles.cache.filter((role) => !after.roles.cache.has(role.id));
TheCodedProf1807fb32023-02-20 14:33:48 -050024 let displayName = "Roles Removed";
25 let color = NucleusColors.red;
26 let emoji = "GUILD.ROLES.DELETE";
Skyler Greyda16adf2023-03-05 10:22:12 +000027 if (rolesAdded.size > 0 && rolesRemoved.size > 0) {
28 displayName = "Roles Changed";
29 color = NucleusColors.yellow;
30 emoji = "GUILD.ROLES.EDIT";
31 } else if (rolesAdded.size > 0) {
32 displayName = "Roles Added";
33 color = NucleusColors.green;
34 emoji = "GUILD.ROLES.CREATE";
35 }
36 const removedEntry = rolesRemoved.map((role) => role.id);
37 const addedEntry = rolesAdded.map((role) => role.id);
TheCodedProf1807fb32023-02-20 14:33:48 -050038
39 let list = {
40 memberId: entry(after.id, `\`${after.id}\``),
Skyler Greyda16adf2023-03-05 10:22:12 +000041 name: entry(after.user.id, renderUser(after.user))
TheCodedProf1807fb32023-02-20 14:33:48 -050042 };
43
44 if (rolesAdded.size > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +000045 list = Object.assign(list, {
46 rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", "))
47 });
TheCodedProf1807fb32023-02-20 14:33:48 -050048 }
49 if (rolesRemoved.size > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +000050 list = Object.assign(list, {
51 rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", "))
52 });
TheCodedProf1807fb32023-02-20 14:33:48 -050053 }
54
55 list = Object.assign(list, {
56 changed: entry(Date.now(), renderDelta(Date.now())),
57 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
58 });
59
60 let data: LoggerOptions = {
61 meta: {
62 type: "memberUpdate",
63 displayName: displayName,
64 calculateType: "guildMemberUpdate",
65 color: color,
66 emoji: emoji,
67 timestamp: Date.now()
68 },
69 list: {},
70 hidden: {
71 guild: after.guild.id
72 }
73 };
74
Skyler Greyda16adf2023-03-05 10:22:12 +000075 if (rolesAdded.size > 0) {
76 list = Object.assign(list, {
77 rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", "))
78 });
TheCodedProf1807fb32023-02-20 14:33:48 -050079 }
Skyler Greyda16adf2023-03-05 10:22:12 +000080 if (rolesRemoved.size > 0) {
81 list = Object.assign(list, {
82 rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", "))
83 });
TheCodedProf1807fb32023-02-20 14:33:48 -050084 }
Skyler Greyda16adf2023-03-05 10:22:12 +000085 data = Object.assign(data, { list: list });
TheCodedProf1807fb32023-02-20 14:33:48 -050086 log(data);
87 }
88 }
Skyler Greyda16adf2023-03-05 10:22:12 +000089 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate)).filter(
90 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id
91 )[0];
PineaFanc4d6c3f2023-01-19 12:17:25 +000092 if (!auditLog) return;
93 if (auditLog.executor!.id === client.user!.id) return;
94 if (before.nickname !== after.nickname) {
95 await client.database.history.create(
96 "nickname",
97 after.guild.id,
98 after.user,
99 auditLog.executor,
100 null,
101 before.nickname ?? before.user.username,
102 after.nickname ?? after.user.username
103 );
104 const data = {
105 meta: {
106 type: "memberUpdate",
107 displayName: "Nickname Changed",
108 calculateType: "guildMemberUpdate",
109 color: NucleusColors.yellow,
110 emoji: "PUNISH.NICKNAME.YELLOW",
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)),
116 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
117 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500118 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000119 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
120 },
121 hidden: {
122 guild: after.guild.id
123 }
124 };
125 log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -0500126 }
127 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -0500128 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
pineafan96228bd2023-02-21 14:22:55 +0000129 new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000130 ) {
131 await client.database.history.create(
132 "mute",
133 after.guild.id,
134 after.user,
135 auditLog.executor,
136 auditLog.reason,
137 null,
138 null,
139 null
140 );
141 const data = {
142 meta: {
143 type: "memberMute",
144 displayName: "Muted",
145 calculateType: "guildMemberPunish",
146 color: NucleusColors.yellow,
147 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500148 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000149 },
150 list: {
151 memberId: entry(after.id, `\`${after.id}\``),
152 name: entry(after.user.id, renderUser(after.user)),
153 mutedUntil: entry(
154 after.communicationDisabledUntilTimestamp,
155 renderDelta(after.communicationDisabledUntilTimestamp!)
156 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500157 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000158 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
159 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
160 },
161 hidden: {
162 guild: after.guild.id
163 }
164 };
165 log(data);
166 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
167 guild: after.guild.id,
168 user: after.id,
169 expires: after.communicationDisabledUntilTimestamp
170 });
TheCodedProfe35c4592023-02-20 12:47:12 -0500171 }
172 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000173 after.communicationDisabledUntil === null &&
174 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500175 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000176 ) {
177 await client.database.history.create(
178 "unmute",
179 after.guild.id,
180 after.user,
181 auditLog.executor,
182 null,
183 null,
184 null,
185 null
186 );
187 const data = {
188 meta: {
189 type: "memberUnmute",
190 displayName: "Unmuted",
191 calculateType: "guildMemberPunish",
192 color: NucleusColors.green,
193 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500194 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000195 },
196 list: {
197 memberId: entry(after.id, `\`${after.id}\``),
198 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500199 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000200 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
201 },
202 hidden: {
203 guild: after.guild.id
204 }
205 };
206 log(data);
207 client.database.eventScheduler.cancel("naturalUnmute", {
208 guild: after.guild.id,
209 user: after.id,
210 expires: before.communicationDisabledUntilTimestamp
211 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100212 }
pineafane625d782022-05-09 18:04:32 +0100213}