diff options
author | 2019-06-21 20:05:34 +0100 | |
---|---|---|
committer | 2019-06-21 20:05:34 +0100 | |
commit | 3d50072191db9fa7b2d03f70c1b23e4cec97e0b7 (patch) | |
tree | dd3dccefa14916945c2438bd89988cd45f882ed0 | |
parent | Merge pull request #223 from Suhail6inkling/pride-avatar-flags (diff) |
Change base logic to allow for listed icon attributes
-rw-r--r-- | bot/seasons/season.py | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/bot/seasons/season.py b/bot/seasons/season.py index e6a262c8..2982bc47 100644 --- a/bot/seasons/season.py +++ b/bot/seasons/season.py @@ -85,6 +85,8 @@ class SeasonBase: date_format: str = "%d/%m/%Y" + index: int = 0 + @staticmethod def current_year() -> int: """Returns the current year.""" @@ -132,11 +134,18 @@ class SeasonBase: """ return f"New Season, {self.name_clean}!" - async def get_icon(self, avatar: bool = False) -> bytes: + @property + def cycle(self) -> bool: + """Return whether the icon attribute is a list and hence needs to be cycled through.""" + return isinstance(self.icon, list) + + async def get_icon(self, avatar: bool = False, index: int = 0) -> List[Union[bytes, str]]: """ Retrieve the season's icon from the branding repository using the Season's icon attribute. + This also returns the relative URL path for logging purposes If `avatar` is True, uses optional bot-only avatar icon if present. + If the Season's icon attribute is a list, returns the data for the given `index`. The icon attribute must provide the url path, starting from the master branch base url, including the starting slash. @@ -146,10 +155,13 @@ class SeasonBase: icon = self.bot_icon or self.icon else: icon = self.icon + if self.cycle and not self.bot_icon: + icon = icon[index] + full_url = ICON_BASE_URL + icon log.debug(f"Getting icon from: {full_url}") async with bot.http_session.get(full_url) as resp: - return await resp.read() + return (await resp.read(), icon) async def apply_username(self, *, debug: bool = False) -> Union[bool, None]: """ @@ -202,17 +214,17 @@ class SeasonBase: old_avatar = bot.user.avatar # Attempt the change - log.debug(f"Changing avatar to {self.bot_icon or self.icon}") - icon = await self.get_icon(avatar=True) + icon, name = await self.get_icon(avatar=True) + log.debug(f"Changing avatar to {name}") with contextlib.suppress(discord.HTTPException, asyncio.TimeoutError): async with async_timeout.timeout(5): await bot.user.edit(avatar=icon) if bot.user.avatar != old_avatar: - log.debug(f"Avatar changed to {self.bot_icon or self.icon}") + log.debug(f"Avatar changed to {name}") return True - log.warning(f"Changing avatar failed: {self.bot_icon or self.icon}") + log.warning(f"Changing avatar failed: {name}") return False async def apply_server_icon(self) -> bool: @@ -227,20 +239,38 @@ class SeasonBase: old_icon = guild.icon # Attempt the change - log.debug(f"Changing server icon to {self.icon}") - icon = await self.get_icon() + + icon, name = await self.get_icon(index=self.index) + + log.debug(f"Changing server icon to {name}") + with contextlib.suppress(discord.HTTPException, asyncio.TimeoutError): async with async_timeout.timeout(5): await guild.edit(icon=icon, reason=f"Seasonbot Season Change: {self.name}") new_icon = bot.get_guild(Client.guild).icon if new_icon != old_icon: - log.debug(f"Server icon changed to {self.icon}") + log.debug(f"Server icon changed to {name}") return True - log.warning(f"Changing server icon failed: {self.icon}") + log.warning(f"Changing server icon failed: {name}") return False + async def change_server_icon(self) -> bool: + """ + Changes the server icon. + + This only has an effect when the Season's icon attribute is a list, in which it cycles through. + Returns True if was successful. + """ + if not self.cycle: + return False + + self.index += 1 + self.index %= len(self.icon) + + return await self.apply_server_icon() + async def announce_season(self): """ Announces a change in season in the announcement channel. @@ -267,8 +297,10 @@ class SeasonBase: embed = discord.Embed(description=f"{announce}\n\n", colour=self.colour or guild.me.colour) embed.set_author(name=self.greeting) - if self.icon: + if isinstance(self.icon, str): embed.set_image(url=ICON_BASE_URL+self.icon) + elif self.cycle: + embed.set_image(url=ICON_BASE_URL+self.icon[0]) # Find any seasonal commands cogs = [] @@ -303,6 +335,7 @@ class SeasonBase: If in debug mode, the avatar, server icon, and announcement will be skipped. """ + self.index = 0 # Prepare all the seasonal cogs, and then the evergreen ones. extensions = [] for ext_folder in {self.name, "evergreen"}: @@ -367,6 +400,8 @@ class SeasonManager(commands.Cog): new_season = get_season(date=datetime.datetime.utcnow()) if new_season.name != self.season.name: await self.season.load() + else: + await self.season.change_server_icon() @with_role(Roles.moderator, Roles.admin, Roles.owner) @commands.command(name="season") |