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

30
bot.py
View file

@ -42,12 +42,22 @@ class Bot:
self.rq = remove_queue self.rq = remove_queue
self.initialized = False self.initialized = False
self.connected = False
@client.event @client.event
async def on_ready(): async def on_ready():
self.logger.info(f"Successfully logged in to Discord with username {client.user}") self.logger.info(f"Successfully logged in to Discord with username {client.user}")
self.initialized = True 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', @client.slash_command(name='sudo',
description='Gives you the sudo role for a certain period of time.', description='Gives you the sudo role for a certain period of time.',
dm_permission=False) dm_permission=False)
@ -139,20 +149,26 @@ class Bot:
async def wrap(): async def wrap():
while True: while True:
await asyncio.sleep(1) await asyncio.sleep(1)
cur_time = int(datetime.utcnow().timestamp()) cur_time = int(datetime.utcnow().timestamp())
while self.rq.get_min_time() <= cur_time and self.rq.queue: while self.rq.get_min_time() <= cur_time and self.rq.queue:
(_, user, guild, role) = self.rq.pop() (_, user, guild, role) = self.rq.pop()
guild = client.get_guild(guild) try:
if not guild: continue guild = client.get_guild(guild)
if not guild: continue
user = await guild.get_or_fetch_member(user) user = await guild.get_or_fetch_member(user)
if not user: continue if not user: continue
role = user.get_role(role) role = user.get_role(role)
if not role: continue if not role: continue
await user.remove_roles(role) 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()) client.loop.create_task(wrap())

View file

@ -20,7 +20,7 @@ class RemoveQueue:
@mutex(lock=lock) @mutex(lock=lock)
@datawrite @datawrite
def add(self, del_time: int, user: int, guild: int, role: int): 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) @mutex(lock=lock)
def get_min_time(self): def get_min_time(self):