blob: 9b75fc06d040cfccfc514df7d2c95fc9e301c051 [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { Role } from "discord.js";
2// @ts-expect-error
3import type { HaikuClient } from "jshaiku";
pineafan32767212022-03-14 21:27:39 +00004import getEmojiByName from "../utils/getEmojiByName.js";
5
pineafan63fc5e22022-08-04 22:04:10 +01006export const event = "roleUpdate";
pineafan32767212022-03-14 21:27:39 +00007
pineafan0f5cc782022-08-12 21:55:42 +01008export async function callback(client: HaikuClient, or: Role, nr: Role) {
Skyler Grey11236ba2022-08-08 21:13:33 +01009 const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser, renderRole } = client.logger;
pineafan32767212022-03-14 21:27:39 +000010
pineafan63fc5e22022-08-04 22:04:10 +010011 const auditLog = await getAuditLog(nr.guild, "ROLE_UPDATE");
12 const audit = auditLog.entries.first();
13 if (audit.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>> = {
pineafan63fc5e22022-08-04 22:04:10 +010016 roleId: entry(nr.id, `\`${nr.id}\``),
17 role: entry(nr.id, renderRole(nr)),
18 edited: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey11236ba2022-08-08 21:13:33 +010019 editedBy: entry(audit.executor.id, renderUser((await nr.guild.members.fetch(audit.executor.id)).user))
pineafan63fc5e22022-08-04 22:04:10 +010020 };
21 const mentionable = ["", ""];
22 const hoist = ["", ""];
Skyler Grey11236ba2022-08-08 21:13:33 +010023 mentionable[0] = or.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
24 mentionable[1] = nr.mentionable ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
25 hoist[0] = or.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
26 hoist[1] = nr.hoist ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No`;
pineafan0f5cc782022-08-12 21:55:42 +010027 if (or.name !== nr.name) changes["name"] = entry([or.name, nr.name], `${or.name} -> ${nr.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010028 if (or.position !== nr.position)
pineafan0f5cc782022-08-12 21:55:42 +010029 changes["position"] = entry([or.position, nr.position], `${or.position} -> ${nr.position}`);
30 if (or.hoist !== nr.hoist) changes["showInMemberList"] = entry([or.hoist, nr.hoist], `${hoist[0]} -> ${hoist[1]}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010031 if (or.mentionable !== nr.mentionable)
pineafan0f5cc782022-08-12 21:55:42 +010032 changes["mentionable"] = entry([or.mentionable, nr.mentionable], `${mentionable[0]} -> ${mentionable[1]}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010033 if (or.hexColor !== nr.hexColor)
pineafan0f5cc782022-08-12 21:55:42 +010034 changes["color"] = entry([or.hexColor, nr.hexColor], `\`${or.hexColor}\` -> \`${nr.hexColor}\``);
pineafan63fc5e22022-08-04 22:04:10 +010035
36 if (Object.keys(changes).length === 4) return;
37
38 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010039 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010040 type: "roleUpdate",
41 displayName: "Role Edited",
42 calculateType: "guildRoleUpdate",
43 color: NucleusColors.yellow,
44 emoji: "GUILD.ROLES.EDIT",
45 timestamp: audit.createdTimestamp
46 },
47 list: changes,
48 hidden: {
49 guild: nr.guild.id
pineafane625d782022-05-09 18:04:32 +010050 }
pineafan63fc5e22022-08-04 22:04:10 +010051 }; // TODO: show perms changed (webpage)
52 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +010053}