UNPKG

@getsolara/solara.js

Version:

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

68 lines (56 loc) 3.04 kB
const { ButtonBuilder, ButtonStyle } = require('discord.js'); const AUTHOR_ONLY_PREFIX = "solara_ao_"; const MAX_ORIGINAL_ID_LENGTH = 100 - AUTHOR_ONLY_PREFIX.length - 19 - 1; module.exports = { name: "$addAuthorOnlyButton", description: "Adds a button that can only be successfully interacted with by the original command author. Args: customID;label;style(Primary/Secondary/Success/Danger);[disabled(true)];[emoji]", takesBrackets: true, execute: async (context, args) => { if (!context.user || !context.user.id) { return "[Error: $addAuthorOnlyButton cannot determine the author. Make sure this is used in a user-initiated context.]"; } if (args.length < 3) { return "[Error: $addAuthorOnlyButton requires at least customID, label, and style]"; } const [originalCustomId, label, styleStr, disabledStr, emoji] = args; if (typeof originalCustomId !== 'string' || originalCustomId.trim() === "") { return "[Error: customID for $addAuthorOnlyButton cannot be empty.]"; } if (originalCustomId.length > MAX_ORIGINAL_ID_LENGTH) { return `[Error: Button customID "${originalCustomId}" is too long for an author-only button. Max original ID length is ${MAX_ORIGINAL_ID_LENGTH} chars.]`; } if (originalCustomId.startsWith(AUTHOR_ONLY_PREFIX)) { return `[Error: Button customID cannot start with the reserved prefix "${AUTHOR_ONLY_PREFIX}".]`; } let style; switch(styleStr?.toLowerCase()){ case 'primary': style = ButtonStyle.Primary; break; case 'secondary': style = ButtonStyle.Secondary; break; case 'success': style = ButtonStyle.Success; break; case 'danger': style = ButtonStyle.Danger; break; case 'link': return "[Error: Link buttons cannot be author-only using $addAuthorOnlyButton. Use $addButton for link buttons.]"; default: return `[Error: Invalid button style "${styleStr}". Use Primary, Secondary, Success, or Danger]`; } 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 button is too long. This is an internal issue or an extremely long User ID. Original ID: "${originalCustomId}"]`; } const button = new ButtonBuilder() .setCustomId(finalCustomId) .setLabel(label) .setStyle(style); if (emoji) { try { button.setEmoji(emoji); } catch (e) { } } if (disabledStr?.toLowerCase() === 'true') { button.setDisabled(true); } context.components = context.components || []; context.components.push(button.toJSON()); return ""; } };