From 3d50072191db9fa7b2d03f70c1b23e4cec97e0b7 Mon Sep 17 00:00:00 2001 From: Suhail Date: Fri, 21 Jun 2019 20:05:34 +0100 Subject: Change base logic to allow for listed icon attributes --- bot/seasons/season.py | 57 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'bot') 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") -- cgit v1.2.3 From fe17381802e4921080e38a42711bd97909c1a8b8 Mon Sep 17 00:00:00 2001 From: Suhail Date: Fri, 21 Jun 2019 20:06:41 +0100 Subject: Fix new season not being reassigned and loaded --- bot/seasons/season.py | 1 + 1 file changed, 1 insertion(+) (limited to 'bot') diff --git a/bot/seasons/season.py b/bot/seasons/season.py index 2982bc47..49882247 100644 --- a/bot/seasons/season.py +++ b/bot/seasons/season.py @@ -399,6 +399,7 @@ class SeasonManager(commands.Cog): # If the season has changed, load it. new_season = get_season(date=datetime.datetime.utcnow()) if new_season.name != self.season.name: + self.season = new_season await self.season.load() else: await self.season.change_server_icon() -- cgit v1.2.3 From 4aae9e30b658e34a77d6322c63621ff6d8adc29d Mon Sep 17 00:00:00 2001 From: Suhail Date: Fri, 21 Jun 2019 20:07:22 +0100 Subject: Add icons for evergreen --- bot/seasons/evergreen/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'bot') diff --git a/bot/seasons/evergreen/__init__.py b/bot/seasons/evergreen/__init__.py index ac32c199..f56f687c 100644 --- a/bot/seasons/evergreen/__init__.py +++ b/bot/seasons/evergreen/__init__.py @@ -5,3 +5,9 @@ class Evergreen(SeasonBase): """Evergreen Seasonal event attributes.""" bot_icon = "/logos/logo_seasonal/evergreen/logo_evergreen.png" + icon = [ + "/logos/logo_animated/heartbeat/heartbeat.gif", + "/logos/logo_animated/spinner/spinner.gif", + "/logos/logo_animated/tongues/tongues.gif", + "/logos/logo_animated/winky/winky.gif" + ] -- cgit v1.2.3 From a45e90c077329beed7b49ffb309441f5be80a911 Mon Sep 17 00:00:00 2001 From: Suhail Date: Fri, 21 Jun 2019 23:58:55 +0100 Subject: Code review changes --- bot/seasons/evergreen/__init__.py | 4 ++-- bot/seasons/season.py | 15 +++++---------- 2 files changed, 7 insertions(+), 12 deletions(-) (limited to 'bot') diff --git a/bot/seasons/evergreen/__init__.py b/bot/seasons/evergreen/__init__.py index f56f687c..1a23c545 100644 --- a/bot/seasons/evergreen/__init__.py +++ b/bot/seasons/evergreen/__init__.py @@ -5,9 +5,9 @@ class Evergreen(SeasonBase): """Evergreen Seasonal event attributes.""" bot_icon = "/logos/logo_seasonal/evergreen/logo_evergreen.png" - icon = [ + icon = ( "/logos/logo_animated/heartbeat/heartbeat.gif", "/logos/logo_animated/spinner/spinner.gif", "/logos/logo_animated/tongues/tongues.gif", "/logos/logo_animated/winky/winky.gif" - ] + ) diff --git a/bot/seasons/season.py b/bot/seasons/season.py index 49882247..6ef7fec7 100644 --- a/bot/seasons/season.py +++ b/bot/seasons/season.py @@ -6,7 +6,7 @@ import inspect import logging import pkgutil from pathlib import Path -from typing import List, Optional, Type, Union +from typing import List, Optional, Sequence, Tuple, Type, Union import async_timeout import discord @@ -134,12 +134,7 @@ class SeasonBase: """ return f"New Season, {self.name_clean}!" - @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]]: + async def get_icon(self, avatar: bool = False, index: int = 0) -> Tuple[bytes, str]: """ Retrieve the season's icon from the branding repository using the Season's icon attribute. @@ -155,7 +150,7 @@ class SeasonBase: icon = self.bot_icon or self.icon else: icon = self.icon - if self.cycle and not self.bot_icon: + if isinstance(self.icon, Sequence) and not self.bot_icon: icon = icon[index] full_url = ICON_BASE_URL + icon @@ -263,7 +258,7 @@ class SeasonBase: 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: + if not isinstance(self.icon, Sequence): return False self.index += 1 @@ -299,7 +294,7 @@ class SeasonBase: if isinstance(self.icon, str): embed.set_image(url=ICON_BASE_URL+self.icon) - elif self.cycle: + elif isinstance(self.icon, Sequence): embed.set_image(url=ICON_BASE_URL+self.icon[0]) # Find any seasonal commands -- cgit v1.2.3 From c0a7e5951cfd6a64f9627a5be1be6659de136556 Mon Sep 17 00:00:00 2001 From: Suhail Date: Sat, 22 Jun 2019 19:53:58 +0100 Subject: Changed all icon attributes to tuples --- bot/seasons/christmas/__init__.py | 4 +++- bot/seasons/easter/__init__.py | 4 +++- bot/seasons/halloween/__init__.py | 4 +++- bot/seasons/pride/__init__.py | 4 +++- bot/seasons/season.py | 2 +- bot/seasons/valentines/__init__.py | 4 +++- 6 files changed, 16 insertions(+), 6 deletions(-) (limited to 'bot') diff --git a/bot/seasons/christmas/__init__.py b/bot/seasons/christmas/__init__.py index f0a7c2c6..239181f4 100644 --- a/bot/seasons/christmas/__init__.py +++ b/bot/seasons/christmas/__init__.py @@ -21,4 +21,6 @@ class Christmas(SeasonBase): end_date = "31/12" colour = Colours.dark_green - icon = "/logos/logo_seasonal/christmas/festive.png" + icon = ( + "/logos/logo_seasonal/christmas/festive.png", + ) diff --git a/bot/seasons/easter/__init__.py b/bot/seasons/easter/__init__.py index 83d12ead..1d77b6a6 100644 --- a/bot/seasons/easter/__init__.py +++ b/bot/seasons/easter/__init__.py @@ -30,4 +30,6 @@ class Easter(SeasonBase): end_date = "30/04" colour = Colours.pink - icon = "/logos/logo_seasonal/easter/easter.png" + icon = ( + "/logos/logo_seasonal/easter/easter.png", + ) diff --git a/bot/seasons/halloween/__init__.py b/bot/seasons/halloween/__init__.py index 74c962ed..aff51423 100644 --- a/bot/seasons/halloween/__init__.py +++ b/bot/seasons/halloween/__init__.py @@ -13,4 +13,6 @@ class Halloween(SeasonBase): end_date = "31/10" colour = Colours.orange - icon = "/logos/logo_seasonal/halloween/spooky.png" + icon = ( + "/logos/logo_seasonal/halloween/spooky.png", + ) diff --git a/bot/seasons/pride/__init__.py b/bot/seasons/pride/__init__.py index 434e3409..b1897eca 100644 --- a/bot/seasons/pride/__init__.py +++ b/bot/seasons/pride/__init__.py @@ -31,4 +31,6 @@ class Pride(SeasonBase): # Season logo colour = Colours.soft_red - icon = "/logos/logo_seasonal/pride/logo_pride.png" + icon = ( + "/logos/logo_seasonal/pride/logo_pride.png", + ) diff --git a/bot/seasons/season.py b/bot/seasons/season.py index 6ef7fec7..4b624653 100644 --- a/bot/seasons/season.py +++ b/bot/seasons/season.py @@ -80,7 +80,7 @@ class SeasonBase: end_date: Optional[str] = None colour: Optional[int] = None - icon: str = "/logos/logo_full/logo_full.png" + icon: Tuple[str, ...] = ("/logos/logo_full/logo_full.png",) bot_icon: Optional[str] = None date_format: str = "%d/%m/%Y" diff --git a/bot/seasons/valentines/__init__.py b/bot/seasons/valentines/__init__.py index e3e04421..6e5d16f7 100644 --- a/bot/seasons/valentines/__init__.py +++ b/bot/seasons/valentines/__init__.py @@ -17,4 +17,6 @@ class Valentines(SeasonBase): end_date = "01/03" colour = Colours.pink - icon = "/logos/logo_seasonal/valentines/loved_up.png" + icon = ( + "/logos/logo_seasonal/valentines/loved_up.png", + ) -- cgit v1.2.3 From 3ecc0c90ee902c4b9c6450e4b154b1783deefc5f Mon Sep 17 00:00:00 2001 From: Suhail Date: Sat, 22 Jun 2019 19:55:26 +0100 Subject: Add trailing comma for ease of use --- bot/seasons/evergreen/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot') diff --git a/bot/seasons/evergreen/__init__.py b/bot/seasons/evergreen/__init__.py index 1a23c545..b95f3528 100644 --- a/bot/seasons/evergreen/__init__.py +++ b/bot/seasons/evergreen/__init__.py @@ -9,5 +9,5 @@ class Evergreen(SeasonBase): "/logos/logo_animated/heartbeat/heartbeat.gif", "/logos/logo_animated/spinner/spinner.gif", "/logos/logo_animated/tongues/tongues.gif", - "/logos/logo_animated/winky/winky.gif" + "/logos/logo_animated/winky/winky.gif", ) -- cgit v1.2.3 From a5019841f4fccf6943e9cc6b789a3f76b189351c Mon Sep 17 00:00:00 2001 From: Suhail Date: Sat, 22 Jun 2019 19:56:25 +0100 Subject: Remove str vs Sequence checking --- bot/seasons/season.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'bot') diff --git a/bot/seasons/season.py b/bot/seasons/season.py index 4b624653..95e3cf1d 100644 --- a/bot/seasons/season.py +++ b/bot/seasons/season.py @@ -6,7 +6,7 @@ import inspect import logging import pkgutil from pathlib import Path -from typing import List, Optional, Sequence, Tuple, Type, Union +from typing import List, Optional, Tuple, Type, Union import async_timeout import discord @@ -140,18 +140,15 @@ class SeasonBase: 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`. + Returns the data for the given `index`, defaulting to the first item. The icon attribute must provide the url path, starting from the master branch base url, including the starting slash. e.g. `/logos/logo_seasonal/valentines/loved_up.png` """ - if avatar: - icon = self.bot_icon or self.icon - else: - icon = self.icon - if isinstance(self.icon, Sequence) and not self.bot_icon: - icon = icon[index] + icon = self.icon[index] + if avatar and self.bot_icon: + icon = self.bot_icon full_url = ICON_BASE_URL + icon log.debug(f"Getting icon from: {full_url}") @@ -258,8 +255,8 @@ class SeasonBase: 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 isinstance(self.icon, Sequence): - return False + if len(self.icon) == 1: + return self.index += 1 self.index %= len(self.icon) @@ -292,9 +289,7 @@ class SeasonBase: embed = discord.Embed(description=f"{announce}\n\n", colour=self.colour or guild.me.colour) embed.set_author(name=self.greeting) - if isinstance(self.icon, str): - embed.set_image(url=ICON_BASE_URL+self.icon) - elif isinstance(self.icon, Sequence): + if self.icon: embed.set_image(url=ICON_BASE_URL+self.icon[0]) # Find any seasonal commands -- cgit v1.2.3