blob: 8889f57a010a35848d3b415e72f1dea2bd1ea494 [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) {
PineaFanc4d6c3f2023-01-19 12:17:25 +00007 const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger;
8 const auditLog = (await getAuditLog(after.guild, AuditLogEvent.EmojiCreate))
9 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0];
10 if (!auditLog) return;
11 if (auditLog.executor!.id === client.user!.id) return;
12 if (before.nickname !== after.nickname) {
13 await client.database.history.create(
14 "nickname",
15 after.guild.id,
16 after.user,
17 auditLog.executor,
18 null,
19 before.nickname ?? before.user.username,
20 after.nickname ?? after.user.username
21 );
22 const data = {
23 meta: {
24 type: "memberUpdate",
25 displayName: "Nickname Changed",
26 calculateType: "guildMemberUpdate",
27 color: NucleusColors.yellow,
28 emoji: "PUNISH.NICKNAME.YELLOW",
29 timestamp: new Date().getTime()
30 },
31 list: {
32 memberId: entry(after.id, `\`${after.id}\``),
33 name: entry(after.user.id, renderUser(after.user)),
34 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(auditLog.executor!.id, renderUser(auditLog.executor!))
38 },
39 hidden: {
40 guild: after.guild.id
41 }
42 };
43 log(data);
44 } else if (
45 (before.communicationDisabledUntilTimestamp ?? 0) < new Date().getTime() &&
46 (after.communicationDisabledUntil ?? 0) > new Date().getTime() // TODO: test this
47 ) {
48 await client.database.history.create(
49 "mute",
50 after.guild.id,
51 after.user,
52 auditLog.executor,
53 auditLog.reason,
54 null,
55 null,
56 null
57 );
58 const data = {
59 meta: {
60 type: "memberMute",
61 displayName: "Muted",
62 calculateType: "guildMemberPunish",
63 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)),
70 mutedUntil: entry(
71 after.communicationDisabledUntilTimestamp,
72 renderDelta(after.communicationDisabledUntilTimestamp!)
73 ),
74 muted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
75 mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
76 reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*")
77 },
78 hidden: {
79 guild: after.guild.id
80 }
81 };
82 log(data);
83 client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, {
84 guild: after.guild.id,
85 user: after.id,
86 expires: after.communicationDisabledUntilTimestamp
87 });
88 } else if (
89 after.communicationDisabledUntil === null &&
90 before.communicationDisabledUntilTimestamp !== null &&
91 new Date().getTime() >= auditLog.createdTimestamp
92 ) {
93 await client.database.history.create(
94 "unmute",
95 after.guild.id,
96 after.user,
97 auditLog.executor,
98 null,
99 null,
100 null,
101 null
102 );
103 const data = {
104 meta: {
105 type: "memberUnmute",
106 displayName: "Unmuted",
107 calculateType: "guildMemberPunish",
108 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)),
115 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
116 unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!))
117 },
118 hidden: {
119 guild: after.guild.id
120 }
121 };
122 log(data);
123 client.database.eventScheduler.cancel("naturalUnmute", {
124 guild: after.guild.id,
125 user: after.id,
126 expires: before.communicationDisabledUntilTimestamp
127 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 }
pineafane625d782022-05-09 18:04:32 +0100129}