blob: 1f21870e968a29822e65361b412d279835c4febe [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { GuildAuditLogsEntry, GuildMember } from "discord.js";
2// @ts-expect-error
3import type { HaikuClient } from "jshaiku";
4
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01006
pineafan0f5cc782022-08-12 21:55:42 +01007export async function callback(client: HaikuClient, before: GuildMember, after: GuildMember) {
pineafane625d782022-05-09 18:04:32 +01008 try {
pineafan0f5cc782022-08-12 21:55:42 +01009 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010010 const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE");
pineafan0f5cc782022-08-12 21:55:42 +010011 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
pineafane23c4ec2022-07-27 21:56:27 +010012 if (audit.executor.id === client.user.id) return;
13 if (before.nickname !== after.nickname) {
pineafan63fc5e22022-08-04 22:04:10 +010014 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010015 "nickname",
16 after.guild.id,
17 after.user,
18 audit.executor,
19 null,
pineafan0f5cc782022-08-12 21:55:42 +010020 before.nickname ?? before.user.username,
21 after.nickname ?? after.user.username
Skyler Grey75ea9172022-08-06 10:22:23 +010022 );
pineafan63fc5e22022-08-04 22:04:10 +010023 const data = {
pineafane625d782022-05-09 18:04:32 +010024 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010025 type: "memberUpdate",
26 displayName: "Nickname Changed",
27 calculateType: "guildMemberUpdate",
pineafane625d782022-05-09 18:04:32 +010028 color: NucleusColors.yellow,
29 emoji: "PUNISH.NICKNAME.YELLOW",
30 timestamp: new Date().getTime()
31 },
32 list: {
pineafanda6e5342022-07-03 10:03:16 +010033 memberId: entry(after.id, `\`${after.id}\``),
pineafan73a7c4a2022-07-24 10:38:04 +010034 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +010035 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
36 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
37 changed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
38 changedBy: entry(audit.executor.id, renderUser(audit.executor))
pineafane625d782022-05-09 18:04:32 +010039 },
40 hidden: {
41 guild: after.guild.id
42 }
pineafan63fc5e22022-08-04 22:04:10 +010043 };
pineafan4edb7762022-06-26 19:21:04 +010044 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010045 } else if (
pineafan0f5cc782022-08-12 21:55:42 +010046 (before.communicationDisabledUntilTimestamp ?? 0) < new Date().getTime() &&
47 (after.communicationDisabledUntil ?? 0) > new Date().getTime() // TODO: test this
Skyler Grey75ea9172022-08-06 10:22:23 +010048 ) {
pineafan63fc5e22022-08-04 22:04:10 +010049 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010050 "mute",
51 after.guild.id,
52 after.user,
53 audit.executor,
54 audit.reason,
55 null,
56 null,
57 null
pineafan63fc5e22022-08-04 22:04:10 +010058 );
59 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010060 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010061 type: "memberMute",
62 displayName: "Muted",
63 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010064 color: NucleusColors.yellow,
65 emoji: "PUNISH.MUTE.YELLOW",
66 timestamp: new Date().getTime()
67 },
68 list: {
69 memberId: entry(after.id, `\`${after.id}\``),
70 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +010071 mutedUntil: entry(
72 after.communicationDisabledUntilTimestamp,
73 renderDelta(after.communicationDisabledUntilTimestamp)
74 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010075 muted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
76 mutedBy: entry(audit.executor.id, renderUser(audit.executor)),
77 reason: entry(audit.reason, audit.reason ? audit.reason : "\n> *No reason provided*")
pineafan73a7c4a2022-07-24 10:38:04 +010078 },
79 hidden: {
80 guild: after.guild.id
81 }
pineafan63fc5e22022-08-04 22:04:10 +010082 };
pineafan73a7c4a2022-07-24 10:38:04 +010083 log(data);
Skyler Grey11236ba2022-08-08 21:13:33 +010084 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil, {
85 guild: after.guild.id,
86 user: after.id,
87 expires: after.communicationDisabledUntilTimestamp
88 });
pineafan73a7c4a2022-07-24 10:38:04 +010089 } else if (
Skyler Grey75ea9172022-08-06 10:22:23 +010090 after.communicationDisabledUntil === null &&
91 before.communicationDisabledUntilTimestamp !== null &&
pineafan73a7c4a2022-07-24 10:38:04 +010092 new Date().getTime() >= audit.createdTimestamp
93 ) {
pineafan63fc5e22022-08-04 22:04:10 +010094 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010095 "unmute",
96 after.guild.id,
97 after.user,
98 audit.executor,
99 null,
100 null,
101 null,
102 null
pineafan63fc5e22022-08-04 22:04:10 +0100103 );
104 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100105 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100106 type: "memberUnmute",
107 displayName: "Unmuted",
108 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100109 color: NucleusColors.green,
110 emoji: "PUNISH.MUTE.GREEN",
111 timestamp: new Date().getTime()
112 },
113 list: {
114 memberId: entry(after.id, `\`${after.id}\``),
115 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100116 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
117 unmutedBy: entry(audit.executor.id, renderUser(audit.executor))
pineafan73a7c4a2022-07-24 10:38:04 +0100118 },
119 hidden: {
120 guild: after.guild.id
121 }
pineafan63fc5e22022-08-04 22:04:10 +0100122 };
pineafan73a7c4a2022-07-24 10:38:04 +0100123 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 client.database.eventScheduler.cancel("naturalUnmute", {
125 guild: after.guild.id,
126 user: after.id,
127 expires: before.communicationDisabledUntilTimestamp
128 });
pineafane625d782022-05-09 18:04:32 +0100129 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 } catch (e) {
131 console.log(e);
132 }
pineafane625d782022-05-09 18:04:32 +0100133}