blob: 4cffe69f15c1a4d263b1eba6f4fe6e0842045c4b [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01002
3export async function callback(client, before, after) {
4 try {
Skyler Grey75ea9172022-08-06 10:22:23 +01005 const {
6 log,
7 NucleusColors,
8 entry,
9 renderUser,
10 renderDelta,
11 getAuditLog
12 } = after.client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010013 const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE");
Skyler Grey75ea9172022-08-06 10:22:23 +010014 const audit = auditLog.entries
15 .filter((entry) => entry.target.id === after.id)
16 .first();
pineafane23c4ec2022-07-27 21:56:27 +010017 if (audit.executor.id === client.user.id) return;
18 if (before.nickname !== after.nickname) {
pineafan63fc5e22022-08-04 22:04:10 +010019 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010020 "nickname",
21 after.guild.id,
22 after.user,
23 audit.executor,
24 null,
25 before.nickname || before.user.username,
26 after.nickname || after.user.username
27 );
pineafan63fc5e22022-08-04 22:04:10 +010028 const data = {
pineafane625d782022-05-09 18:04:32 +010029 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010030 type: "memberUpdate",
31 displayName: "Nickname Changed",
32 calculateType: "guildMemberUpdate",
pineafane625d782022-05-09 18:04:32 +010033 color: NucleusColors.yellow,
34 emoji: "PUNISH.NICKNAME.YELLOW",
35 timestamp: new Date().getTime()
36 },
37 list: {
pineafanda6e5342022-07-03 10:03:16 +010038 memberId: entry(after.id, `\`${after.id}\``),
pineafan73a7c4a2022-07-24 10:38:04 +010039 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +010040 before: entry(
41 before.nickname,
42 before.nickname ? before.nickname : "*None*"
43 ),
44 after: entry(
45 after.nickname,
46 after.nickname ? after.nickname : "*None*"
47 ),
48 changed: entry(
49 new Date().getTime(),
50 renderDelta(new Date().getTime())
51 ),
52 changedBy: entry(
53 audit.executor.id,
54 renderUser(audit.executor)
55 )
pineafane625d782022-05-09 18:04:32 +010056 },
57 hidden: {
58 guild: after.guild.id
59 }
pineafan63fc5e22022-08-04 22:04:10 +010060 };
pineafan4edb7762022-06-26 19:21:04 +010061 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010062 } else if (
63 before.communicationDisabledUntilTimestamp < new Date().getTime() &&
64 after.communicationDisabledUntil > new Date().getTime()
65 ) {
pineafan63fc5e22022-08-04 22:04:10 +010066 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010067 "mute",
68 after.guild.id,
69 after.user,
70 audit.executor,
71 audit.reason,
72 null,
73 null,
74 null
pineafan63fc5e22022-08-04 22:04:10 +010075 );
76 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010077 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010078 type: "memberMute",
79 displayName: "Muted",
80 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010081 color: NucleusColors.yellow,
82 emoji: "PUNISH.MUTE.YELLOW",
83 timestamp: new Date().getTime()
84 },
85 list: {
86 memberId: entry(after.id, `\`${after.id}\``),
87 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +010088 mutedUntil: entry(
89 after.communicationDisabledUntilTimestamp,
90 renderDelta(after.communicationDisabledUntilTimestamp)
91 ),
92 muted: entry(
93 new Date().getTime(),
94 renderDelta(new Date().getTime())
95 ),
96 mutedBy: entry(
97 audit.executor.id,
98 renderUser(audit.executor)
99 ),
100 reason: entry(
101 audit.reason,
102 audit.reason ? audit.reason : "\n> *No reason provided*"
103 )
pineafan73a7c4a2022-07-24 10:38:04 +0100104 },
105 hidden: {
106 guild: after.guild.id
107 }
pineafan63fc5e22022-08-04 22:04:10 +0100108 };
pineafan73a7c4a2022-07-24 10:38:04 +0100109 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 client.database.eventScheduler.schedule(
111 "naturalUnmute",
112 after.communicationDisabledUntil,
113 {
114 guild: after.guild.id,
115 user: after.id,
116 expires: after.communicationDisabledUntilTimestamp
117 }
pineafan73a7c4a2022-07-24 10:38:04 +0100118 );
119 } else if (
Skyler Grey75ea9172022-08-06 10:22:23 +0100120 after.communicationDisabledUntil === null &&
121 before.communicationDisabledUntilTimestamp !== null &&
pineafan73a7c4a2022-07-24 10:38:04 +0100122 new Date().getTime() >= audit.createdTimestamp
123 ) {
pineafan63fc5e22022-08-04 22:04:10 +0100124 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 "unmute",
126 after.guild.id,
127 after.user,
128 audit.executor,
129 null,
130 null,
131 null,
132 null
pineafan63fc5e22022-08-04 22:04:10 +0100133 );
134 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100135 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100136 type: "memberUnmute",
137 displayName: "Unmuted",
138 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100139 color: NucleusColors.green,
140 emoji: "PUNISH.MUTE.GREEN",
141 timestamp: new Date().getTime()
142 },
143 list: {
144 memberId: entry(after.id, `\`${after.id}\``),
145 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 unmuted: entry(
147 new Date().getTime(),
148 renderDelta(new Date().getTime())
149 ),
150 unmutedBy: entry(
151 audit.executor.id,
152 renderUser(audit.executor)
153 )
pineafan73a7c4a2022-07-24 10:38:04 +0100154 },
155 hidden: {
156 guild: after.guild.id
157 }
pineafan63fc5e22022-08-04 22:04:10 +0100158 };
pineafan73a7c4a2022-07-24 10:38:04 +0100159 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 client.database.eventScheduler.cancel("naturalUnmute", {
161 guild: after.guild.id,
162 user: after.id,
163 expires: before.communicationDisabledUntilTimestamp
164 });
pineafane625d782022-05-09 18:04:32 +0100165 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100166 } catch (e) {
167 console.log(e);
168 }
pineafane625d782022-05-09 18:04:32 +0100169}