blob: 41a22c1e953f6c28be0090cd7d528f816f84b779 [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: {
PineaFanc4d6c3f2023-01-19 12:17:25 +0000114 name: entry(after.user.id, renderUser(after.user)),
115 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
116 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500117 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000118 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
119 },
120 hidden: {
121 guild: after.guild.id
122 }
123 };
124 log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -0500125 }
126 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -0500127 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
pineafan96228bd2023-02-21 14:22:55 +0000128 new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000129 ) {
130 await client.database.history.create(
131 "mute",
132 after.guild.id,
133 after.user,
134 auditLog.executor,
135 auditLog.reason,
136 null,
137 null,
138 null
139 );
140 const data = {
141 meta: {
142 type: "memberMute",
143 displayName: "Muted",
144 calculateType: "guildMemberPunish",
145 color: NucleusColors.yellow,
146 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500147 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000148 },
149 list: {
150 memberId: entry(after.id, `\`${after.id}\``),
151 name: entry(after.user.id, renderUser(after.user)),
152 mutedUntil: entry(
153 after.communicationDisabledUntilTimestamp,
154 renderDelta(after.communicationDisabledUntilTimestamp!)
155 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500156 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000157 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
158 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
159 },
160 hidden: {
161 guild: after.guild.id
162 }
163 };
164 log(data);
165 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
166 guild: after.guild.id,
167 user: after.id,
168 expires: after.communicationDisabledUntilTimestamp
169 });
TheCodedProfe35c4592023-02-20 12:47:12 -0500170 }
171 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000172 after.communicationDisabledUntil === null &&
173 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500174 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000175 ) {
176 await client.database.history.create(
177 "unmute",
178 after.guild.id,
179 after.user,
180 auditLog.executor,
181 null,
182 null,
183 null,
184 null
185 );
186 const data = {
187 meta: {
188 type: "memberUnmute",
189 displayName: "Unmuted",
190 calculateType: "guildMemberPunish",
191 color: NucleusColors.green,
192 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500193 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000194 },
195 list: {
196 memberId: entry(after.id, `\`${after.id}\``),
197 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500198 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000199 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
200 },
201 hidden: {
202 guild: after.guild.id
203 }
204 };
205 log(data);
206 client.database.eventScheduler.cancel("naturalUnmute", {
207 guild: after.guild.id,
208 user: after.id,
209 expires: before.communicationDisabledUntilTimestamp
210 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100211 }
pineafane625d782022-05-09 18:04:32 +0100212}