如何在discord.py中执行ping命令?

时间:2020-09-27 12:07:26

标签: discord.py

我使用了一些技巧,但它们不起作用。
例如:

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

请帮助!

1 个答案:

答案 0 :(得分:0)

我使用了以下代码:

async def ping(ctx):
    if round(client.latency * 1000) <= 50:
        embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x44ff44)
    elif round(client.latency * 1000) <= 100:
        embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xffd000)
    elif round(client.latency * 1000) <= 200:
        embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xff6600)
    else:
        embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x990000)
    await ctx.send(embed=embed)

确保在顶部执行@ client.command(),由于使用了不同的命令处理程序,因此将其删除。显然,我决定通过对其进行颜色编码并把所有内容都嵌入来使其精美。如果您想要一个简单的命令来返回ping命令,并且仅返回ping命令,请尝试以下操作:

async def ping(ctx):
    await ctx.send(f"{client.latency}")

真的没有比这更简单的了。如果这不起作用,请尝试:

async def ping(ctx):
    await ctx.channel.send(f"{client.latency}")

一如既往,不要忘记在顶部添加@ client.command()。