Discord.py 中的延迟命令

Ruf*_*fpi 7 python discord discord.py

我查看了很多地方,但找不到使用 ping(延迟)命令的方法discord.py,如下所示:

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

men*_*tal 18

真的在这一点上你应该使用discord.py重写分支

这将是我使用命令扩展的解决方案。

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


小智 8

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}!')
Run Code Online (Sandbox Code Playgroud)

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


小智 5

使用此代码

@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')
Run Code Online (Sandbox Code Playgroud)


小智 5

可能有一百万行更好的代码可以用于此,但这是我使用的

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