@getsolara/solara.js
Version:
A lightweight and modular Discord bot framework built on discord.js v14, with truly optional feature packages.
62 lines (52 loc) • 2.97 kB
JavaScript
const { RoleSelectMenuBuilder } = require('discord.js');
const AUTHOR_ONLY_PREFIX = "solara_ao_";
const MAX_ORIGINAL_ID_LENGTH = 100 - AUTHOR_ONLY_PREFIX.length - 19 - 1;
module.exports = {
name: "$addAuthorOnlyRoleSelectMenu",
description: "Adds a role select menu that can only be successfully interacted with by the original command author. Args: customID;placeholder;[minValues=1];[maxValues=1];[disabled?]",
takesBrackets: true,
execute: async (context, args) => {
if (!context.user || !context.user.id) {
return "[Error: $addAuthorOnlyRoleSelectMenu cannot determine the author. Make sure this is used in a user-initiated context.]";
}
if (!args[0]) {
return "[Error: $addAuthorOnlyRoleSelectMenu requires at least a customID]";
}
const [originalCustomId, placeholder, minValuesStr, maxValuesStr, disabledStr] = args;
if (typeof originalCustomId !== 'string' || originalCustomId.trim() === "") {
return "[Error: customID for $addAuthorOnlyRoleSelectMenu cannot be empty.]";
}
if (originalCustomId.length > MAX_ORIGINAL_ID_LENGTH) {
return `[Error: Role select menu customID "${originalCustomId}" is too long for an author-only menu. Max original ID length is ${MAX_ORIGINAL_ID_LENGTH} chars.]`;
}
if (originalCustomId.startsWith(AUTHOR_ONLY_PREFIX)) {
return `[Error: Role select menu customID cannot start with the reserved prefix "${AUTHOR_ONLY_PREFIX}".]`;
}
const authorId = context.user.id;
const finalCustomId = `${AUTHOR_ONLY_PREFIX}${authorId}_${originalCustomId}`;
if (finalCustomId.length > 100) {
return `[Error: The final generated customID for the author-only role select menu is too long. Original ID: "${originalCustomId}"]`;
}
const minValues = minValuesStr ? parseInt(minValuesStr, 10) : 1;
const maxValues = maxValuesStr ? parseInt(maxValuesStr, 10) : 1;
const disabled = disabledStr?.toLowerCase() === 'true';
if (isNaN(minValues) || minValues < 0 || minValues > 25) {
return "[Error: Invalid minValues for role select menu (0-25)]";
}
if (isNaN(maxValues) || maxValues < 1 || maxValues > 25) {
return "[Error: Invalid maxValues for role select menu (1-25)]";
}
if (minValues > maxValues) {
return "[Error: minValues cannot be greater than maxValues]";
}
const selectMenu = new RoleSelectMenuBuilder()
.setCustomId(finalCustomId)
.setPlaceholder(placeholder || "Select roles...")
.setMinValues(minValues)
.setMaxValues(maxValues)
.setDisabled(disabled);
context.components = context.components || [];
context.components.push(selectMenu.toJSON());
return "";
}
};