This repository serves as a simple, python-based template to start off any Discord Bot. It is solely a guild-based application that does NOT implement slash commands. Because slash commands are icky.
Documentation for discord.py can be found here
Last updated May 8th, 2024.
First, clone into this repository by using the following command:
git clone https://github.com/cwhitti/Easy-Discord-Bot
Make note of where the repo was saved to so you can access it later.
To use this repo, you will need to install the Discord library.
This can be done with pip. If pip is not installed, install it with this tutorial.
Install the discord library with the following command:
pip install discord.py
or, for python3 users...
pip3 install discord.py
You will need to register a new bot application though Discord.
- Head to the Discord Developer Portal and sign in.
- At the top right, click
New Application. This will be your bot. - Name your bot whatever you want and accept the terms and conditions.
- Click the
Bottab on the left side of the panel. - PUBLIC BOT: On by default, this lets others add your bot with its invite-link.
- TOKEN: This is your bot's personal id. Create your token and save it somewhere safe.
- PRIVILEDGED INTENTS: These are permissions for your bot to communicate with Discord. Be sure to enable
PRESENCE INTENT,SERVER MEMBERS INTENT, andMESSAGE CONTENT INTENT. Your bot will likely not work without these three intents enabled. - Save your changes.
That's it for the creation!
Discord absolutely loves changing how to do this every three months. You need to generate an invite-link that is specific to your bot.
- Head to the Discord Developer Portal and sign in.
- Navigate to your new bot.
Assuming that Discord still has this under the Installation tab, use this method:
- Click the
Installationtab on the left side of the panel. - If prompted, Select
Guild Install. - If
Install Linkis set toNone, chooseDiscord Provided Linkfrom the dropdown menu. - You should have a link that looks like
https://discord.com/oauth2/authorize?client_id=1234567890123456789. - Be sure to set any permissions, as outlined below.
- Save your changes.
- Invite your bot by pasting the generated link in a Discord chat or through your browser.
- Select the server to invite the bot to and confirm the bot's permissions.
This should be in the same area as generating your invite link.
Discord loves to change how you do this, but you want to make sure your bot has both applications.commands and bot enabled.
Once bot has been enabled, choose the permissions you'd like for your bot.
For a personal, small-scale bot, select Administrator to automatically enable all permissions. However, bots which will be used in other servers will likely need stricter permissions. It's just a security thing.
Find your bot's token that you (hopefully) saved earlier. If you have lost your token, generate and save a new one.
Open secret.py and paste your token in the file.
TOKEN = "YOUR.TOKEN.HERE"
Running the bot is easy. Ensure that you have these three files: run.py, bot.py, and secret.py.
- run.py: The script to run the bot.
- bot.py: The main code of the bot.
- secret.py: The "secrets" of your bot. If publishing to GitHub, I strongly recommend adding this file to .gitignore!
To run your new bot, use the following command in your console:
python run.py
or for Python3 users...
python3 run.py
The script should start and your bot should be logging in with the following message:
YYYY-MM-DD HR:MN:SC INFO discord.client logging in using static token
YYYY-MM-DD HR:MN:SC INFO discord.gateway Shard ID None has connected to Gateway (Session ID: <SOME LONG STRING>).
This template comes with a build in prefix and command. The prefix for this bot is set to !, and the one command is hello.
Once you have added your bot to your server and are running it, type !hello into the chat. The bot will respond with Hello, <YOUR USERNAME>.
Discord bots can do so much more than just send messages. They can read user input, generate images, play music, and so much more.
I have created an example bot which shows you how to take your bot a step further. Please see this example for more!
To get started, here are attributes of msg you may find interesting!
| Attribute/Method | Description |
|---|---|
id |
Unique ID of the message. |
content |
The text content of the message. |
author |
The discord.Member or discord.User who sent the message. |
channel |
The discord.TextChannel, discord.DMChannel, or discord.Thread the message was sent in. |
guild |
The discord.Guild the message belongs to (None if in a DM). |
created_at |
Timestamp when the message was created. |
edited_at |
Timestamp of the last edit (None if never edited). |
type |
Type of message (e.g., default, system, reply). |
flags |
A discord.MessageFlags object containing metadata (e.g., crossposted, ephemeral). |
jump_url |
A URL that jumps to the message. |
attachments |
List of discord.Attachment objects representing files attached to the message. |
embeds |
List of discord.Embed objects representing rich content. |
stickers |
List of discord.StickerItem objects representing stickers. |
reactions |
List of discord.Reaction objects representing reactions. |
mention_everyone |
True if the message mentions @everyone or @here. |
mentions |
List of discord.User or discord.Member objects mentioned. |
role_mentions |
List of discord.Role objects mentioned. |
channel_mentions |
List of discord.TextChannel or discord.Thread objects mentioned. |
reference |
A discord.MessageReference if the message is a reply. |
referenced_message |
The discord.Message the current message replied to (None if not cached). |
thread |
The discord.Thread associated with the message, if any. |
interaction |
A discord.Interaction object if triggered by an interaction (e.g., slash command). |
components |
List of discord.Component objects (buttons, select menus). |
| Methods | Description |
.delete() |
Deletes the message. |
.edit(content=None, embed=None, attachments=None, ...) |
Edits the message. |
.add_reaction(emoji) |
Adds a reaction to the message. |
.remove_reaction(emoji, user) |
Removes a reaction from a message. |
.clear_reactions() |
Removes all reactions from a message. |
.reply(content, **kwargs) |
Sends a reply to the message. |
