Как узнать сколько прошло времени от последнего запуска бота?

Пробовал через библиотеку time, но ничего не вышло.

import discord
from discord.ext import commands
import time

start = time.monotonic()
result = time.monotonic() - start


class Test(commands.Cog):
    def __init__(self,bot):
        self.bot = bot

    @commands.command()
    @commands.is_owner()
    async def test(self, ctx):
         await ctx.send("Program time: {:>.3f}".format(result) + " seconds.")
    
def setup(bot):
    bot.add_cog(Test(bot))

введите сюда описание изображения


Ответы (1 шт):

Автор решения: gil9red

Засуньте получение результата в саму функцию:

    async def test(self, ctx):
         result = time.monotonic() - start
         await ctx.send("Program time: {:>.3f}".format(result) + " seconds.")

Либо сделайте result функцией, тогда останется ее вызывать для получения разницы:

result = lambda: time.monotonic() - start
...

    async def test(self, ctx):
         await ctx.send("Program time: {:>.3f}".format(result()) + " seconds.")

Для приведения секунд в человеческий вид, можно использовать datetime.timedelta, пример:

import datetime as DT
print(DT.timedelta(seconds=10000))
# 2:46:40

print(DT.timedelta(seconds=10000000))
# 115 days, 17:46:40.900000
→ Ссылка