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
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:
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:
Thanks for reading, see you in the next part.
Top comments (0)