0
votes
    @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)

I need help with this command

i get following error: [10.02.2021 16:31:10][WARN] Unknown error:[0mCommand raised an exception: AttributeError: 'Bot' object has no attribute 'startTime'

i checked all my imports.. my imports are

import discord from discord.ext import commands import datetime import json import aiohttp import random import ast from pymongo import MongoClient from discord.utils import get from pytz import timezone import os import math import itertools from discord.ext.commands import CommandNotFound, MissingPermissions, MessageNotFound import asyncio

1
self.bot.startTime replace with start_timeAjay
@Ajay now i get Unknown error:[0mCommand raised an exception: NameError: name 'start_time' is not definedFabi-Chan

1 Answers

0
votes

discord.ext.commands.Bot does not save the date the bot started. However, you could do that manually very easily. Inside the on_ready function in the main file, create a bot variable and save the current datetime. Make sure you import it as well!

In case you didn't know, bot variables are declared like so; bot.your_var_name = value. Be careful, however, because you might overwrite something. In your case, you could try;

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()

Then you can use bot.start_time outside that file. For instance, in a cog it would be self.bot.start_time.

And in your code, I recommend using datetime instead of time, it's much easier to work with.