1
0
Fork 0

Compare commits

..

6 Commits
dev ... master

Author SHA1 Message Date
Harry 54845033f1 Update discord/events/MessageReactionRemove.ts
Switched error messages to ephermal: true

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 20:34:31 -05:00
Harry 65f4bd92dc Update discord/events/MessageReactionAdd.ts
switched error messages to Ephemeral: true

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 20:33:31 -05:00
Harry e6dae3ae48 MessageReactionAdd.ts
Pins a message when a partner adds a 📌 reaction to it

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 18:02:28 -05:00
Harry 9374a1bca6 MessageReactionRemove.ts
Unpins a message when a partner removes the 📌 reaction from it

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 18:00:58 -05:00
Harry b37ac915c2 MessageReactionAdd.ts
Pins messages in a channel when a partner adds a 📌 reaction

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 17:54:35 -05:00
Harry 6f561f3b44 MessagePinReactionHandler.ts
/**
 * Handles both 'messageReactionAdd' and 'messageReactionRemove' events.
 * When a reaction is added or removed:
 * - If the reaction matches the specified emoji and the user has the required role,
 *   the message will be pinned or unpinned in the channel accordingly.
 */

Signed-off-by: Harry <harry@harryrosestudios.com>
2024-12-20 02:12:27 -05:00
12 changed files with 187 additions and 172 deletions

View File

@ -1,4 +1,4 @@
# Contributing to LOC's Community Relations Gamma Edition System
# Contributing to LOC's Community Relations Alpha Edition System
Thank you for considering contributing to this project! Your contributions are highly valued, and were excited to collaborate with you.

38
MessageReactionAdd.ts Normal file
View File

@ -0,0 +1,38 @@
import DiscordEvent from "../../util/DiscordEvent";
import { MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js";
import MemberUtil from "../../util/MemberUtil";
const PUSH_PIN_EMOJI = "📌"; // Unicode
export default class MessageReactionAdd extends DiscordEvent {
constructor(client) {
super(Events.MessageReactionAdd, client);
}
public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise<void> {
try {
// Fetch partials if necessary
if (reaction.partial) await reaction.fetch();
// Condensed check for guild existence and emoji name
if (!reaction.message.guild || reaction.emoji.name !== PUSH_PIN_EMOJI) return;
const guild = reaction.message.guild;
const member = await guild.members.fetch(user.id);
// Combine all partner roles from PartnerDiscordRoleMap
const allPartnerRoles = Object.values(MemberUtil.PartnerDiscordRoleMap).flat();
// Check if the user has any of the partner roles
if (!member.roles.cache.some(role => allPartnerRoles.includes(role.id))) return;
// Attempt to pin the message
await reaction.message.pin();
const channel = reaction.message.channel;
await channel.send(`Pinned message: ${reaction.message.id}`);
} catch (error) {
const channel = reaction.message.channel;
await channel.send(`Error pinning message: ${error}`);
}
}
}

View File

@ -1,7 +1,9 @@
# Community Relations v2 Gamma Edition System - CRRA/G
# Community Relations Alpha Edition System - CRRA
[![License](https://img.shields.io/badge/license-AGPLv3-blue.svg)](LICENSE)
A brief description of what your project does, what problem it solves, and why its useful.
---
## Table of Contents
@ -86,4 +88,4 @@ __Library of Code Department of Engineering__ @:
- [Email](mailto:engineering@libraryofcode.org)
---
Thank you for checking out CRv2!
Thank you for checking out CRRA!

View File

@ -1,4 +1,4 @@
import { prop, getModelForClass, Ref } from "@typegoose/typegoose";
import { prop, getModelForClass } from "@typegoose/typegoose";
import Member from "./Member";
/* TODO
@ -60,9 +60,9 @@ export default class Partner implements SharedMemberAttributes {
@prop({ required: true })
public title: PartnerTitle | "Partner" | undefined;
@prop({ ref: () => Partner })
@prop()
//
public directReport?: Ref<Partner> | string | undefined;
public directReport: Partner | string | undefined;
@prop()
// this field dictates if the partner is able to perform developer commands, such as "eval"

View File

@ -2,11 +2,10 @@ import DiscordInteractionCommand from "../../util/DiscordInteractionCommand";
import { ChatInputCommandInteraction } from "discord.js";
import { inspect } from "util";
import { discordBotToken } from "../../config.json";
import { PartnerModel } from "../../database/Partner";
export default class Eval extends DiscordInteractionCommand {
// This is a list of IDs that are allowed to use this command.
private listOfAllowedIDs: string[] = [];
private listOfAllowedIDs: string[];
constructor() {
super("eval", "Executes arbitrary JS code and returns the output.");
@ -26,13 +25,9 @@ export default class Eval extends DiscordInteractionCommand {
option.setName("depth").setDescription("The depth of the inspection.").setRequired(false)
);
// this checks against the database and adds all of the partners that are "allowed to perform dev commands"
// doing the database check in the initialization prevents us from having to check the database every time this command is ran
PartnerModel.find({ canPerformDevCommands: true }).then((partners) => {
for (const partner of partners) {
this.listOfAllowedIDs.push(partner.discordID as string);
}
});
this.listOfAllowedIDs = [
"278620217221971968", // Matthew
];
}
public async execute(interaction: ChatInputCommandInteraction) {

View File

@ -1,15 +1,14 @@
import DiscordInteractionCommand from "../../util/DiscordInteractionCommand";
import { MemberAdditionalAcknowledgement, MemberModel } from "../../database/Member";
import { MemberModel } from "../../database/Member";
import Partner, {
PartnerCommissionType,
PartnerDepartment,
PartnerModel,
PartnerRoleType,
} from "../../database/Partner";
import { ChatInputCommandInteraction, EmbedBuilder, GuildMember, Snowflake } from "discord.js";
import { ChatInputCommandInteraction, EmbedBuilder, GuildMember } from "discord.js";
import MemberUtil from "../../util/MemberUtil";
import EmojiConfig from "../../util/EmojiConfig";
import Formatters from "../../util/Formatters";
export default class Whois extends DiscordInteractionCommand {
constructor() {
@ -37,7 +36,7 @@ export default class Whois extends DiscordInteractionCommand {
const embed = new EmbedBuilder();
// if the role type is managerial, add a [k] to the end of the name
// if the partner exists, set the iconURL to the organizational logo
const formattedName = Formatters.formatName(guildMember, partner);
const formattedName = MemberUtil.formatName(guildMember, partner);
embed.setAuthor({ name: formattedName.text, iconURL: formattedName.iconURL });
// set the thumbnail to the user's avatar
embed.setThumbnail(guildMember.user.displayAvatarURL());
@ -80,23 +79,16 @@ export default class Whois extends DiscordInteractionCommand {
break;
}
if (partner.directReport) {
// fetch direct report object ref
await partner.populate("directReport");
// ensures that the population propagated correctly before adding to embed
if (partner.directReport instanceof PartnerModel) {
const directReportGuildMember = await guild?.members.fetch(
partner.directReport.discordID as Snowflake
);
// fetches GuildMember for the direct report
embedDescription += `**Direct Report**: ${directReportGuildMember ? Formatters.formatName(directReportGuildMember, partner.directReport).text + ", " : ""}${partner.directReport.title}\n`;
if (partner.directReport instanceof Partner) {
embedDescription += `**Direct Report**: ${partner.directReport.title}\n`;
}
}
}
embed.setColor(guildMember.displayColor);
if (embedDescription?.length > 0)
embed.setDescription(`${embedDescription}\n\n<@${guildMember.id}>`);
if (embedDescription?.length > 0) embed.setDescription(embedDescription);
// add status to embed
if (guildMember.presence?.status) {
// TODO: this currently doesn't work for some reason
switch (guildMember.presence.status) {
case "online":
embed.addFields({ name: "Status", value: "Online", inline: true });
@ -107,7 +99,7 @@ export default class Whois extends DiscordInteractionCommand {
case "dnd":
embed.addFields({ name: "Status", value: "Do Not Disturb", inline: true });
break;
case "offline":
case "offline" || "invisible":
embed.addFields({ name: "Status", value: "Online", inline: true });
break;
default:
@ -115,87 +107,9 @@ export default class Whois extends DiscordInteractionCommand {
embed.addFields({ name: "Status", value: "", inline: true });
break;
}
} else {
embed.addFields({ name: "Status", value: "Offline", inline: true });
}
// calculations for joined / created at
embed.addFields(
{
name: "Joined At",
value: guildMember.joinedTimestamp
? `<t:${Math.floor(guildMember.joinedTimestamp / 1000)}>`
: "Invalid Date",
inline: true,
},
{
name: "Created At",
value: guildMember.user.createdTimestamp
? `<t:${Math.floor(guildMember.user.createdTimestamp / 1000)}>`
: "Invalid Date",
inline: true,
}
);
// TODO: calculations for commscore
embed.addFields({ name: "CommScore™", value: "[PLACEHOLDER]", inline: false });
// role calculation (sorting roles by their position)
let roleString = "";
for (const role of guildMember.roles.valueOf().sort((a, b) => b.position - a.position)) {
roleString += `<@&${role[1].id}> `;
}
if (roleString) {
embed.addFields({ name: "Roles", value: roleString });
} else {
embed.addFields({ name: "Roles", value: "None" });
}
// listing permissions
const serializedPermissions = guildMember.permissions.serialize();
const permissionsArray: string[] = [];
// adding serialized string representation of permissions to array to use in embed field
if (serializedPermissions.Administrator) permissionsArray.push("Administrator");
if (serializedPermissions.ManageGuild) permissionsArray.push("Manage Guild");
if (serializedPermissions.ManageChannels) permissionsArray.push("Manage Channels");
if (serializedPermissions.ManageRoles) permissionsArray.push("Manage Roles");
if (serializedPermissions.ManageMessages) permissionsArray.push("Manage Messages");
if (serializedPermissions.ManageEvents) permissionsArray.push("Manage Events");
if (serializedPermissions.ManageNicknames) permissionsArray.push("Manage Nicknames");
if (serializedPermissions.ManageEmojisAndStickers) permissionsArray.push("Manage Emojis");
if (serializedPermissions.ManageWebhooks) permissionsArray.push("Manage Webhooks");
if (serializedPermissions.ModerateMembers) permissionsArray.push("Moderate Members");
if (serializedPermissions.BanMembers) permissionsArray.push("Ban Members");
if (serializedPermissions.KickMembers) permissionsArray.push("Kick Members");
if (serializedPermissions.DeafenMembers) permissionsArray.push("Deafen Members");
// setting key permissions embed field
if (permissionsArray?.length > 0) {
embed.addFields({ name: "Key Permissions", value: permissionsArray.join(", ") });
}
// determining acknowledgements: MemberAdditionalAcknowledgement || "Guild Owner", "Guild Admin", "Guild Manager", "Guild Moderator"
const acknowledgementsArray: MemberAdditionalAcknowledgement[] = [];
if (guildMember.id === guildMember.guild.ownerId) {
acknowledgementsArray.push("Guild Owner");
} else if (serializedPermissions.Administrator) {
acknowledgementsArray.push("Guild Admin");
} else if (serializedPermissions.ManageGuild) {
acknowledgementsArray.push("Guild Manager");
} else if (serializedPermissions.ModerateMembers || serializedPermissions.ManageMessages) {
acknowledgementsArray.push("Guild Moderator");
}
if (partner?.canPerformDevCommands) {
acknowledgementsArray.push("System Developer");
}
// adding acknowledgements to embed
if (acknowledgementsArray.length > 0) {
embed.addFields({ name: "Acknowledgements", value: acknowledgementsArray.join(", ") });
}
embed.setFooter({
text: `Discord ID: ${guildMember.id}${databaseMember ? `| Internal ID: ${databaseMember?._id}` : ""}${partner ? ` | Partner ID: ${partner?.id}` : ""}`,
text: `Discord ID: ${guildMember.id}${databaseMember ? `Internal ID: ${databaseMember?._id}` : ""}`,
});
return await interaction.editReply({ embeds: [embed] });

View File

@ -0,0 +1,44 @@
import DiscordEvent from "../../util/DiscordEvent";
import { MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js";
import MemberUtil from "../../util/MemberUtil";
const PUSH_PIN_EMOJI = "📌"; // Unicode
export default class MessageReactionAdd extends DiscordEvent {
constructor(client) {
super(Events.MessageReactionAdd, client);
}
public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise<void> {
try {
// Fetch partials if necessary
if (reaction.partial) await reaction.fetch();
// Condensed check for guild existence and emoji name
if (!reaction.message.guild || reaction.emoji.name !== PUSH_PIN_EMOJI) return;
const guild = reaction.message.guild;
const member = await guild.members.fetch(user.id);
// Combine all partner roles from PartnerDiscordRoleMap
const allPartnerRoles = Object.values(MemberUtil.PartnerDiscordRoleMap).flat();
// Check if the user has any of the partner roles
if (!member.roles.cache.some(role => allPartnerRoles.includes(role.id))) return;
// Attempt to pin the message
await reaction.message.pin();
console.log(`Pinned message: ${reaction.message.id}`);
} catch (error) {
try {
const dmChannel = await user.createDM();
await dmChannel.send({
content: `There was an error pinning the message: ${error.message}`,
ephemeral: true,
});
} catch (dmError) {
console.error(`Failed to send ephemeral error message to ${user.tag}: ${dmError}`);
}
}
}
}

View File

@ -0,0 +1,44 @@
import DiscordEvent from "../../util/DiscordEvent";
import { MessageReaction, PartialMessageReaction, PartialUser, User, Events } from "discord.js";
import MemberUtil from "../../util/MemberUtil";
const PUSH_PIN_EMOJI = "📌"; // Unicode
export default class MessageReactionRemove extends DiscordEvent {
constructor(client) {
super(Events.MessageReactionRemove, client);
}
public async execute(reaction: MessageReaction | PartialMessageReaction, user: User | PartialUser): Promise<void> {
try {
// Fetch partials if necessary
if (reaction.partial) await reaction.fetch();
// Condensed check for guild existence and emoji name
if (!reaction.message.guild || reaction.emoji.name !== PUSH_PIN_EMOJI) return;
const guild = reaction.message.guild;
const member = await guild.members.fetch(user.id);
// Combine all partner roles from PartnerDiscordRoleMap
const allPartnerRoles = Object.values(MemberUtil.PartnerDiscordRoleMap).flat();
// Check if the user has any of the partner roles
if (!member.roles.cache.some(role => allPartnerRoles.includes(role.id))) return;
// Attempt to unpin the message
await reaction.message.unpin();
console.log(`Unpinned message: ${reaction.message.id}`);
} catch (error) {
try {
const dmChannel = await user.createDM();
await dmChannel.send({
content: `There was an error unpinning the message: ${error.message}`,
ephemeral: true,
});
} catch (dmError) {
console.error(`Failed to send ephemeral error message to ${user.tag}: ${dmError}`);
}
}
}
}

View File

@ -1,5 +1,4 @@
{
"name": "crv2",
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@eslint/js": "^9.13.0",
@ -19,7 +18,6 @@
},
"dependencies": {
"@typegoose/typegoose": "^12.2.0",
"auth0": "^4.12.0",
"discord.js": "^14.14.1",
"mongoose": "^8.2.2",
"stripe": "^14.21.0",

View File

@ -8,5 +8,5 @@ export default abstract class DiscordEvent {
this.client = client;
this.execute = this.execute.bind(this);
}
public abstract execute(...args: never[]): Error | Promise<void>;
public abstract execute(...args: any[]): Error | Promise<void>;
}

View File

@ -1,55 +0,0 @@
import { GuildMember, User } from "discord.js";
import Partner, { PartnerCommissionType, PartnerRoleType } from "../database/Partner";
import { FormatNameOptions } from "./MemberUtil";
export default class Formatters {
public static formatStandardDate(date: Date | string | number): string {
const resolvedDate = new Date(date);
if (!resolvedDate) return "";
const year = resolvedDate.getFullYear();
const month = String(resolvedDate.getMonth() + 1).padStart(2, "0");
const day = String(resolvedDate.getDate()).padStart(2, "0");
const hours = String(resolvedDate.getHours()).padStart(2, "0");
const minutes = String(resolvedDate.getMinutes()).padStart(2, "0");
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return `${year}-${month}-${day} @ ${hours}:${minutes} (${timeZone})`;
}
// TODO: comments and extended formatting
public static formatName(
target: GuildMember | User,
partner?: Partner | null
): FormatNameOptions {
console.debug(
`[MemberUtil] Formatting name for ${target.displayName} at url ${target instanceof GuildMember ? target.user.displayAvatarURL() : target.displayAvatarURL()}`
);
// if the role type is managerial, add a [k] to the end of the name
// if the partner exists, set the iconURL to the organizational logo
if (partner?.roleType == PartnerRoleType.MANAGERIAL) {
return {
text: `${target.displayName} [k]`,
iconURL: target.displayAvatarURL(),
};
} else if (partner?.commissionType == PartnerCommissionType.CONTRACTUAL) {
// if the commission type is contractual, add a [c] to the end of the name
return {
text: `${target.displayName} [c]`,
iconURL:
target instanceof GuildMember
? target.user.displayAvatarURL()
: target.displayAvatarURL(),
};
} else {
// otherwise, just set the author to the member's display name
return {
text: target.displayName,
iconURL:
target instanceof GuildMember
? target.user.displayAvatarURL()
: target.displayAvatarURL(),
};
}
}
}

View File

@ -8,14 +8,13 @@ import Partner, {
import Member, { MemberAdditionalAcknowledgement, MemberModel } from "../database/Member";
import { Client, GuildMember, User } from "discord.js";
import { guildID } from "../config.json";
import { Ref } from "@typegoose/typegoose";
export interface PartnerOptions {
roleType: PartnerRoleType;
commissionType: PartnerCommissionType;
department: PartnerDepartment;
title: PartnerTitle;
directReport: Ref<Partner>;
directReport: Partner | string;
}
export interface FormatNameOptions {
@ -26,7 +25,7 @@ export interface FormatNameOptions {
// TODO: Add the rest of the remaining role configurations
export const PartnerDiscordRoleMap = {
// Director of Engineering, Management, Staff, Technician, Core Team, Play Caller
Engineering: [
"Director of Engineering": [
"1077646568091570236",
"1077646956890951690",
"446104438969466890",
@ -35,7 +34,7 @@ export const PartnerDiscordRoleMap = {
"1014978134573064293",
],
// Director of Operations, Management, Staff, Moderator, Core Team, Play Caller
Operations: [
"Director of Operations": [
"1077647072163020840",
"1077646956890951690",
"446104438969466890",
@ -80,4 +79,40 @@ export default class MemberUtil {
{ $push: { additionalAcknowledgement: acknowledgement } }
);
}
// TODO: comments and extended formatting
public static formatName(
target: GuildMember | User,
partner?: Partner | null
): FormatNameOptions {
console.log(
`[MemberUtil] Formatting name for ${target.displayName} at url ${target instanceof GuildMember ? target.user.displayAvatarURL() : target.displayAvatarURL()}`
);
// if the role type is managerial, add a [k] to the end of the name
// if the partner exists, set the iconURL to the organizational logo
if (partner?.roleType == PartnerRoleType.MANAGERIAL) {
return {
text: `${target.displayName} [k]`,
iconURL: target.displayAvatarURL(),
};
} else if (partner?.commissionType == PartnerCommissionType.CONTRACTUAL) {
// if the commission type is contractual, add a [c] to the end of the name
return {
text: `${target.displayName} [c]`,
iconURL:
target instanceof GuildMember
? target.user.displayAvatarURL()
: target.displayAvatarURL(),
};
} else {
// otherwise, just set the author to the member's display name
return {
text: target.displayName,
iconURL:
target instanceof GuildMember
? target.user.displayAvatarURL()
: target.displayAvatarURL(),
};
}
}
}