blob: 5788f610cd91cb91ac9f9725af055cee2d9b6931 [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;
10
TheCodedProf1807fb32023-02-20 14:33:48 -050011 if(!before.roles.cache.equals(after.roles.cache)) {
12 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberRoleUpdate))
13 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
14 if (!auditLog) return;
15 if (client.noLog.includes(`${after.guild.id}${after.id}${auditLog.id}`)) return;
16 generalException(`${after.guild.id}${after.id}${auditLog.id}`);
17 if (auditLog.executor!.id !== client.user!.id) {
18 const rolesAdded = after.roles.cache.filter(role => !before.roles.cache.has(role.id));
19 const rolesRemoved = before.roles.cache.filter(role => !after.roles.cache.has(role.id));
20 let displayName = "Roles Removed";
21 let color = NucleusColors.red;
22 let emoji = "GUILD.ROLES.DELETE";
23 if(rolesAdded.size > 0 && rolesRemoved.size > 0) {displayName = "Roles Changed"; color = NucleusColors.yellow; emoji = "GUILD.ROLES.EDIT";}
24 else if(rolesAdded.size > 0) {displayName = "Roles Added"; color = NucleusColors.green; emoji = "GUILD.ROLES.CREATE";}
25 const removedEntry = rolesRemoved.map(role => role.id);
26 const addedEntry = rolesAdded.map(role => role.id);
27
28 let list = {
29 memberId: entry(after.id, `\`${after.id}\``),
30 name: entry(after.user.id, renderUser(after.user)),
31 };
32
33 if (rolesAdded.size > 0) {
34 list = Object.assign(list, {rolesAdded: entry(addedEntry, addedEntry.map(id => `<@&${id}>`).join(", "))});
35 }
36 if (rolesRemoved.size > 0) {
37 list = Object.assign(list, {rolesRemoved: entry(removedEntry, removedEntry.map(id => `<@&${id}>`).join(", "))});
38 }
39
40 list = Object.assign(list, {
41 changed: entry(Date.now(), renderDelta(Date.now())),
42 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
43 });
44
45 let data: LoggerOptions = {
46 meta: {
47 type: "memberUpdate",
48 displayName: displayName,
49 calculateType: "guildMemberUpdate",
50 color: color,
51 emoji: emoji,
52 timestamp: Date.now()
53 },
54 list: {},
55 hidden: {
56 guild: after.guild.id
57 }
58 };
59
60 if(rolesAdded.size > 0) {
61 list = Object.assign(list, {rolesAdded: entry(addedEntry, addedEntry.map(id => `<@&${id}>`).join(", "))});
62 }
63 if(rolesRemoved.size > 0) {
64 list = Object.assign(list, {rolesRemoved: entry(removedEntry, removedEntry.map(id => `<@&${id}>`).join(", "))});
65 }
66 data = Object.assign(data, {list: list});
67 log(data);
68 }
69 }
TheCodedProfe35c4592023-02-20 12:47:12 -050070 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate))
PineaFanc4d6c3f2023-01-19 12:17:25 +000071 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
72 if (!auditLog) return;
73 if (auditLog.executor!.id === client.user!.id) return;
74 if (before.nickname !== after.nickname) {
75 await client.database.history.create(
76 "nickname",
77 after.guild.id,
78 after.user,
79 auditLog.executor,
80 null,
81 before.nickname ?? before.user.username,
82 after.nickname ?? after.user.username
83 );
84 const data = {
85 meta: {
86 type: "memberUpdate",
87 displayName: "Nickname Changed",
88 calculateType: "guildMemberUpdate",
89 color: NucleusColors.yellow,
90 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050091 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000092 },
93 list: {
94 memberId: entry(after.id, `\`${after.id}\``),
95 name: entry(after.user.id, renderUser(after.user)),
96 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
97 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -050098 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +000099 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
100 },
101 hidden: {
102 guild: after.guild.id
103 }
104 };
105 log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -0500106 }
107 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -0500108 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
pineafan96228bd2023-02-21 14:22:55 +0000109 new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000110 ) {
111 await client.database.history.create(
112 "mute",
113 after.guild.id,
114 after.user,
115 auditLog.executor,
116 auditLog.reason,
117 null,
118 null,
119 null
120 );
121 const data = {
122 meta: {
123 type: "memberMute",
124 displayName: "Muted",
125 calculateType: "guildMemberPunish",
126 color: NucleusColors.yellow,
127 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500128 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000129 },
130 list: {
131 memberId: entry(after.id, `\`${after.id}\``),
132 name: entry(after.user.id, renderUser(after.user)),
133 mutedUntil: entry(
134 after.communicationDisabledUntilTimestamp,
135 renderDelta(after.communicationDisabledUntilTimestamp!)
136 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500137 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000138 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
139 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
140 },
141 hidden: {
142 guild: after.guild.id
143 }
144 };
145 log(data);
146 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
147 guild: after.guild.id,
148 user: after.id,
149 expires: after.communicationDisabledUntilTimestamp
150 });
TheCodedProfe35c4592023-02-20 12:47:12 -0500151 }
152 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000153 after.communicationDisabledUntil === null &&
154 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500155 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000156 ) {
157 await client.database.history.create(
158 "unmute",
159 after.guild.id,
160 after.user,
161 auditLog.executor,
162 null,
163 null,
164 null,
165 null
166 );
167 const data = {
168 meta: {
169 type: "memberUnmute",
170 displayName: "Unmuted",
171 calculateType: "guildMemberPunish",
172 color: NucleusColors.green,
173 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500174 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000175 },
176 list: {
177 memberId: entry(after.id, `\`${after.id}\``),
178 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500179 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000180 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
181 },
182 hidden: {
183 guild: after.guild.id
184 }
185 };
186 log(data);
187 client.database.eventScheduler.cancel("naturalUnmute", {
188 guild: after.guild.id,
189 user: after.id,
190 expires: before.communicationDisabledUntilTimestamp
191 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 }
pineafane625d782022-05-09 18:04:32 +0100193}