DEV Community

Cover image for Get started with discord.py!
Tejas Kathuria
Tejas Kathuria

Posted on • Updated on

Get started with discord.py!

[](url)Discord.py is a very fantastic API. It helps us in creating Discord bots easily. People have made game bots, RPG bots, Moderation Bots, Economy bots, and even more! Using this guide, you can learn how to use it.

Install discord.py

Make sure you have python installed on your computer. You have to install discord.py with

pip install discord.py
Enter fullscreen mode Exit fullscreen mode

voilà! discord.py has been installed!

For making bots you'll need to have a developer account. You can check this guide!

Basics of developing a bot

When starting you have to choose weather 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:
• Is best if your bot is going to have commands
• Supports easy discord object conversion

In this series we will make a bot with commands.Bot.

We have to start by importing discord and discord.ext.commands

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")

bot.run('YOUR TOKEN HERE')
Enter fullscreen mode Exit fullscreen mode

We initialized a class as an object. We defined this class to the variable bot, which can be named whatever you want. Most people use bot or client.

Now when our bot is defined, we can start with our first command. In discord.py we make commands like this:

@bot.command(name='command_name', description="description for help command")
async def command(ctx, other_arguments_here):
   # Tell bot what to do here
Enter fullscreen mode Exit fullscreen mode

Let's make a command which will greet the user with a hello. We'll use ctx.author for doing this:

@bot.command(name='Hello', description="Greets the sender")
async def hello(ctx):
    await ctx.reply(f"Hello {ctx.author.name}!")
# f-string
Enter fullscreen mode Exit fullscreen mode

Now, when we greet the user, it will say Hello USER!

Congrats!! You've just written a bot using discord.py! Run the code. When you'll type $hello it should respond!
Image description
Source Code:

Top comments (0)