diff options
| -rw-r--r-- | bot/cogs/events.py | 80 | ||||
| -rw-r--r-- | bot/constants.py | 3 |
2 files changed, 83 insertions, 0 deletions
diff --git a/bot/cogs/events.py b/bot/cogs/events.py index 7224b5a5c..ec04d2912 100644 --- a/bot/cogs/events.py +++ b/bot/cogs/events.py @@ -1,10 +1,18 @@ # coding=utf-8 +from aiohttp import ClientSession +from discord import Member, Embed from discord.ext.commands import ( AutoShardedBot, BadArgument, BotMissingPermissions, CommandError, CommandInvokeError, Context, NoPrivateMessage, UserInputError ) +from bot.constants import ( + SITE_API_KEY, SITE_API_USER_URL, PYTHON_GUILD, OWNER_ROLE, ADMIN_ROLE, MODERATOR_ROLE, + DEVOPS_ROLE, + DEVLOG_CHANNEL +) + class Events: """ @@ -14,6 +22,16 @@ class Events: def __init__(self, bot: AutoShardedBot): self.bot = bot + async def send_updated_users(self, *users): + session = ClientSession( + headers={"": SITE_API_KEY} + ) + + await session.post( + url=SITE_API_USER_URL, + json=list(users) + ) + async def on_command_error(self, ctx: Context, e: CommandError): command = ctx.command parent = None @@ -47,6 +65,68 @@ class Events: raise e.original print(e) + async def on_ready(self): + users = [] + + for member in self.bot.get_guild(PYTHON_GUILD).members: # type: Member + roles = [r.id for r in member.roles] # type: List[int] + + if OWNER_ROLE in roles: + users.append({ + "user_id": member.id, + "role": OWNER_ROLE + }) + elif ADMIN_ROLE in roles: + users.append({ + "user_id": member.id, + "role": ADMIN_ROLE + }) + elif MODERATOR_ROLE in roles: + users.append({ + "user_id": member.id, + "role": MODERATOR_ROLE + }) + elif DEVOPS_ROLE in roles: + users.append({ + "user_id": member.id, + "role": DEVOPS_ROLE + }) + + if users: + await self.send_updated_users(*users) + await self.bot.get_channel(DEVLOG_CHANNEL).send( + embed=Embed( + title="User roles updated", description=f"Updated {len(users)} users." + ) + ) + + async def on_member_update(self, before: Member, after: Member): + if before.roles == after.roles: + return + + roles = [r.id for r in after.roles] # type: List[int] + + if OWNER_ROLE in roles: + self.send_updated_users({ + "user_id": after.id, + "role": OWNER_ROLE + }) + elif ADMIN_ROLE in roles: + self.send_updated_users({ + "user_id": after.id, + "role": ADMIN_ROLE + }) + elif MODERATOR_ROLE in roles: + self.send_updated_users({ + "user_id": after.id, + "role": MODERATOR_ROLE + }) + elif DEVOPS_ROLE in roles: + self.send_updated_users({ + "user_id": after.id, + "role": DEVOPS_ROLE + }) + def setup(bot): bot.add_cog(Events(bot)) diff --git a/bot/constants.py b/bot/constants.py index 7fd4d9258..04d8e6a0c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -20,4 +20,7 @@ STATUS_URL = os.environ.get("STATUS_URL") DEPLOY_BOT_KEY = os.environ.get("DEPLOY_BOT_KEY") DEPLOY_SITE_KEY = os.environ.get("DEPLOY_SITE_KEY") +SITE_API_KEY = os.environ.get("BOT_API_KEY") +SITE_API_USER_URL = "https://api.pythondiscord.com/user" + HELP_PREFIX = "bot." |