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