Slash Command Deployment
Relevant source files
The following files were used as context for generating this wiki page:Slash Command Deployment is the process of registering the bot's application commands with Discord's API. This makes the commands discoverable and usable by users within a server (guild) or across all servers the bot is in. The discord-live-translator project provides two primary methods for deploying these commands: a manual script for global deployment and an automatic process for guild-specific deployment on bot startup.
This system is crucial for the bot's operation, as all user interactions are initiated through these registered slash commands. The deployment process gathers command definitions from various files, formats them into the structure required by the Discord API, and sends them via REST requests.
Sources: README.md:30-35, src/deploy.ts, src/app.ts:43-49
Deployment Mechanisms
The project supports two distinct deployment strategies: manual global deployment and automatic guild-level deployment.
Manual Global Deployment
This method is intended for registering commands globally, making them available in all guilds the bot joins. It is triggered by running a script defined in package.json.
- Script Invocation: The deployment is initiated by running
pnpm run deploy.
Sources: package.json:11, README.md:33-35 - Execution: This command executes the
src/deploy.tsfile usingts-node.
Sources: package.json:10-12"scripts": { "deploy": "ts-node src/deploy.ts", }, - Process: The
src/deploy.tsscript reads theBOT_TOKENandCLIENT_IDenvironment variables, then calls theinitGlobalCommandsfunction to perform the registration.
Sources: src/deploy.ts:3-7
The following diagram illustrates the sequence of events during manual global deployment.
This diagram shows the flow from a user running the deploy script to the commands being registered with the Discord API.
Sources: package.json:11, src/deploy.ts, src/interactions.ts:30-41
Automatic Guild Deployment
For development or testing, commands can be deployed automatically to a specific guild whenever the bot starts. This provides a faster update cycle than global deployment.
- Trigger: This process is triggered on bot startup if the
GUILD_ID,CLIENT_ID, andBOT_TOKENenvironment variables are all set.
Sources: README.md:36-38, src/app.ts:43-49 - Process: The main application file,
src/app.ts, checks for these environment variables and, if present, calls theinitGuildCommandsfunction fromsrc/interactions.ts.
Sources: src/app.ts:43-49
The logic for this automatic deployment is shown in the flowchart below.
This diagram outlines the decision process for automatic guild command deployment on application start.
Sources: src/app.ts:43-49, src/interactions.ts:19-28
Command Structure and Registration
All slash commands are built using @discordjs/builders and aggregated in src/interactions.ts before being sent to Discord.
Command Definitions
Each command is defined in its own file within the src/commands/ directory. The SlashCommandBuilder is used to define the command's name, description, and any options it accepts.
For example, the /start command includes a required string option for the target language, with choices populated dynamically from a list of supported languages.
// From src/commands/start.ts
export const startCommand = new SlashCommandBuilder()
.setName('start')
.setDescription('Start the translation')
.addStringOption((option) =>
option
.setName('target')
.setDescription('Target Language')
.addChoices(...languageChoices)
.setRequired(true)
);
Sources: src/commands/start.ts:12-22
Command Aggregation
The src/interactions.ts file imports all command builders and compiles them into a single array. This array's JSON representation is used as the body for the API request to Discord.
// From src/interactions.ts
const commands = [
joinCommand.toJSON(),
languagesCommand.toJSON(),
leaveCommand.toJSON(),
startCommand.toJSON(),
statusCommand.toJSON(),
stopCommand.toJSON()
];
Sources: src/interactions.ts:7-17
Available Commands
The following slash commands are defined and deployed by this system:
| Command | Description | Source File |
|---|---|---|
/join | Move the bot into your voice channel | src/commands/join.ts |
/languages | Show all available languages | src/commands/languages.ts |
/leave | Move the bot out of your voice channel | src/commands/leave.ts |
/start | Start the translation | src/commands/start.ts |
/status | Show your current settings | src/commands/status.ts |
/stop | Stop the translation | src/commands/stop.ts |
Sources: src/interactions.ts:7-17, src/commands/join.ts:25-27, src/commands/languages.ts:8-10, src/commands/leave.ts:10-12, src/commands/start.ts:12-14, src/commands/status.ts:8-10, src/commands/stop.ts:6-8
Registration Functions
The core logic for communicating with the Discord API resides in src/interactions.ts. It uses @discordjs/rest to send a PUT request.
initGlobalCommands: Registers commands globally. It makes aPUTrequest toRoutes.applicationCommands(clientId).
Sources: src/interactions.ts:30-41initGuildCommands: Registers commands for a specific guild. It makes aPUTrequest toRoutes.applicationGuildCommands(clientId, guildId).
Sources: src/interactions.ts:19-28
Configuration
Proper configuration via environment variables is essential for the deployment process to succeed. These variables are parsed in src/env.ts.
| Variable | Description | Required for Deployment |
|---|---|---|
BOT_TOKEN | The authentication token for the Discord bot. | Always |
CLIENT_ID | The application's client ID. | Always |
GUILD_ID | The ID of the guild for automatic deployment. | For automatic deployment only |
Sources: src/env.ts:11-14, src/deploy.ts:3-5, src/app.ts:43
Summary
The slash command deployment system for discord-live-translator is a well-defined process that leverages discord.js libraries to register commands with Discord. It offers flexibility with both a manual, global deployment script (pnpm run deploy) for production and an automatic, guild-specific deployment on startup for development and testing. This dual approach ensures that the bot's primary user interface—its slash commands—can be managed efficiently and reliably.
Sources: README.md:30-38, src/deploy.ts, src/app.ts:43-49
Relevant source files
The following files were used as context for generating this wiki page:Slash Command Deployment is the process of registering the bot's application commands with Discord's API. This makes the commands discoverable and usable by users within a server (guild) or across all servers the bot is in. The discord-live-translator project provides two primary methods for deploying these commands: a manual script for global deployment and an automatic process for guild-specific deployment on bot startup.
This system is crucial for the bot's operation, as all user interactions are initiated through these registered slash commands. The deployment process gathers command definitions from various files, formats them into the structure required by the Discord API, and sends them via REST requests.
Sources: README.md:30-35, src/deploy.ts, src/app.ts:43-49
Deployment Mechanisms
The project supports two distinct deployment strategies: manual global deployment and automatic guild-level deployment.
Manual Global Deployment
This method is intended for registering commands globally, making them available in all guilds the bot joins. It is triggered by running a script defined in package.json.
- Script Invocation: The deployment is initiated by running
pnpm run deploy.
Sources: package.json:11, README.md:33-35 - Execution: This command executes the
src/deploy.tsfile usingts-node.
Sources: package.json:10-12"scripts": { "deploy": "ts-node src/deploy.ts", }, - Process: The
src/deploy.tsscript reads theBOT_TOKENandCLIENT_IDenvironment variables, then calls theinitGlobalCommandsfunction to perform the registration.
Sources: src/deploy.ts:3-7
The following diagram illustrates the sequence of events during manual global deployment.
This diagram shows the flow from a user running the deploy script to the commands being registered with the Discord API.
Sources: package.json:11, src/deploy.ts, src/interactions.ts:30-41
Automatic Guild Deployment
For development or testing, commands can be deployed automatically to a specific guild whenever the bot starts. This provides a faster update cycle than global deployment.
- Trigger: This process is triggered on bot startup if the
GUILD_ID,CLIENT_ID, andBOT_TOKENenvironment variables are all set.
Sources: README.md:36-38, src/app.ts:43-49 - Process: The main application file,
src/app.ts, checks for these environment variables and, if present, calls theinitGuildCommandsfunction fromsrc/interactions.ts.
Sources: src/app.ts:43-49
The logic for this automatic deployment is shown in the flowchart below.
This diagram outlines the decision process for automatic guild command deployment on application start.
Sources: src/app.ts:43-49, src/interactions.ts:19-28
Command Structure and Registration
All slash commands are built using @discordjs/builders and aggregated in src/interactions.ts before being sent to Discord.
Command Definitions
Each command is defined in its own file within the src/commands/ directory. The SlashCommandBuilder is used to define the command's name, description, and any options it accepts.
For example, the /start command includes a required string option for the target language, with choices populated dynamically from a list of supported languages.
// From src/commands/start.ts
export const startCommand = new SlashCommandBuilder()
.setName('start')
.setDescription('Start the translation')
.addStringOption((option) =>
option
.setName('target')
.setDescription('Target Language')
.addChoices(...languageChoices)
.setRequired(true)
);
Sources: src/commands/start.ts:12-22
Command Aggregation
The src/interactions.ts file imports all command builders and compiles them into a single array. This array's JSON representation is used as the body for the API request to Discord.
// From src/interactions.ts
const commands = [
joinCommand.toJSON(),
languagesCommand.toJSON(),
leaveCommand.toJSON(),
startCommand.toJSON(),
statusCommand.toJSON(),
stopCommand.toJSON()
];
Sources: src/interactions.ts:7-17
Available Commands
The following slash commands are defined and deployed by this system:
| Command | Description | Source File |
|---|---|---|
/join | Move the bot into your voice channel | src/commands/join.ts |
/languages | Show all available languages | src/commands/languages.ts |
/leave | Move the bot out of your voice channel | src/commands/leave.ts |
/start | Start the translation | src/commands/start.ts |
/status | Show your current settings | src/commands/status.ts |
/stop | Stop the translation | src/commands/stop.ts |
Sources: src/interactions.ts:7-17, src/commands/join.ts:25-27, src/commands/languages.ts:8-10, src/commands/leave.ts:10-12, src/commands/start.ts:12-14, src/commands/status.ts:8-10, src/commands/stop.ts:6-8
Registration Functions
The core logic for communicating with the Discord API resides in src/interactions.ts. It uses @discordjs/rest to send a PUT request.
initGlobalCommands: Registers commands globally. It makes aPUTrequest toRoutes.applicationCommands(clientId).
Sources: src/interactions.ts:30-41initGuildCommands: Registers commands for a specific guild. It makes aPUTrequest toRoutes.applicationGuildCommands(clientId, guildId).
Sources: src/interactions.ts:19-28
Configuration
Proper configuration via environment variables is essential for the deployment process to succeed. These variables are parsed in src/env.ts.
| Variable | Description | Required for Deployment |
|---|---|---|
BOT_TOKEN | The authentication token for the Discord bot. | Always |
CLIENT_ID | The application's client ID. | Always |
GUILD_ID | The ID of the guild for automatic deployment. | For automatic deployment only |
Sources: src/env.ts:11-14, src/deploy.ts:3-5, src/app.ts:43
Summary
The slash command deployment system for discord-live-translator is a well-defined process that leverages discord.js libraries to register commands with Discord. It offers flexibility with both a manual, global deployment script (pnpm run deploy) for production and an automatic, guild-specific deployment on startup for development and testing. This dual approach ensures that the bot's primary user interface—its slash commands—can be managed efficiently and reliably.
Sources: README.md:30-38, src/deploy.ts, src/app.ts:43-49