Recents in Beach

How to make ping command in discord.py - pyGod

How to make ping command in discord bot with python

HOW TO MAKE PING COMMAND IN DISCORD BOT WITH PYTHON

In this particular article you will how to create different commands and make your bot respond to it . We will create ping command in our python bot that we created earlier, this ping command will do nothing it is just a simple command which makes the bot say pong ! and its ping in ms.

BY THE END OF THIS ARTICLE YOU'LL LEARN:

  • How to make and use commands in discord.py
  • How to make ping command and that makes the bot reply with pong
  • How to make a command and make the bot answer what you desire
  • How to make a command in discord.py that makes the bot to tell its ping

WHAT TO YOU NEED TO MAKE COMMANDS IN BOT :

You don't need anything that you don't have already, 
  • You need your favourite code editor , mine is Visual Studio Code
  • You need Python 
  • You need discord.py library.
  • You need your main.py file , We will create it again if you don't have one.

    HOW TO MAKE A COMMAND IN DISCORD.PY


    In order to create commands in discord.py , you need to import commands from discord.ext , you can do this simply by adding a line in your main.py file :

     import discord  
     from discord.ext import commands  
     client = commands.Bot(command_prefix="-")  
     token = "YOUR TOKEN HERE"  
     @client.event  
     async def on_ready():  
       print(" Bot is online!")  
     client.run(token)  
    

    We added the 2nd line - from discord.ext import commands  in order to use commands in our bot.

    Let's add ping command to the bot that makes the bot reply with pong!,

     import discord  
     from discord.ext import commands  
     client = commands.Bot(command_prefix="-")  
     token = "YOUR TOKEN HERE"  
     @client.event  
     async def on_ready():  
       print(" Bot is online!")  
     @client.command()  
     async def ping(ctx):  
       await ctx.send(f"pong! ")  
     client.run(token)  
    

    So, now we added the ping command, when you run this file and use -ping command in your guild, the bot would reply with pong.

    Let's create a command that makes the discord.py to tell its latency.

    MAKING THE LATENCY COMMAND IN DISCORD.PY 

    We will not create any separate command for this, we will just add some code to our ping command, so that when we use ping the bot says ping and also its latency.

     import discord  
     from discord.ext import commands  
     client = commands.Bot(command_prefix="-")  
     token = "YOUR TOKEN HERE"  
     @client.event  
     async def on_ready():  
       print(" Bot is online!")  
     @client.command()  
     async def ping(ctx):  
       await ctx.send(f"pong! Latency is {round(client.latency * 1000)}ms")  
     client.run(token)  
    
    So what we did here is we added the latency to ping command and multiplied it with 1000 so that the bot shows its latency in ms.

    CONCLUSION

    Congratulations, now you can create commands in discord.py bot and make it whatever you desire.
    You learnt :
    • What are commands in discord.py
    • How to make commands in discord.py
    • How to make ping command in discord.py
    • How to make latency command in discord.py
    Although, I share code here, but I don't really recommend you to copy & paste what I write, You should see my code and then try to understand it .
    I am very thankul to you if you are reading till here.
    You can contact me and ask your queries in the comments section!
    You can also checkout my youtube channel for All the tutorials in Hindi.


    Post a Comment

    0 Comments