terabyte.
Posted on
#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!
Final Code:
PSSST! Need some web development resources? Check out this article, by @devlorenzo!
Top comments (13)
Subscribe
Deepak Raj
โข 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