Skip to content

DiscordScripts

This file defines the DiscordScripts class, which provides CLI script commands for managing Discord slash and context menu commands in the @sodacore/core Discord integration. These scripts allow users to register or unregister commands globally or for specific guilds, using interactive CLI prompts.


Features

  • Interactive CLI Scripts: Prompts the user for input to determine where to register or unregister Discord commands.
  • Global and Guild Support: Supports both global (application-wide) and guild-specific command registration and unregistration.
  • Integration with Providers: Uses dependency injection to access Discord configuration and the SlashCommandsProvider.
  • User Feedback: Returns clear success or failure messages based on the operation outcome.

Usage

These scripts are intended to be run via the Sodacore CLI interface.

typescript
import DiscordScripts from './script/general';

// Example CLI commands:
// > discord:commands:register
// > discord:commands:unregister

API

Methods

@Script('commands:register') async register(context: ScriptContext): Promise<string>

Prompts the user to choose between global or guild registration.

  • If "global" is selected, registers all commands globally.
  • If "guild" is selected, prompts for a guild ID and registers commands for that guild.
  • Returns a success or failure message.

@Script('commands:unregister') async unregister(context: ScriptContext): Promise<string>

Prompts the user to choose between global or guild unregistration.

  • If "global" is selected, unregisters all global commands.
  • If "guild" is selected, prompts for a guild ID and unregisters commands for that guild.
  • Returns a success or failure message.

How It Works

  1. Namespace Registration: The class is registered under the discord namespace using the @Namespace('discord') decorator.
  2. Script Registration: Each method is registered as a CLI script using the @Script decorator with a unique command name.
  3. Interactive Prompts: Uses the context.prompts API to interactively ask the user for input (e.g., global vs. guild, guild ID).
  4. Provider Integration: Uses injected SlashCommandsProvider to perform the actual registration or unregistration logic.

Notes

  • The scripts provide a user-friendly way to manage Discord commands without editing code or using the Discord Developer Portal.
  • If no selection or guild ID is provided, the operation is cancelled with a message.
  • Designed to be used within the Sodacore CLI environment.

Released under the Apache-2.0 License.