Developer Setup
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the technical details of the developer setup for the Discord Live Translator bot. It covers project structure, dependencies, configuration, build processes, and execution scripts. The project is a TypeScript application that leverages Docker for containerizing its services, including the main bot, a speech-to-text (STT) service, a translation service, and a Redis database.
The development environment is designed for hot-reloading using nodemon, while the production setup is managed via docker-compose. Understanding these components is crucial for contributing to the project or self-hosting the bot.
Prerequisites
The project requires Node.js version 20 to be installed in the development environment.
Sources: package.json:8
Core Technologies
The project is built upon a modern stack for both the bot logic and its supporting services.
| Technology | Role | Source Files |
|---|---|---|
| Node.js | Runtime for the main bot application. | package.json:8 |
| TypeScript | Primary programming language. | tsconfig.json, package.json:47 |
| Discord.js | Library for interacting with the Discord API. | package.json:23 |
| Rust | Language for the high-performance STT service. | stt/Cargo.toml |
| Docker | Containerization for production and development services. | docker-compose.prod.yml |
| Redis | In-memory data store, likely for session or settings management. | docker-compose.prod.yml:28-33, package.json:26 |
Project Scripts
Several scripts are defined in package.json to streamline common development tasks.
| Script | Description | Command |
|---|---|---|
build | Compiles the TypeScript source code into JavaScript in the dist directory. | sh build.sh |
deploy | Deploys the bot's slash commands globally to Discord. | ts-node src/deploy.ts |
start | Runs the compiled JavaScript application from the dist directory. | node -r source-map-support/register dist/app.js |
dev | Starts the application in development mode with hot-reloading using nodemon. | nodemon |
format | Formats the source code using Prettier. | prettier src/** --write |
Sources: package.json:10-15
Configuration
Application configuration is managed through environment variables, which are loaded via dotenv and validated using @t3-oss/env-core with zod.
Sources: src/env.ts:2-4
Environment Variables
The following server-side environment variables are defined:
| Variable | Type | Required | Description |
|---|---|---|---|
BOT_TOKEN | string | Yes | The Discord bot token. |
CLIENT_ID | string | No | The Discord application client ID. Used for auto-deploying commands. |
GUILD_ID | string | No | The Discord server ID for auto-deploying guild-specific commands. |
LOG_PATH | string | No | Optional file path for writing logs. |
POSTHOG_API_KEY | string | No | API key for PostHog analytics. |
POSTHOG_HOST | string | No | Host for PostHog instance. Defaults to https://app.posthog.com. |
REDIS_URL | string | No | Connection URL for the Redis instance. |
STT_HOST | string | Yes | URL for the Speech-to-Text (STT) service. |
TRANSLATION_HOST | string | Yes | URL for the translation service (e.g., LibreTranslate). |
HEARTBEAT_URL | string | No | A URL to send a POST request to as a heartbeat/health check. |
Sources: src/env.ts:11-21
Build Process
The build process is orchestrated by the build.sh script and the TypeScript compiler (tsc).
This diagram illustrates the steps executed by the pnpm run build command.
Sources: build.sh:1-4, tsconfig.json:3-6, package.json:11
TypeScript Configuration
The tsconfig.json file configures the TypeScript compiler to:
- Inherit base settings from
@tsconfig/node20/tsconfig.json. - Place compiled JavaScript files into the
./distdirectory. - Generate source maps for easier debugging.
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true
}
}
Sources: tsconfig.json:1-6
Running in Development
For development, the project uses nodemon to monitor file changes and automatically restart the application.
The dev script uses nodemon, which is configured by nodemon.json to watch for changes in .ts, .js, and .json files and execute ts-node src/app.ts on any change.
{
"ext": "js,ts,json",
"exec": "ts-node src/app.ts"
}
Sources: nodemon.json:1-4, package.json:14
This sequence diagram shows the hot-reloading process.
Sources: nodemon.json:3
Docker Architecture (Production)
The production environment is defined in docker-compose.prod.yml and consists of four main services.
This diagram shows the interaction between the containerized services.
Service Details
bot: The main Discord bot application. It depends on all other services to function.- Image:
ghcr.io/wotschofsky/discord-live-translator - Connects to
stt,libretranslate, andredisusing internal Docker networking.
- Image:
stt: The Rust-based Speech-to-Text service.- Image:
ghcr.io/wotschofsky/discord-live-translator/stt - Exposes an HTTP server on port 8000 to process audio files.
- Image:
libretranslate: An open-source machine translation service.- Image:
libretranslate/libretranslate:v1.3.11 - The environment is configured to load only specific languages (
de,en,es,fr,it) to conserve resources.
- Image:
redis: A Redis server for data persistence.- Image:
redis:alpine - Uses a Docker volume named
redisto persist data across container restarts.
- Image:
Sources: docker-compose.prod.yml
Slash Command Deployment
Slash commands are registered with Discord's API using scripts. There are two methods for deployment:
-
Global Deployment: The
pnpm run deployscript executessrc/deploy.ts, which registers all commands globally for any server the bot is in. This is suitable for production bots.
Sources: package.json:12, src/deploy.ts:6, src/interactions.ts:31-43 -
Guild-Specific Deployment: If
CLIENT_IDandGUILD_IDenvironment variables are set, the application will automatically register/update the commands for that specific guild on startup. This is ideal for development and testing as updates are nearly instantaneous.
Sources: src/app.ts:50-56, src/interactions.ts:19-29