Discord.py中的延迟命令

2024-10-01 15:40:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: 方法py命令clienttrueasyncdef地方
3条回答

这个ping命令从bot和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)))

实际上,此时您应该使用rewrite branch of discord.py

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

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

使用此代码

@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')

相关问题 更多 >

    热门问题