blob: 236bc0f8e46da911387350c2897d0a96ddd8d719 [file] [log] [blame]
TheCodedProfa16d1672023-01-18 18:58:34 -05001import { AuditLogEvent, Guild, GuildAuditLogsEntry, Role } from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00002import type { NucleusClient } from "../utils/client.js";
pineafan32767212022-03-14 21:27:39 +00003import getEmojiByName from "../utils/getEmojiByName.js";
4
pineafan63fc5e22022-08-04 22:04:10 +01005export const event = "roleUpdate";
pineafan32767212022-03-14 21:27:39 +00006
TheCodedProfa16d1672023-01-18 18:58:34 -05007export async function callback(client: NucleusClient, oldRole: Role, newRole: Role) {
TheCodedProf6ec331b2023-02-20 12:13:06 -05008 const { getAuditLog, isLogging, log, NucleusColors, entry, renderDelta, renderUser, renderRole } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +00009 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];
TheCodedProf267563a2023-01-21 17:00:57 -050013 if (!auditLog) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050014 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000015
pineafan0f5cc782022-08-12 21:55:42 +010016 const changes: Record<string, ReturnType<typeof entry>> = {
TheCodedProfa16d1672023-01-18 18:58:34 -050017 roleId: entry(newRole.id, `\`${newRole.id}\``),
18 role: entry(newRole.id, renderRole(newRole)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050019 edited: entry(Date.now(), renderDelta(Date.now())),
Skyler Greyda16adf2023-03-05 10:22:12 +000020 editedBy: entry(
21 auditLog.executor!.id,
22 renderUser((await newRole.guild.members.fetch(auditLog.executor!.id)).user)
23 )
pineafan63fc5e22022-08-04 22:04:10 +010024 };
25 const mentionable = ["", ""];
26 const hoist = ["", ""];
Skyler Greyda16adf2023-03-05 10:22:12 +000027 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`;
TheCodedProfa16d1672023-01-18 18:58:34 -050033 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 Greyda16adf2023-03-05 10:22:12 +000035 if (oldRole.name !== newRole.name)
36 changes["name"] = entry([oldRole.name, newRole.name], `${oldRole.name} -> ${newRole.name}`);
TheCodedProfa16d1672023-01-18 18:58:34 -050037 if (oldRole.position !== newRole.position)
Skyler Greyda16adf2023-03-05 10:22:12 +000038 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]}`);
TheCodedProfa16d1672023-01-18 18:58:34 -050044 if (oldRole.mentionable !== newRole.mentionable)
Skyler Greyda16adf2023-03-05 10:22:12 +000045 changes["mentionable"] = entry(
46 [oldRole.mentionable, newRole.mentionable],
47 `${mentionable[0]} -> ${mentionable[1]}`
48 );
TheCodedProfa16d1672023-01-18 18:58:34 -050049 if (oldRole.hexColor !== newRole.hexColor)
Skyler Greyda16adf2023-03-05 10:22:12 +000050 changes["color"] = entry(
51 [oldRole.hexColor, newRole.hexColor],
52 `\`${oldRole.hexColor}\` -> \`${newRole.hexColor}\``
53 );
TheCodedProf1f675042023-02-16 17:01:29 -050054 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 }
pineafan63fc5e22022-08-04 22:04:10 +010060
61 if (Object.keys(changes).length === 4) return;
62
63 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010065 type: "roleUpdate",
66 displayName: "Role Edited",
67 calculateType: "guildRoleUpdate",
68 color: NucleusColors.yellow,
69 emoji: "GUILD.ROLES.EDIT",
TheCodedProfa16d1672023-01-18 18:58:34 -050070 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +010071 },
72 list: changes,
73 hidden: {
TheCodedProfa16d1672023-01-18 18:58:34 -050074 guild: newRole.guild.id
pineafane625d782022-05-09 18:04:32 +010075 }
TheCodedProf1f675042023-02-16 17:01:29 -050076 }; // TODO: make our own page for this
pineafan63fc5e22022-08-04 22:04:10 +010077 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010078}