pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | export const event = "guildMemberUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 2 | |
| 3 | export async function callback(client, before, after) { |
| 4 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = after.client.logger; |
| 6 | const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE"); |
| 7 | const audit = auditLog.entries.filter(entry => entry.target.id === after.id).first(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 8 | if (audit.executor.id === client.user.id) return; |
| 9 | if (before.nickname !== after.nickname) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | await client.database.history.create( |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 11 | "nickname", after.guild.id, after.user, audit.executor, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | null, before.nickname || before.user.username, after.nickname || after.user.username); |
| 13 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 14 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | type: "memberUpdate", |
| 16 | displayName: "Nickname Changed", |
| 17 | calculateType: "guildMemberUpdate", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 18 | color: NucleusColors.yellow, |
| 19 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 20 | timestamp: new Date().getTime() |
| 21 | }, |
| 22 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 23 | memberId: entry(after.id, `\`${after.id}\``), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 24 | name: entry(after.user.id, renderUser(after.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | before: entry(before.nickname, before.nickname ? before.nickname : "*None*"), |
| 26 | after: entry(after.nickname, after.nickname ? after.nickname : "*None*"), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 27 | changed: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 28 | changedBy: entry(audit.executor.id, renderUser(audit.executor)) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 29 | }, |
| 30 | hidden: { |
| 31 | guild: after.guild.id |
| 32 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 34 | log(data); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 35 | } else if (before.communicationDisabledUntilTimestamp < new Date().getTime() && after.communicationDisabledUntil > new Date().getTime()) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | await client.database.history.create( |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 37 | "mute", after.guild.id, after.user, audit.executor, audit.reason, null, null, null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | ); |
| 39 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 40 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | type: "memberMute", |
| 42 | displayName: "Muted", |
| 43 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 44 | color: NucleusColors.yellow, |
| 45 | emoji: "PUNISH.MUTE.YELLOW", |
| 46 | timestamp: new Date().getTime() |
| 47 | }, |
| 48 | list: { |
| 49 | memberId: entry(after.id, `\`${after.id}\``), |
| 50 | name: entry(after.user.id, renderUser(after.user)), |
| 51 | mutedUntil: entry(after.communicationDisabledUntilTimestamp, renderDelta(after.communicationDisabledUntilTimestamp)), |
| 52 | muted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 53 | mutedBy: entry(audit.executor.id, renderUser(audit.executor)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | reason: entry(audit.reason, audit.reason ? audit.reason : "\n> *No reason provided*") |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | }, |
| 56 | hidden: { |
| 57 | guild: after.guild.id |
| 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 60 | log(data); |
| 61 | client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil, |
| 62 | {guild: after.guild.id, user: after.id, expires: after.communicationDisabledUntilTimestamp} |
| 63 | ); |
| 64 | } else if ( |
| 65 | after.communicationDisabledUntil === null && before.communicationDisabledUntilTimestamp !== null && |
| 66 | new Date().getTime() >= audit.createdTimestamp |
| 67 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | await client.database.history.create( |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 69 | "unmute", after.guild.id, after.user, audit.executor, null, null, null, null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | ); |
| 71 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 72 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | type: "memberUnmute", |
| 74 | displayName: "Unmuted", |
| 75 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 76 | color: NucleusColors.green, |
| 77 | emoji: "PUNISH.MUTE.GREEN", |
| 78 | timestamp: new Date().getTime() |
| 79 | }, |
| 80 | list: { |
| 81 | memberId: entry(after.id, `\`${after.id}\``), |
| 82 | name: entry(after.user.id, renderUser(after.user)), |
| 83 | unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 84 | unmutedBy: entry(audit.executor.id, renderUser(audit.executor)) |
| 85 | }, |
| 86 | hidden: { |
| 87 | guild: after.guild.id |
| 88 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 90 | log(data); |
| 91 | client.database.eventScheduler.cancel("naturalUnmute", {guild: after.guild.id, user: after.id, expires: before.communicationDisabledUntilTimestamp}); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 92 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | } catch (e) { console.log(e); } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 94 | } |