首页 > 解决方案 > discord py中的正常运行时间命令,我总是得到同样的错误

问题描述

    @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'

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

从discord.ext 导入discord 导入命令 导入日期时间 导入json 导入aiohttp 导入随机 从pymongo 导入ast 从discord.utils 导入MongoClient 从pytz 导入get 导入时区 导入os 导入数学 从discord.ext.commands 导入itertools 导入CommandNotFound,MissingPermissions,MessageNotFound导入异步

标签: pythonpython-3.xdiscordbots

解决方案


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

如果你不知道,机器人变量是这样声明的;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在该文件之外使用。例如,在 cog 中它会是self.bot.start_time.

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


推荐阅读