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

2024-04-25 04:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

    @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导入命令 导入日期时间 导入json 进口aiohttp 随机输入 导入ast 从pymongo导入MongoClient 从discord.utils导入获取 从pytz导入时区 导入操作系统 输入数学 进口itertools 从discord.ext.commands导入CommandNotFound、MissingPermissions、MessageNotFound 导入异步


Tags: nameselfaddfalsetruefieldvaluebot
1条回答
网友
1楼 · 发布于 2024-04-25 04:28:17

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

如果您不知道,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。例如,在cog中,它将是self.bot.start_time

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

相关问题 更多 >