Get started with discord.py (2024)

terabyte.

Posted on

Get started with discord.py (3) Get started with discord.py (4) Get started with discord.py (5) Get started with discord.py (6) Get started with discord.py (7)

#python #programming #discord #discordpy

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.

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

@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.

@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:

@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!
Get started with discord.py (8)
Final Code:

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

The ultimate Cheat sheets compilation (200+) - ๐Ÿ”ฅ๐ŸŽ / Roadmap to dev ๐Ÿš€ DevLorenzo for World In Dev ใƒป Mar 2 '21 ใƒป 17 min read #productivity #webdev #beginners #javascript

Top comments (13)

Subscribe

Deepak Raj

Deepak Raj

With 3+ years of experience in Python & Data Science. I love to write about Machine Learning, Deep Learning, and Python.

  • Email

    deepak008@live.com

  • Location

    India

  • Education

    Post Graduation in Data Science

  • Work

    Data Scinetist

  • Joined

โ€ข Apr 15 '21

  • Copy link
@bot.command(name='hello', description="Greet the user!")async def hello(ctx): await ctx.send(f"Hello {ctx.author.name}!") # f-string

It's not working for me

terabyte.

terabyte.

a super young defloper who speaks the mystical *snake language ๐Ÿ*

  • Location

    place

  • Work

    deflop.

  • Joined

โ€ข Apr 17 '21

  • Copy link

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

Deepak Raj

Deepak Raj

With 3+ years of experience in Python & Data Science. I love to write about Machine Learning, Deep Learning, and Python.

  • Email

    deepak008@live.com

  • Location

    India

  • Education

    Post Graduation in Data Science

  • Work

    Data Scinetist

  • Joined

โ€ข Apr 17 '21

  • Copy link

It worked latter issue was due to other asynchronous function.

terabyte.

terabyte.

a super young defloper who speaks the mystical *snake language ๐Ÿ*

  • Location

    place

  • Work

    deflop.

  • Joined

โ€ข Apr 17 '21

  • Copy link

Oh, ok! Glad it works for you then!

Sarmqn

Sarmqn

Learning to code a discord bot!

  • Email

    sarmanchowdhury@gmail.com

  • Location

    London

  • Joined

โ€ข Apr 28 '21

  • Copy link

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

terabyte.

terabyte.

a super young defloper who speaks the mystical *snake language ๐Ÿ*

  • Location

    place

  • Work

    deflop.

  • Joined

โ€ข Apr 28 '21 โ€ข Edited on Apr 28 โ€ข Edited

  • Copy link

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

Sarmqn

Sarmqn

Learning to code a discord bot!

  • Email

    sarmanchowdhury@gmail.com

  • Location

    London

  • Joined

โ€ข Apr 29 '21

  • Copy link

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

Kate

Kate

  • Joined

โ€ข Nov 28 '21

  • Copy link

how do i send a message in a specific channel

DevLorenzo

DevLorenzo

Hello World ๐Ÿ‘‹๐ŸปI'm a young man working to be a full stack developer. My goal is to create a programming community for exchanging ideas. Send weekly newsletter! Thx for 160k viewโค

  • Email

    lorenzo.udemy.dev@gmail.com

  • Location

    Rome ๐Ÿ›๏ธ

  • Education

    Dream to be a Full stack dev

  • Work

    Follow me for more cool content!

  • Joined

โ€ข Mar 4 '21 โ€ข Edited on Mar 4 โ€ข Edited

  • Copy link

Thank for the mention. Nice first article!

terabyte.

terabyte.

a super young defloper who speaks the mystical *snake language ๐Ÿ*

  • Location

    place

  • Work

    deflop.

  • Joined

โ€ข Mar 4 '21

  • Copy link

Thanks! :)

0dD3K

0dD3K

  • Joined

โ€ข Jul 14 '21

  • Copy link

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

terabyte.

terabyte.

a super young defloper who speaks the mystical *snake language ๐Ÿ*

  • Location

    place

  • Work

    deflop.

  • Joined

โ€ข Jul 22 '21 โ€ข Edited on Jul 22 โ€ข Edited

  • Copy link

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

If it matches, just return.

MisterFenek

MisterFenek

  • Joined

โ€ข Jul 31 '22

  • Copy link

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

For further actions, you may consider blocking this person and/or reporting abuse

Get started with discord.py (2024)

References

Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5651

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.