Discord.py is a very powerful API. It aims to make creating Discord bots incredibly easy while still giving lots of power to the user. People have made game bots, RPG bots, Moderation Bots, Economy bots, and even more! Carl-bot, Auttaja, and lots more bots use discord.py. Using this guide, you can learn how to use it.
Installing discord.py
Assuming you have Python already installed, you should install discord.py with
pip install discord.py
Ta-da! discord.py is ready for use!
You will need to make a bot account for your bot. A guide to this is written here.
The basics of a bot
When starting out and creating your bot, you need to decide whether to use discord.Client
or commands.Bot
.
discord.Client:
- Is more lightweight than commands.Bot
- Is best if you're not going to be using commands
commands.Bot:
- Has an extensive commands system
- Is best if your bot is going to have commands
- Supports a high amount of code splitting through 'cogs'
- Supports easy discord object conversion
In this guide we will make a bot with commands, so we will use commands.Bot.
We want to start our code by importing discord and discord.ext.commands, and defining our bot:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!", case_insensitive=True)
bot.run('paste bot token in this string')
What we just did was initialize a class as an object. This is similar to having an idea for an invention, then creating your invention. We defined this class to the variable bot
, which can be named whatever you want. Most people use bot
or client
.
Now that our bot is defined, we can start on the first commands. discord.py's commands.Bot creates commands like this:
@bot.command(name='command_name', description="description for help command")
async def command(ctx, other_arguments_here):
# Do stuff...
Let's make our first command, which will say "Hello" to the user. We need to add our commands in between the bot's initialization and the bot.run line.
@bot.command(name='hello', description="Greet the user!")
async def hello(ctx):
await ctx.send("Hello!")
ctx
is the context of our command, which contains lots of data that can be used. It also has the send method, which will let us send a message to the channel the command was used in.
Now, let's make it say the command user's username! To do this we can use ctx.author
:
@bot.command(name='hello', description="Greet the user!")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.name}!") # f-string
Now, when we greet the user, it will say Hello mikey π!
Congratulations! You've just written a bot using discord.py! Run the code, and your bot should come online! When you type !hello
, the bot will respond!
Final Code:
PSSST! Need some web development resources? Check out this article, by @devlorenzo!
Top comments (13)
It's not working for me
Is there an error, or is there just nothing happening?
It worked latter issue was due to other asynchronous function.
Oh, ok! Glad it works for you then!
Is there a way I can contact you personally to talk to you about a few of these things?
Sure! you can join my discord server! You can also send me a DM on here!
you need to open your inbox in your settings under extensions so people can DM you on here I think.
how do i send a message in a specific channel
Thank for the mention. Nice first article!
Thanks! :)
i want to only be able to send links to links channel, what do i do?
You could check if
message.channel.id == 123456789098
(123456789098 is your channel id, of course)
If it matches, just return.
Thank you so much! your module for discord.py very much helped me in bot developement