【问题标题】:Latency command in Discord.pyDiscord.py 中的延迟命令
【发布时间】:2018-02-28 15:04:46
【问题描述】:

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

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

【问题讨论】:

  • 你到底想用这个做什么?您是否希望系统在收到特定输入后立即发布响应,以便人们意识到他们对系统的延迟,或者您是否希望通过不和谐的命令问题来 ping 外部站点?
  • 发送消息后,它会回复收到消息需要多长时间

标签: python discord discord.py


【解决方案1】:

此时你真的应该使用rewrite branch of discord.py

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

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

【讨论】:

    【解决方案2】:

    在discord.py的rewrite分支上,你可以使用如下:

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

    当然,如果您使用不同的变量名,则需要更改 bot

    【讨论】:

      【解决方案3】:

      使用此代码

      @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】:

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

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

        【讨论】:

          【解决方案5】:

          这个问题的大部分答案都没有绕过 ping,所以我写了一个脚本。

          使用这个:

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

          【讨论】:

            【解决方案6】:

            这个 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)))
            

            【讨论】:

              【解决方案7】:

              好的,之前我向您展示了如何做一个简单的。 现在我去让它变得更好了 它遵循如下

              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)
              

              这是新版本,它更好,而且 100% 正常工作,因为我自己在我的机器人中使用它

              【讨论】:

              • 伙计们,如果您使用我的内容,我会问一件事......只需在您的文件中添加一条评论,说明我在某种程度上提供了帮助......这会让我感觉更好
              【解决方案8】:
              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)))
              

              这不会非常有效,老实说,没有真正的方法来测试它,因为您无法在客户端运行脚本来获得时间。但这将测试脚本开始时系统时钟与不和谐说他们收到消息之间的时间。不是真正的 ping,没有任何定义,但如果您的机器人速度变慢,它会给您一个提示。

              【讨论】:

              • 别忘了导入日期时间
              • 请记住,这实际上并不是测量发送消息所花费的时间,而是测量报告的时间与机器人开始处理响应的时间之间的偏移量。
              • 你也可以成功;测量当前时间,发送消息,测量发送消息的时间并比较这两个时间并将其作为消息发送。最好是编辑原始消息。
              • 不要从 Stack Overflow 复制粘贴代码。这没有经过测试,我在手机上写了这个。什么错误?
              【解决方案9】:

              discord.py 重写的更新答案:

                  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() 使"Blank" is typing... 文本出现。 通过这样做,我们可以根据机器人发送ctx.trigger_typing() 所需的时间来半准确地获取机器人的 ping。当然,您需要 import time 并定义整个 bot 命令。

              【讨论】:

                【解决方案10】:

                简单的解决方案:

                这是以毫秒为单位获取 Bot 延迟的方法。

                round(client.latency * 1000)
                

                完整代码:

                @client.command()
                async def ping(ctx):
                    await ctx.reply(f'Ping is {round(client.latency * 1000)} ms')
                

                【讨论】:

                  【解决方案11】:

                  Discord.py异步不重写

                  EMBED

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

                  没有EMBED

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

                  【讨论】:

                    【解决方案12】:

                    您可以使用 message.author.mention。例如(这可能不是您使用异步编码的方式):

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

                    只是一个基本的例子:D

                    【讨论】:

                      【解决方案13】:
                      @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)))
                      

                      【讨论】:

                        【解决方案14】:
                        @client.command() #ping
                        async def ping(ctx):
                            await ctx.send(f'Pong! In {round(client.latency * 1000)}ms')
                        

                        【讨论】:

                        • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
                        • 此代码与 Wild_Loli (stackoverflow.com/a/62405878/11848657) 中的代码 100% 相同,但您有 ping 命令...
                        【解决方案15】:

                        我这样做只是为了获得平均延迟。

                        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)
                        

                        虽然,真正的亮点是client.latency(),但我建议使用上面的代码。

                        【讨论】:

                          【解决方案16】:

                          嗯,你应该做所有你可以做的事情,比如下面的代码

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

                          【讨论】:

                            【解决方案17】:
                            @bot.command()
                            async def ping(ctx):
                                await ctx.send(f'Pong! `{bot.latency * 1000}`ms')
                            

                            【讨论】:

                              猜你喜欢
                              • 2020-08-17
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2021-03-26
                              • 1970-01-01
                              • 2015-03-25
                              • 2013-07-22
                              相关资源
                              最近更新 更多