UNPKG

@getsolara/solara.js

Version:

A lightweight and modular Discord bot framework built on discord.js v14, with truly optional feature packages.

86 lines (74 loc) 4.38 kB
const { StringSelectMenuBuilder, UserSelectMenuBuilder, RoleSelectMenuBuilder, ChannelSelectMenuBuilder, 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: "$addAuthorOnlySelectMenu", description: "Adds a select menu of a specified type that only the author can interact with. For String type, follow with $addSelectMenuOption. Args: type(string/user/role/channel/mentionable);customID;placeholder;[minValues=1];[maxValues=1];[disabled(true)]", takesBrackets: true, execute: async (context, args) => { if (!context.user || !context.user.id) { return "[Error: $addAuthorOnlySelectMenu cannot determine the author. Make sure this is used in a user-initiated context.]"; } if (args.length < 2) { return "[Error: $addAuthorOnlySelectMenu requires at least type and customID]"; } const [typeStr, originalCustomId, placeholder, minValuesStr, maxValuesStr, disabledStr] = args; if (typeof originalCustomId !== 'string' || originalCustomId.trim() === "") { return "[Error: customID for $addAuthorOnlySelectMenu cannot be empty.]"; } if (originalCustomId.length > MAX_ORIGINAL_ID_LENGTH) { return `[Error: 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: 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 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 (0-25)]"; } if (isNaN(maxValues) || maxValues < 1 || maxValues > 25) { return "[Error: Invalid maxValues (1-25)]"; } if (minValues > maxValues) { return "[Error: minValues > maxValues]"; } let selectMenuBuilder; const type = typeStr?.toLowerCase(); switch(type) { case 'string': selectMenuBuilder = new StringSelectMenuBuilder(); break; case 'user': selectMenuBuilder = new UserSelectMenuBuilder(); break; case 'role': selectMenuBuilder = new RoleSelectMenuBuilder(); break; case 'channel': selectMenuBuilder = new ChannelSelectMenuBuilder(); break; case 'mentionable': selectMenuBuilder = new MentionableSelectMenuBuilder(); break; default: return "[Error: Invalid select menu type for $addAuthorOnlySelectMenu. Use string, user, role, channel, or mentionable.]"; } selectMenuBuilder .setCustomId(finalCustomId) .setPlaceholder(placeholder || `Select ${type}... (author only)`) .setMinValues(minValues) .setMaxValues(maxValues) .setDisabled(disabled); if (type === 'string') { context.currentComponent = selectMenuBuilder; context.components = context.components || []; const existingComponentIndex = context.components.findIndex(c => c.custom_id === finalCustomId && c.type === 3); if (existingComponentIndex !== -1) { context.components[existingComponentIndex] = selectMenuBuilder.toJSON(); } else { context.components.push(selectMenuBuilder.toJSON()); } } else { context.components = context.components || []; context.components.push(selectMenuBuilder.toJSON()); context.currentComponent = null; } return ""; } };