Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 24, 2021 03:39 pm GMT

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 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 discordfrom discord.ext import commandsbot = 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:


Original Link: https://dev.to/tejasdev/get-started-with-discordpy-14l0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To