Get started with discord.py
Tags: python, programming, discord, discordpy
Published: 2021-03-04 15:41:12

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](https://discordpy.readthedocs.io/en/latest/discord.html#creating-a-bot-account).

## 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:
```py
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:
```py
@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.

```py
@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`:
```py
@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!
![Demo](https://mikey.has-no-bra.in/y8KFx9.png)
Final Code:
{% gist https://gist.github.com/isigebengu-mikey/b88bf383d478490354a9f3b47ec1cc7e.js %}

*PSSST! Need some web development resources? Check out this article, by [@devlorenzo](https://dev.to/devlorenzo)!* {% link https://dev.to/devlorenzo/the-ultimate-compilation-of-cheat-sheets-100-268g %}