diff --git a/bot.py b/bot.py index 11b3cad..7cf8495 100644 --- a/bot.py +++ b/bot.py @@ -59,6 +59,7 @@ class DiscordClient(commands.Bot): self.loop.create_task(self.loaded_extensions["stats_loader"].task()) self.loop.create_task(self.loaded_extensions["stats_presence"].task()) self.loop.create_task(self.loaded_extensions["vip_roles"].task()) + self.loop.create_task(self.loaded_extensions["ban_roles"].task()) def setup_events(self): @self.event diff --git a/other_ext/ban_roles.py b/other_ext/ban_roles.py new file mode 100644 index 0000000..dbf289c --- /dev/null +++ b/other_ext/ban_roles.py @@ -0,0 +1,59 @@ +from discord.ext import tasks +import traceback +import asyncio, aiohttp +import discord + +class Extension: + BAN_ROLE = 1084208988012412938 + + core = None + def __init__(self, core): + self.core = core + + async def task(self, timeout = 60): + await self.core.wait_until_ready() + await asyncio.sleep(5) + while True: + await self.updater() + await asyncio.sleep(timeout) + + async def updater(self): + try: + await self.checkRoles(self.BAN_ROLE) + except: + traceback.print_exc() + + async def getBanned_DiscordIDs(self): + async with aiohttp.ClientSession(cookies={"secretkey":self.core.secret_key}) as session: + async with session.get(f"{self.core.backend_url}/api/bot/discord/banlist", ssl=False) as response: + return await response.json() + + async def checkRoles(self, role_id): + current_members = self.core.get_guild(int(self.core.main_server_id)).get_role(role_id).members + if role_id == self.BAN_ROLE: + active_bans = await self.getBanned_DiscordIDs() + else: + print(f"Error: not setted role_id to check ban roles") + return + + ids_4_remove_role = [member for member in current_members if not member.id in active_bans] + ids_4_add_role = [self.core.get_guild(self.core.main_server_id).get_member(i) for i in active_bans if not i in [member.id for member in current_members]] + + BAN_ROLE = self.core.get_guild(self.core.main_server_id).get_role(role_id) + for member in ids_4_remove_role: + if member is None: + continue + try: + print(f"Remove role({role_id}) from {member}") + await member.remove_roles(BAN_ROLE, reason = "Автоматически, игрок больше не в бане на серверах") + except Exception as err: + print(f"Failed remove role ({role_id}) to {member}, err: {err}") + + for member in ids_4_add_role: + if member is None: + continue + try: + print(f"Add role({role_id}) to {member}") + await member.add_roles(BAN_ROLE, reason = "Автоматически, игрок в бане на серверах") + except Exception as err: + print(f"Failed add role ({role_id}) to {member}, err: {err}") \ No newline at end of file