UNPKG

@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) 3.06 kB
const { MentionableSelectMenuBuilder } = require('discord.js'); const AUTHOR_ONLY_PREFIX = "solara_ao_"; const MAX_ORIGINAL_ID_LENGTH = 100 - AUTHOR_ONLY_PREFIX.length - 19 - 1; module.exports = { name: "$addAuthorOnlyMentionableSelectMenu", description: "Adds a user/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: $addAuthorOnlyMentionableSelectMenu cannot determine the author. Make sure this is used in a user-initiated context.]"; } if (!args[0]) { return "[Error: $addAuthorOnlyMentionableSelectMenu requires at least a customID]"; } const [originalCustomId, placeholder, minValuesStr, maxValuesStr, disabledStr] = args; if (typeof originalCustomId !== 'string' || originalCustomId.trim() === "") { return "[Error: customID for $addAuthorOnlyMentionableSelectMenu cannot be empty.]"; } if (originalCustomId.length > MAX_ORIGINAL_ID_LENGTH) { return `[Error: Mentionable 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: Mentionable 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 mentionable 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 mentionable select menu (0-25)]"; } if (isNaN(maxValues) || maxValues < 1 || maxValues > 25) { return "[Error: Invalid maxValues for mentionable select menu (1-25)]"; } if (minValues > maxValues) { return "[Error: minValues cannot be greater than maxValues]"; } const selectMenu = new MentionableSelectMenuBuilder() .setCustomId(finalCustomId) .setPlaceholder(placeholder || "Select users or roles...") .setMinValues(minValues) .setMaxValues(maxValues) .setDisabled(disabled); context.components = context.components || []; context.components.push(selectMenu.toJSON()); return ""; } };