Slash Command: /languages
Relevant source files
The following files were used as context for generating this wiki page:The /languages slash command is an informational command that provides users with a list of all languages available for translation within the Discord Live Translator bot. Its primary purpose is to inform users of the supported languages they can select when using the /start command.
When executed, the command retrieves a statically defined list of languages, formats it for readability, and sends it as an ephemeral message, visible only to the user who invoked the command. This ensures that the channel is not spammed with bot responses. The list of languages is centralized, ensuring consistency across different parts of the application, such as the /start command's choices.
Command Definition and Registration
The /languages command is defined using the @discordjs/builders package, which provides a fluent API for creating slash command structures. It is then registered with Discord's API to make it available to users.
Sources: src/commands/languages.ts, src/interactions.ts
Command Structure
The command is defined with a name (languages) and a simple description. It does not require any options or parameters from the user.
// src/commands/languages.ts:5-7
export const languagesCommand = new SlashCommandBuilder()
.setName('languages')
.setDescription('Show all available languages');
Sources: src/commands/languages.ts:5-7
Command Registration
The command's JSON representation is added to an array of commands that are deployed to Discord. This can be done globally or for a specific guild. The initGuildCommands and initGlobalCommands functions handle this deployment using @discordjs/rest.
// src/interactions.ts:10-16
const commands = [
joinCommand.toJSON(),
languagesCommand.toJSON(),
leaveCommand.toJSON(),
startCommand.toJSON(),
statusCommand.toJSON(),
stopCommand.toJSON()
];
Sources: src/interactions.ts:10-16
Command Execution Flow
When a user executes the /languages command, the bot's main interaction handler routes the request to the languagesCommandHandler. This handler is responsible for processing the request and generating the response.
The following diagram illustrates the sequence of events when a user invokes the command.
This diagram shows the flow from user input to the ephemeral bot response for the /languages command.
Sources: src/app.ts:20, src/app.ts:32-35, src/commands/languages.ts:9-16
Handler Logic
The languagesCommandHandler function performs the following steps:
- It accesses the
languagesobject imported fromsrc/languages.ts. - It iterates over the entries of the
languagesobject usingObject.entries(). - For each language, it constructs a formatted string containing the language's icon, key (e.g., 'en'), and display name.
- It joins all the formatted strings with newline characters.
- Finally, it replies to the interaction with the complete list as an ephemeral message.
// src/commands/languages.ts:9-16
export const languagesCommandHandler: CommandHandler = async (interaction) => {
const response =
'**Available languages:**\n' +
Object.entries(languages)
.map(([key, value]) => `${value.icon} ${key} *(${value.displayName})*`)
.join('\n');
await interaction.reply({ content: response, ephemeral: true });
};
Sources: src/commands/languages.ts:9-16
Language Data Source
The list of available languages is defined in a single, constant object within src/languages.ts. This centralized approach ensures that all parts of the application that need language information refer to the same source of truth.
Sources: src/languages.ts, src/commands/start.ts, docker-compose.prod.yml
Data Structure
Each language in the languages object is defined with a key (a two-letter language code) and an object containing its properties.
The following diagram shows the structure of the language data.
This diagram illustrates the structure of the languages constant, where each key maps to an object with specific properties for that language.
Sources: src/languages.ts:1-25
Available Languages
The table below lists all the languages currently configured in the system. The translatorCode is used for the translation service, and the ttsModel is the path to the text-to-speech model for that language.
| Key | Icon | Display Name | Translator Code | TTS Model |
|---|---|---|---|---|
en | :flag_us: | English | en | tts_models/en/ljspeech/vits |
de | :flag_de: | German | de | tts_models/de/thorsten/vits |
fr | :flag_fr: | French | fr | tts_models/fr/mai/tacotron2-DDC |
es | :flag_es: | Spanish | es | tts_models/es/mai/tacotron2-DDC |
it | :flag_it: | Italian | it | tts_models/it/mai_female/vits |
Sources: src/languages.ts:1-25
This same list of languages is used to populate the choices for the /start command, ensuring a consistent user experience.
Sources: src/commands/start.ts:9-12
Furthermore, the libretranslate service, which handles the translation, is configured in docker-compose.prod.yml to load only these specific languages, optimizing resource usage.
# docker-compose.prod.yml:25-26
environment:
- LT_LOAD_ONLY=de,en,es,fr,it
Sources: docker-compose.prod.yml:25-26
Summary
The /languages command is a simple yet crucial utility for the Discord Live Translator bot. It provides users with a clear, up-to-date list of supported languages for translation. By sourcing its data from a centralized languages.ts file, it maintains consistency with other features like the /start command and the underlying translation service configuration. Its implementation as an ephemeral response ensures a clean and user-friendly interaction.