UNPKG

@getsolara/solara.js

Version:

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

75 lines (64 loc) 3.71 kB
const { ChannelSelectMenuBuilder } = require('discord.js'); const AUTHOR_ONLY_PREFIX = "solara_ao_"; const MAX_ORIGINAL_ID_LENGTH = 100 - AUTHOR_ONLY_PREFIX.length - 19 - 1; module.exports = { name: "$addAuthorOnlyChannelSelectMenu", description: "Adds a channel select menu that can only be successfully interacted with by the original command author. Args: customID;placeholder;[minValues=1];[maxValues=1];[channelTypesJsonArrayString?];[disabled?]", takesBrackets: true, execute: async (context, args) => { if (!context.user || !context.user.id) { return "[Error: $addAuthorOnlyChannelSelectMenu cannot determine the author. Make sure this is used in a user-initiated context.]"; } if (!args[0]) { return "[Error: $addAuthorOnlyChannelSelectMenu requires at least a customID]"; } const [originalCustomId, placeholder, minValuesStr, maxValuesStr, typesJson, disabledStr] = args; if (typeof originalCustomId !== 'string' || originalCustomId.trim() === "") { return "[Error: customID for $addAuthorOnlyChannelSelectMenu cannot be empty.]"; } if (originalCustomId.length > MAX_ORIGINAL_ID_LENGTH) { return `[Error: Channel 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: Channel 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 channel 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 channel select menu (0-25)]"; } if (isNaN(maxValues) || maxValues < 1 || maxValues > 25) { return "[Error: Invalid maxValues for channel select menu (1-25)]"; } if (minValues > maxValues) { return "[Error: minValues cannot be greater than maxValues]"; } const selectMenu = new ChannelSelectMenuBuilder() .setCustomId(finalCustomId) .setPlaceholder(placeholder || "Select channels...") .setMinValues(minValues) .setMaxValues(maxValues) .setDisabled(disabled); if (typesJson) { try { const channelTypes = JSON.parse(typesJson); if (Array.isArray(channelTypes) && channelTypes.every(type => typeof type === 'number')) { selectMenu.setChannelTypes(channelTypes); } else { return "[Error: channelTypes must be a JSON array of valid Discord.js ChannelType numbers (e.g., [0, 5] for Text and Announcement).]"; } } catch (e) { return "[Error: Invalid channelTypes JSON array string. Example: '[0, 2]' for Text and Voice channels.]"; } } context.components = context.components || []; context.components.push(selectMenu.toJSON()); return ""; } };