【问题标题】:How to make a ping command in discord.py?如何在 discord.py 中发出 ping 命令?
【发布时间】:2021-01-13 05:11:42
【问题描述】:

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

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

请帮忙!

【问题讨论】:

  • 你能提供更多代码吗?你真的在运行机器人吗?

标签: discord.py


【解决方案1】:

我使用了这个代码:

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()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 2018-06-07
    • 1970-01-01
    • 2021-06-06
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多