From bfe5f4775262853394d7f73be861005d8d66c3fb Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 17:44:58 +0000 Subject: Remove persistent methods from evergreen branding manager --- bot/exts/evergreen/branding.py | 60 ++---------------------------------------- 1 file changed, 2 insertions(+), 58 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index 7e531011..96d67f6f 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -174,14 +174,6 @@ class BrandingManager(commands.Cog): with self.config_file.open("r", encoding="utf8") as persistent_file: return json.load(persistent_file) - def _write_config(self, key: str, value: bool) -> None: - """Write a `key`, `value` pair to persistent config file.""" - current_config = self._read_config() - current_config[key] = value - - with self.config_file.open("w", encoding="utf8") as persistent_file: - json.dump(current_config, persistent_file) - async def _daemon_func(self) -> None: """ Manage all automated behaviour of the BrandingManager cog. @@ -306,10 +298,7 @@ class BrandingManager(commands.Cog): 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 (FILE_BANNER, FILE_AVATAR, SERVER_ICONS) - ) + branding_incomplete = any(asset not in seasonal_dir for asset in (FILE_BANNER, FILE_AVATAR, SERVER_ICONS)) if branding_incomplete and self.current_season is not SeasonBase: fallback_dir = await self._get_files(SeasonBase.branding_path, include_dirs=True) else: @@ -407,10 +396,7 @@ class BrandingManager(commands.Cog): else: active_when = f"in {human_months(season.months)}" - description = ( - f"Active {active_when}\n" - f"Branding: {season.branding_path}" - ) + 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) @@ -495,48 +481,6 @@ class BrandingManager(commands.Cog): 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 BrandingError("Daemon already running!") - - self.daemon = self.bot.loop.create_task(self._daemon_func()) - self._write_config("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 BrandingError("Daemon not running!") - - self.daemon.cancel() - self._write_config("daemon_active", False) - - response = discord.Embed(description=f"Daemon stopped {Emojis.ok_hand}", colour=Colours.soft_green) - await ctx.send(embed=response) - def setup(bot: SeasonalBot) -> None: """Load BrandingManager cog.""" -- cgit v1.2.3 From e3a825ae44874ea775a6f681c7b3ad14e109b917 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 17:48:15 +0000 Subject: Don't load Monster survey cog --- bot/exts/halloween/monstersurvey.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/halloween/monstersurvey.py b/bot/exts/halloween/monstersurvey.py index 7b1a1e84..80196825 100644 --- a/bot/exts/halloween/monstersurvey.py +++ b/bot/exts/halloween/monstersurvey.py @@ -202,4 +202,3 @@ class MonsterSurvey(Cog): def setup(bot: Bot) -> None: """Monster survey Cog load.""" - bot.add_cog(MonsterSurvey(bot)) -- cgit v1.2.3 From dae64477b9be088fc04e5eecd5eb3dea7c3512c0 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:04:28 +0000 Subject: Revert autoformatter changes --- bot/exts/evergreen/branding.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index 96d67f6f..9204527b 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -298,7 +298,10 @@ class BrandingManager(commands.Cog): 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 (FILE_BANNER, FILE_AVATAR, SERVER_ICONS)) + branding_incomplete = any( + asset not in seasonal_dir + for asset in (FILE_BANNER, FILE_AVATAR, SERVER_ICONS) + ) if branding_incomplete and self.current_season is not SeasonBase: fallback_dir = await self._get_files(SeasonBase.branding_path, include_dirs=True) else: @@ -396,7 +399,10 @@ class BrandingManager(commands.Cog): else: active_when = f"in {human_months(season.months)}" - description = f"Active {active_when}\n" f"Branding: {season.branding_path}" + 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) -- cgit v1.2.3 From 9573ed1930289339a0bd75ad487f89fdda9f94ad Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:07:31 +0000 Subject: Remove HacktoberStats cog from loaded cogs --- bot/exts/halloween/hacktoberstats.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index d2762513..85f70ef0 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -489,4 +489,3 @@ class HacktoberStats(commands.Cog): def setup(bot: commands.Bot) -> None: """Hacktoberstats Cog load.""" - bot.add_cog(HacktoberStats(bot)) -- cgit v1.2.3 From d7763e5a459807b847f929d3ed6857c2bedd4d6d Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:09:12 +0000 Subject: Remove CandyCollection cogs from loaded cogs --- bot/exts/halloween/candy_collection.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index a862e1af..815b0d16 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -197,4 +197,3 @@ class CandyCollection(commands.Cog): def setup(bot: commands.Bot) -> None: """Candy Collection game Cog load.""" - bot.add_cog(CandyCollection(bot)) -- cgit v1.2.3 From 020d234e2bf1db015662dcefd978475d9bfad87f Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:12:11 +0000 Subject: Remove unused arrow import --- bot/exts/evergreen/branding.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index 9204527b..fe736bc4 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -7,7 +7,6 @@ import typing as t from datetime import datetime, time, timedelta from pathlib import Path -import arrow import discord from discord.embeds import EmptyEmbed from discord.ext import commands -- cgit v1.2.3 From 6102deda32307c060124b9681a37aa5921370138 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:20:51 +0000 Subject: Remove persist bot util --- bot/utils/persist.py | 69 ---------------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 bot/utils/persist.py (limited to 'bot') diff --git a/bot/utils/persist.py b/bot/utils/persist.py deleted file mode 100644 index 1e178569..00000000 --- a/bot/utils/persist.py +++ /dev/null @@ -1,69 +0,0 @@ -import sqlite3 -from pathlib import Path -from shutil import copyfile - -from bot.exts import get_package_names - -DIRECTORY = Path("data") # directory that has a persistent volume mapped to it - - -def make_persistent(file_path: Path) -> Path: - """ - Copy datafile at the provided file_path to the persistent data directory. - - A persistent data file is needed by some features in order to not lose data - after bot rebuilds. - - This function will ensure that a clean data file with default schema, - structure or data is copied over to the persistent volume before returning - the path to this new persistent version of the file. - - If the persistent file already exists, it won't be overwritten with the - clean default file, just returning the Path instead to the existing file. - - Note: Avoid using the same file name as other features in the same seasons - as otherwise only one datafile can be persistent and will be returned for - both cases. - - Ensure that all open files are using explicit appropriate encoding to avoid - encoding errors from diffent OS systems. - - Example Usage: - >>> import json - >>> template_datafile = Path("bot", "resources", "evergreen", "myfile.json") - >>> path_to_persistent_file = make_persistent(template_datafile) - >>> print(path_to_persistent_file) - data/evergreen/myfile.json - >>> with path_to_persistent_file.open("w+", encoding="utf8") as f: - >>> data = json.load(f) - """ - # ensure the persistent data directory exists - DIRECTORY.mkdir(exist_ok=True) - - if not file_path.is_file(): - raise OSError(f"File not found at {file_path}.") - - # detect season in datafile path for assigning to subdirectory - season = next((s for s in get_package_names() if s in file_path.parts), None) - - if season: - # make sure subdirectory exists first - subdirectory = Path(DIRECTORY, season) - subdirectory.mkdir(exist_ok=True) - - persistent_path = Path(subdirectory, file_path.name) - - else: - persistent_path = Path(DIRECTORY, file_path.name) - - # copy base/template datafile to persistent directory - if not persistent_path.exists(): - copyfile(file_path, persistent_path) - - return persistent_path - - -def sqlite(db_path: Path) -> sqlite3.Connection: - """Copy sqlite file to the persistent data directory and return an open connection.""" - persistent_path = make_persistent(db_path) - return sqlite3.connect(persistent_path) -- cgit v1.2.3 From bda1d3342adff17c85e18828098a30e69ded34e3 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:41:32 +0000 Subject: Raise NotImplementedError on write/read to config --- bot/exts/evergreen/branding.py | 55 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index fe736bc4..f9857b6c 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -7,6 +7,7 @@ import typing as t from datetime import datetime, time, timedelta from pathlib import Path +import arrow import discord from discord.embeds import EmptyEmbed from discord.ext import commands @@ -17,7 +18,8 @@ from bot.seasons import SeasonBase, get_all_seasons, get_current_season, get_sea from bot.utils import human_months from bot.utils.decorators import with_role from bot.utils.exceptions import BrandingError -from bot.utils.persist import make_persistent +# TODO: Implement substitute for current volume persistence requirements +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) @@ -156,7 +158,7 @@ class BrandingManager(commands.Cog): self.days_since_cycle = itertools.cycle([None]) self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json")) - should_run = self._read_config()["daemon_active"] + # should_run = self._read_config()["daemon_active"] if should_run: self.daemon = self.bot.loop.create_task(self._daemon_func()) @@ -170,8 +172,11 @@ class BrandingManager(commands.Cog): def _read_config(self) -> t.Dict[str, bool]: """Read and return persistent config file.""" - with self.config_file.open("r", encoding="utf8") as persistent_file: - return json.load(persistent_file) + raise NotImplementedError("read_config functionality requires mounting a persistent volume.") + + def _write_config(self, key: str, value: bool) -> None: + """Write a `key`, `value` pair to persistent config file.""" + raise NotImplementedError("write_config functionality requires mounting a persistent volume.") async def _daemon_func(self) -> None: """ @@ -486,6 +491,48 @@ class BrandingManager(commands.Cog): 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", disabled=True) + async def daemon_start(self, ctx: commands.Context) -> None: + """If the daemon isn't running, start it.""" + if self._daemon_running: + raise BrandingError("Daemon already running!") + + self.daemon = self.bot.loop.create_task(self._daemon_func()) + self._write_config("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", disabled=True) + async def daemon_stop(self, ctx: commands.Context) -> None: + """If the daemon is running, stop it.""" + if not self._daemon_running: + raise BrandingError("Daemon not running!") + + self.daemon.cancel() + self._write_config("daemon_active", False) + + response = discord.Embed(description=f"Daemon stopped {Emojis.ok_hand}", colour=Colours.soft_green) + await ctx.send(embed=response) + def setup(bot: SeasonalBot) -> None: """Load BrandingManager cog.""" -- cgit v1.2.3 From f02ff11e15d35ada3a905006197a85952b016041 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 18:44:01 +0000 Subject: Remove references to deprecated methods in cog init --- bot/exts/evergreen/branding.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index f9857b6c..9ff91e96 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -157,13 +157,14 @@ class BrandingManager(commands.Cog): self.days_since_cycle = itertools.cycle([None]) - self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json")) + # self.config_file = make_persistent(Path("bot", "resources", "evergreen", "branding.json")) + # should_run = self._read_config()["daemon_active"] - if should_run: - self.daemon = self.bot.loop.create_task(self._daemon_func()) - else: - self.daemon = None + # if should_run: + # self.daemon = self.bot.loop.create_task(self._daemon_func()) + # else: + self.daemon = None @property def _daemon_running(self) -> bool: -- cgit v1.2.3 From 7706228a8cec7789a3af3ff3a449491d860540a4 Mon Sep 17 00:00:00 2001 From: Jeremiah Boby Date: Sat, 14 Nov 2020 20:29:33 +0000 Subject: Remove make_persistent import from bot extensions --- bot/exts/evergreen/branding.py | 4 ++-- bot/exts/halloween/candy_collection.py | 4 +++- bot/exts/halloween/hacktoberstats.py | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) (limited to 'bot') diff --git a/bot/exts/evergreen/branding.py b/bot/exts/evergreen/branding.py index 9ff91e96..b55c9b14 100644 --- a/bot/exts/evergreen/branding.py +++ b/bot/exts/evergreen/branding.py @@ -510,7 +510,7 @@ class BrandingManager(commands.Cog): await ctx.send(embed=response) - @daemon_group.command(name="start", disabled=True) + @daemon_group.command(name="start", enabled=False) async def daemon_start(self, ctx: commands.Context) -> None: """If the daemon isn't running, start it.""" if self._daemon_running: @@ -522,7 +522,7 @@ class BrandingManager(commands.Cog): response = discord.Embed(description=f"Daemon started {Emojis.ok_hand}", colour=Colours.soft_green) await ctx.send(embed=response) - @daemon_group.command(name="stop", disabled=True) + @daemon_group.command(name="stop", enabled=False) async def daemon_stop(self, ctx: commands.Context) -> None: """If the daemon is running, stop it.""" if not self._daemon_running: diff --git a/bot/exts/halloween/candy_collection.py b/bot/exts/halloween/candy_collection.py index 815b0d16..7bdc6ef0 100644 --- a/bot/exts/halloween/candy_collection.py +++ b/bot/exts/halloween/candy_collection.py @@ -9,7 +9,9 @@ from discord.ext import commands from bot.constants import Channels, Month from bot.utils.decorators import in_month -from bot.utils.persist import make_persistent + +# TODO: Implement substitutes for volume-persistent methods. +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) diff --git a/bot/exts/halloween/hacktoberstats.py b/bot/exts/halloween/hacktoberstats.py index 85f70ef0..beff86e3 100644 --- a/bot/exts/halloween/hacktoberstats.py +++ b/bot/exts/halloween/hacktoberstats.py @@ -12,7 +12,9 @@ from discord.ext import commands from bot.constants import Channels, Month, Tokens, WHITELISTED_CHANNELS from bot.utils.decorators import in_month, override_in_channel -from bot.utils.persist import make_persistent + +# TODO: Implement substitutes for volume-persistent methods. +# from bot.utils.persist import make_persistent log = logging.getLogger(__name__) -- cgit v1.2.3