Hii Guys I am Mannu and this is my 2nd post on tutorial of discord.py. If you haven't seen the previous post yet please go check it out to understand from start.
So as we have suceessfully created our bot and now its time to make it working.
What we will do is create a file named main.py. You can create file by any name but go with main.py for now so that you remain with me......
So what we will be doing today is make our bot online and create our first command.
This is how our codebase is looking (ignore the License and README file for a while)
Some of you might be wondering why there is .env file and what is it used for. Well .env file is used to store environment variables like my discord bot token. I will use some fuctions to import it into my main file.you can directly use token in your main file it will work its just for safety purpose and make sure to remove the token from there while publishing it on github.
So let's write our first line of code. import the following things into your main file.
import discord
from discord.ext import commands
now some of you might say what are these. These are just packages we need to run our bot. discord.ext is just extension of discord.py that's why we will be using discord.ext so that our code remains beginner friendly.
So in the Next line we will be setting intents for our bot to work.
intents = discord.Intents.default()
intents.message_content = True
What we done here is create a variable which store all default intents from discord package for our discord bot. then we Set the intents.message_content = True
. This Means we are allowing our bot to read and send messages.
then we create a variable for out bot.
bot = commands.Bot(command_prefix=".",intents=intents)
What we are doing here is creating a bot variable which store commands.Bot() and as param we are giving command_prefix which is our discord bot prefix and setting intents = intents, which means we are giving our bot send and read messages permissions mentioned earlier.
now at the end type this
bot.run("your bot token here")
What this does is that it connects with our bot and start it/make it online.
now our whole code will look like this
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix=".",intents=intents)
bot.run("your bot token here")
run this code by running this command
python main.py
if there are any errors in the terminal do comment on this post so that I can know
Congrats! Our bot is now online.
now let's add our first command.
We Will be writing our code for commands in between these two
let's create a command. to create a command we need to use this syntax.
@bot.command()
async def hello(ctx):
await ctx.send("Hii There! I am Mannu And Welcome to the discord.py tutorial")
What we are doing here is adding a decorator named bot.command(). What it does is it calls our def when the command is runned on bot. Don't get too confused you will understand what I am trying to say.
Now we created an async def named hello which is also our command name. The good thing is that the above decorator creates a command with the same name as our function. Now in this async def we are giving ctx as param. Then in this def we await our line of code which is ctx.send("Your Message")
. What it does is it makes the bot send the message.
Save your program and get ready to to use our first command.
Run the program and wait for our bot to get online.
Type .hello in the discord chat. And press Enter
And there you go. We successfully created our first command.
Here's the complete code just in case
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
load_dotenv()
token = os.environ["TOKEN"]
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix=".",intents=intents)
@bot.command()
async def hello(ctx):
await ctx.send("Hii There! I am Mannu And Welcome to the discord.py tutorial")
bot.run(token)
We will be creating more complex commands in future so do try to understand what this stuff is. Please Do like if it helps.
Happy Coding!
Mannu
Top comments (4)
Why don't we use a plain text file instead of .env ?
well env are more secured than plain text
Waiting for the next part <3
Will post at night <3