【问题标题】:How to make an Uptime command for a Discord administrator bot in discord.py如何在 discord.py 中为 Discord 管理员机器人创建 Uptime 命令
【发布时间】:2020-12-24 05:25:16
【问题描述】:

所以...为了给你一些背景信息,我需要告诉你我做了什么。我创建了一个用于审核的不和谐机器人 (mellobot.net),我需要有关命令的帮助。我想为实际的机器人本身添加一个 -uptime 命令以显示 DD HH MM 中的时间,但不知道命令行会是什么样子。 (我想要 twitch 用户用于 twitch 流的 NightBots !uptime 之类的东西)是否有任何 discord.py 书呆子可以帮助我解决这个困境?

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    我不确定,但也许你可以使用datetime 模块。当bot是on_ready时,你可以取日期,并像这样发出!uptime命令:

    @client.event
    async def on_ready():
        global startdate
        startdate = datetime.now()
    @client.command()
    async def uptime(ctx):
        now = datetime.now()
        uptime = startdate - now
        uptime = uptime.strftime('%d/%h/%M')# I don't remember the date's well, might be the lowercases are false.
        await ctx.send(f'Uptime: {uptime}')
    

    【讨论】:

      【解决方案2】:

      存储程序启动的时间,然后做一些数学运算。 on_ready() 事件可以并且将会在机器人正常运行期间触发多次,如果您在其中打印一条消息之外的任何其他操作,通常会发生坏事。

      from datetime import datetime
      
      bot = commands.Bot(command_prefix='>')
      bot.launch_time = datetime.utcnow()
      
      @bot.command()
      async def uptime(ctx):
          delta_uptime = datetime.utcnow() - bot.launch_time
          hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
          minutes, seconds = divmod(remainder, 60)
          days, hours = divmod(hours, 24)
          await ctx.send(f"{days}d, {hours}h, {minutes}m")
      

      【讨论】:

        【解决方案3】:

        这是 discord.py re-write 的最新 workign 命令

        @commands.command()
            async def uptime(self, ctx):
                delta_uptime = datetime.datetime.utcnow() - self.bot.launch_time
                hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
                minutes, seconds = divmod(remainder, 60)
                days, hours = divmod(hours, 24)
                e = discord.Embed(title=f"I've been up  for {days}d, {hours}h, {minutes}m, {seconds}s,", color=discord.Color.green())
                await ctx.send(embed=e)```
        

        【讨论】:

          猜你喜欢
          • 2021-01-01
          • 2021-01-07
          • 2023-01-26
          • 2021-09-19
          • 2021-06-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-10
          • 2021-04-03
          相关资源
          最近更新 更多