blob: 440d7862d524e335e0022b765c38856e3eebd271 [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";
pineafan0f5cc782022-08-12 21:55:42 +01003
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "guildMemberUpdate";
pineafane625d782022-05-09 18:04:32 +01005
PineaFan752af462022-12-31 21:59:38 +00006export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) {
pineafane625d782022-05-09 18:04:32 +01007 try {
pineafan0f5cc782022-08-12 21:55:42 +01008 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
PineaFan0d06edc2023-01-17 22:10:31 +00009 const auditLog = await getAuditLog(after.guild, AuditLogEvent.MemberUpdate);
pineafan0f5cc782022-08-12 21:55:42 +010010 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
PineaFan0d06edc2023-01-17 22:10:31 +000011 if (audit.executor.id === client.user!.id) return;
pineafane23c4ec2022-07-27 21:56:27 +010012 if (before.nickname !== after.nickname) {
pineafan63fc5e22022-08-04 22:04:10 +010013 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010014 "nickname",
15 after.guild.id,
16 after.user,
17 audit.executor,
18 null,
pineafan0f5cc782022-08-12 21:55:42 +010019 before.nickname ?? before.user.username,
20 after.nickname ?? after.user.username
Skyler Grey75ea9172022-08-06 10:22:23 +010021 );
pineafan63fc5e22022-08-04 22:04:10 +010022 const data = {
pineafane625d782022-05-09 18:04:32 +010023 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010024 type: "memberUpdate",
25 displayName: "Nickname Changed",
26 calculateType: "guildMemberUpdate",
pineafane625d782022-05-09 18:04:32 +010027 color: NucleusColors.yellow,
28 emoji: "PUNISH.NICKNAME.YELLOW",
29 timestamp: new Date().getTime()
30 },
31 list: {
pineafanda6e5342022-07-03 10:03:16 +010032 memberId: entry(after.id, `\`${after.id}\``),
pineafan73a7c4a2022-07-24 10:38:04 +010033 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +010034 before: entry(before.nickname, before.nickname ? before.nickname : "*None*"),
35 after: entry(after.nickname, after.nickname ? after.nickname : "*None*"),
36 changed: entry(new Date().getTime(), renderDelta(new Date().getTime())),
37 changedBy: entry(audit.executor.id, renderUser(audit.executor))
pineafane625d782022-05-09 18:04:32 +010038 },
39 hidden: {
40 guild: after.guild.id
41 }
pineafan63fc5e22022-08-04 22:04:10 +010042 };
pineafan4edb7762022-06-26 19:21:04 +010043 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010044 } else if (
pineafan0f5cc782022-08-12 21:55:42 +010045 (before.communicationDisabledUntilTimestamp ?? 0) < new Date().getTime() &&
46 (after.communicationDisabledUntil ?? 0) > new Date().getTime() // TODO: test this
Skyler Grey75ea9172022-08-06 10:22:23 +010047 ) {
pineafan63fc5e22022-08-04 22:04:10 +010048 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010049 "mute",
50 after.guild.id,
51 after.user,
52 audit.executor,
53 audit.reason,
54 null,
55 null,
56 null
pineafan63fc5e22022-08-04 22:04:10 +010057 );
58 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010059 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010060 type: "memberMute",
61 displayName: "Muted",
62 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010063 color: NucleusColors.yellow,
64 emoji: "PUNISH.MUTE.YELLOW",
65 timestamp: new Date().getTime()
66 },
67 list: {
68 memberId: entry(after.id, `\`${after.id}\``),
69 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +010070 mutedUntil: entry(
71 after.communicationDisabledUntilTimestamp,
72 renderDelta(after.communicationDisabledUntilTimestamp)
73 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010074 muted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
75 mutedBy: entry(audit.executor.id, renderUser(audit.executor)),
76 reason: entry(audit.reason, audit.reason ? audit.reason : "\n> *No reason provided*")
pineafan73a7c4a2022-07-24 10:38:04 +010077 },
78 hidden: {
79 guild: after.guild.id
80 }
pineafan63fc5e22022-08-04 22:04:10 +010081 };
pineafan73a7c4a2022-07-24 10:38:04 +010082 log(data);
Skyler Grey11236ba2022-08-08 21:13:33 +010083 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil, {
84 guild: after.guild.id,
85 user: after.id,
86 expires: after.communicationDisabledUntilTimestamp
87 });
pineafan73a7c4a2022-07-24 10:38:04 +010088 } else if (
Skyler Grey75ea9172022-08-06 10:22:23 +010089 after.communicationDisabledUntil === null &&
90 before.communicationDisabledUntilTimestamp !== null &&
pineafan73a7c4a2022-07-24 10:38:04 +010091 new Date().getTime() >= audit.createdTimestamp
92 ) {
pineafan63fc5e22022-08-04 22:04:10 +010093 await client.database.history.create(
Skyler Grey75ea9172022-08-06 10:22:23 +010094 "unmute",
95 after.guild.id,
96 after.user,
97 audit.executor,
98 null,
99 null,
100 null,
101 null
pineafan63fc5e22022-08-04 22:04:10 +0100102 );
103 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100104 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100105 type: "memberUnmute",
106 displayName: "Unmuted",
107 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100108 color: NucleusColors.green,
109 emoji: "PUNISH.MUTE.GREEN",
110 timestamp: new Date().getTime()
111 },
112 list: {
113 memberId: entry(after.id, `\`${after.id}\``),
114 name: entry(after.user.id, renderUser(after.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100115 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
116 unmutedBy: entry(audit.executor.id, renderUser(audit.executor))
pineafan73a7c4a2022-07-24 10:38:04 +0100117 },
118 hidden: {
119 guild: after.guild.id
120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121 };
pineafan73a7c4a2022-07-24 10:38:04 +0100122 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 client.database.eventScheduler.cancel("naturalUnmute", {
124 guild: after.guild.id,
125 user: after.id,
126 expires: before.communicationDisabledUntilTimestamp
127 });
pineafane625d782022-05-09 18:04:32 +0100128 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 } catch (e) {
130 console.log(e);
131 }
pineafane625d782022-05-09 18:04:32 +0100132}