2 回答

TA貢獻1812條經驗 獲得超5個贊
您可以通過多線程來完成此任務。您讓函數與您的時間一起運行,并且函數運行完畢后兩者都會立即終止:
import threading
import time
def calc(): #this function generates the square of all numbers between 1 and 56000000
for i in range(1,56000000):
i*i
t1 = threading.Thread(target=calc)
t1.start() #starting a thread with the calc function
i = 1
while t1.is_alive(): #Check if the thread is alive
time.sleep(1)# print time after every second
print(f'Time elapsed ----------- {i}s')
i = i+1
t1.join() #terminate thread once calc function is done
print('Done!')

TA貢獻1856條經驗 獲得超5個贊
您永遠不應該使用time.sleepwithdiscord.py因為它會停止您應該使用的整個機器人await asyncio.sleep(1)。
您也可以創建此命令。
import datetime as dt
bot.launch_time = dt.datetime.utcnow()
@bot.command()
async def uptime(ctx):
delta_uptime = dt.datetime.utcnow() - bot.launch_time
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
days, hours = divmod(hours, 24)
await ctx.send(f"{days}d, {hours}h, {minutes}m, {seconds}s")
現在您可以使用{prefix}uptime它來了解它已經運行了多長時間。
添加回答
舉報