discord py 中的正常运行时间命令,我总是遇到相同的错误

时间:2021-02-10 15:40:58

标签: python python-3.x discord bots

    @commands.command(aliases=['uptime', 'up'])
    async def status(self, ctx):

        timeUp = time.time() - self.bot.startTime
        hours = timeUp / 3600
        minutes = (timeUp / 60) % 60
        seconds = timeUp % 60

        admin = self.bot.AppInfo.owner
        users = 0
        channel = 0
        if len(self.bot.commands_used.items()):
            commandsChart = sorted(self.bot.commands_used.items(), key=lambda t: t[1], reverse=False)
            topCommand = commandsChart.pop()
            commandsInfo = '{} (Top-Command: {} x {})'.format(sum(self.bot.commands_used.values()), topCommand[1], topCommand[0])
        else:
            commandsInfo = str(sum(self.bot.commands_used.values()))
        for guild in self.bot.guilds:
            users += len(guild.members)
            channel += len(guild.channels)

        embed = discord.Embed(color=ctx.me.top_role.colour)
        embed.set_footer(text='UwU')
        embed.set_thumbnail(url=ctx.me.avatar_url)
        embed.add_field(name='Admin', value=admin, inline=False)
        embed.add_field(name='Uptime', value='{0:.0f} Stunden, {1:.0f} Minuten und {2:.0f} Sekunden\n'.format(hours, minutes, seconds), inline=False)
        embed.add_field(name='Beobachtete Benutzer', value=users, inline=True)
        embed.add_field(name='Beobachtete Channel', value=channel, inline=True)
        embed.add_field(name='Ausgeführte Commands', value=commandsInfo, inline=True)
        embed.add_field(name='Bot Version', value=self.bot.botVersion, inline=True)
        embed.add_field(name='Discord.py Version', value=discord.__version__, inline=True)
        embed.add_field(name='Python Version', value=platform.python_version(), inline=True)
        embed.add_field(name='Betriebssystem', value=f'{platform.system()} {platform.release()} {platform.version()}', inline=False)
        await ctx.send('**:information_source:** Informationen über diesen Bot:', embed=embed)

我需要有关此命令的帮助

我收到以下错误:[10.02.2021 16:31:10][WARN] Unknown error:[0mCommand raised an exception: AttributeError: 'Bot' object has no attribute 'startTime'

我检查了我所有的进口……我的进口是

导入不和谐 from discord.ext 导入命令 导入日期时间 导入json 导入 aiohttp 随机导入 进口AST 从 pymongo 导入 MongoClient 从 discord.utils 导入获取 从 pytz 导入时区 导入操作系统 导入数学 导入迭代工具 从 discord.ext.commands 导入 CommandNotFound、MissingPermissions、MessageNotFound 导入异步

1 个答案:

答案 0 :(得分:0)

discord.ext.commands.Bot 不保存机器人启动的日期。但是,您可以非常轻松地手动执行此操作。在 ma​​in 文件的 on_ready 函数内,创建一个机器人变量并保存当前日期时间。确保你也导入它!

如果您不知道,bot 变量是这样声明的; bot.your_var_name = value。但是要小心,因为您可能会覆盖某些内容。在你的情况下,你可以尝试;

import datetime

'''
Import everything else you need and
make sure to declare the bot client
'''

@bot.event
async def on_ready():
    bot.start_time = datetime.datetime.now()

然后您可以在该文件外使用 bot.start_time。例如,在一个齿轮中,它将是 self.bot.start_time

在您的代码中,我建议使用 datetime 而不是 time,它更易于使用。