discord.py bot template
A discord.py bot template with easy deployment through Github Actions. You can use this template to just run a Python instance, Docker image, or Docker compose with other microservices. This is why it features a mockup API aswell, to showcase the flexibility of it.
Registering a new bot
Go to Discord Developer Applications Portal And create a new application. Then go to Bot -> Add Bot. Give it a name and the required permissions for your use case. Now use Click to Reveal Token to get your token, you will need this in configuration. Do not share this with anyone! If for some reason the token got leaked, you can regenerate a new one. This means you need to this this in the configuration later on aswell, so only do this when necessary.
Configuration
First you'll have to create a .env file. This is a file where all configuration is stored for this bot. If you are going to run this in a simple way locally, use the example.env file and fill in the field with your discord token. And save this with the file name only being: ".env" The script will now use this file to access the token.
Suggested: If you are planning to take the automated deployment approach, you will not need to put this key inside the .env file. Go to the Automated deployment pipeline section to set this up.
Running in docker
I'm going straight to running in docker. First you will have to build the docker image. You can do this by going inside the directory that contains the dockerfile and running the command:
docker build . -t
To run the bot you can use:
docker run --env-file
you can add the -d flag to run in detached mode. Even easier is using docker-compose. When working with different microservices this is must!
docker-compose -f
up --build
Use the --build flag if you've made changes. It won't rebuild every layer because it can use the cached one's, this means the whole process just takes a couple of seconds.
Automated deployment pipeline
First you'll have to generate a Personal Access Token. Keep a copy of this, you wont be able to access it once it's generated. You will put this key and the discord token inside the repository secrets under Settings -> Secrets -> Actions and create a new secret with the same key value pair. Now the tokens are accessible by Github Actions, but remain anonymous. Also other people with access to the repo can't see it, including yourself. From now on you're only able to write to these. Also add your user
Now set up a self-hosted runner. Ideally this is configured as a service so that it runs permanently and launches on startup. You can add a runner by going to Settings -> Actions -> Runners and add a new one. Follow the instructions for your platform. For this example I did it in my WSL Debian. Make sure docker is installed on this machine when running the action runner.
When starting the runner you should see the it appear with the idle status. I recommend this doc to get it up and running as a service. If you want to just quickly run it using the run.sh script, make sure that your user has the right privileges to do this. Check out this post if you encounter an error due to this problem.
Now Github Actions will use this machine to deploy the bot.