pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 1 | import type { GuildAuditLogsEntry, GuildMember } from "discord.js"; |
| 2 | // @ts-expect-error |
| 3 | import type { HaikuClient } from "jshaiku"; |
| 4 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | export const event = "guildMemberUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 6 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 7 | export async function callback(client: HaikuClient, before: GuildMember, after: GuildMember) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 8 | try { |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 9 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE"); |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 11 | const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 12 | if (audit.executor.id === client.user.id) return; |
| 13 | if (before.nickname !== after.nickname) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | "nickname", |
| 16 | after.guild.id, |
| 17 | after.user, |
| 18 | audit.executor, |
| 19 | null, |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 20 | before.nickname ?? before.user.username, |
| 21 | after.nickname ?? after.user.username |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 24 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | type: "memberUpdate", |
| 26 | displayName: "Nickname Changed", |
| 27 | calculateType: "guildMemberUpdate", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 28 | color: NucleusColors.yellow, |
| 29 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 30 | timestamp: new Date().getTime() |
| 31 | }, |
| 32 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 33 | memberId: entry(after.id, `\`${after.id}\``), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 34 | name: entry(after.user.id, renderUser(after.user)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 35 | 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)) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 39 | }, |
| 40 | hidden: { |
| 41 | guild: after.guild.id |
| 42 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 44 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | } else if ( |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame^] | 46 | (before.communicationDisabledUntilTimestamp ?? 0) < new Date().getTime() && |
| 47 | (after.communicationDisabledUntil ?? 0) > new Date().getTime() // TODO: test this |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 50 | "mute", |
| 51 | after.guild.id, |
| 52 | after.user, |
| 53 | audit.executor, |
| 54 | audit.reason, |
| 55 | null, |
| 56 | null, |
| 57 | null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | ); |
| 59 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 60 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | type: "memberMute", |
| 62 | displayName: "Muted", |
| 63 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 64 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | mutedUntil: entry( |
| 72 | after.communicationDisabledUntilTimestamp, |
| 73 | renderDelta(after.communicationDisabledUntilTimestamp) |
| 74 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 75 | 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*") |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 78 | }, |
| 79 | hidden: { |
| 80 | guild: after.guild.id |
| 81 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 83 | log(data); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 84 | client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil, { |
| 85 | guild: after.guild.id, |
| 86 | user: after.id, |
| 87 | expires: after.communicationDisabledUntilTimestamp |
| 88 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 89 | } else if ( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | after.communicationDisabledUntil === null && |
| 91 | before.communicationDisabledUntilTimestamp !== null && |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 92 | new Date().getTime() >= audit.createdTimestamp |
| 93 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | "unmute", |
| 96 | after.guild.id, |
| 97 | after.user, |
| 98 | audit.executor, |
| 99 | null, |
| 100 | null, |
| 101 | null, |
| 102 | null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 103 | ); |
| 104 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 105 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | type: "memberUnmute", |
| 107 | displayName: "Unmuted", |
| 108 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 109 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 116 | unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 117 | unmutedBy: entry(audit.executor.id, renderUser(audit.executor)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 118 | }, |
| 119 | hidden: { |
| 120 | guild: after.guild.id |
| 121 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 122 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 123 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | client.database.eventScheduler.cancel("naturalUnmute", { |
| 125 | guild: after.guild.id, |
| 126 | user: after.id, |
| 127 | expires: before.communicationDisabledUntilTimestamp |
| 128 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 129 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | } catch (e) { |
| 131 | console.log(e); |
| 132 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 133 | } |