Slash Command: /leave
Relevant source files
The following files were used as context for generating this wiki page:
The /leave command is a user-facing slash command that instructs the bot to disconnect from the voice channel it currently occupies. It is a crucial part of the bot's lifecycle, providing a clean way to end a session and free up resources. The typical user workflow involves using /join to bring the bot into a channel, other commands like /start to use its features, and finally /leave to dismiss it.
This command performs several validation checks to ensure it is executed in a valid context, such as verifying that the bot is connected and that the user issuing the command is in the same voice channel. Upon successful execution, it plays a disconnect sound, stops all audio processing, and destroys the voice connection.
Sources: README.md:14-18, src/commands/leave.ts
Command Definition
The /leave command is defined using the SlashCommandBuilder from the @discordjs/builders library. This definition sets the command's name and its user-visible description.
// src/commands/leave.ts:9-11
export const leaveCommand = new SlashCommandBuilder()
.setName('leave')
.setDescription('Move the bot out of your voice channel');
This command definition is then exported and aggregated with other commands in src/interactions.ts to be registered with Discord's API for either a specific guild or globally.
Sources: src/commands/leave.ts:9-11, src/interactions.ts:12, 17
Command Handling
When a user executes the /leave command, the interactionCreate event listener in src/app.ts routes the interaction to the leaveCommandHandler.
This diagram shows how the command is routed from Discord to the specific handler.
Sources: src/app.ts:16, 32-34
Execution Logic
The leaveCommandHandler contains the logic for safely disconnecting the bot from a voice channel. It follows a sequence of validation checks before proceeding with the disconnection.
Sources: src/commands/leave.ts:13-52
Validation Checks
Before executing the leave logic, the handler performs a series of checks. If any check fails, it sends an ephemeral error message to the user and terminates execution.
| Check | Error Message | Source Lines (in src/commands/leave.ts) |
|---|---|---|
| Interaction is from a valid guild member. | An error occurred! :grimacing: | 14-17 |
| Bot is connected to a voice channel in the guild. | The bot currently is not connected to a voice channel! :x: | 21-24 |
| User is in any voice channel. | You are currently not in a voice channel! :x: | 26-29 |
| User is in the same voice channel as the bot. | You are currently not in the same voice channel as the bot is in! :x: | 31-36 |
Disconnection Flow
If all validations pass, the command proceeds with the disconnection sequence. The following flowchart illustrates this process.
This logic is implemented in the leaveCommandHandler. A key part of this flow is playing a disconnect sound and waiting for it to finish before destroying the connection. This ensures a smooth user experience.
- Defer Reply: The interaction reply is deferred to allow time for the disconnect sound to play.
Sources: [src/commands/leave.ts:38](https://github.com/wotschofsky/discord-live-translator/blob/HEAD/src/commands/leave.ts#L38) - Play Disconnect Sound: The bot retrieves the guild's audio player from the
audioQueueand plays a pre-recordeddisconnect.mp3file.
Sources: [src/commands/leave.ts:41-43](https://github.com/wotschofsky/discord-live-translator/blob/HEAD/src/commands/leave.ts#L41-L43) - Wait for Idle State: The handler listens for the
AudioPlayerStatus.Idleevent, which is emitted once the disconnect sound has finished playing.
Sources: [src/commands/leave.ts:45](https://github.com/wotschofsky/discord-live-translator/blob/HEAD/src/commands/leave.ts#L45) - Stop and Destroy: Inside the event listener, the
audioQueueis stopped for the connection, and the voice connection itself is destroyed. This cleans up all resources related to the voice session.
Sources: [src/commands/leave.ts:46-47](https://github.com/wotschofsky/discord-live-translator/blob/HEAD/src/commands/leave.ts#L46-L47) - Confirm Success: Finally, the original deferred reply is edited to inform the user that the bot has successfully left the channel.
Sources: [src/commands/leave.ts:49](https://github.com/wotschofsky/discord-live-translator/blob/HEAD/src/commands/leave.ts#L49)
Summary
The /leave command provides a robust and user-friendly mechanism for terminating the bot's presence in a voice channel. It is a simple command from the user's perspective but includes important internal checks and a graceful shutdown procedure involving audio feedback and proper resource cleanup. Its implementation ensures that the bot only leaves when requested by an authorized user in the correct context, preventing accidental or erroneous disconnections.
Sources: src/commands/leave.ts, README.md:18