pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 1 | export const event = 'guildMemberUpdate' |
| 2 | |
| 3 | export async function callback(client, before, after) { |
| 4 | try { |
| 5 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = after.client.logger |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 6 | let auditLog = await getAuditLog(after.guild, 'MEMBER_UPDATE'); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 7 | let audit = auditLog.entries.filter(entry => entry.target.id === after.id).first(); |
| 8 | if (audit.executor.id === client.user.id) return; |
| 9 | if (before.nickname !== after.nickname) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 10 | try { await client.database.history.create( |
| 11 | "nickname", after.guild.id, after.user, audit.executor, |
| 12 | null, before.nickname || before.user.username, after.nickname || after.user.username) } catch {} |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | let data = { |
| 14 | meta: { |
| 15 | type: 'memberUpdate', |
| 16 | displayName: 'Nickname Changed', |
| 17 | calculateType: 'guildMemberUpdate', |
| 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 | e625d78 | 2022-05-09 18:04:32 +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 | } |
| 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()) { |
| 36 | try { await client.database.history.create( |
| 37 | "mute", after.guild.id, after.user, audit.executor, audit.reason, null, null, null |
| 38 | )} catch {} |
| 39 | let data = { |
| 40 | meta: { |
| 41 | type: 'memberMute', |
| 42 | displayName: 'Muted', |
| 43 | calculateType: 'guildMemberPunish', |
| 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)), |
| 54 | reason: entry(audit.reason, audit.reason ? audit.reason : '\n> *No reason provided*') |
| 55 | }, |
| 56 | hidden: { |
| 57 | guild: after.guild.id |
| 58 | } |
| 59 | } |
| 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 | ) { |
| 68 | try { await client.database.history.create( |
| 69 | "unmute", after.guild.id, after.user, audit.executor, null, null, null, null |
| 70 | )} catch {} |
| 71 | let data = { |
| 72 | meta: { |
| 73 | type: 'memberUnmute', |
| 74 | displayName: 'Unmuted', |
| 75 | calculateType: 'guildMemberPunish', |
| 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 | } |
| 89 | } |
| 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 | } |
| 93 | } catch (e) { console.log(e) } |
| 94 | } |