blob: fc26d6cdb12a6551684dd9860cde7065c891968b [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { 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
PineaFan752af462022-12-31 21:59:38 +00007export async function callback(client: NucleusClient, or: Role, nr: 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
pineafan63fc5e22022-08-04 22:04:10 +010010 const auditLog = await getAuditLog(nr.guild, "ROLE_UPDATE");
11 const audit = auditLog.entries.first();
12 if (audit.executor.id === client.user.id) return;
pineafan32767212022-03-14 21:27:39 +000013
pineafan0f5cc782022-08-12 21:55:42 +010014 const changes: Record<string, ReturnType<typeof entry>> = {
pineafan63fc5e22022-08-04 22:04:10 +010015 roleId: entry(nr.id, `\`${nr.id}\``),
16 role: entry(nr.id, renderRole(nr)),
17 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey11236ba2022-08-08 21:13:33 +010018 editedBy: entry(audit.executor.id, renderUser((await nr.guild.members.fetch(audit.executor.id)).user))
pineafan63fc5e22022-08-04 22:04:10 +010019 };
20 const mentionable = ["", ""];
21 const hoist = ["", ""];
Skyler Grey11236ba2022-08-08 21:13:33 +010022 mentionable[0] = or.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
23 mentionable[1] = nr.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
24 hoist[0] = or.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
25 hoist[1] = nr.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
pineafan0f5cc782022-08-12 21:55:42 +010026 if (or.name !== nr.name) changes["name"] = entry([or.name, nr.name], `${or.name} -> ${nr.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010027 if (or.position !== nr.position)
pineafan0f5cc782022-08-12 21:55:42 +010028 changes["position"] = entry([or.position, nr.position], `${or.position} -> ${nr.position}`);
29 if (or.hoist !== nr.hoist) changes["showInMemberList"] = entry([or.hoist, nr.hoist], `${hoist[0]} -> ${hoist[1]}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (or.mentionable !== nr.mentionable)
pineafan0f5cc782022-08-12 21:55:42 +010031 changes["mentionable"] = entry([or.mentionable, nr.mentionable], `${mentionable[0]} -> ${mentionable[1]}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010032 if (or.hexColor !== nr.hexColor)
pineafan0f5cc782022-08-12 21:55:42 +010033 changes["color"] = entry([or.hexColor, nr.hexColor], `\`${or.hexColor}\` -> \`${nr.hexColor}\``);
pineafan63fc5e22022-08-04 22:04:10 +010034
35 if (Object.keys(changes).length === 4) return;
36
37 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010038 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010039 type: "roleUpdate",
40 displayName: "Role Edited",
41 calculateType: "guildRoleUpdate",
42 color: NucleusColors.yellow,
43 emoji: "GUILD.ROLES.EDIT",
44 timestamp: audit.createdTimestamp
45 },
46 list: changes,
47 hidden: {
48 guild: nr.guild.id
pineafane625d782022-05-09 18:04:32 +010049 }
pineafan63fc5e22022-08-04 22:04:10 +010050 }; // TODO: show perms changed (webpage)
51 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010052}