Create Your First Discord Bot With Python 1

Create Your First Discord Bot With Python

hey guys Today I’ll tell you How To Create Your First Discord Bot With Python, I am pretty sure you always heard about this language known as “python”, because its pretty famous and useful. It is used in many fields some of the examples are – Instagram, Google, Spotfiy, Netflix, etc. According to Wikipedia “Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace”.

While I was learning about python I noticed that it is very interesting language, so I decided to create guide and tutorials on discord.py. Still I am not professional in developing discord bot using python but I’ll try my best to make you guys understand how to create discord bot using python.

How To Create A Bot Token

If you are newbie to discord developer world you might not know how to create a bot token, no worries I’ll be telling you how you can generate your bot token because without token you won’t be able to get your bot online.

To create a bot token go to discord developer portal, create a new application, add name and click on create.

Create Your First Discord Bot With Python 2
Create Your First Discord Bot With Python 3

After creating your first application go to bot section under the setting on left side.

Create Your First Discord Bot With Python 4

Now click on add bot option on the right side. Your bot will be generated.

Create Your First Discord Bot With Python 5
Create Your First Discord Bot With Python 6

Now Copy the bot token and use it in this project. You will have to invite the bot in your testing server to do that go to OAuth2.

Create Your First Discord Bot With Python 7

Under scopes tick on bot option.

Create Your First Discord Bot With Python 8

Then setup the bot permission as per your need then copy the url and invite the bot to your server.

Create Your First Discord Bot With Python 9
Create Your First Discord Bot With Python 10

How To Create Your First Discord Bot With Python

First of all install discord.py in your IDE by using terminal, if you don’t know how to install discord.py then here is the link for guide to install disocord.py.

We will start with importing discord and importing commands from discord extension

import discord
from discord.ext import commands

Before going any further, it is mandatory to define client and we will be defining command prefix in it

client = commands.Bot(command_prefix="!", case_insensitive=True)

Now we will call event of on_ready and print something on console, I will be setting the bot status in on_ready event.

@client.event
async def on_ready():
  print(f"I am ready to go - {client.user.name}")
  await client.change_presence(activity=discord.Game(name=f"{client.command_prefix}ping"))

We created our first event to know that bot is ready, it means whenever you start the bot then the bot will send message to console. Between if you want to run the bot now then you can use client.run(“TOKEN”), But i will be creating the simple ping command before running the bot 😉

Now we will be creating command and for that we will be using @client.command()

@client.command(name="ping")
async def _ping(ctx):
  ctx.send(f"Pong: {client.latency}")

At last we will add client.run() and add Token in it.

client.run("TOKEN")
Create Your First Discord Bot With Python

Now we will be adding simple embed in ping command by using discord.Embed()

@client.command(name="ping")
async def _ping(ctx):
  embed= discord.Embed(title=f"{client.user.name} - PING LATENCY", description=f"Pong: {client.latency}", color=0xff2050)
  ctx.send(embed=embed)
Create Your First Discord Bot With Python 11

Video Tutorial To Create Your First Discord Bot With Python

Source Code

Conclusion

In this post I have shown you how you can create your first python bot. I have made it as simple as possible and easy to understand, I have used repl.it as IDE in this tutorial you can use any IDE you want. If you are still facing some issue please let me know in the comments, I’ll be glad to help.

Also Read: CREATE DISCORD BOT IN GLITCH WITH V12 OF DISCORD.JS

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *