TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 1 | import { AuditLogEvent, Guild, GuildAuditLogsEntry, Role } from "discord.js"; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 2 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 3 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 4 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 5 | export const event = "roleUpdate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 6 | |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 7 | export async function callback(client: NucleusClient, oldRole: Role, newRole: Role) { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 8 | const { getAuditLog, isLogging, log, NucleusColors, entry, renderDelta, renderUser, renderRole } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 9 | if (!(await isLogging(newRole.guild.id, "guildRoleUpdate"))) return; |
| 10 | const auditLog = (await getAuditLog(newRole.guild as Guild, AuditLogEvent.RoleUpdate)).filter( |
| 11 | (entry: GuildAuditLogsEntry) => (entry.target as Role)!.id === newRole.id |
| 12 | )[0]; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 13 | if (!auditLog) return; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 14 | if (auditLog.executor!.id === client.user!.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 15 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 16 | const changes: Record<string, ReturnType<typeof entry>> = { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 17 | roleId: entry(newRole.id, `\`${newRole.id}\``), |
| 18 | role: entry(newRole.id, renderRole(newRole)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 19 | edited: entry(Date.now(), renderDelta(Date.now())), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 20 | editedBy: entry( |
| 21 | auditLog.executor!.id, |
| 22 | renderUser((await newRole.guild.members.fetch(auditLog.executor!.id)).user) |
| 23 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | }; |
| 25 | const mentionable = ["", ""]; |
| 26 | const hoist = ["", ""]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 27 | mentionable[0] = oldRole.mentionable |
| 28 | ? `${getEmojiByName("CONTROL.TICK")} Yes` |
| 29 | : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 30 | mentionable[1] = newRole.mentionable |
| 31 | ? `${getEmojiByName("CONTROL.TICK")} Yes` |
| 32 | : `${getEmojiByName("CONTROL.CROSS")} No`; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 33 | hoist[0] = oldRole.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
| 34 | hoist[1] = newRole.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 35 | if (oldRole.name !== newRole.name) |
| 36 | changes["name"] = entry([oldRole.name, newRole.name], `${oldRole.name} -> ${newRole.name}`); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 37 | if (oldRole.position !== newRole.position) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 38 | changes["position"] = entry( |
| 39 | [oldRole.position.toString(), newRole.position.toString()], |
| 40 | `${oldRole.position} -> ${newRole.position}` |
| 41 | ); |
| 42 | if (oldRole.hoist !== newRole.hoist) |
| 43 | changes["showInMemberList"] = entry([oldRole.hoist, newRole.hoist], `${hoist[0]} -> ${hoist[1]}`); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 44 | if (oldRole.mentionable !== newRole.mentionable) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 45 | changes["mentionable"] = entry( |
| 46 | [oldRole.mentionable, newRole.mentionable], |
| 47 | `${mentionable[0]} -> ${mentionable[1]}` |
| 48 | ); |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 49 | if (oldRole.hexColor !== newRole.hexColor) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 50 | changes["color"] = entry( |
| 51 | [oldRole.hexColor, newRole.hexColor], |
| 52 | `\`${oldRole.hexColor}\` -> \`${newRole.hexColor}\`` |
| 53 | ); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 54 | if (oldRole.permissions.bitfield !== newRole.permissions.bitfield) { |
| 55 | changes["permissions"] = entry( |
| 56 | [oldRole.permissions.bitfield.toString(), newRole.permissions.bitfield.toString()], |
| 57 | `[[Old]](https://discordapi.com/permissions.html#${oldRole.permissions.bitfield.toString()}) -> [[New]](https://discordapi.com/permissions.html#${newRole.permissions.bitfield.toString()})` |
| 58 | ); |
| 59 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | |
| 61 | if (Object.keys(changes).length === 4) return; |
| 62 | |
| 63 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | type: "roleUpdate", |
| 66 | displayName: "Role Edited", |
| 67 | calculateType: "guildRoleUpdate", |
| 68 | color: NucleusColors.yellow, |
| 69 | emoji: "GUILD.ROLES.EDIT", |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 70 | timestamp: auditLog.createdTimestamp |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | }, |
| 72 | list: changes, |
| 73 | hidden: { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 74 | guild: newRole.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 75 | } |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 76 | }; // TODO: make our own page for this |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 77 | await log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | } |