From 81df0eed3b60c1f899179f313d29ab6cc36b872b Mon Sep 17 00:00:00 2001 From: gsd Date: Fri, 29 Sep 2023 14:46:45 +0300 Subject: [PATCH] reg sync --- bot.py | 22 +++++++++++++++++----- other_ext/register.py | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/bot.py b/bot.py index 1d880b6..ab2e032 100644 --- a/bot.py +++ b/bot.py @@ -57,11 +57,23 @@ class DiscordClient(commands.Bot): await self.tree.sync(guild=discord.Object(self.main_server_id)) await self.tree.sync() - 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()) - self.loop.create_task(self.loaded_extensions["register"].task()) + print(f"[init] check exts for have prepairs") + for ext_name, ext in self.loaded_extensions.items(): + if getattr(ext, "prepair", None): + print(f"[init.prepair] {ext_name} have prepair") + self.loop.create_task(self.loaded_extensions[ext_name].prepair()) + + print(f"[init] check exts for have primary task") + for ext_name, ext in self.loaded_extensions.items(): + if getattr(ext, "task", None): + print(f"[init.task] {ext_name} have task") + self.loop.create_task(self.loaded_extensions[ext_name].task()) + + print(f"[init] check exts for have secondary task") + for ext_name, ext in self.loaded_extensions.items(): + if getattr(ext, "task_secondary", None): + print(f"[init.task] {ext_name} have secondary task") + self.loop.create_task(self.loaded_extensions[ext_name].task_secondary()) def setup_events(self): @self.event diff --git a/other_ext/register.py b/other_ext/register.py index ef7cb06..be34d74 100644 --- a/other_ext/register.py +++ b/other_ext/register.py @@ -112,14 +112,8 @@ class Extension: @discord.app_commands.checks.has_role("Администратор Фактов13") async def sync_list_not_have_role(interaction: discord.Interaction): await interaction.response.defer(thinking=True) - current_members:List[discord.Member] = self.core.get_guild(int(self.core.main_server_id)).members - current_members = [str(i.id) for i in current_members if i.get_role(self.default_role) == None] - - not_synced = None - async with aiohttp.ClientSession(cookies={"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session: - async with session.post(f"{os.getenv('BACKEND_URL')}/api/discord/sync", ssl=False, json=current_members) as response: - not_synced = await response.json() - + + not_synced = await self.get_authusers_without_sync() message = "Текущий список:\n" for uid in not_synced: message += f"<@{uid}>\n" @@ -128,6 +122,26 @@ class Extension: return await interaction.followup.send(content=message, ephemeral=True) + async def prepair(self): + not_synced = await self.get_authusers_without_sync() + for uid in not_synced: + try: + member = self.core.get_guild(self.core.main_server_id).get_member(uid) + await self.final_stage(member, member.guild, "Автоматически, синхронизация с сайтом, во время иницилизации") + except Exception as e: + print(f"[register.prepair] Cannot add role, uid:{uid}, error:{e}") + traceback.print_exc() + + async def get_authusers_without_sync(self): + current_members:List[discord.Member] = self.core.get_guild(int(self.core.main_server_id)).members + current_members = [str(i.id) for i in current_members if i.get_role(self.default_role) == None] + + not_synced = None + async with aiohttp.ClientSession(cookies={"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session: + async with session.post(f"{os.getenv('BACKEND_URL')}/api/discord/sync", ssl=False, json=current_members) as response: + not_synced = await response.json() + return not_synced + async def final_stage(self, user: discord.Member, guild: discord.Guild, reason = ""): try: await user.add_roles(guild.get_role(self.default_role), reason = "Автоматически, зарегистрировался через сайтик" if not reason else reason) @@ -148,6 +162,14 @@ class Extension: await self.updater() await asyncio.sleep(timeout) + async def task_secondary(self, timeout = 60 * 60): + await self.core.wait_until_ready() + await asyncio.sleep(5) + print("[register] update register cache who dont have role") + while True: + await self.prepair() + await asyncio.sleep(timeout) + async def updater(self): try: hmap = None @@ -158,6 +180,8 @@ class Extension: if not hmap: return + print(f"[register] need sync, payload: {hmap}") + for reg in hmap.get("reg", []): try: member = self.core.get_guild(self.core.main_server_id).get_member(reg)