DEV Community

Cover image for Discord.py | 01
Hamza Hesham
Hamza Hesham

Posted on

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 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")
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Running the code:I'm Alive

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

Top comments (0)