python - 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导入异步
解决方案
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
,它更容易使用。
推荐阅读
- c++ - 合并排序基本案例
- reactjs - React Re-Render Component on props Change
- jsp - 获取删除按钮以工作 Java Servlet
- neo4j - 使用双向图查找最大深度
- html - 为什么在检查单选按钮时,图像没有从容器中滑出?
- python - 如何删除 | 在 python 中使用 re.sub 从文本签名
- python - 映射策略以计算 JSON 对象数组的平均值
- go - 为什么克隆的结构值仍然等于原始结构
- amazon-web-services - Cloudfront 分发指向 S3 而不是 API Gateway,产生 404 错误 NoSuchKey
- python - 如何在 Python 的 for 循环中更改列表元素的位置