aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2021-03-10 22:27:30 +0100
committerGravatar kwzrd <[email protected]>2021-03-13 12:39:42 +0100
commit53db28b0a2c126efd1ead201d9053eac81d95758 (patch)
tree02e4ef5ea4e06f57a46504a33534850f0b0d37b6
parentBranding: do not require 'RemoteObject' instance to fetch file (diff)
Branding: implement asset application logic
-rw-r--r--bot/exts/backend/branding/_cog.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/bot/exts/backend/branding/_cog.py b/bot/exts/backend/branding/_cog.py
index cef17a614..79106d694 100644
--- a/bot/exts/backend/branding/_cog.py
+++ b/bot/exts/backend/branding/_cog.py
@@ -1,13 +1,30 @@
+import asyncio
import logging
+from enum import Enum
+import async_timeout
+import discord
from discord.ext import commands
from bot.bot import Bot
+from bot.constants import Guild
+from bot.decorators import mock_in_debug
from bot.exts.backend.branding._repository import BrandingRepository
log = logging.getLogger(__name__)
+class AssetType(Enum):
+ """
+ Recognised Discord guild asset types.
+
+ The value of each member corresponds exactly to a kwarg that can be passed to `Guild.edit`.
+ """
+
+ BANNER = "banner"
+ ICON = "icon"
+
+
class Branding(commands.Cog):
"""Guild branding management."""
@@ -15,3 +32,37 @@ class Branding(commands.Cog):
"""Instantiate repository abstraction."""
self.bot = bot
self.repository = BrandingRepository(bot)
+
+ # region: Internal utility
+
+ @mock_in_debug(return_value=None)
+ async def apply_asset(self, asset_type: AssetType, download_url: str) -> None:
+ """
+ Download asset from `download_url` and apply it to PyDis as `asset_type`.
+
+ This function is mocked in the development environment in order to prevent API spam during testing.
+ Decorator should be temporarily removed in order to test internal methodology.
+ """
+ log.info(f"Applying {asset_type.value} asset to the guild")
+
+ file = await self.repository.fetch_file(download_url)
+
+ if file is None:
+ log.error(f"Failed to download {asset_type.value} from branding repository!")
+ return
+
+ await self.bot.wait_until_guild_available()
+ pydis: discord.Guild = self.bot.get_guild(Guild.id)
+
+ timeout = 10 # Seconds
+ try:
+ with async_timeout.timeout(timeout):
+ await pydis.edit(**{asset_type.value: file})
+ except discord.HTTPException as http_exc:
+ log.error(f"Asset upload to Discord failed: {http_exc}")
+ except asyncio.TimeoutError:
+ log.error(f"Asset upload to Discord timed out after {timeout} seconds!")
+ else:
+ log.debug("Asset uploaded successfully!")
+
+ # endregion