You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
2.0 KiB

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