diff options
| -rw-r--r-- | bot/__main__.py | 3 | ||||
| -rw-r--r-- | bot/cogs/sync.py | 86 | 
2 files changed, 88 insertions, 1 deletions
| diff --git a/bot/__main__.py b/bot/__main__.py index 0018cba9c..666c2d61f 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -59,8 +59,8 @@ if not DEBUG_MODE:      bot.load_extension("bot.cogs.verification")  # Feature cogs -bot.load_extension("bot.cogs.deployment")  bot.load_extension("bot.cogs.defcon") +bot.load_extension("bot.cogs.deployment")  bot.load_extension("bot.cogs.eval")  bot.load_extension("bot.cogs.fun")  bot.load_extension("bot.cogs.hiphopify") @@ -71,6 +71,7 @@ bot.load_extension("bot.cogs.reddit")  bot.load_extension("bot.cogs.site")  bot.load_extension("bot.cogs.snakes")  bot.load_extension("bot.cogs.snekbox") +bot.load_extension("bot.cogs.sync")  bot.load_extension("bot.cogs.tags")  bot.load_extension("bot.cogs.token_remover")  bot.load_extension("bot.cogs.utils") diff --git a/bot/cogs/sync.py b/bot/cogs/sync.py new file mode 100644 index 000000000..cccaa7d28 --- /dev/null +++ b/bot/cogs/sync.py @@ -0,0 +1,86 @@ +import logging +from collections import namedtuple +from typing import Callable, Iterable + +from discord import Guild, Role +from discord.ext.commands import Bot + + +log = logging.getLogger(__name__) +Role = namedtuple('Role', ('id', 'name', 'colour', 'permissions')) + + +async def sync_roles(bot: Bot, guild: Guild): +    """ +    Synchronize roles found on the given `guild` with the ones on the API. +    """ + +    def convert_role(role: Role): +        return { +            'id': role.id, +            'name': role.name, +            'colour': role.colour, +            'permissions': role.permissions +        } + +    roles = await bot.api_client.get('bot/roles') +    site_roles = { +        Role(**role_dict) +        for role_dict in roles +    } +    server_roles = { +        Role( +            id=role.id, name=role.name, +            colour=role.colour.value, permissions=role.permissions.value +        ) +        for role in guild.roles +    } +    roles_to_update = server_roles - site_roles + +    for role in roles_to_update: +        log.info(f"Updating role `{role.name}` on the site.") +        await bot.api_client.post( +            'bot/roles', +            json={ +                'id': role.id, +                'name': role.name, +                'colour': role.colour, +                'permissions': role.permissions +            } +        ) + + +async def sync_members(bot: Bot, guild: Guild): +    """ +    Synchronize members found on the given `guild` with the ones on the API. +    """ + +    current_members = await bot.api_client.get('bot/members') + + +class Sync: +    """Captures relevant events and sends them to the site.""" + +    # The server to synchronize events on. +    # Note that setting this wrongly will result in things getting deleted +    # that possibly shouldn't be. +    SYNC_SERVER_ID = 267624335836053506 + +    # An iterable of callables that are called when the bot is ready. +    ON_READY_SYNCERS: Iterable[Callable[[Bot, Guild], None]] = ( +        sync_roles, +    ) + +    def __init__(self, bot): +        self.bot = bot + +    async def on_ready(self): +        guild = self.bot.get_guild(self.SYNC_SERVER_ID) +        if guild is not None: +            for syncer in self.ON_READY_SYNCERS: +                await syncer(self.bot, guild) + + +def setup(bot): +    bot.add_cog(Sync(bot)) +    log.info("Cog loaded: Sync") | 
