A barebones example Discord bot, written in Python 3.6 using the discord.py library.
See also CSSBot, same idea just in C#.
This project is meant for Python 3.5 and up. Please insure that you have that
Python 3.5 or higher installed. You can check this using python --version
or python3 --version
.
Python 3.5 is shipped by default in the latest versions of Ubuntu.
You'll need to install some packages first. These are included in the
requirements.txt
file for you.
Linux:
python3 -m pip install -r requirements.txt
# only required for doing voice on Linux environments
sudo apt-get install libffi-dev python3-dev
See the discord.py documentation for why the additional tools are necessary.
Windows:
py -3 -m pip install -r requirements.txt
Mac:
python3 -m pip install -r requirements.txt
- Register your own Discord bot for testing with.
- Navigate to the Discord API docs and log in: https://discordapp.com/developers/applications/me (If you log in for the first time, it'll probably take you to the app, so go back to this link again).
- Click on the "New App" button.
- Name your app. Click "Create App".
- Click "Create a Bot User". (Discord API supports a few types of apps/bots, but we are building a bot)
- Locate your bot's user token. Your user token must not be shared with anyone. If it is posted publicly, change it ASAP.
- Please refer to the best practices for storing and managing connection tokens.
todo
- Please refer to the best practices for storing and managing connection tokens.
- Fork this repo.
- Clone your copy of this repo using
git clone https://github.com/USERNAME/CSSBot_Py.git
- Create a new file in your CSSBot_Py directory:
config.ini
- Populate the file with the following contents:
[Configuration] connection_token=XXXXXXXXXYOURTOKENGOESHEREXXXXXXX
Run the following command:
python3 main.py
The first lines should read something like:
using discordpy version 1.0.0a
Logged in SomeUser - 1234567890
Version 1.0.0a
When I run python3 main.py
I get this error.
TypeError: Inheritance a class <class 'aiohttp.http_writer.URL'> from URL is forbidden
Update your requirements.txt with the latest version from this repo with the latest contents. The file should contain:
yarl<1.2
git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]
Run your python3 -m pip install -r requirements.txt
(or equivalent) command again, then python3 main.py
.
On Mac, with Python 3.6, I cannot get my bot to connect online, and the error log complains about some SSL issue.
You need to install certificates. Navigate to Applications/Python 3.6
, and double click the Install Certificates.command
file. Give it a few seconds. Once the terminal window stops doing stuff, you can close it. Retry running the bot with python3 main.py
.
When I run py -3 -m pip install -r requirements.txt
, the installation fails for some reason.
Ensure that you have git installed, since the requirements installation process makes use of git.
On windows, you need to use py
isntead of python3
. Ensure that you have Python installed, and re-open your command window
to ensure that your %PATH%
has been updated.
This bot is intended to serve as an example. Either use this code as a reference or a base to make your own bot!
Here are some resources to get you started:
https://github.com/Rapptz/discord.py
https://discordapp.com/developers/docs/intro
http://discordpy.readthedocs.io/en/rewrite/
http://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html
https://gist.github.com/MysterialPy/d78c061a4798ae81be9825468fe146be