diff --git a/bot.py b/bot.py index ef6e803..9011b18 100644 --- a/bot.py +++ b/bot.py @@ -6,6 +6,7 @@ import os, sys import aiohttp from datetime import datetime from discord.ext import tasks +from discord.app_commands.errors import * from player import * import traceback from exceptions import * diff --git a/other_ext/register.py b/other_ext/register.py index 2c4caf6..78a70d4 100644 --- a/other_ext/register.py +++ b/other_ext/register.py @@ -1,6 +1,8 @@ import discord from discord.ext import commands import os +import asyncio, aiohttp +import traceback class Extension: auth_channel = 960796520247091201 @@ -102,14 +104,61 @@ class Extension: print(f"Cannot delete message from auth channel, error: {err}") return - async def final_stage(self, user: discord.Member, guild: discord.Guild): + '''@core.tree.command(name = "synclist", description = "Сихронизировать работяг") + @discord.app_commands.describe() + @discord.app_commands.checks.has_role("Администратор Фактов13") + async def sync_list_not_have_role(interaction: discord.Interaction): + current_members = self.core.get_guild(int(self.core.main_server_id)).members + current_members = [i for i in current_members if i] + + 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, payload=current_members) as response: + await response.json()''' + + async def final_stage(self, user: discord.Member, guild: discord.Guild, reason = ""): try: - await user.add_roles(guild.get_role(self.default_role)) + await user.add_roles(guild.get_role(self.default_role), reason = "Автоматически, зарегистрировался через сайтик" if not reason else reason) except Exception as err: print(f"Cannot set role({self.default_role}) to {user}, error: {err}") - async def remove_role(self, user: discord.Member, guild: discord.Guild): + async def remove_role(self, user: discord.Member, guild: discord.Guild, reason = ""): try: - await user.remove_roles(guild.get_role(self.default_role)) + await user.remove_roles(guild.get_role(self.default_role), reason = "Игрок отвязал аккаунт через бота" if not reason else reason) except Exception as err: - print(f"Cannot remove role({self.default_role}) from {user}, error: {err}") \ No newline at end of file + print(f"Cannot remove role({self.default_role}) from {user}, error: {err}") + + async def task(self, timeout = 60): + await self.core.wait_until_ready() + await asyncio.sleep(5) + print("[register] update register cache") + while True: + await self.updater() + await asyncio.sleep(timeout) + + async def updater(self): + try: + hmap = None + async with aiohttp.ClientSession(cookies={"secretkey":os.getenv("BACKEND_SECRETKEY")}) as session: + async with session.get(f"{os.getenv('BACKEND_URL')}/api/discord/sync", ssl=False) as response: + hmap = await response.json() + + if not hmap: + return + + for reg in hmap.get("reg", []): + try: + member = self.core.get_guild(self.core.main_server_id).get_member(reg) + await self.final_stage(member, member.guild, "Автоматически, синхронизация с сайтом") + except: + print(f"[register] cannot add role to {reg}") + traceback.print_exc() + + for unreg in hmap.get("unreg", []): + try: + member = self.core.get_guild(self.core.main_server_id).get_member(unreg) + await self.final_stage(member, member.guild, "Автоматически, синхронизация с сайтом") + except: + print(f"[register] cannot add role to {unreg}") + traceback.print_exc() + except: + traceback.print_exc() \ No newline at end of file diff --git a/player.py b/player.py index f314416..901964a 100644 --- a/player.py +++ b/player.py @@ -102,6 +102,20 @@ class Player: else: message += f"Дата разбана: {utime2human(self.current['ban']['ban_utime'] + self.current['ban']['ban_length_seconds'])}\n" embed.add_field(name="Имеется бан на сервере", value=message, inline=False) + #Привязаные профили ранее + if self.current.get("attached_discord", []): + message_head = "" + message = "" + for d in self.current.get("attached_discord", []): + if d['active'] == 1: + message_head += f"<@{d['discord_id']}> | {utime2human(d['u_time'])}" + else: + message += f"<@{d['discord_id']}> | {utime2human(d['u_time'])}" + + if message_head: + embed.add_field(name="Привязаный аккаунт Discord:", value=message_head, inline=False) + if message: + embed.add_field(name="История аккаунта Discord:", value=message, inline=False) return embed