From 3848deb8879c56494f0fae3b7d4ca074e5ccbc12 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Sun, 5 Jan 2025 19:23:50 -0500 Subject: [PATCH] feat: set status to idle appropriately --- core.py | 6 +++++- events.py | 5 +++++ main.py | 2 ++ state.py | 1 + tasks.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tasks.py diff --git a/core.py b/core.py index 85723f6..71f69d9 100644 --- a/core.py +++ b/core.py @@ -4,6 +4,7 @@ import importlib import inspect import io import textwrap +import time import traceback import disnake_paginator @@ -12,7 +13,7 @@ import commands import constants import core import utils -from state import client, command_locks, executed_messages +from state import client, command_locks, last_used async def on_message(message, edited=False): @@ -26,6 +27,9 @@ async def on_message(message, edited=False): if not matched: return + global last_used + last_used = time.time() + if len(matched) > 1: await utils.reply( message, diff --git a/events.py b/events.py index 5d416f6..a50f7ac 100644 --- a/events.py +++ b/events.py @@ -1,8 +1,13 @@ import commands import core +import tasks from state import client +async def on_ready(): + await tasks.check_idle() + + async def on_message(message): await core.on_message(message) diff --git a/main.py b/main.py index ac19e1d..76ca42b 100644 --- a/main.py +++ b/main.py @@ -9,5 +9,7 @@ from state import client, start_time async def on_ready(): print(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s") + await events.on_ready() + client.run(constants.SECRETS["TOKEN"]) diff --git a/state.py b/state.py index 2c5f79d..7dadd08 100644 --- a/state.py +++ b/state.py @@ -11,4 +11,5 @@ intents.message_content = True intents.members = True client = disnake.Client(intents=intents) +last_used = time.time() start_time = time.time() diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..53a965c --- /dev/null +++ b/tasks.py @@ -0,0 +1,28 @@ +import asyncio +import time + +import disnake + +from state import client, last_used, players + + +async def check_idle(): + while True: + await asyncio.sleep(3600) + + if time.time() - last_used >= 3600: + await client.change_presence(status=disnake.Status.idle) + else: + await client.change_presence(status=disnake.Status.online) + + +async def cleanup(): + while True: + await asyncio.sleep(3600) + + targets = [] + for id, player in players: + if len(player.queue) == 0: + targets.append(id) + for target in targets: + del players[target]