blob: 09e062f44c6e90d8575a80a6d7748f107e68165e [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) {
Skyler Grey11236ba2022-08-08 21:13:33 +01008 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderRole } = client.logger;
pineafan32767212022-03-14 21:27:39 +00009
TheCodedProfa16d1672023-01-18 18:58:34 -050010 const auditLog = (await getAuditLog(newRole.guild as Guild, AuditLogEvent.RoleUpdate))
TheCodedProf267563a2023-01-21 17:00:57 -050011 .filter((entry: GuildAuditLogsEntry) => (entry.target as Role)!.id === newRole.id)[0];
12 if (!auditLog) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050013 if (auditLog.executor!.id === client.user!.id) return;
pineafan32767212022-03-14 21:27:39 +000014
pineafan0f5cc782022-08-12 21:55:42 +010015 const changes: Record<string, ReturnType<typeof entry>> = {
TheCodedProfa16d1672023-01-18 18:58:34 -050016 roleId: entry(newRole.id, `\`${newRole.id}\``),
17 role: entry(newRole.id, renderRole(newRole)),
pineafan63fc5e22022-08-04 22:04:10 +010018 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProfa16d1672023-01-18 18:58:34 -050019 editedBy: entry(auditLog.executor!.id, renderUser((await newRole.guild.members.fetch(auditLog.executor!.id)).user))
pineafan63fc5e22022-08-04 22:04:10 +010020 };
21 const mentionable = ["", ""];
22 const hoist = ["", ""];
TheCodedProfa16d1672023-01-18 18:58:34 -050023 mentionable[0] = oldRole.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
24 mentionable[1] = newRole.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
25 hoist[0] = oldRole.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
26 hoist[1] = newRole.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
27 if (oldRole.name !== newRole.name) changes["name"] = entry([oldRole.name, newRole.name], `${oldRole.name} -> ${newRole.name}`);
28 if (oldRole.position !== newRole.position)
29 changes["position"] = entry([oldRole.position.toString(), newRole.position.toString()], `${oldRole.position} -> ${newRole.position}`);
30 if (oldRole.hoist !== newRole.hoist) changes["showInMemberList"] = entry([oldRole.hoist, newRole.hoist], `${hoist[0]} -> ${hoist[1]}`);
31 if (oldRole.mentionable !== newRole.mentionable)
32 changes["mentionable"] = entry([oldRole.mentionable, newRole.mentionable], `${mentionable[0]} -> ${mentionable[1]}`);
33 if (oldRole.hexColor !== newRole.hexColor)
34 changes["color"] = entry([oldRole.hexColor, newRole.hexColor], `\`${oldRole.hexColor}\` -> \`${newRole.hexColor}\``);
TheCodedProf1f675042023-02-16 17:01:29 -050035 if (oldRole.permissions.bitfield !== newRole.permissions.bitfield) {
36 changes["permissions"] = entry(
37 [oldRole.permissions.bitfield.toString(), newRole.permissions.bitfield.toString()],
38 `[[Old]](https://discordapi.com/permissions.html#${oldRole.permissions.bitfield.toString()}) -> [[New]](https://discordapi.com/permissions.html#${newRole.permissions.bitfield.toString()})`
39 );
40 }
pineafan63fc5e22022-08-04 22:04:10 +010041
42 if (Object.keys(changes).length === 4) return;
43
44 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010046 type: "roleUpdate",
47 displayName: "Role Edited",
48 calculateType: "guildRoleUpdate",
49 color: NucleusColors.yellow,
50 emoji: "GUILD.ROLES.EDIT",
TheCodedProfa16d1672023-01-18 18:58:34 -050051 timestamp: auditLog.createdTimestamp
pineafan63fc5e22022-08-04 22:04:10 +010052 },
53 list: changes,
54 hidden: {
TheCodedProfa16d1672023-01-18 18:58:34 -050055 guild: newRole.guild.id
pineafane625d782022-05-09 18:04:32 +010056 }
TheCodedProf1f675042023-02-16 17:01:29 -050057 }; // TODO: make our own page for this
pineafan63fc5e22022-08-04 22:04:10 +010058 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010059}