crra/discord/commands/Whois.ts

204 lines
9.0 KiB
TypeScript
Raw Permalink Normal View History

2024-04-02 16:39:49 -04:00
import DiscordInteractionCommand from "../../util/DiscordInteractionCommand";
2024-11-09 18:15:05 -05:00
import { MemberAdditionalAcknowledgement, MemberModel } from "../../database/Member";
2024-10-25 16:57:33 -04:00
import Partner, {
PartnerCommissionType,
PartnerDepartment,
PartnerModel,
PartnerRoleType,
} from "../../database/Partner";
2024-11-09 18:15:05 -05:00
import { ChatInputCommandInteraction, EmbedBuilder, GuildMember, Snowflake } from "discord.js";
2024-04-02 16:39:49 -04:00
import MemberUtil from "../../util/MemberUtil";
2024-10-25 16:57:33 -04:00
import EmojiConfig from "../../util/EmojiConfig";
2024-11-09 18:15:05 -05:00
import Formatters from "../../util/Formatters";
2024-04-02 16:39:49 -04:00
export default class Whois extends DiscordInteractionCommand {
2024-10-25 16:57:33 -04:00
constructor() {
super("whois", "Retrieves information about a user.");
this.builder.addUserOption((option) =>
option
.setName("member")
.setDescription("The member to get information about.")
.setRequired(true)
);
}
2024-04-02 16:39:49 -04:00
2024-10-25 16:57:33 -04:00
public async execute(interaction: ChatInputCommandInteraction) {
// defer our reply and perform database/external API operations/lookups
await interaction.deferReply({ ephemeral: false });
const target = interaction.options.getUser("member", true);
const guild = interaction.guild || interaction.client.guilds.cache.get(this.GUILD_ID);
const guildMember = await guild?.members.fetch(target.id);
const databaseMember = await MemberModel.findOne({ discordID: target.id });
const partner = await PartnerModel.findOne({ discordID: target.id });
// return an error if target was not located
if (!guildMember)
return interaction.editReply({ content: `Member target ${target.id} was not located.` });
// build our embed
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
2024-11-09 18:15:05 -05:00
const formattedName = Formatters.formatName(guildMember, partner);
2024-10-25 16:57:33 -04:00
embed.setAuthor({ name: formattedName.text, iconURL: formattedName.iconURL });
// set the thumbnail to the user's avatar
embed.setThumbnail(guildMember.user.displayAvatarURL());
// initialize the description string
let embedDescription = "";
if (partner) {
// set the title to the partner's title if applicable
if (partner.title) embedDescription += `## __${EmojiConfig.LOC} ${partner.title}__\n`;
embedDescription += "### Partner Information\n";
if (partner.emailAddress) embedDescription += `**Email Address**: ${partner.emailAddress}\n`;
switch (partner.department) {
case PartnerDepartment.ENGINEERING:
embedDescription += "**Department**: Dept. of Engineering\n";
break;
case PartnerDepartment.OPERATIONS:
embedDescription += "**Department**: Dept. of Operations\n";
break;
case PartnerDepartment.INDEPENDENT_AGENCY:
embedDescription += "**Department**: Independent Agency/Contractor\n";
break;
}
switch (partner.commissionType) {
case PartnerCommissionType.TENURE:
embedDescription += "**Commission Type**: Tenure\n";
break;
case PartnerCommissionType.PROVISIONAL:
embedDescription += "**Commission Type**: Provisional\n";
break;
case PartnerCommissionType.CONTRACTUAL:
embedDescription += "**Commission Type**: Contractual/Independent/Collaborator\n";
break;
case PartnerCommissionType.ACTING:
embedDescription += "**Commission Type**: Acting\n";
break;
case PartnerCommissionType.INTERIM:
embedDescription += "**Commission Type**: Interim\n";
break;
case PartnerCommissionType.TRIAL:
embedDescription += "**Commission Type**: Trial/Intern\n";
break;
}
if (partner.directReport) {
2024-11-09 18:15:05 -05:00
// 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`;
2024-10-24 20:40:08 -04:00
}
2024-10-25 16:57:33 -04:00
}
2024-04-02 16:39:49 -04:00
}
2024-10-25 16:57:33 -04:00
embed.setColor(guildMember.displayColor);
2024-11-09 18:15:05 -05:00
if (embedDescription?.length > 0)
embed.setDescription(`${embedDescription}\n\n<@${guildMember.id}>`);
2024-10-25 16:57:33 -04:00
// add status to embed
if (guildMember.presence?.status) {
switch (guildMember.presence.status) {
case "online":
embed.addFields({ name: "Status", value: "Online", inline: true });
break;
case "idle":
embed.addFields({ name: "Status", value: "Idle", inline: true });
break;
case "dnd":
embed.addFields({ name: "Status", value: "Do Not Disturb", inline: true });
break;
2024-11-09 18:15:05 -05:00
case "offline":
2024-10-25 16:57:33 -04:00
embed.addFields({ name: "Status", value: "Online", inline: true });
break;
default:
// TODO: decide what placeholder we should use for values that fall "out of range"
embed.addFields({ name: "Status", value: "", inline: true });
break;
}
2024-11-09 18:15:05 -05:00
} 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(", ") });
2024-10-25 16:57:33 -04:00
}
embed.setFooter({
2024-11-09 18:15:05 -05:00
text: `Discord ID: ${guildMember.id}${databaseMember ? `| Internal ID: ${databaseMember?._id}` : ""}${partner ? ` | Partner ID: ${partner?.id}` : ""}`,
2024-10-25 16:57:33 -04:00
});
return await interaction.editReply({ embeds: [embed] });
}
2024-04-02 16:39:49 -04:00
}