This bot translates messages automatically for users who don't speek english.
It uses LibreTranslate API self-hosting API to detect the language of the message. It bases it off of a confidence variable to determine the language. This is can be run on a server (which is what I'm using).
- Node.js and npm: Ensure Node.js and npm are installed on your system. You can download them from nodejs.org.
- Clone the repository to machine:
git clone https://github.com/JayNightmare/Translator-Discord-Bot.git cd Translator-Discord-Bot
- Navigate to the project directory and install the required npm packages:
npm init -y npm install discord.js axios dotenv
-
Go to the Discord Developer Portal.
-
Create a new application and add a bot to it.
-
Go to Bot tab and enable Public Bot and Message Content Intent
-
Copy the bot token in the Bot tab
-
Generate an invite link in the Installation tab. Make sure to enable Guide Install
- Create a
.env
file in the directory:BOT_TOKEN=your-discord-bot-token API_URL=http://your-linode-ip:5000/translate
- Replace
your-discord-bot-token
with the token from the Discord Developer Portal. - Replace
http://your-linode-ip:5000/translate
with the API endpoint of your LibreTranslate instance or y.
- If you haven't already, set up LibreTranslate on a server (e.g., Linode) and ensure it's running.
- If self-hosting, ensure your server is properly configured to handle requests.
- Start the bot using the following command:
node index.js
- The bot should log in to your server and start listening for messages.
- Send messages in different languages in your Discord server to see if the bot translates them and displays the corresponding flag and language.
- Once you have your code running smoothly on your own machine, transfer it to your server. To run the bot, use PM2.
-
Create a Linode Server:
- Deploy a new Linode instance with a Linux distribution like Debian 12 or Ubuntu 24.04 LTS. I'm using the $5 shared CPU server which is more than enough to run this bot.
-
Install Dependencies:
- SSH into your Linode server and install the necessary dependencies, including Docker.
sudo apt-get update
sudo apt-get install docker.io
- Run LibreTranslate:
- Pull the LibreTranslate Docker image and run it.
sudo docker run -d -p 5000:5000 libretranslate/libretranslate
- Access the API:
- The API will be accessible at
http://your-linode-ip:5000/translate
.
- The API will be accessible at
To test to see if the API is working, you can run a CURL line:
curl -X POST http://your-linode-ip:5000/translate -H "Content-Type: application/json" -d '{"q": "Hola", "source": "es", "target": "en"}'
sudo npm install -g pm2
- To monitor the bot on the server, type the command:
pm2 login
- This will open the PM2 website where you can create an account
- Connect to the monitor bucket if you have one, if not just type
none
- To link your pm2 account type the command:
pm2 link <secret_key> <public_key>
- Navigate to your bot's directory and start it with PM2:
pm2 start index.js --name "discord-bot" --monitor
--monitor
add the bot to the monitor list which can be seen on the pm2 site.- type
pm2 monit
to see a GUI of the Process List and the bot logs.
- Set up PM2 to start on server boot:
pm2 startup pm2 save
- View logs:
pm2 logs discord-bot-name
- Monitor running processes:
pm2 list