blob: a7f8c47ff2380fa17971138c7770d2eaa37c8905 [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 {
pineafan63fc5e22022-08-04 22:04:10 +01005 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = after.client.logger;
6 const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE");
7 const audit = auditLog.entries.filter(entry => entry.target.id === after.id).first();
pineafane23c4ec2022-07-27 21:56:27 +01008 if (audit.executor.id === client.user.id) return;
9 if (before.nickname !== after.nickname) {
pineafan63fc5e22022-08-04 22:04:10 +010010 await client.database.history.create(
pineafan4edb7762022-06-26 19:21:04 +010011 "nickname", after.guild.id, after.user, audit.executor,
pineafan63fc5e22022-08-04 22:04:10 +010012 null, before.nickname || before.user.username, after.nickname || after.user.username);
13 const data = {
pineafane625d782022-05-09 18:04:32 +010014 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010015 type: "memberUpdate",
16 displayName: "Nickname Changed",
17 calculateType: "guildMemberUpdate",
pineafane625d782022-05-09 18:04:32 +010018 color: NucleusColors.yellow,
19 emoji: "PUNISH.NICKNAME.YELLOW",
20 timestamp: new Date().getTime()
21 },
22 list: {
pineafanda6e5342022-07-03 10:03:16 +010023 memberId: entry(after.id, `\`${after.id}\``),
pineafan73a7c4a2022-07-24 10:38:04 +010024 name: entry(after.user.id, renderUser(after.user)),
pineafan63fc5e22022-08-04 22:04:10 +010025 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
26 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
pineafan73a7c4a2022-07-24 10:38:04 +010027 changed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
28 changedBy: entry(audit.executor.id, renderUser(audit.executor))
pineafane625d782022-05-09 18:04:32 +010029 },
30 hidden: {
31 guild: after.guild.id
32 }
pineafan63fc5e22022-08-04 22:04:10 +010033 };
pineafan4edb7762022-06-26 19:21:04 +010034 log(data);
pineafan73a7c4a2022-07-24 10:38:04 +010035 } else if (before.communicationDisabledUntilTimestamp < new Date().getTime() && after.communicationDisabledUntil > new Date().getTime()) {
pineafan63fc5e22022-08-04 22:04:10 +010036 await client.database.history.create(
pineafan73a7c4a2022-07-24 10:38:04 +010037 "mute", after.guild.id, after.user, audit.executor, audit.reason, null, null, null
pineafan63fc5e22022-08-04 22:04:10 +010038 );
39 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010040 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010041 type: "memberMute",
42 displayName: "Muted",
43 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010044 color: NucleusColors.yellow,
45 emoji: "PUNISH.MUTE.YELLOW",
46 timestamp: new Date().getTime()
47 },
48 list: {
49 memberId: entry(after.id, `\`${after.id}\``),
50 name: entry(after.user.id, renderUser(after.user)),
51 mutedUntil: entry(after.communicationDisabledUntilTimestamp, renderDelta(after.communicationDisabledUntilTimestamp)),
52 muted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
53 mutedBy: entry(audit.executor.id, renderUser(audit.executor)),
pineafan63fc5e22022-08-04 22:04:10 +010054 reason: entry(audit.reason, audit.reason ? audit.reason : "\n> *No reason provided*")
pineafan73a7c4a2022-07-24 10:38:04 +010055 },
56 hidden: {
57 guild: after.guild.id
58 }
pineafan63fc5e22022-08-04 22:04:10 +010059 };
pineafan73a7c4a2022-07-24 10:38:04 +010060 log(data);
61 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil,
62 {guild: after.guild.id, user: after.id, expires: after.communicationDisabledUntilTimestamp}
63 );
64 } else if (
65 after.communicationDisabledUntil === null && before.communicationDisabledUntilTimestamp !== null &&
66 new Date().getTime() >= audit.createdTimestamp
67 ) {
pineafan63fc5e22022-08-04 22:04:10 +010068 await client.database.history.create(
pineafan73a7c4a2022-07-24 10:38:04 +010069 "unmute", after.guild.id, after.user, audit.executor, null, null, null, null
pineafan63fc5e22022-08-04 22:04:10 +010070 );
71 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010072 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010073 type: "memberUnmute",
74 displayName: "Unmuted",
75 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010076 color: NucleusColors.green,
77 emoji: "PUNISH.MUTE.GREEN",
78 timestamp: new Date().getTime()
79 },
80 list: {
81 memberId: entry(after.id, `\`${after.id}\``),
82 name: entry(after.user.id, renderUser(after.user)),
83 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
84 unmutedBy: entry(audit.executor.id, renderUser(audit.executor))
85 },
86 hidden: {
87 guild: after.guild.id
88 }
pineafan63fc5e22022-08-04 22:04:10 +010089 };
pineafan73a7c4a2022-07-24 10:38:04 +010090 log(data);
91 client.database.eventScheduler.cancel("naturalUnmute", {guild: after.guild.id, user: after.id, expires: before.communicationDisabledUntilTimestamp});
pineafane625d782022-05-09 18:04:32 +010092 }
pineafan63fc5e22022-08-04 22:04:10 +010093 } catch (e) { console.log(e); }
pineafane625d782022-05-09 18:04:32 +010094}