This project is a proof of concept of a discord bot that highlights basic functionalities.
The README contains the following information:
- setup steps
- steps to create a discord application and bot via
https://discord.com/developers/applications
- how to configure the bot and some comments on it
- how to setup on a Raspberry Pi
Then comments within the code should be self explanatory.
The project was done with Python 3.9.0
. The Python packages are
listed in the file requirements.txt
. As of now they are:
aiohttp==3.6.3 async-timeout==3.0.1 attrs==20.2.0 chardet==3.0.4 discord==1.0.1 discord.py==1.5.1 emoji==0.6.0 idna==2.10 multidict==4.7.6 yarl==1.5.1
Which is the result of only installing discord
and emoji
.
Instal pyenv
(see ‘https://github.com/pyenv/pyenv’). For Ubuntu in short:
- Dependencies:
$sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \ libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
- Use pyenv installer:
$curl https://pyenv.run | bash
- Add the following in your
.bashrc
:export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
- Install Python with
pyenv
:$pyenv install -v 3.9.0
- Create a virtual environment for the project, use it locally and configure it:
$pyenv virtualenv 3.9.0 discord-bot $pyenv local discord-bot $pip install -r requirements.txt
Your environment in now ready!
- First of all go to ‘https://discord.com/developers/applications’ and sign in.
- Select
New Application
, name it and selectCreate
- Your newly created application should appears in the list of applications, select it to start configuring it.
- On the left menu, select
Bot
then in the new pageAdd Bot
. - You can find on that page that the bot has a token. It will be used by the
script
main.py
. Never reveal it as it can be use to compromise your bot. - To add your bot to your server head to section
OAuth2
.
- In
scopes
selectbot
, then select the permissions you want your bot will need. At the bottom ofscopes
, an URI is available at the bottom, copy it, open a new tab in your browser with it. It will bring a page to allow you to invite your bot to the server you select (given you have the permissions and access to the server) with the persmissions you selected.
To go further in setting up a bot, you will need to enable the developer mode of Discord: it allows to copy the ID of multiple entities (server, channel, message, etc.)
- Open the settings with the little cogwheel next to your username in the discord application
- In
Appearance
, scroll down and enable the developer mode. Tadaa, you are now a developer! - This will allow you to get the ID of different entities with a right click (channel, user, etc.) which you will use to configure your bot.
First of all, in your discord developers page, go to the section Bot
. Here
you can select a avatar picture for your bot. Most importantly, you can get
the bot token. This token is what will make the link between the python
script and the bot. At the end of main.py
, the token is given to the function
client.run(<token>)
.
For this little project, the token is in a file token.txt
at the root of
the project. Copy and paste your token in that file. Remember DO NOT publish
this token as it may compromise your bot. If you think it has been
compromised, you can always regenerate it.
Remember, this is a proof of concept and it does not achieve anything particular. To test it, you can create a server for yourself with 2 text channels and 2 roles (A and B). You will use one channel to test the bot’s commands and the other to test the role management via emoji. Test server structure:
Server: - Text channels | - General (to test chat commands and background task) | - Role management - Roles | - Role A | - Role B
- To get a text channel ID, just right click on it.
- To get a role ID, go to the
Roles
section in the server configuration and right click on it. - To get a server ID, right click on it in your server list on the left.
Copy the IDs of those 2 channels and roles and paste them at that place in the code:
GENERAL_CHANNEL_ID=#Text channel ID for background task (1 message per minute) and bot commands ROLE_MANAGEMENT_CHANNEL_ID=#Text channel ID for role management via emoji GUILD_ID=#Server ID ROLE_A_ID=#Role A ID ROLE_B_ID=#Role B ID
You have now configured the project and it is ready to be started!
To start the bot:
- Make sure you configured the server accordingly to test it.
- Make sure you added it to your server according to the section of this
README
Add the bot to a server
: once added to a server, the bot should appear amongst the members and offline) - Start the bot with
python main.py
. - The bot should now be online and great you with messages.
There is nothing complex to deploy the bot on a Raspberry Pi. It needs the
same environment as described in Environment Setup
. You don’t need a screen
and can access your Raspberry Pi via SSH.
Hope you liked that little project to demonstrate bot capabilities on Discord with Python. Bare in mind, there are other ways to make a bot and the discord Python library may changes that could make this code obsolete.
Feel free to fork it and start playing around yourself. Cheers!
Other sources used to do this project:
- Guide for pyenv: https://realpython.com/intro-to-pyenv/
- Discord Python API: https://discordpy.readthedocs.io/en/latest/
Discord Python examples (not all are up to date):