blob: 412ac963014bc704cc5dbdba6d65388bae9218df [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";
pineafan0f5cc782022-08-12 21:55:42 +01004
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01006
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) {
TheCodedProfe35c4592023-02-20 12:47:12 -05008 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
9
10 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate))
PineaFanc4d6c3f2023-01-19 12:17:25 +000011 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
12 if (!auditLog) return;
13 if (auditLog.executor!.id === client.user!.id) return;
TheCodedProfe35c4592023-02-20 12:47:12 -050014 if(!before.roles.cache.equals(after.roles.cache)) {
15 let rolesAdded = after.roles.cache.filter(role => !before.roles.cache.has(role.id));
16 let rolesRemoved = before.roles.cache.filter(role => !after.roles.cache.has(role.id));
17 let displayName = "Roles Removed";
18 let color = NucleusColors.red;
19 let emoji = "GUILD.ROLES.DELETE";
20 if(rolesAdded.size > 0 && rolesRemoved.size > 0) {displayName = "Roles Changed"; color = NucleusColors.yellow; emoji = "GUILD.ROLES.EDIT";}
21 else if(rolesAdded.size > 0) {displayName = "Roles Added"; color = NucleusColors.green; emoji = "GUILD.ROLES.CREATE";}
22 let removedEntry = rolesRemoved.map(role => role.id);
23 let addedEntry = rolesAdded.map(role => role.id);
24
25 let list = {
26 memberId: entry(after.id, `\`${after.id}\``),
27 name: entry(after.user.id, renderUser(after.user)),
28 changed: entry(Date.now(), renderDelta(Date.now())),
29 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
30 };
31
32 let data: LoggerOptions = {
33 meta: {
34 type: "memberUpdate",
35 displayName: displayName,
36 calculateType: "guildMemberUpdate",
37 color: color,
38 emoji: emoji,
39 timestamp: Date.now()
40 },
41 list: {
42
43 },
44 hidden: {
45 guild: after.guild.id
46 }
47 };
48
49 if(rolesAdded.size > 0) {
50 list = Object.assign(list, {rolesAdded: entry(addedEntry, addedEntry.map(id => `<@&${id}>`).join(", "))});
51 }
52 if(rolesRemoved.size > 0) {
53 list = Object.assign(list, {rolesRemoved: entry(removedEntry, removedEntry.map(id => `<@&${id}>`).join(", "))});
54 }
55 data = Object.assign(data, {list: list});
56 log(data);
57 }
PineaFanc4d6c3f2023-01-19 12:17:25 +000058 if (before.nickname !== after.nickname) {
59 await client.database.history.create(
60 "nickname",
61 after.guild.id,
62 after.user,
63 auditLog.executor,
64 null,
65 before.nickname ?? before.user.username,
66 after.nickname ?? after.user.username
67 );
68 const data = {
69 meta: {
70 type: "memberUpdate",
71 displayName: "Nickname Changed",
72 calculateType: "guildMemberUpdate",
73 color: NucleusColors.yellow,
74 emoji: "PUNISH.NICKNAME.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -050075 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000076 },
77 list: {
78 memberId: entry(after.id, `\`${after.id}\``),
79 name: entry(after.user.id, renderUser(after.user)),
80 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
81 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
TheCodedProf6ec331b2023-02-20 12:13:06 -050082 changed: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +000083 changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
84 },
85 hidden: {
86 guild: after.guild.id
87 }
88 };
89 log(data);
TheCodedProfe35c4592023-02-20 12:47:12 -050090 }
91 if (
TheCodedProf6ec331b2023-02-20 12:13:06 -050092 (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() &&
93 (after.communicationDisabledUntil ?? 0) > Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +000094 ) {
95 await client.database.history.create(
96 "mute",
97 after.guild.id,
98 after.user,
99 auditLog.executor,
100 auditLog.reason,
101 null,
102 null,
103 null
104 );
105 const data = {
106 meta: {
107 type: "memberMute",
108 displayName: "Muted",
109 calculateType: "guildMemberPunish",
110 color: NucleusColors.yellow,
111 emoji: "PUNISH.MUTE.YELLOW",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500112 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000113 },
114 list: {
115 memberId: entry(after.id, `\`${after.id}\``),
116 name: entry(after.user.id, renderUser(after.user)),
117 mutedUntil: entry(
118 after.communicationDisabledUntilTimestamp,
119 renderDelta(after.communicationDisabledUntilTimestamp!)
120 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500121 muted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000122 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
123 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
124 },
125 hidden: {
126 guild: after.guild.id
127 }
128 };
129 log(data);
130 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
131 guild: after.guild.id,
132 user: after.id,
133 expires: after.communicationDisabledUntilTimestamp
134 });
TheCodedProfe35c4592023-02-20 12:47:12 -0500135 }
136 if (
PineaFanc4d6c3f2023-01-19 12:17:25 +0000137 after.communicationDisabledUntil === null &&
138 before.communicationDisabledUntilTimestamp !== null &&
TheCodedProf6ec331b2023-02-20 12:13:06 -0500139 Date.now() >= auditLog.createdTimestamp
PineaFanc4d6c3f2023-01-19 12:17:25 +0000140 ) {
141 await client.database.history.create(
142 "unmute",
143 after.guild.id,
144 after.user,
145 auditLog.executor,
146 null,
147 null,
148 null,
149 null
150 );
151 const data = {
152 meta: {
153 type: "memberUnmute",
154 displayName: "Unmuted",
155 calculateType: "guildMemberPunish",
156 color: NucleusColors.green,
157 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500158 timestamp: Date.now()
PineaFanc4d6c3f2023-01-19 12:17:25 +0000159 },
160 list: {
161 memberId: entry(after.id, `\`${after.id}\``),
162 name: entry(after.user.id, renderUser(after.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500163 unmuted: entry(Date.now(), renderDelta(Date.now())),
PineaFanc4d6c3f2023-01-19 12:17:25 +0000164 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
165 },
166 hidden: {
167 guild: after.guild.id
168 }
169 };
170 log(data);
171 client.database.eventScheduler.cancel("naturalUnmute", {
172 guild: after.guild.id,
173 user: after.id,
174 expires: before.communicationDisabledUntilTimestamp
175 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 }
pineafane625d782022-05-09 18:04:32 +0100177}