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 | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 7 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 129 | } |