A Discord bot that allows users to execute and beautify code in Python, JavaScript, HTML/CSS, Java, and Ruby.
This bot listens for messages in Discord. When a message starts with !code, it:
- Extracts the code snippet from the message.
- Determines the programming language based on code block syntax or initial tokens.
- For executable languages (Python, JavaScript, Java, Ruby): runs the code via a sandboxed API (Judge0 CE) and returns the output.
- For markup languages (HTML/CSS): beautifies and highlights the code before sending it back.
- Python
- JavaScript
- HTML/CSS
- Java
- Ruby
-
Clone the repository
git clone https://github.com/Golgrax/Code-Execution-Bot.git cd Code-Execution-Bot -
Create and activate a virtual environment (optional but recommended)
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables Create a
.envfile in the project root and add:DISCORD_TOKEN=your_discord_bot_token JUDGE0_API_KEY=your_rapidapi_key
Start the bot:
python main.pyIn any channel the bot has access to, type !code followed by your code snippet. Use one of the supported patterns:
| Language | Trigger Syntax |
|---|---|
| Python | !code <whatever> python print("Hello!") |
| JavaScript | !code <whatever> javascript console.log("Hello!") |
| HTML | !code <whatever> html <h1>Hello!</h1> |
| CSS | !code <whatever> css body { color: red; } |
| Java | !code <whatever> java public class Main { ... } |
| Ruby | !code <whatever> ruby puts "Hello!" |
Or wrap your code in a fenced code block:
```python
print("Hello, World!")
```
-
Python:
!code <whatever> python print("Hello, World!")
-
JavaScript:
!code <whatever> javascript console.log("Hello, World!")
-
HTML:
!code <whatever> html <h1>Hello, World!</h1>
-
CSS:
!code <whatever> css body { font-size: 16px; }
-
Java:
!code <whatever> java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } }
-
Ruby:
!code <whatever> ruby puts "Hello, World!"
!code [snippet]— Execute or beautify the provided code.!help— Display help information and list of supported languages.
-
Python 3.6+
-
Discord bot token (from the Discord Developer Portal)
-
RapidAPI account subscribed to Judge0 CE (free tier)
-
Python libraries:
discordrequestsbeautifulsoup4jsbeautifierjs2pypygmentspython-dotenvkeep_alive(optional)
Install all with:
pip install -r requirements.txtSet the following environment variables in .env:
DISCORD_TOKEN: Your Discord bot token.JUDGE0_API_KEY: Your RapidAPI key for Judge0 CE API.
- "You are not subscribed" error: Ensure your RapidAPI key has an active subscription to Judge0 CE.
- Missing DISCORD_TOKEN: Verify
.envis loaded and the token is correct. - Execution errors: Check the API quotas and inspect bot logs for details.
This project is licensed under the MIT License. See LICENSE for details.