|
|
@ -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}") |
|
|
|
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() |