add rednudancies

This commit is contained in:
CAG2Mark 2024-05-13 01:27:16 +08:00
parent 2365a0c85b
commit 9edbd4ea7a
2 changed files with 26 additions and 10 deletions

16
bot.py
View file

@ -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,10 +149,12 @@ 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()
try:
guild = client.get_guild(guild)
if not guild: continue
@ -153,6 +165,10 @@ class Bot:
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())

View file

@ -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):