from discord.ext import tasks import traceback import asyncio, aiohttp import discord class Extension: DONATE_VIP_ROLE = 684725881557614689 FREE_VIP_ROLE = 1084187245025689630 TRACK_WEBHOOK = 819627591501479937 core = None def __init__(self, core): self.core = core @core.listen() async def on_message(message: discord.Message): if message.webhook_id != self.TRACK_WEBHOOK: return await self.updater() return 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.DONATE_VIP_ROLE) await self.checkRoles(self.FREE_VIP_ROLE) except: traceback.print_exc() async def getVIP_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/viplist", ssl=False) as response: return await response.json() async def getFreeVIP_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/freeviplist", 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.DONATE_VIP_ROLE: active_vips = await self.getVIP_DiscordIDs() elif role_id == self.FREE_VIP_ROLE: active_vips = await self.getFreeVIP_DiscordIDs() else: print(f"Error: not setted role_id to check roles") return ids_4_remove_role = [member for member in current_members if not member.id in active_vips] ids_4_add_role = [self.core.get_guild(self.core.main_server_id).get_member(i) for i in active_vips if not i in [member.id for member in current_members]] VIP_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(VIP_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(VIP_ROLE, reason = "Автоматически, игрок приобрел(не точно) вип на серверах") except Exception as err: print(f"Failed add role ({role_id}) to {member}, err: {err}")