Latency command in Discord.py

巧了我就是萌 提交于 2021-02-18 22:41:30

问题


I've looked in a lot of places and I can't find a way to make a ping (latency) command using discord.py, something like this:

@client.command(pass_context=True)
async def pong(ctx):
    # Somehow find 'pingtime'
    await client.say(pingtime)

回答1:


Really at this point you should be using the rewrite branch of discord.py

This would be my solution using the commands extension.

@bot.command()
async def ping(ctx):
    await ctx.send('Pong! {0}'.format(round(bot.latency, 1)))



回答2:


On the rewrite branch of discord.py, you can use the following:

@bot.command()
async def ping(ctx):
    await ctx.send(f'My ping is {bot.latency}!')

Of course, you'll need to change bot if you're using a different variable name.




回答3:


Use this code

@bot.command(pass_context=True)
async def ping(ctx):
    """ Pong! """
    await delete_message(ctx.message)
    before = time.monotonic()
    message = await ctx.send("Pong!")
    ping = (time.monotonic() - before) * 1000
    await message.edit(content=f"Pong!  `{int(ping)}ms`")
    print(f'Ping {int(ping)}ms')



回答4:


This ping command give a response back from how long it took between the bot and discord

import discord 
import time
Client = commands.Bot(commands.when_mentioned_or('...'))


@Client.command(pass_context=True)
async def ping_ms(ctx):
    t = await Client.say('Pong!')
    ms = (t.timestamp-ctx.message.timestamp).total_seconds() * 1000
    await Client.edit_message(t, new_content='Pong! Took: {}ms'.format(int(ms)))



回答5:


There is probably a million better lines of code to use for this but this is what i use

@client.command()
async def ping(ctx):
     await ctx.send(f'Pong! In {round(client.latency * 1000)}ms')



回答6:


import datetime

@client.command(pass_context=True)
async def ping(ctx):
    now = datetime.datetime.utcnow()
    delta = now - ctx.message.timestamp
    await client.say('{}ms'.format(delta(microseconds=1)))

This won't be super effective, let's be honest, there isn't really a way to test this as you can't run a script clientside to get the time. But this will test the time between your system clock when the script begins and when discord says they got the message. Not really a ping, not in any definition but it will give you a hint if your bot is slowed down.




回答7:


Discord.py async not REWRITE

With EMBED

@bot.command(pass_context=True)
async def ping(ctx):
    embed = discord.Embed(title="Pong! :ping_pong:")
    await bot.say(embed=embed)

Without EMBED

@bot.comand(pass_context=True)
async def ping(ctx):
    await bot.say(":ping_pong: Pong!")



回答8:


you can use message.author.mention. For an example (this may not be how you code using async):

await client.send_message(message.channel, str(message.author.mention))

Just a basic example :D




回答9:


Okay so previously i showed you how to do a simple one. Now i went and made it a bit better And it follows as

Import discord
Import time


@Client.command(pass_context=True)
async def ms_ping(ctx):
channel = ctx.message.channel   
try:
    t1 = time.perf_counter()
    await Client.send_typing(channel)
    ta = t1
    t2 = time.perf_counter()
    await Client.send_typing(channel)
    tb = t2
    ra = round((tb - ta) * 1000)
finally:
    pass
try:
    t1a = time.perf_counter()
    await Client.send_typing(channel)
    ta1 = t1a
    t2a = time.perf_counter()
    await Client.send_typing(channel)
    tb1 = t2a
    ra1 = round((tb1 - ta1) * 1000)
finally:
    pass
try:
    t1b = time.perf_counter()
    await Client.send_typing(channel)
    ta2 = t1b
    t2b = time.perf_counter()
    await Client.send_typing(channel)
    tb2 = t2b
    ra2 = round((tb2 - ta2) * 1000)
finally:
    pass
try:
    t1c = time.perf_counter()
    await Client.send_typing(channel)
    ta3 = t1c

    t2c = time.perf_counter()
    await Client.send_typing(channel)
    tb3 = t2c

    ra3 = round((tb3 - ta3) * 1000)
finally:
    pass
try:
    t1d = time.perf_counter()
    await Client.send_typing(channel)
    ta4 = t1d

    t2d = time.perf_counter()
    await Client.send_typing(channel)
    tb4 = t2d

    ra4 = round((tb4 - ta4) * 1000)
finally:
    pass

e = discord.Embed(title="Connection", colour = 909999)
e.add_field(name='Ping 1', value=str(ra))
e.add_field(name='Ping 2', value=str(ra2))
e.add_field(name='Ping 3', value=str(ra3))
e.add_field(name='Ping 4', value=str(ra4))
await Client.say(embed=e)

Here is the new verion and it is better and it is 100% working because i am using it myself in my bot




回答10:


@client.command(pass_context=True)
    async def ping(ctx):
        """Shows the Client Latency."""
        t = await client.say('Pong!')
        ms = (t.timestamp-ctx.message.timestamp).total_seconds() * 1000
        await client.edit_message(t, new_content='Pong! Client Latency: {}ms'.format(int(ms)))



回答11:


@client.command() #ping
async def ping(ctx):
    await ctx.send(f'Pong! In {round(client.latency * 1000)}ms')



回答12:


The way I do it is just to get an average latency.

tests = 500 #the amount of tests to conduct
latency_list = [] #this is where the tests go
for x in range(tests): #this is the loop
    latency = client.latency() #this gathers the latency
    latency_list.append(latency) #puts the latency in the list
lavg = sum(latency_list)/test #averages the list out
print(lavg)

Although, the real star of the show is client.latency(), but I suggest using the code above.




回答13:


Umm insted of doing all that you could do something like the code bellow

@bot.command()
async def ping(ctx):
            await ctx.send(f"pong! my latency is {str(len(bot.latency))}")



回答14:


@bot.command()
async def ping(ctx):
    await ctx.send(f'Pong! `{bot.latency * 1000}`ms')



回答15:


Most of the answers for this question don't round the ping, so I wrote a script that does.

Use this:

async def ping(ctx):
    await ctx.send(f'Pong! {round (bot.latency * 1000)} ms')



回答16:


Updated answer for discord.py rewrite:

    async def latency(ctx):
        time_1 = time.perf_counter()
        await ctx.trigger_typing()
        time_2 = time.perf_counter()
        ping = round((time_2-time_1)*1000)
        await ctx.send(f"ping = {ping}")

await ctx.trigger_typing() makes the "Blank" is typing... text appear. By doing this we can semi-accurately get the ping of the bot based on how long it takes the bot to send ctx.trigger_typing(). Of course, you will need to import time and have the whole bot command defined.



来源:https://stackoverflow.com/questions/46307035/latency-command-in-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!