DEV Community

Cover image for Get started with discord.py
terabyte.
terabyte.

Posted on

Get started with discord.py

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
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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...
Enter fullscreen mode Exit fullscreen mode

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!")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!
Demo
Final Code:

PSSST! Need some web development resources? Check out this article, by @devlorenzo!

Top comments (13)

Collapse
 
codeperfectplus profile image
Deepak Raj
@bot.command(name='hello', description="Greet the user!")
async def hello(ctx):
    await ctx.send(f"Hello {ctx.author.name}!") # f-string
Enter fullscreen mode Exit fullscreen mode

It's not working for me

Collapse
 
mikeywastaken profile image
terabyte.

Is there an error, or is there just nothing happening?

Collapse
 
codeperfectplus profile image
Deepak Raj

It worked latter issue was due to other asynchronous function.

Thread Thread
 
mikeywastaken profile image
terabyte.

Oh, ok! Glad it works for you then!

Collapse
 
sarmqn profile image
Sarmqn

Is there a way I can contact you personally to talk to you about a few of these things?

Collapse
 
mikeywastaken profile image
terabyte. • Edited

Sure! you can join my discord server! You can also send me a DM on here!

Collapse
 
sarmqn profile image
Sarmqn

you need to open your inbox in your settings under extensions so people can DM you on here I think.

Collapse
 
pythondev1 profile image
Kate

how do i send a message in a specific channel

Collapse
 
devlorenzo profile image
DevLorenzo • Edited

Thank for the mention. Nice first article!

Collapse
 
mikeywastaken profile image
terabyte.

Thanks! :)

Collapse
 
gnypkuba profile image
0dD3K

i want to only be able to send links to links channel, what do i do?

Collapse
 
mikeywastaken profile image
terabyte. • Edited

You could check if message.channel.id == 123456789098
(123456789098 is your channel id, of course)

If it matches, just return.

Collapse
 
mrfenek profile image
MisterFenek

Thank you so much! your module for discord.py very much helped me in bot developement