I can only write really great or really bad JavaScript for so long, so when I need to take a break, I’ve been spending some time on the Code Golf StackExchange site. Code Golf is the art of solving a small computing problem in the shortest amount of code possible measured in bytes.  Since participants tend to use single letter variable names and often remove all the spaces, Code Golf code is typically nearly unreadable and this appeal to me greatly. I liked it so much that I created a #code channel in a friend group Discord server and posted a couple challenges.  Then I thought about how cool it would be to have a bot just compile and execute the code that people post.

So I made one.

It’s written in python 3.7 because python is the best language, and runs in a docker container for ease of use.  It’s actually very simple and just needs a discord bot token and volume access to the host machine’s docker socket.

When a user posts a message that uses Discord’s markdown syntax highlighting syntax ,

the bot spins up the appropriate docker container, executes the code, and prints the output to the same channel.

Execution is relatively safe.  The container cannot use more than 50MB of memory, it only runs for 10 seconds max, and container storage is removed after execution.

Supported languages can be added and configured via editing a config file (example below) and restarting the container. User posted code is substituted in for the {}

The discord-codebot image is available on Docker Hub.

Leave a Reply

Your email address will not be published. Required fields are marked *