Get started with discord.py!

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…


This content originally appeared on DEV Community and was authored by Tejas Kathuria

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

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')

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

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

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:


This content originally appeared on DEV Community and was authored by Tejas Kathuria


Print Share Comment Cite Upload Translate Updates
APA

Tejas Kathuria | Sciencx (2021-11-24T15:39:12+00:00) Get started with discord.py!. Retrieved from https://www.scien.cx/2021/11/24/get-started-with-discord-py/

MLA
" » Get started with discord.py!." Tejas Kathuria | Sciencx - Wednesday November 24, 2021, https://www.scien.cx/2021/11/24/get-started-with-discord-py/
HARVARD
Tejas Kathuria | Sciencx Wednesday November 24, 2021 » Get started with discord.py!., viewed ,<https://www.scien.cx/2021/11/24/get-started-with-discord-py/>
VANCOUVER
Tejas Kathuria | Sciencx - » Get started with discord.py!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/24/get-started-with-discord-py/
CHICAGO
" » Get started with discord.py!." Tejas Kathuria | Sciencx - Accessed . https://www.scien.cx/2021/11/24/get-started-with-discord-py/
IEEE
" » Get started with discord.py!." Tejas Kathuria | Sciencx [Online]. Available: https://www.scien.cx/2021/11/24/get-started-with-discord-py/. [Accessed: ]
rf:citation
» Get started with discord.py! | Tejas Kathuria | Sciencx | https://www.scien.cx/2021/11/24/get-started-with-discord-py/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.