How to make a Discord bot in Python

Hello Everyone! In this tutorial, we are going to learn how to make a discord bot in Python. All you need is to have an account in discord. Discord is a communication platform for gamers. A Discord bot is an automated program that works as designed in the Discord platform. Let’s say you have to build a custom chat messages that chat by itself to others. I hope that this helps many gamers to build their own custom automated bot in Discord.

Firstly log in to your Discord account. On the left-hand side of your window, you will see a plus symbol, click the plus symbol to create a server.

Discord Developer Portal

Name your server and click create.

Discord Developer Portal

Next to create a bot go to Discord Developer Portal link https://discordapp.com/developers/docs/intro. On the left-hand side, there will be a tab called Applications. Click on Applications and click New Applications. Name your App and click create. You will see the client id, which will be used later.

Discord Developer Portal

On the right-hand side, click Bot and click create New Bot User and you will find the bot token, which will be used later. And you will see this.

Build a discord bot in Python

In the Discord Developer Portal, https://discordapp.com/developers/docs/intro.
Click OAuth2 on the left-side. Copy the Base Authorization URL from the OAuth2 URLs.

How to make a Discord bot in Python

Get the client id from the Application page. Edit the copied link by adding ?client_id={your client id}&scope=bot. For Example https://discordapp.com/api/oauth2/authorize?client_id={your id}&scope=bot.

Go to the above link. And select the created server name and click Authorize. Finally, you will see this success page.

Python program to build a discord bot

Implementation: Python program to build a discord bot

Now we will look in the Python code implementation. Install the module called Discord.py by this command “pip install discord.py

Imagine this scenario, a person wants to talk to you and get some help for some game. He is messaging to you and you got to reply. what if your bot reply to those messages.

#importing the discord module
import discord
#creating a new discord client for us to use. cool_bot be the client
client=discord.Client()
#methods waiting for the event
@client.event
#when the bot started running, we may print its name, id etc
async def on_ready():
    print('Logged in')
    print("Username: ",end='')
    print(client.user.name)
    print("Userid: ",end='')
    print(client.user.id)
@client.event
#when the bot gets the message this method gets triggered
async def on_message(message):
    if message.author.id == client.user.id:
        return
    #message starts with hi or hello then print these
    if message.content.startswith('Hi') or message.content.startswith('Hello'):
        await message.channel.send('Hello {0.author.mention} Welcome Man'.format(message))
    #when the message with help then do this
    elif message.content.startswith('help'):
        await message.channel.send("Let me check with that level and come back to you")
#finnaly we have to run our bot. previous stuffs are defining the functions of the bot
client.run('your token_id')

Output:

Logged in
Username: cool bot
Userid: 706738322688704513

How to make a Discord bot in Python

See the image, the bot replies as we defined. please go through the API functions and you can do a lot more with the Discord bot.

Also read: Chatbot Using Deep Learning in Python

Leave a Reply

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