From 9edbd4ea7a11fb2ab5dc6caf27f92613f203b611 Mon Sep 17 00:00:00 2001 From: CAG2Mark Date: Mon, 13 May 2024 01:27:16 +0800 Subject: [PATCH] add rednudancies --- bot.py | 34 +++++++++++++++++++++++++--------- removequeue.py | 2 +- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index 64eb857..df06cb2 100644 --- a/bot.py +++ b/bot.py @@ -42,12 +42,22 @@ class Bot: self.rq = remove_queue self.initialized = False - + self.connected = False + @client.event async def on_ready(): self.logger.info(f"Successfully logged in to Discord with username {client.user}") self.initialized = True + @client.event + async def on_connect(): + self.logger.info(f"Succesfully reconnected") + self.connected = True + + @client.event + async def on_disconnect(): + self.connected = False + @client.slash_command(name='sudo', description='Gives you the sudo role for a certain period of time.', dm_permission=False) @@ -139,20 +149,26 @@ class Bot: async def wrap(): while True: await asyncio.sleep(1) + cur_time = int(datetime.utcnow().timestamp()) while self.rq.get_min_time() <= cur_time and self.rq.queue: (_, user, guild, role) = self.rq.pop() - guild = client.get_guild(guild) - if not guild: continue + try: + guild = client.get_guild(guild) + if not guild: continue - user = await guild.get_or_fetch_member(user) - if not user: continue + user = await guild.get_or_fetch_member(user) + if not user: continue - role = user.get_role(role) - if not role: continue - - await user.remove_roles(role) + role = user.get_role(role) + if not role: continue + + await user.remove_roles(role) + except Exception as e: + self.logger.error("Could not remove role from user. Trying again in 10 seconds. The error is as follows:") + self.logger.exception(e) + self.rq.add((cur_time + 10, user, guild, role)) client.loop.create_task(wrap()) diff --git a/removequeue.py b/removequeue.py index 217233f..09b8486 100644 --- a/removequeue.py +++ b/removequeue.py @@ -20,7 +20,7 @@ class RemoveQueue: @mutex(lock=lock) @datawrite def add(self, del_time: int, user: int, guild: int, role: int): - heapq.heappush(self.queue, (del_time, user, guild, role)) + heapq.heappush(self.queue, [del_time, user, guild, role]) # need to use list instead of tuple because json parses to list @mutex(lock=lock) def get_min_time(self):