blob: 721978fd57084a96e9e474d10ff119daf5b975c6 [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;
TheCodedProf94ff6de2023-02-22 17:47:26 -050010 if(before.guild.id === "684492926528651336") {
11 await client.database.premium.checkAllPremium(after)
12 }
TheCodedProfe35c4592023-02-20 12:47:12 -050013
TheCodedProf1807fb32023-02-20 14:33:48 -050014 if(!before.roles.cache.equals(after.roles.cache)) {
15 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberRoleUpdate))
16 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
17 if (!auditLog) return;
18 if (client.noLog.includes(`${after.guild.id}${after.id}${auditLog.id}`)) return;
19 generalException(`${after.guild.id}${after.id}${auditLog.id}`);
20 if (auditLog.executor!.id !== client.user!.id) {
21 const rolesAdded = after.roles.cache.filter(role => !before.roles.cache.has(role.id));
22 const rolesRemoved = before.roles.cache.filter(role => !after.roles.cache.has(role.id));
23 let displayName = "Roles Removed";
24 let color = NucleusColors.red;
25 let emoji = "GUILD.ROLES.DELETE";
26 if(rolesAdded.size > 0 && rolesRemoved.size > 0) {displayName = "Roles Changed"; color = NucleusColors.yellow; emoji = "GUILD.ROLES.EDIT";}
27 else if(rolesAdded.size > 0) {displayName = "Roles Added"; color = NucleusColors.green; emoji = "GUILD.ROLES.CREATE";}
28 const removedEntry = rolesRemoved.map(role => role.id);
29 const addedEntry = rolesAdded.map(role => role.id);
30
31 let list = {
32 memberId: entry(after.id, `\`${after.id}\``),
33 name: entry(after.user.id, renderUser(after.user)),
34 };
35
36 if (rolesAdded.size > 0) {
37 list = Object.assign(list, {rolesAdded: entry(addedEntry, addedEntry.map(id => `<@&${id}>`).join(", "))});
38 }
39 if (rolesRemoved.size > 0) {
40 list = Object.assign(list, {rolesRemoved: entry(removedEntry, removedEntry.map(id => `<@&${id}>`).join(", "))});
41 }
42
43 list = Object.assign(list, {
44 changed: entry(Date.now(), renderDelta(Date.now())),
45 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
46 });
47
48 let data: LoggerOptions = {
49 meta: {
50 type: "memberUpdate",
51 displayName: displayName,
52 calculateType: "guildMemberUpdate",
53 color: color,
54 emoji: emoji,
55 timestamp: Date.now()
56 },
57 list: {},
58 hidden: {
59 guild: after.guild.id
60 }
61 };
62
63 if(rolesAdded.size > 0) {
64 list = Object.assign(list, {rolesAdded: entry(addedEntry, addedEntry.map(id => `<@&${id}>`).join(", "))});
65 }
66 if(rolesRemoved.size > 0) {
67 list = Object.assign(list, {rolesRemoved: entry(removedEntry, removedEntry.map(id => `<@&${id}>`).join(", "))});
68 }
69 data = Object.assign(data, {list: list});
70 log(data);
71 }
72 }
TheCodedProfe35c4592023-02-20 12:47:12 -050073 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate))
PineaFanc4d6c3f2023-01-19 12:17:25 +000074 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
75 if (!auditLog) return;
76 if (auditLog.executor!.id === client.user!.id) return;
77 if (before.nickname !== after.nickname) {
78 await client.database.history.create(
79 "nickname",
80 after.guild.id,
81 after.user,
82 auditLog.executor,
83 null,
84 before.nickname ?? before.user.username,
85 after.nickname ?? after.user.username
86 );
87 const data = {
88 meta: {
89 type: "memberUpdate",
90 displayName: "Nickname Changed",
91 calculateType: "guildMemberUpdate",
92 color: NucleusColors.yellow,
93 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050094 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000095 },
96 list: {
97 memberId: entry(after.id, `\`${after.id}\``),
98 name: entry(after.user.id, renderUser(after.user)),
99 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
100 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500101 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000102 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
103 },
104 hidden: {
105 guild: after.guild.id
106 }
107 };
108 log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -0500109 }
110 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -0500111 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
pineafan96228bd2023-02-21 14:22:55 +0000112 new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000113 ) {
114 await client.database.history.create(
115 "mute",
116 after.guild.id,
117 after.user,
118 auditLog.executor,
119 auditLog.reason,
120 null,
121 null,
122 null
123 );
124 const data = {
125 meta: {
126 type: "memberMute",
127 displayName: "Muted",
128 calculateType: "guildMemberPunish",
129 color: NucleusColors.yellow,
130 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500131 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000132 },
133 list: {
134 memberId: entry(after.id, `\`${after.id}\``),
135 name: entry(after.user.id, renderUser(after.user)),
136 mutedUntil: entry(
137 after.communicationDisabledUntilTimestamp,
138 renderDelta(after.communicationDisabledUntilTimestamp!)
139 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500140 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000141 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
142 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
143 },
144 hidden: {
145 guild: after.guild.id
146 }
147 };
148 log(data);
149 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
150 guild: after.guild.id,
151 user: after.id,
152 expires: after.communicationDisabledUntilTimestamp
153 });
TheCodedProfe35c4592023-02-20 12:47:12 -0500154 }
155 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000156 after.communicationDisabledUntil === null &&
157 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500158 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000159 ) {
160 await client.database.history.create(
161 "unmute",
162 after.guild.id,
163 after.user,
164 auditLog.executor,
165 null,
166 null,
167 null,
168 null
169 );
170 const data = {
171 meta: {
172 type: "memberUnmute",
173 displayName: "Unmuted",
174 calculateType: "guildMemberPunish",
175 color: NucleusColors.green,
176 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500177 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000178 },
179 list: {
180 memberId: entry(after.id, `\`${after.id}\``),
181 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500182 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000183 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
184 },
185 hidden: {
186 guild: after.guild.id
187 }
188 };
189 log(data);
190 client.database.eventScheduler.cancel("naturalUnmute", {
191 guild: after.guild.id,
192 user: after.id,
193 expires: before.communicationDisabledUntilTimestamp
194 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 }
pineafane625d782022-05-09 18:04:32 +0100196}