From e3ec9fca54bb9f8efb2ebc27aaa80bbe18e7abf8 Mon Sep 17 00:00:00 2001 From: gsd Date: Sat, 11 Mar 2023 23:26:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BA=D1=80=D1=83=D1=82=D1=8B=D0=B5=20=D1=84?= =?UTF-8?q?=D0=B8=D1=88=D0=BA=D0=B8=20=D0=BE=D1=82=20=D0=B4=D0=B0=D1=80?= =?UTF-8?q?=D1=8C=D0=BA=D1=8E=20=D0=BF=D0=B5=D0=BB=D1=8C=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 1 + other_ext/ban_roles.py | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 other_ext/ban_roles.py 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