diff options
| -rw-r--r-- | Pipfile | 1 | ||||
| -rw-r--r-- | Pipfile.lock | 40 | ||||
| -rw-r--r-- | bot/constants.py | 13 | ||||
| -rw-r--r-- | bot/exts/backend/branding/__init__.py | 7 | ||||
| -rw-r--r-- | bot/exts/backend/branding/_cog.py | 566 | ||||
| -rw-r--r-- | bot/exts/backend/branding/_constants.py | 51 | ||||
| -rw-r--r-- | bot/exts/backend/branding/_decorators.py | 27 | ||||
| -rw-r--r-- | bot/exts/backend/branding/_errors.py | 2 | ||||
| -rw-r--r-- | bot/exts/backend/branding/_seasons.py | 175 | ||||
| -rw-r--r-- | bot/exts/backend/error_handler.py | 46 | ||||
| -rw-r--r-- | bot/exts/backend/sync/_syncers.py | 10 | ||||
| -rw-r--r-- | bot/exts/filters/filtering.py | 7 | ||||
| -rw-r--r-- | bot/exts/info/help.py | 17 | ||||
| -rw-r--r-- | bot/exts/info/tags.py | 28 | ||||
| -rw-r--r-- | bot/exts/moderation/modlog.py | 3 | ||||
| -rw-r--r-- | bot/exts/moderation/verification.py | 4 | ||||
| -rw-r--r-- | bot/exts/moderation/watchchannels/talentpool.py | 3 | ||||
| -rw-r--r-- | config-default.yml | 13 | ||||
| -rw-r--r-- | tests/bot/exts/backend/sync/test_users.py | 29 | 
19 files changed, 983 insertions, 59 deletions
| @@ -26,6 +26,7 @@ requests = "~=2.22"  sentry-sdk = "~=0.19"  sphinx = "~=2.2"  statsd = "~=3.3" +arrow = "~=0.17"  emoji = "~=0.6"  [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index fbae5b3db..636d07b1a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@  {      "_meta": {          "hash": { -            "sha256": "f9f28d3d98e12f92c179e6d88444d1a9ad57557683b7116a91f0b1650d399848" +            "sha256": "26c8089f17d6d6bac11dbed366b1b46818b4546f243af756a106a32af5d9d8f6"          },          "pipfile-spec": 6,          "requires": { @@ -106,6 +106,14 @@              ],              "version": "==0.7.12"          }, +        "arrow": { +            "hashes": [ +                "sha256:e098abbd9af3665aea81bdd6c869e93af4feb078e98468dd351c383af187aac5", +                "sha256:ff08d10cda1d36c68657d6ad20d74fbea493d980f8b2d45344e00d6ed2bf6ed4" +            ], +            "index": "pypi", +            "version": "==0.17.0" +        },          "async-rediscache": {              "extras": [                  "fakeredis" @@ -211,7 +219,6 @@                  "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b",                  "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"              ], -            "index": "pypi",              "markers": "sys_platform == 'win32'",              "version": "==0.4.4"          }, @@ -569,29 +576,20 @@          },          "pygments": {              "hashes": [ -                "sha256:ccf3acacf3782cbed4a989426012f1c535c9a90d3a7fc3f16d231b9372d2b716", -                "sha256:f275b6c0909e5dafd2d6269a656aa90fa58ebf4a74f8fcf9053195d226b24a08" +                "sha256:bc9591213a8f0e0ca1a5e68a479b4887fdc3e75d0774e5c71c31920c427de435", +                "sha256:df49d09b498e83c1a73128295860250b0b7edd4c723a32e9bc0d295c7c2ec337"              ],              "markers": "python_version >= '3.5'", -            "version": "==2.7.3" +            "version": "==2.7.4"          },          "pyparsing": {              "hashes": [                  "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1",                  "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"              ], -            "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", +            "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2'",              "version": "==2.4.7"          }, -        "pyreadline": { -            "hashes": [ -                "sha256:4530592fc2e85b25b1a9f79664433da09237c1a270e4d78ea5aa3a2c7229e2d1", -                "sha256:65540c21bfe14405a3a77e4c085ecfce88724743a4ead47c66b84defcf82c32e", -                "sha256:9ce5fa65b8992dfa373bddc5b6e0864ead8f291c94fbfec05fbd5c836162e67b" -            ], -            "markers": "sys_platform == 'win32'", -            "version": "==2.1" -        },          "python-dateutil": {              "hashes": [                  "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c", @@ -655,7 +653,7 @@                  "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259",                  "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"              ], -            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", +            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'",              "version": "==1.15.0"          },          "snowballstemmer": { @@ -1097,7 +1095,7 @@                  "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259",                  "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced"              ], -            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", +            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'",              "version": "==1.15.0"          },          "snowballstemmer": { @@ -1112,7 +1110,7 @@                  "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b",                  "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"              ], -            "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", +            "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2'",              "version": "==0.10.2"          },          "urllib3": { @@ -1125,11 +1123,11 @@          },          "virtualenv": {              "hashes": [ -                "sha256:205a7577275dd0d9223c730dd498e21a8910600085c3dee97412b041fc4b853b", -                "sha256:7992b8de87e544a4ab55afc2240bf8388c4e3b5765d03784dad384bfdf9097ee" +                "sha256:0c111a2236b191422b37fe8c28b8c828ced39aab4bf5627fa5c331aeffb570d9", +                "sha256:14b34341e742bdca219e10708198e704e8a7064dd32f474fc16aca68ac53a306"              ],              "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", -            "version": "==20.3.0" +            "version": "==20.3.1"          }      }  } diff --git a/bot/constants.py b/bot/constants.py index d813046ab..be8d303f6 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -13,7 +13,7 @@ their default values from `config-default.yml`.  import logging  import os  from collections.abc import Mapping -from enum import Enum +from enum import Enum, IntEnum  from pathlib import Path  from typing import Dict, List, Optional @@ -249,6 +249,9 @@ class Colours(metaclass=YAMLGetter):      soft_green: int      soft_orange: int      bright_green: int +    orange: int +    pink: int +    purple: int  class DuckPond(metaclass=YAMLGetter): @@ -299,6 +302,8 @@ class Emojis(metaclass=YAMLGetter):      comments: str      user: str +    ok_hand: str +  class Icons(metaclass=YAMLGetter):      section = "style" @@ -601,6 +606,12 @@ class VoiceGate(metaclass=YAMLGetter):      voice_ping_delete_delay: int +class Branding(metaclass=YAMLGetter): +    section = "branding" + +    cycle_frequency: int + +  class Event(Enum):      """      Event names. This does not include every event (for example, raw diff --git a/bot/exts/backend/branding/__init__.py b/bot/exts/backend/branding/__init__.py new file mode 100644 index 000000000..81ea3bf49 --- /dev/null +++ b/bot/exts/backend/branding/__init__.py @@ -0,0 +1,7 @@ +from bot.bot import Bot +from bot.exts.backend.branding._cog import BrandingManager + + +def setup(bot: Bot) -> None: +    """Loads BrandingManager cog.""" +    bot.add_cog(BrandingManager(bot)) diff --git a/bot/exts/backend/branding/_cog.py b/bot/exts/backend/branding/_cog.py new file mode 100644 index 000000000..20df83a89 --- /dev/null +++ b/bot/exts/backend/branding/_cog.py @@ -0,0 +1,566 @@ +import asyncio +import itertools +import logging +import random +import typing as t +from datetime import datetime, time, timedelta + +import arrow +import async_timeout +import discord +from async_rediscache import RedisCache +from discord.ext import commands + +from bot.bot import Bot +from bot.constants import Branding, Colours, Emojis, Guild, MODERATION_ROLES +from bot.exts.backend.branding import _constants, _decorators, _errors, _seasons + +log = logging.getLogger(__name__) + + +class GitHubFile(t.NamedTuple): +    """ +    Represents a remote file on GitHub. + +    The `sha` hash is kept so that we can determine that a file has changed, +    despite its filename remaining unchanged. +    """ + +    download_url: str +    path: str +    sha: str + + +def pretty_files(files: t.Iterable[GitHubFile]) -> str: +    """Provide a human-friendly representation of `files`.""" +    return "\n".join(file.path for file in files) + + +def time_until_midnight() -> timedelta: +    """ +    Determine amount of time until the next-up UTC midnight. + +    The exact `midnight` moment is actually delayed to 5 seconds after, in order +    to avoid potential problems due to imprecise sleep. +    """ +    now = datetime.utcnow() +    tomorrow = now + timedelta(days=1) +    midnight = datetime.combine(tomorrow, time(second=5)) + +    return midnight - now + + +class BrandingManager(commands.Cog): +    """ +    Manages the guild's branding. + +    The purpose of this cog is to help automate the synchronization of the branding +    repository with the guild. It is capable of discovering assets in the repository +    via GitHub's API, resolving download urls for them, and delegating +    to the `bot` instance to upload them to the guild. + +    BrandingManager is designed to be entirely autonomous. Its `daemon` background task awakens +    once a day (see `time_until_midnight`) to detect new seasons, or to cycle icons within a single +    season. The daemon can be turned on and off via the `daemon` cmd group. The value set via +    its `start` and `stop` commands is persisted across sessions. If turned on, the daemon will +    automatically start on the next bot start-up. Otherwise, it will wait to be started manually. + +    All supported operations, e.g. setting seasons, applying the branding, or cycling icons, can +    also be invoked manually, via the following API: + +        branding list +            - Show all available seasons + +        branding set <season_name> +            - Set the cog's internal state to represent `season_name`, if it exists +            - If no `season_name` is given, set chronologically current season +            - This will not automatically apply the season's branding to the guild, +              the cog's state can be detached from the guild +            - Seasons can therefore be 'previewed' using this command + +        branding info +            - View detailed information about resolved assets for current season + +        branding refresh +            - Refresh internal state, i.e. synchronize with branding repository + +        branding apply +            - Apply the current internal state to the guild, i.e. upload the assets + +        branding cycle +            - If there are multiple available icons for current season, randomly pick +              and apply the next one + +    The daemon calls these methods autonomously as appropriate. The use of this cog +    is locked to moderation roles. As it performs media asset uploads, it is prone to +    rate-limits - the `apply` command should be used with caution. The `set` command can, +    however, be used freely to 'preview' seasonal branding and check whether paths have been +    resolved as appropriate. + +    While the bot is in debug mode, it will 'mock' asset uploads by logging the passed +    download urls and pretending that the upload was successful. Make use of this +    to test this cog's behaviour. +    """ + +    current_season: t.Type[_seasons.SeasonBase] + +    banner: t.Optional[GitHubFile] + +    available_icons: t.List[GitHubFile] +    remaining_icons: t.List[GitHubFile] + +    days_since_cycle: t.Iterator + +    daemon: t.Optional[asyncio.Task] + +    # Branding configuration +    branding_configuration = RedisCache() + +    def __init__(self, bot: Bot) -> None: +        """ +        Assign safe default values on init. + +        At this point, we don't have information about currently available branding. +        Most of these attributes will be overwritten once the daemon connects, or once +        the `refresh` command is used. +        """ +        self.bot = bot +        self.current_season = _seasons.get_current_season() + +        self.banner = None + +        self.available_icons = [] +        self.remaining_icons = [] + +        self.days_since_cycle = itertools.cycle([None]) + +        self.daemon = None +        self._startup_task = self.bot.loop.create_task(self._initial_start_daemon()) + +    async def _initial_start_daemon(self) -> None: +        """Checks is daemon active and when is, start it at cog load.""" +        if await self.branding_configuration.get("daemon_active"): +            self.daemon = self.bot.loop.create_task(self._daemon_func()) + +    @property +    def _daemon_running(self) -> bool: +        """True if the daemon is currently active, False otherwise.""" +        return self.daemon is not None and not self.daemon.done() + +    async def _daemon_func(self) -> None: +        """ +        Manage all automated behaviour of the BrandingManager cog. + +        Once a day, the daemon will perform the following tasks: +            - Update `current_season` +            - Poll GitHub API to see if the available branding for `current_season` has changed +            - Update assets if changes are detected (banner, guild icon, bot avatar, bot nickname) +            - Check whether it's time to cycle guild icons + +        The internal loop runs once when activated, then periodically at the time +        given by `time_until_midnight`. + +        All method calls in the internal loop are considered safe, i.e. no errors propagate +        to the daemon's loop. The daemon itself does not perform any error handling on its own. +        """ +        await self.bot.wait_until_guild_available() + +        while True: +            self.current_season = _seasons.get_current_season() +            branding_changed = await self.refresh() + +            if branding_changed: +                await self.apply() + +            elif next(self.days_since_cycle) == Branding.cycle_frequency: +                await self.cycle() + +            until_midnight = time_until_midnight() +            await asyncio.sleep(until_midnight.total_seconds()) + +    async def _info_embed(self) -> discord.Embed: +        """Make an informative embed representing current season.""" +        info_embed = discord.Embed(description=self.current_season.description, colour=self.current_season.colour) + +        # If we're in a non-evergreen season, also show active months +        if self.current_season is not _seasons.SeasonBase: +            title = f"{self.current_season.season_name} ({', '.join(str(m) for m in self.current_season.months)})" +        else: +            title = self.current_season.season_name + +        # Use the author field to show the season's name and avatar if available +        info_embed.set_author(name=title) + +        banner = self.banner.path if self.banner is not None else "Unavailable" +        info_embed.add_field(name="Banner", value=banner, inline=False) + +        icons = pretty_files(self.available_icons) or "Unavailable" +        info_embed.add_field(name="Available icons", value=icons, inline=False) + +        # Only display cycle frequency if we're actually cycling +        if len(self.available_icons) > 1 and Branding.cycle_frequency: +            info_embed.set_footer(text=f"Icon cycle frequency: {Branding.cycle_frequency}") + +        return info_embed + +    async def _reset_remaining_icons(self) -> None: +        """Set `remaining_icons` to a shuffled copy of `available_icons`.""" +        self.remaining_icons = random.sample(self.available_icons, k=len(self.available_icons)) + +    async def _reset_days_since_cycle(self) -> None: +        """ +        Reset the `days_since_cycle` iterator based on configured frequency. + +        If the current season only has 1 icon, or if `Branding.cycle_frequency` is falsey, +        the iterator will always yield None. This signals that the icon shouldn't be cycled. + +        Otherwise, it will yield ints in range [1, `Branding.cycle_frequency`] indefinitely. +        When the iterator yields a value equal to `Branding.cycle_frequency`, it is time to cycle. +        """ +        if len(self.available_icons) > 1 and Branding.cycle_frequency: +            sequence = range(1, Branding.cycle_frequency + 1) +        else: +            sequence = [None] + +        self.days_since_cycle = itertools.cycle(sequence) + +    async def _get_files(self, path: str, include_dirs: bool = False) -> t.Dict[str, GitHubFile]: +        """ +        Get files at `path` in the branding repository. + +        If `include_dirs` is False (default), only returns files at `path`. +        Otherwise, will return both files and directories. Never returns symlinks. + +        Return dict mapping from filename to corresponding `GitHubFile` instance. +        This may return an empty dict if the response status is non-200, +        or if the target directory is empty. +        """ +        url = f"{_constants.BRANDING_URL}/{path}" +        async with self.bot.http_session.get( +            url, headers=_constants.HEADERS, params=_constants.PARAMS +        ) as resp: +            # Short-circuit if we get non-200 response +            if resp.status != _constants.STATUS_OK: +                log.error(f"GitHub API returned non-200 response: {resp}") +                return {} +            directory = await resp.json()  # Directory at `path` + +        allowed_types = {"file", "dir"} if include_dirs else {"file"} +        return { +            file["name"]: GitHubFile(file["download_url"], file["path"], file["sha"]) +            for file in directory +            if file["type"] in allowed_types +        } + +    async def refresh(self) -> bool: +        """ +        Synchronize available assets with branding repository. + +        If the current season is not the evergreen, and lacks at least one asset, +        we use the evergreen seasonal dir as fallback for missing assets. + +        Finally, if neither the seasonal nor fallback branding directories contain +        an asset, it will simply be ignored. + +        Return True if the branding has changed. This will be the case when we enter +        a new season, or when something changes in the current seasons's directory +        in the branding repository. +        """ +        old_branding = (self.banner, self.available_icons) +        seasonal_dir = await self._get_files(self.current_season.branding_path, include_dirs=True) + +        # Only make a call to the fallback directory if there is something to be gained +        branding_incomplete = any( +            asset not in seasonal_dir +            for asset in (_constants.FILE_BANNER, _constants.FILE_AVATAR, _constants.SERVER_ICONS) +        ) +        if branding_incomplete and self.current_season is not _seasons.SeasonBase: +            fallback_dir = await self._get_files( +                _seasons.SeasonBase.branding_path, include_dirs=True +            ) +        else: +            fallback_dir = {} + +        # Resolve assets in this directory, None is a safe value +        self.banner = ( +            seasonal_dir.get(_constants.FILE_BANNER) +            or fallback_dir.get(_constants.FILE_BANNER) +        ) + +        # Now resolve server icons by making a call to the proper sub-directory +        if _constants.SERVER_ICONS in seasonal_dir: +            icons_dir = await self._get_files( +                f"{self.current_season.branding_path}/{_constants.SERVER_ICONS}" +            ) +            self.available_icons = list(icons_dir.values()) + +        elif _constants.SERVER_ICONS in fallback_dir: +            icons_dir = await self._get_files( +                f"{_seasons.SeasonBase.branding_path}/{_constants.SERVER_ICONS}" +            ) +            self.available_icons = list(icons_dir.values()) + +        else: +            self.available_icons = []  # This should never be the case, but an empty list is a safe value + +        # GitHubFile instances carry a `sha` attr so this will pick up if a file changes +        branding_changed = old_branding != (self.banner, self.available_icons) + +        if branding_changed: +            log.info(f"New branding detected (season: {self.current_season.season_name})") +            await self._reset_remaining_icons() +            await self._reset_days_since_cycle() + +        return branding_changed + +    async def cycle(self) -> bool: +        """ +        Apply the next-up server icon. + +        Returns True if an icon is available and successfully gets applied, False otherwise. +        """ +        if not self.available_icons: +            log.info("Cannot cycle: no icons for this season") +            return False + +        if not self.remaining_icons: +            log.info("Reset & shuffle remaining icons") +            await self._reset_remaining_icons() + +        next_up = self.remaining_icons.pop(0) +        success = await self.set_icon(next_up.download_url) + +        return success + +    async def apply(self) -> t.List[str]: +        """ +        Apply current branding to the guild and bot. + +        This delegates to the bot instance to do all the work. We only provide download urls +        for available assets. Assets unavailable in the branding repo will be ignored. + +        Returns a list of names of all failed assets. An asset is considered failed +        if it isn't found in the branding repo, or if something goes wrong while the +        bot is trying to apply it. + +        An empty list denotes that all assets have been applied successfully. +        """ +        report = {asset: False for asset in ("banner", "icon")} + +        if self.banner is not None: +            report["banner"] = await self.set_banner(self.banner.download_url) + +        report["icon"] = await self.cycle() + +        failed_assets = [asset for asset, succeeded in report.items() if not succeeded] +        return failed_assets + +    @commands.has_any_role(*MODERATION_ROLES) +    @commands.group(name="branding") +    async def branding_cmds(self, ctx: commands.Context) -> None: +        """Manual branding control.""" +        if not ctx.invoked_subcommand: +            await ctx.send_help(ctx.command) + +    @branding_cmds.command(name="list", aliases=["ls"]) +    async def branding_list(self, ctx: commands.Context) -> None: +        """List all available seasons and branding sources.""" +        embed = discord.Embed(title="Available seasons", colour=Colours.soft_green) + +        for season in _seasons.get_all_seasons(): +            if season is _seasons.SeasonBase: +                active_when = "always" +            else: +                active_when = f"in {', '.join(str(m) for m in season.months)}" + +            description = ( +                f"Active {active_when}\n" +                f"Branding: {season.branding_path}" +            ) +            embed.add_field(name=season.season_name, value=description, inline=False) + +        await ctx.send(embed=embed) + +    @branding_cmds.command(name="set") +    async def branding_set(self, ctx: commands.Context, *, season_name: t.Optional[str] = None) -> None: +        """ +        Manually set season, or reset to current if none given. + +        Season search is a case-less comparison against both seasonal class name, +        and its `season_name` attr. + +        This only pre-loads the cog's internal state to the chosen season, but does not +        automatically apply the branding. As that is an expensive operation, the `apply` +        command must be called explicitly after this command finishes. + +        This means that this command can be used to 'preview' a season gathering info +        about its available assets, without applying them to the guild. + +        If the daemon is running, it will automatically reset the season to current when +        it wakes up. The season set via this command can therefore remain 'detached' from +        what it should be - the daemon will make sure that it's set back properly. +        """ +        if season_name is None: +            new_season = _seasons.get_current_season() +        else: +            new_season = _seasons.get_season(season_name) +            if new_season is None: +                raise _errors.BrandingError("No such season exists") + +        if self.current_season is new_season: +            raise _errors.BrandingError(f"Season {self.current_season.season_name} already active") + +        self.current_season = new_season +        await self.branding_refresh(ctx) + +    @branding_cmds.command(name="info", aliases=["status"]) +    async def branding_info(self, ctx: commands.Context) -> None: +        """ +        Show available assets for current season. + +        This can be used to confirm that assets have been resolved properly. +        When `apply` is used, it attempts to upload exactly the assets listed here. +        """ +        await ctx.send(embed=await self._info_embed()) + +    @branding_cmds.command(name="refresh") +    async def branding_refresh(self, ctx: commands.Context) -> None: +        """Sync currently available assets with branding repository.""" +        async with ctx.typing(): +            await self.refresh() +            await self.branding_info(ctx) + +    @branding_cmds.command(name="apply") +    async def branding_apply(self, ctx: commands.Context) -> None: +        """ +        Apply current season's branding to the guild. + +        Use `info` to check which assets will be applied. Shows which assets have +        failed to be applied, if any. +        """ +        async with ctx.typing(): +            failed_assets = await self.apply() +            if failed_assets: +                raise _errors.BrandingError( +                    f"Failed to apply following assets: {', '.join(failed_assets)}" +                ) + +            response = discord.Embed(description=f"All assets applied {Emojis.ok_hand}", colour=Colours.soft_green) +            await ctx.send(embed=response) + +    @branding_cmds.command(name="cycle") +    async def branding_cycle(self, ctx: commands.Context) -> None: +        """ +        Apply the next-up guild icon, if multiple are available. + +        The order is random. +        """ +        async with ctx.typing(): +            success = await self.cycle() +            if not success: +                raise _errors.BrandingError("Failed to cycle icon") + +            response = discord.Embed(description=f"Success {Emojis.ok_hand}", colour=Colours.soft_green) +            await ctx.send(embed=response) + +    @branding_cmds.group(name="daemon", aliases=["d", "task"]) +    async def daemon_group(self, ctx: commands.Context) -> None: +        """Control the background daemon.""" +        if not ctx.invoked_subcommand: +            await ctx.send_help(ctx.command) + +    @daemon_group.command(name="status") +    async def daemon_status(self, ctx: commands.Context) -> None: +        """Check whether daemon is currently active.""" +        if self._daemon_running: +            remaining_time = (arrow.utcnow() + time_until_midnight()).humanize() +            response = discord.Embed(description=f"Daemon running {Emojis.ok_hand}", colour=Colours.soft_green) +            response.set_footer(text=f"Next refresh {remaining_time}") +        else: +            response = discord.Embed(description="Daemon not running", colour=Colours.soft_red) + +        await ctx.send(embed=response) + +    @daemon_group.command(name="start") +    async def daemon_start(self, ctx: commands.Context) -> None: +        """If the daemon isn't running, start it.""" +        if self._daemon_running: +            raise _errors.BrandingError("Daemon already running!") + +        self.daemon = self.bot.loop.create_task(self._daemon_func()) +        await self.branding_configuration.set("daemon_active", True) + +        response = discord.Embed(description=f"Daemon started {Emojis.ok_hand}", colour=Colours.soft_green) +        await ctx.send(embed=response) + +    @daemon_group.command(name="stop") +    async def daemon_stop(self, ctx: commands.Context) -> None: +        """If the daemon is running, stop it.""" +        if not self._daemon_running: +            raise _errors.BrandingError("Daemon not running!") + +        self.daemon.cancel() +        await self.branding_configuration.set("daemon_active", False) + +        response = discord.Embed(description=f"Daemon stopped {Emojis.ok_hand}", colour=Colours.soft_green) +        await ctx.send(embed=response) + +    async def _fetch_image(self, url: str) -> bytes: +        """Retrieve and read image from `url`.""" +        log.debug(f"Getting image from: {url}") +        async with self.bot.http_session.get(url) as resp: +            return await resp.read() + +    async def _apply_asset(self, target: discord.Guild, asset: _constants.AssetType, url: str) -> bool: +        """ +        Internal method for applying media assets to the guild. + +        This shouldn't be called directly. The purpose of this method is mainly generic +        error handling to reduce needless code repetition. + +        Return True if upload was successful, False otherwise. +        """ +        log.info(f"Attempting to set {asset.name}: {url}") + +        kwargs = {asset.value: await self._fetch_image(url)} +        try: +            async with async_timeout.timeout(5): +                await target.edit(**kwargs) + +        except asyncio.TimeoutError: +            log.info("Asset upload timed out") +            return False + +        except discord.HTTPException as discord_error: +            log.exception("Asset upload failed", exc_info=discord_error) +            return False + +        else: +            log.info("Asset successfully applied") +            return True + +    @_decorators.mock_in_debug(return_value=True) +    async def set_banner(self, url: str) -> bool: +        """Set the guild's banner to image at `url`.""" +        guild = self.bot.get_guild(Guild.id) +        if guild is None: +            log.info("Failed to get guild instance, aborting asset upload") +            return False + +        return await self._apply_asset(guild, _constants.AssetType.BANNER, url) + +    @_decorators.mock_in_debug(return_value=True) +    async def set_icon(self, url: str) -> bool: +        """Sets the guild's icon to image at `url`.""" +        guild = self.bot.get_guild(Guild.id) +        if guild is None: +            log.info("Failed to get guild instance, aborting asset upload") +            return False + +        return await self._apply_asset(guild, _constants.AssetType.SERVER_ICON, url) + +    def cog_unload(self) -> None: +        """Cancels startup and daemon task.""" +        self._startup_task.cancel() +        if self.daemon is not None: +            self.daemon.cancel() diff --git a/bot/exts/backend/branding/_constants.py b/bot/exts/backend/branding/_constants.py new file mode 100644 index 000000000..dbc7615f2 --- /dev/null +++ b/bot/exts/backend/branding/_constants.py @@ -0,0 +1,51 @@ +from enum import Enum, IntEnum + +from bot.constants import Keys + + +class Month(IntEnum): +    """All month constants for seasons.""" + +    JANUARY = 1 +    FEBRUARY = 2 +    MARCH = 3 +    APRIL = 4 +    MAY = 5 +    JUNE = 6 +    JULY = 7 +    AUGUST = 8 +    SEPTEMBER = 9 +    OCTOBER = 10 +    NOVEMBER = 11 +    DECEMBER = 12 + +    def __str__(self) -> str: +        return self.name.title() + + +class AssetType(Enum): +    """ +    Discord media assets. + +    The values match exactly the kwarg keys that can be passed to `Guild.edit`. +    """ + +    BANNER = "banner" +    SERVER_ICON = "icon" + + +STATUS_OK = 200  # HTTP status code + +FILE_BANNER = "banner.png" +FILE_AVATAR = "avatar.png" +SERVER_ICONS = "server_icons" + +BRANDING_URL = "https://api.github.com/repos/python-discord/branding/contents" + +PARAMS = {"ref": "master"}  # Target branch +HEADERS = {"Accept": "application/vnd.github.v3+json"}  # Ensure we use API v3 + +# A GitHub token is not necessary for the cog to operate, +# unauthorized requests are however limited to 60 per hour +if Keys.github: +    HEADERS["Authorization"] = f"token {Keys.github}" diff --git a/bot/exts/backend/branding/_decorators.py b/bot/exts/backend/branding/_decorators.py new file mode 100644 index 000000000..6a1e7e869 --- /dev/null +++ b/bot/exts/backend/branding/_decorators.py @@ -0,0 +1,27 @@ +import functools +import logging +import typing as t + +from bot.constants import DEBUG_MODE + +log = logging.getLogger(__name__) + + +def mock_in_debug(return_value: t.Any) -> t.Callable: +    """ +    Short-circuit function execution if in debug mode and return `return_value`. + +    The original function name, and the incoming args and kwargs are DEBUG level logged +    upon each call. This is useful for expensive operations, i.e. media asset uploads +    that are prone to rate-limits but need to be tested extensively. +    """ +    def decorator(func: t.Callable) -> t.Callable: +        @functools.wraps(func) +        async def wrapped(*args, **kwargs) -> t.Any: +            """Short-circuit and log if in debug mode.""" +            if DEBUG_MODE: +                log.debug(f"Function {func.__name__} called with args: {args}, kwargs: {kwargs}") +                return return_value +            return await func(*args, **kwargs) +        return wrapped +    return decorator diff --git a/bot/exts/backend/branding/_errors.py b/bot/exts/backend/branding/_errors.py new file mode 100644 index 000000000..7cd271af3 --- /dev/null +++ b/bot/exts/backend/branding/_errors.py @@ -0,0 +1,2 @@ +class BrandingError(Exception): +    """Exception raised by the BrandingManager cog.""" diff --git a/bot/exts/backend/branding/_seasons.py b/bot/exts/backend/branding/_seasons.py new file mode 100644 index 000000000..5f6256b30 --- /dev/null +++ b/bot/exts/backend/branding/_seasons.py @@ -0,0 +1,175 @@ +import logging +import typing as t +from datetime import datetime + +from bot.constants import Colours +from bot.exts.backend.branding._constants import Month +from bot.exts.backend.branding._errors import BrandingError + +log = logging.getLogger(__name__) + + +class SeasonBase: +    """ +    Base for Seasonal classes. + +    This serves as the off-season fallback for when no specific +    seasons are active. + +    Seasons are 'registered' simply by inheriting from `SeasonBase`. +    We discover them by calling `__subclasses__`. +    """ + +    season_name: str = "Evergreen" + +    colour: str = Colours.soft_green +    description: str = "The default season!" + +    branding_path: str = "seasonal/evergreen" + +    months: t.Set[Month] = set(Month) + + +class Christmas(SeasonBase): +    """Branding for December.""" + +    season_name = "Festive season" + +    colour = Colours.soft_red +    description = ( +        "The time is here to get into the festive spirit! No matter who you are, where you are, " +        "or what beliefs you may follow, we hope every one of you enjoy this festive season!" +    ) + +    branding_path = "seasonal/christmas" + +    months = {Month.DECEMBER} + + +class Easter(SeasonBase): +    """Branding for April.""" + +    season_name = "Easter" + +    colour = Colours.bright_green +    description = ( +        "Bunny here, bunny there, bunny everywhere! Here at Python Discord, we celebrate " +        "our version of Easter during the entire month of April." +    ) + +    branding_path = "seasonal/easter" + +    months = {Month.APRIL} + + +class Halloween(SeasonBase): +    """Branding for October.""" + +    season_name = "Halloween" + +    colour = Colours.orange +    description = "Trick or treat?!" + +    branding_path = "seasonal/halloween" + +    months = {Month.OCTOBER} + + +class Pride(SeasonBase): +    """Branding for June.""" + +    season_name = "Pride" + +    colour = Colours.pink +    description = ( +        "The month of June is a special month for us at Python Discord. It is very important to us " +        "that everyone feels welcome here, no matter their origin, identity or sexuality. During the " +        "month of June, while some of you are participating in Pride festivals across the world, " +        "we will be celebrating individuality and commemorating the history and challenges " +        "of the LGBTQ+ community with a Pride event of our own!" +    ) + +    branding_path = "seasonal/pride" + +    months = {Month.JUNE} + + +class Valentines(SeasonBase): +    """Branding for February.""" + +    season_name = "Valentines" + +    colour = Colours.pink +    description = "Love is in the air!" + +    branding_path = "seasonal/valentines" + +    months = {Month.FEBRUARY} + + +class Wildcard(SeasonBase): +    """Branding for August.""" + +    season_name = "Wildcard" + +    colour = Colours.purple +    description = "A season full of surprises!" + +    months = {Month.AUGUST} + + +def get_all_seasons() -> t.List[t.Type[SeasonBase]]: +    """Give all available season classes.""" +    return [SeasonBase] + SeasonBase.__subclasses__() + + +def get_current_season() -> t.Type[SeasonBase]: +    """Give active season, based on current UTC month.""" +    current_month = Month(datetime.utcnow().month) + +    active_seasons = tuple( +        season +        for season in SeasonBase.__subclasses__() +        if current_month in season.months +    ) + +    if not active_seasons: +        return SeasonBase + +    return active_seasons[0] + + +def get_season(name: str) -> t.Optional[t.Type[SeasonBase]]: +    """ +    Give season such that its class name or its `season_name` attr match `name` (caseless). + +    If no such season exists, return None. +    """ +    name = name.casefold() + +    for season in get_all_seasons(): +        matches = (season.__name__.casefold(), season.season_name.casefold()) + +        if name in matches: +            return season + + +def _validate_season_overlap() -> None: +    """ +    Raise BrandingError if there are any colliding seasons. + +    This serves as a local test to ensure that seasons haven't been misconfigured. +    """ +    month_to_season = {} + +    for season in SeasonBase.__subclasses__(): +        for month in season.months: +            colliding_season = month_to_season.get(month) + +            if colliding_season: +                raise BrandingError(f"Season {season} collides with {colliding_season} in {month.name}") +            else: +                month_to_season[month] = season + + +_validate_season_overlap() diff --git a/bot/exts/backend/error_handler.py b/bot/exts/backend/error_handler.py index 5b5840858..b8bb3757f 100644 --- a/bot/exts/backend/error_handler.py +++ b/bot/exts/backend/error_handler.py @@ -1,5 +1,7 @@  import contextlib +import difflib  import logging +import random  import typing as t  from discord import Embed @@ -8,9 +10,10 @@ from sentry_sdk import push_scope  from bot.api import ResponseCodeError  from bot.bot import Bot -from bot.constants import Colours +from bot.constants import Colours, ERROR_REPLIES, Icons, MODERATION_ROLES  from bot.converters import TagNameConverter  from bot.errors import LockedResourceError +from bot.exts.backend.branding._errors import BrandingError  from bot.utils.checks import InWhitelistCheckFailure  log = logging.getLogger(__name__) @@ -76,6 +79,9 @@ class ErrorHandler(Cog):                  await self.handle_api_error(ctx, e.original)              elif isinstance(e.original, LockedResourceError):                  await ctx.send(f"{e.original} Please wait for it to finish and try again later.") +            elif isinstance(e.original, BrandingError): +                await ctx.send(embed=self._get_error_embed(random.choice(ERROR_REPLIES), str(e.original))) +                return              else:                  await self.handle_unexpected_error(ctx, e.original)              return  # Exit early to avoid logging. @@ -154,10 +160,46 @@ class ErrorHandler(Cog):              )          else:              with contextlib.suppress(ResponseCodeError): -                await ctx.invoke(tags_get_command, tag_name=tag_name) +                if await ctx.invoke(tags_get_command, tag_name=tag_name): +                    return + +        if not any(role.id in MODERATION_ROLES for role in ctx.author.roles): +            await self.send_command_suggestion(ctx, ctx.invoked_with) +          # Return to not raise the exception          return +    async def send_command_suggestion(self, ctx: Context, command_name: str) -> None: +        """Sends user similar commands if any can be found.""" +        # No similar tag found, or tag on cooldown - +        # searching for a similar command +        raw_commands = [] +        for cmd in self.bot.walk_commands(): +            if not cmd.hidden: +                raw_commands += (cmd.name, *cmd.aliases) +        if similar_command_data := difflib.get_close_matches(command_name, raw_commands, 1): +            similar_command_name = similar_command_data[0] +            similar_command = self.bot.get_command(similar_command_name) + +            if not similar_command: +                return + +            log_msg = "Cancelling attempt to suggest a command due to failed checks." +            try: +                if not await similar_command.can_run(ctx): +                    log.debug(log_msg) +                    return +            except errors.CommandError as cmd_error: +                log.debug(log_msg) +                await self.on_command_error(ctx, cmd_error) +                return + +            misspelled_content = ctx.message.content +            e = Embed() +            e.set_author(name="Did you mean:", icon_url=Icons.questionmark) +            e.description = f"{misspelled_content.replace(command_name, similar_command_name, 1)}" +            await ctx.send(embed=e, delete_after=10.0) +      async def handle_user_input_error(self, ctx: Context, e: errors.UserInputError) -> None:          """          Send an error message in `ctx` for UserInputError, sometimes invoking the help command too. diff --git a/bot/exts/backend/sync/_syncers.py b/bot/exts/backend/sync/_syncers.py index 2eb9f9971..c9f2d2da8 100644 --- a/bot/exts/backend/sync/_syncers.py +++ b/bot/exts/backend/sync/_syncers.py @@ -5,12 +5,15 @@ from collections import namedtuple  from discord import Guild  from discord.ext.commands import Context +from more_itertools import chunked  import bot  from bot.api import ResponseCodeError  log = logging.getLogger(__name__) +CHUNK_SIZE = 1000 +  # These objects are declared as namedtuples because tuples are hashable,  # something that we make use of when diffing site roles against guild roles.  _Role = namedtuple('Role', ('id', 'name', 'colour', 'permissions', 'position')) @@ -207,10 +210,13 @@ class UserSyncer(Syncer):      @staticmethod      async def _sync(diff: _Diff) -> None:          """Synchronise the database with the user cache of `guild`.""" +        # Using asyncio.gather would still consume too many resources on the site.          log.trace("Syncing created users...")          if diff.created: -            await bot.instance.api_client.post("bot/users", json=diff.created) +            for chunk in chunked(diff.created, CHUNK_SIZE): +                await bot.instance.api_client.post("bot/users", json=chunk)          log.trace("Syncing updated users...")          if diff.updated: -            await bot.instance.api_client.patch("bot/users/bulk_patch", json=diff.updated) +            for chunk in chunked(diff.updated, CHUNK_SIZE): +                await bot.instance.api_client.patch("bot/users/bulk_patch", json=chunk) diff --git a/bot/exts/filters/filtering.py b/bot/exts/filters/filtering.py index 208fc9e1f..3527bf8bb 100644 --- a/bot/exts/filters/filtering.py +++ b/bot/exts/filters/filtering.py @@ -48,7 +48,6 @@ class Stats(NamedTuple):      message_content: str      additional_embeds: Optional[List[discord.Embed]] -    additional_embeds_msg: Optional[str]  class Filtering(Cog): @@ -358,7 +357,6 @@ class Filtering(Cog):              channel_id=Channels.mod_alerts,              ping_everyone=ping_everyone,              additional_embeds=stats.additional_embeds, -            additional_embeds_msg=stats.additional_embeds_msg          )      def _add_stats(self, name: str, match: FilterMatch, content: str) -> Stats: @@ -375,7 +373,6 @@ class Filtering(Cog):              message_content = content          additional_embeds = None -        additional_embeds_msg = None          self.bot.stats.incr(f"filters.{name}") @@ -392,13 +389,11 @@ class Filtering(Cog):                  embed.set_thumbnail(url=data["icon"])                  embed.set_footer(text=f"Guild ID: {data['id']}")                  additional_embeds.append(embed) -            additional_embeds_msg = "For the following guild(s):"          elif name == "watch_rich_embeds":              additional_embeds = match -            additional_embeds_msg = "With the following embed(s):" -        return Stats(message_content, additional_embeds, additional_embeds_msg) +        return Stats(message_content, additional_embeds)      @staticmethod      def _check_filter(msg: Message) -> bool: diff --git a/bot/exts/info/help.py b/bot/exts/info/help.py index 461ff82fd..3a05b2c8a 100644 --- a/bot/exts/info/help.py +++ b/bot/exts/info/help.py @@ -5,7 +5,7 @@ from contextlib import suppress  from typing import List, Union  from discord import Colour, Embed -from discord.ext.commands import Bot, Cog, Command, Context, Group, HelpCommand +from discord.ext.commands import Bot, Cog, Command, CommandError, Context, DisabledCommand, Group, HelpCommand  from fuzzywuzzy import fuzz, process  from fuzzywuzzy.utils import full_process @@ -20,6 +20,8 @@ log = logging.getLogger(__name__)  COMMANDS_PER_PAGE = 8  PREFIX = constants.Bot.prefix +NOT_ALLOWED_TO_RUN_MESSAGE = "***You cannot run this command.***\n\n" +  Category = namedtuple("Category", ["name", "description", "cogs"]) @@ -173,9 +175,16 @@ class CustomHelpCommand(HelpCommand):          if aliases:              command_details += f"**Can also use:** {aliases}\n\n" -        # check if the user is allowed to run this command -        if not await command.can_run(self.context): -            command_details += "***You cannot run this command.***\n\n" +        # when command is disabled, show message about it, +        # when other CommandError or user is not allowed to run command, +        # add this to help message. +        try: +            if not await command.can_run(self.context): +                command_details += NOT_ALLOWED_TO_RUN_MESSAGE +        except DisabledCommand: +            command_details += "***This command is disabled.***\n\n" +        except CommandError: +            command_details += NOT_ALLOWED_TO_RUN_MESSAGE          command_details += f"*{command.help or 'No details provided.'}*\n"          embed.description = command_details diff --git a/bot/exts/info/tags.py b/bot/exts/info/tags.py index da4154316..00b4d1a78 100644 --- a/bot/exts/info/tags.py +++ b/bot/exts/info/tags.py @@ -182,10 +182,15 @@ class Tags(Cog):          matching_tags = self._get_tags_via_content(any, keywords or 'any', ctx.author)          await self._send_matching_tags(ctx, keywords, matching_tags) -    @tags_group.command(name='get', aliases=('show', 'g')) -    async def get_command(self, ctx: Context, *, tag_name: TagNameConverter = None) -> None: -        """Get a specified tag, or a list of all tags if no tag is specified.""" +    async def display_tag(self, ctx: Context, tag_name: str = None) -> bool: +        """ +        If a tag is not found, display similar tag names as suggestions. +        If a tag is not specified, display a paginated embed of all tags. + +        Tags are on cooldowns on a per-tag, per-channel basis. If a tag is on cooldown, display +        nothing and return False. +        """          def _command_on_cooldown(tag_name: str) -> bool:              """              Check if the command is currently on cooldown, on a per-tag, per-channel basis. @@ -212,7 +217,7 @@ class Tags(Cog):                  f"{ctx.author} tried to get the '{tag_name}' tag, but the tag is on cooldown. "                  f"Cooldown ends in {time_left:.1f} seconds."              ) -            return +            return False          if tag_name is not None:              temp_founds = self._get_tag(tag_name) @@ -237,6 +242,7 @@ class Tags(Cog):                      await ctx.send(embed=Embed.from_dict(tag['embed'])),                      [ctx.author.id],                  ) +                return True              elif founds and len(tag_name) >= 3:                  await wait_for_deletion(                      await ctx.send( @@ -247,6 +253,7 @@ class Tags(Cog):                      ),                      [ctx.author.id],                  ) +                return True          else:              tags = self._cache.values() @@ -255,6 +262,7 @@ class Tags(Cog):                      description="**There are no tags in the database!**",                      colour=Colour.red()                  )) +                return True              else:                  embed: Embed = Embed(title="**Current tags**")                  await LinePaginator.paginate( @@ -268,6 +276,18 @@ class Tags(Cog):                      empty=False,                      max_lines=15                  ) +                return True + +        return False + +    @tags_group.command(name='get', aliases=('show', 'g')) +    async def get_command(self, ctx: Context, *, tag_name: TagNameConverter = None) -> bool: +        """ +        Get a specified tag, or a list of all tags if no tag is specified. + +        Returns False if a tag is on cooldown, or if no matches are found. +        """ +        return await self.display_tag(ctx, tag_name)  def setup(bot: Bot) -> None: diff --git a/bot/exts/moderation/modlog.py b/bot/exts/moderation/modlog.py index b01de0ee3..e4b119f41 100644 --- a/bot/exts/moderation/modlog.py +++ b/bot/exts/moderation/modlog.py @@ -92,7 +92,6 @@ class ModLog(Cog, name="ModLog"):          files: t.Optional[t.List[discord.File]] = None,          content: t.Optional[str] = None,          additional_embeds: t.Optional[t.List[discord.Embed]] = None, -        additional_embeds_msg: t.Optional[str] = None,          timestamp_override: t.Optional[datetime] = None,          footer: t.Optional[str] = None,      ) -> Context: @@ -133,8 +132,6 @@ class ModLog(Cog, name="ModLog"):          )          if additional_embeds: -            if additional_embeds_msg: -                await channel.send(additional_embeds_msg)              for additional_embed in additional_embeds:                  await channel.send(embed=additional_embed) diff --git a/bot/exts/moderation/verification.py b/bot/exts/moderation/verification.py index 2a24c8ec6..bfe9b74b4 100644 --- a/bot/exts/moderation/verification.py +++ b/bot/exts/moderation/verification.py @@ -81,13 +81,11 @@ class Verification(Cog):          if member.guild.id != constants.Guild.id:              return  # Only listen for PyDis events -        raw_member = await self.bot.http.get_member(member.guild.id, member.id) -          # If the user has the pending flag set, they will be using the alternate          # gate and will not need a welcome DM with verification instructions.          # We will send them an alternate DM once they verify with the welcome          # video when they pass the gate. -        if raw_member.get("pending"): +        if member.pending:              return          log.trace(f"Sending on join message to new member: {member.id}") diff --git a/bot/exts/moderation/watchchannels/talentpool.py b/bot/exts/moderation/watchchannels/talentpool.py index df2ce586e..dd3349c3a 100644 --- a/bot/exts/moderation/watchchannels/talentpool.py +++ b/bot/exts/moderation/watchchannels/talentpool.py @@ -122,8 +122,7 @@ class TalentPool(WatchChannel, Cog, name="Talentpool"):          if history:              total = f"({len(history)} previous nominations in total)"              start_reason = f"Watched: {textwrap.shorten(history[0]['reason'], width=500, placeholder='...')}" -            end_reason = f"Unwatched: {textwrap.shorten(history[0]['end_reason'], width=500, placeholder='...')}" -            msg += f"\n\nUser's previous watch reasons {total}:```{start_reason}\n\n{end_reason}```" +            msg += f"\n\nUser's previous watch reasons {total}:```{start_reason}```"          await ctx.send(msg) diff --git a/config-default.yml b/config-default.yml index 175460a31..f8368c5d2 100644 --- a/config-default.yml +++ b/config-default.yml @@ -28,6 +28,9 @@ style:          soft_green: 0x68c290          soft_orange: 0xf9cb54          bright_green: 0x01d277 +        orange: 0xe67e22 +        pink: 0xcf84e0 +        purple: 0xb734eb      emojis:          defcon_disabled: "<:defcondisabled:470326273952972810>" @@ -68,6 +71,8 @@ style:          comments:       "<:reddit_comments:755845255001014384>"          user:           "<:reddit_users:755845303822974997>" +        ok_hand: ":ok_hand:" +      icons:          crown_blurple: "https://cdn.discordapp.com/emojis/469964153289965568.png"          crown_green:   "https://cdn.discordapp.com/emojis/469964154719961088.png" @@ -185,6 +190,8 @@ guild:          mods:               &MODS           305126844661760000          mod_alerts:                         473092532147060736          mod_spam:           &MOD_SPAM       620607373828030464 +        mod_tools:          &MOD_TOOLS      775413915391098921 +        mod_meta:           &MOD_META       775412552795947058          organisation:       &ORGANISATION   551789653284356126          staff_lounge:       &STAFF_LOUNGE   464905259261755392          duck_pond:          &DUCK_POND      637820308341915648 @@ -218,6 +225,8 @@ guild:      moderation_channels:          - *ADMINS          - *ADMIN_SPAM +        - *MOD_META +        - *MOD_TOOLS          - *MODS          - *MOD_SPAM @@ -519,5 +528,9 @@ voice_gate:      voice_ping_delete_delay: 60  # Seconds before deleting the bot's ping to user in Voice Gate +branding: +    cycle_frequency: 3  # How many days bot wait before refreshing server icon + +  config:      required_keys: ['bot.token'] diff --git a/tests/bot/exts/backend/sync/test_users.py b/tests/bot/exts/backend/sync/test_users.py index 61673e1bb..27932be95 100644 --- a/tests/bot/exts/backend/sync/test_users.py +++ b/tests/bot/exts/backend/sync/test_users.py @@ -188,30 +188,37 @@ class UserSyncerSyncTests(unittest.IsolatedAsyncioTestCase):      """Tests for the API requests that sync users."""      def setUp(self): -        patcher = mock.patch("bot.instance", new=helpers.MockBot()) -        self.bot = patcher.start() -        self.addCleanup(patcher.stop) +        bot_patcher = mock.patch("bot.instance", new=helpers.MockBot()) +        self.bot = bot_patcher.start() +        self.addCleanup(bot_patcher.stop) + +        chunk_patcher = mock.patch("bot.exts.backend.sync._syncers.CHUNK_SIZE", 2) +        self.chunk_size = chunk_patcher.start() +        self.addCleanup(chunk_patcher.stop) + +        self.chunk_count = 2 +        self.users = [fake_user(id=i) for i in range(self.chunk_size * self.chunk_count)]      async def test_sync_created_users(self):          """Only POST requests should be made with the correct payload.""" -        users = [fake_user(id=111), fake_user(id=222)] - -        diff = _Diff(users, [], None) +        diff = _Diff(self.users, [], None)          await UserSyncer._sync(diff) -        self.bot.api_client.post.assert_called_once_with("bot/users", json=diff.created) +        self.bot.api_client.post.assert_any_call("bot/users", json=diff.created[:self.chunk_size]) +        self.bot.api_client.post.assert_any_call("bot/users", json=diff.created[self.chunk_size:]) +        self.assertEqual(self.bot.api_client.post.call_count, self.chunk_count)          self.bot.api_client.put.assert_not_called()          self.bot.api_client.delete.assert_not_called()      async def test_sync_updated_users(self):          """Only PUT requests should be made with the correct payload.""" -        users = [fake_user(id=111), fake_user(id=222)] - -        diff = _Diff([], users, None) +        diff = _Diff([], self.users, None)          await UserSyncer._sync(diff) -        self.bot.api_client.patch.assert_called_once_with("bot/users/bulk_patch", json=diff.updated) +        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[:self.chunk_size]) +        self.bot.api_client.patch.assert_any_call("bot/users/bulk_patch", json=diff.updated[self.chunk_size:]) +        self.assertEqual(self.bot.api_client.patch.call_count, self.chunk_count)          self.bot.api_client.post.assert_not_called()          self.bot.api_client.delete.assert_not_called() | 
