Discord.py | 01

Simply discord.py is an API wrapper for discord APi written in python, For making discord bots.

Let’s start by installing discord.py
pip install discord.py

The code

We need to import discord.py and define our client(bot):

#importing disc…


This content originally appeared on DEV Community and was authored by Hamza Hesham

Simply discord.py is an API wrapper for discord APi written in python, For making discord bots.

Let's start by installing discord.py
pip install discord.py

The code

We need to import discord.py and define our client(bot):

#importing discord.py.
import discord
from discord.ext import commands
#defining our client(bot) variable and initializing the bot class.
#you can change the name of the variable as you like 
client = commands.Bot(command_prefix="!")
#running our bot.
client.run("Your bot token")

You can get your bot token from here:





Then we are going to make an event:

#This part is not important, But it is used to verify that the bot had successfully started.
#This simply prints I'm alive when the bot is ready.
@client.event
async def on_ready():
    print("I'm Alive")

Defining our first command:

@client.command()
async def hi(ctx):
    await ctx.send("Hello World")
#Simply the name of the function is the name of the command

Generating invite link

Head to your bot application:




In the permissions section you can choose permissions suitable with your bot, But i prefer using Administrator

The final code:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = "!")

@client.event
async def on_ready():
    print("I'm Alive")

@client.command()
async def hi(ctx):
    await ctx.send("Hello World")

client.run("your token")

Running the code:I'm Alive

Image description
It's working.
The code
Thanks for reading, see you in the next part.


This content originally appeared on DEV Community and was authored by Hamza Hesham


Print Share Comment Cite Upload Translate Updates
APA

Hamza Hesham | Sciencx (2022-03-19T22:48:19+00:00) Discord.py | 01. Retrieved from https://www.scien.cx/2022/03/19/discord-py-01/

MLA
" » Discord.py | 01." Hamza Hesham | Sciencx - Saturday March 19, 2022, https://www.scien.cx/2022/03/19/discord-py-01/
HARVARD
Hamza Hesham | Sciencx Saturday March 19, 2022 » Discord.py | 01., viewed ,<https://www.scien.cx/2022/03/19/discord-py-01/>
VANCOUVER
Hamza Hesham | Sciencx - » Discord.py | 01. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/19/discord-py-01/
CHICAGO
" » Discord.py | 01." Hamza Hesham | Sciencx - Accessed . https://www.scien.cx/2022/03/19/discord-py-01/
IEEE
" » Discord.py | 01." Hamza Hesham | Sciencx [Online]. Available: https://www.scien.cx/2022/03/19/discord-py-01/. [Accessed: ]
rf:citation
» Discord.py | 01 | Hamza Hesham | Sciencx | https://www.scien.cx/2022/03/19/discord-py-01/ |

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.