A Discord bot that translates messages using Google's Gemini AI. Supports both context menu commands and slash commands for easy message translation.
- 🌍 Context Menu Translation: Right-click any message to translate it
- ⌨️ Slash Command: Use
/translateto translate custom text - 🤖 Powered by Gemini AI: Advanced language detection and translation
- 🐳 Docker Ready: Easy deployment with Docker and Docker Compose
- 🔒 Secure: Input sanitization and proper error handling
- ✅ Health Checks: Built-in health monitoring for container orchestration
- Bun runtime (v1.0 or higher)
- Discord Bot Application (Create one here)
- Google Gemini API Key (Get one here)
- Docker and Docker Compose (for containerized deployment)
git clone <your-repo-url>
cd message_translatorbun installCopy the example environment file:
cp .env.example .envEdit .env and fill in your credentials:
# Discord Bot Configuration
APP_ID=your_discord_app_id
DISCORD_TOKEN=your_discord_bot_token
PUBLIC_KEY=your_discord_public_key
# Optional: For instant testing in a specific guild
DEV_GUILD_ID=your_development_guild_id
# Google AI Configuration
GEMINI_API_KEY=your_google_genai_api_key- Go to Discord Developer Portal
- Create a new application or select an existing one
- Go to the "Bot" section to get your
DISCORD_TOKEN - Go to "General Information" to get your
APP_IDandPUBLIC_KEY - Enable the "Message Content Intent" in the Bot settings
- Visit Google AI Studio
- Sign in with your Google account
- Create a new API key
- Copy the key to your
.envfile
The bot automatically registers commands on startup. Commands include:
- Translate Message (Context Menu): Right-click on any message
- /translate (Slash Command): Translate custom text
Commands are registered globally and may take up to 1 hour to propagate. If you set DEV_GUILD_ID, they'll also be registered instantly in that specific guild for testing.
# Run with auto-reload
bun run dev
# Or run normally
bun run startThe server will start on http://0.0.0.0:3000
# Build and start the container
docker-compose up -d
# View logs
docker-compose logs -f
# Stop the container
docker-compose down# Build the image
docker build -t message-translator .
# Run the container
docker run -d \
--name message-translator \
-p 3000:3000 \
--env-file .env \
message-translator- Go to your application in the Discord Developer Portal
- Navigate to "General Information"
- Set the "Interactions Endpoint URL" to your public URL (e.g.,
https://your-domain.com) - Discord will send a POST request to verify the endpoint
Important: Your bot must be publicly accessible for Discord to send interactions. Consider using:
- A VPS or cloud hosting service
- ngrok for local development testing
- Reverse proxy (nginx, Caddy) for production
When inviting the bot to your server, ensure it has:
applications.commandsscope- No special permissions required (bot responds with ephemeral messages)
Invite URL format:
https://discord.com/api/oauth2/authorize?client_id=YOUR_APP_ID&scope=applications.commands
- Right-click (or long-press on mobile) any message
- Select "Apps" → "Translate Message"
- The bot will detect the language and translate it to English
/translate message:"Hello, how are you?" language:"spanish"
The bot will translate the provided text to the specified language.
message_translator/
├── src/
│ ├── index.ts # Main server and Discord interaction handler
│ ├── gemini.ts # Google Gemini AI translation logic
│ └── register.ts # Discord command registration
├── .github/
│ └── workflows/
│ └── docker-build.yml # CI/CD for building Docker images
├── Dockerfile # Multi-stage Docker build
├── docker-compose.yml # Docker Compose configuration
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── .env.example # Environment variable template
└── README.md # This file
The bot includes a health check endpoint at /health that returns:
{
"status": "ok",
"timestamp": "2025-12-15T10:30:00.000Z"
}This is used by Docker's health check system to monitor the container.
- ✅ Discord signature verification for all incoming requests
- ✅ Input sanitization to prevent prompt injection attacks
- ✅ Character limits on user inputs (2000 chars for messages, 50 for language)
- ✅ Environment variable validation on startup
- ✅ Proper error handling with user-friendly error messages
- Wait up to 1 hour for global commands to propagate
- Use
DEV_GUILD_IDfor instant testing in a specific server - Check bot logs for registration errors
- Verify your
PUBLIC_KEYis correct in.env - Ensure your public URL is correctly set in Discord Developer Portal
- Check that
GEMINI_API_KEYis valid - Verify you haven't exceeded Google AI API quotas
- Check container logs:
docker-compose logs -f
- Ensure port 3000 is accessible
- Check if curl is available in the container
- View health status:
docker inspect message-translator
This project includes a GitHub Actions workflow that:
- Builds multi-platform Docker images (linux/amd64, linux/arm64)
- Pushes to GitHub Container Registry (ghcr.io)
- Runs on pushes to main/master and on tags
- Creates build attestations for supply chain security
To use it:
- Enable GitHub Actions in your repository
- Ensure GitHub Packages write permissions are enabled
- Push to main branch or create a version tag (e.g.,
v1.0.0)
Images are available at: ghcr.io/<username>/message_translator:latest
# No tests configured yet
# Add your test commands hereThe project uses TypeScript with strict mode enabled. Run type checking:
bun run tsc --noEmit- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source. Add your preferred license here.
For issues and questions:
- Open an issue on GitHub
- Check the Discord Developer Documentation
- Review Google Gemini API Documentation
- Built with Bun
- Powered by Google Gemini AI
- Discord integration via discord.js