Browse Source

vip roles

huy
gsd 2 years ago
parent
commit
86cedfe473
  1. 1
      bot.py
  2. 76
      other_ext/vip_roles.py

1
bot.py

@ -58,6 +58,7 @@ class DiscordClient(commands.Bot):
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())
def setup_events(self):
@self.event

76
other_ext/vip_roles.py

@ -0,0 +1,76 @@
from discord.ext import tasks
import traceback
import asyncio, aiohttp
import discord
from ..bot import DiscordClient
class Extension:
DONATE_VIP_ROLE = 684725881557614689
FREE_VIP_ROLE = 1084187245025689630
MAIN_GUILD_ID = 0
TRACK_WEBHOOK = 819627591501479937
core: DiscordClient = None
def __init__(self, core):
self.core = core
self.MAIN_GUILD_ID = core.main_server_id
@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()
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(self.MAIN_GUILD_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.MAIN_GUILD_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.MAIN_GUILD_ID).get_role(role_id)
for member in ids_4_remove_role:
try:
print(f"Remove role({role_id}) from {member}")
await member.remove_roles(VIP_ROLE)
except Exception as err:
print(f"Failed remove role ({role_id}) to {member}, err: {err}")
for member in ids_4_add_role:
try:
print(f"Add role({role_id}) to {member}")
await member.add_roles(VIP_ROLE)
except Exception as err:
print(f"Failed add role ({role_id}) to {member}, err: {err}")
Loading…
Cancel
Save