A repo containing a standard file structure template for a Discord bot!
- Written using Discord.py
- Read the docs here
- Cogs Directory
- Utils Directory
- Custom Bot Class
- Main File
Template/
├─── cogs/
│ ├─── __init__.py
│ ├─── cog1.py
│ ├─── cog2.py
│ ├─── cog3.py
│ ├─── ...
├─── utils/
│ ├─── __init__.py
│ ├─── util1.py
│ ├─── util2.py
│ ├─── util3.py
│ ├─── ...
├─── __init__.py
├─── .env
├─── botclient.py
├─── __main__.py
Note
Cogs are used to organize a collection of commands, listeners, and some state into one class. Cogs Documentation
Note
Utils are helper methods or reusable assets.
Note
Bot file is used to create a custom Discord Bot Client. This lets you import your bot into different files and make calls when your Client is created.
- A Discord account
- Python installed
- If you want to use JavaScript to make a Discord bot, I recommend using this template made by @PillowGit for the coding portion
- Internet access
- A IDE to edit and run code (Like Visual Studio Code)
CLONE THIS REPOSITORY
- HTTPS
git clone https://github.com/JOwen-ster/Discord.py-Bot-TEMPLATE.git
- SSH
git clone [email protected]:JOwen-ster/Discord.py-Bot-TEMPLATE.git
- GitHub CLI
gh repo clone JOwen-ster/Discord.py-Bot-TEMPLATE.git
Head over to the discord developer page, log in, and at the top right of your screen click New Application
, type the name of your Discord bot, and then click create
Note
For simplicity we will not select a team, but you can create a team in the 'Teams' tab and add people that will be associated with the development of the bot!
On the side, click on the Bot
tab and then scroll down to Privileged Gateway Intents
on that page. These are the different types of data that your bot will have access to when in a server. You can read about them on developer gateway intents page to see what each intent covers and if you may need a single or multiple when you make your own bot!
Note
For simplicity, we will toggle on all gateway intents in case you want to add more to your first bot. In real practice, you want to read up on these intents and see which your bot would need since when you apply to get your bot verified at 75 servers, Discord will ask you why you are using them! You will need to apply for gateway intents separately with the verification process. If you have any questions about verifying a discord bot, ask me on Discord (typos.
) since I have a bot that is in 300+ servers and is verified!
Next, on the side of your screen click on the Installation
tab.
- Scroll down to the
Install Link
dropdown menu, make sureDiscord Provided Link
is selected.
Now, scroll down to Default Install Settings
and click on the SCOPES
dropdown menu under Guild Install
.
- Under the
SCOPES
-> selectbot
. - Under
PERMISSIONS
-> select any server permissions that your bot will need to fully function.
Note
For this workshop we will use the Administrator
permission for ease. Giving a user or bot Administrator
will give access to all channels with all permissions regardless of how they are setup in your server.
Bots are treated like regular membersm in a server with their access to channels and ways they interact with the server. For example able to manage member
is not a usual default permission for most servers, it will not be for a bot unless you give it that permission.
Warning
Unless your Discord bot's function is for server management such as raid protection, server setup, moderation, or various non member interactive things, I would NOT set your permission to Administrator
just because it is "easy". From my bot developing experience, when getting bots into bigger servers, some owners really wanna limit what it can do for security purposes. As an example, if your token gets exposed, someone logs into your bot and with a total of 20 lines of code (not joking) every server that bot is in, it will nuke, mass ping, and ban every member.
Under Install Link
, there is a link you send to others. When clicked, that user can add your bot with all the permissions you selected to any server they have the Manage Server
permission in (or it will not appear under the list of servers when adding).
Important
Go to the Bot
tab.
Click Reset Token
near the top of the page
Caution
Caution
If you do not type .env
in your .gitignore
file, (the .env
file is where you should put your token) , then GitHub bots will scrape your token (it has happened to me) and may use it. Discord will hopefully send you a message very fast saying they caught it and reset it since they are also scraping for Discord Bot Tokens to watch out for you and keeping your bots secure :)
By the end, you will have a bot that has a simple slash command that sends messages in an embed, a simple slash command that uses a modal/form, a simple slash command that has buttons and a dropdown menu, a background task that will always be updating the bots Discord status to display how many servers it is in, and a logging setup for all actions the bot does. We will be using the discord.py API wrapper in this workshop.
Read the docs (How to do Commands)
Read the docs (How to do Events/Listeners)
First, activate a virtual ENV by running this command once you have changed directory to this repository.
python -m venv botenv
Then (only for Windows Powershell Users)
Set-ExecutionPolicy RemoteSigned
.\botenv\Scripts\Activate.ps1
For Linux users
source botenv/bin/activate
Then, install all requirements and dependancies from the requirements.txt in this repo...
pip install -r requirements.txt
After you have successfully installed the libraries...
Create a new file named .env
(no name before the dot) and put the following in it.
Note
To get a Canvas API token, login to Canvas, click Account
-> Settings
-> Scroll down to the bottom of the Integrations
tab and then click + New Access Token
.
DISCORD_BOT_TOKEN = 'YOUR_BOT_TOKEN_GOES_HERE'
CANVAS_TOKEN = 'YOUR_CANVAS_INTEGRATION_TOKEN_HERE'
Normally you would create a new file named .gitignore
and put the following inside it.
.env
I have already done this for you so all you need to do is run the bot once we add some functionality!
Refer to main.py for the full finished bot code if you are haivng trouble.