From e263ac1e7f95cbc4b6b949258d0c09312b550c49 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Thu, 30 Jun 2022 22:29:39 +0100 Subject: Move startup checks and logs to their own cog This is to avoid needed to use wait_until_guild_available during the setup hook. --- bot/exts/utilities/logging.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 bot/exts/utilities/logging.py (limited to 'bot/exts/utilities/logging.py') diff --git a/bot/exts/utilities/logging.py b/bot/exts/utilities/logging.py new file mode 100644 index 00000000..e1d09fdf --- /dev/null +++ b/bot/exts/utilities/logging.py @@ -0,0 +1,42 @@ +from botcore.utils.logging import get_logger +from discord.ext.commands import Cog + +from bot import constants +from bot.bot import Bot + +log = get_logger(__name__) + + +class Logging(Cog): + """Debug logging module.""" + + def __init__(self, bot: Bot): + self.bot = bot + + async def cog_load(self) -> None: + """Announce our presence to the configured dev-log channel after checking channel constants.""" + await self.check_channels() + await self.bot.log_to_dev_log( + title=self.bot.name, + details="Connected!", + ) + + async def check_channels(self) -> None: + """Verifies that all channel constants refer to channels which exist.""" + await self.bot.wait_until_guild_available() + + if constants.Client.debug: + log.info("Skipping Channels Check.") + return + + all_channels_ids = [channel.id for channel in self.bot.get_all_channels()] + for name, channel_id in vars(constants.Channels).items(): + if name.startswith("_"): + continue + if channel_id not in all_channels_ids: + log.error(f'Channel "{name}" with ID {channel_id} missing') + + +async def setup(bot: Bot) -> None: + """Load the Logging cog.""" + await bot.add_cog(Logging(bot)) -- cgit v1.2.3 From 952a3a7849d4694cc3e4aa89021d3c976cdc537b Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Fri, 23 Sep 2022 22:12:26 +0100 Subject: Remove all wait_until_guil_available as this is now done in bot-core --- bot/bot.py | 1 - bot/exts/events/advent_of_code/_cog.py | 1 - bot/exts/events/advent_of_code/_helpers.py | 10 ---------- bot/exts/holidays/easter/egg_facts.py | 2 -- bot/exts/holidays/halloween/spookynamerate.py | 1 - bot/exts/holidays/pride/pride_facts.py | 2 -- bot/exts/utilities/logging.py | 2 -- bot/exts/utilities/reddit.py | 5 +---- 8 files changed, 1 insertion(+), 23 deletions(-) (limited to 'bot/exts/utilities/logging.py') diff --git a/bot/bot.py b/bot/bot.py index 9309f50c..636946f1 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -42,7 +42,6 @@ class Bot(BotBase): async def log_to_dev_log(self, title: str, details: str = None, *, icon: str = None) -> None: """Send an embed message to the dev-log channel.""" - await self.wait_until_guild_available() devlog = self.get_channel(constants.Channels.devlog) if not icon: diff --git a/bot/exts/events/advent_of_code/_cog.py b/bot/exts/events/advent_of_code/_cog.py index ab5a7a34..49140a3f 100644 --- a/bot/exts/events/advent_of_code/_cog.py +++ b/bot/exts/events/advent_of_code/_cog.py @@ -70,7 +70,6 @@ class AdventOfCode(commands.Cog): Runs on a schedule, as defined in the task.loop decorator. """ - await self.bot.wait_until_guild_available() guild = self.bot.get_guild(Client.guild) completionist_role = guild.get_role(Roles.aoc_completionist) if completionist_role is None: diff --git a/bot/exts/events/advent_of_code/_helpers.py b/bot/exts/events/advent_of_code/_helpers.py index 6c004901..abd80b77 100644 --- a/bot/exts/events/advent_of_code/_helpers.py +++ b/bot/exts/events/advent_of_code/_helpers.py @@ -523,13 +523,6 @@ async def countdown_status(bot: Bot) -> None: # Log that we're going to start with the countdown status. log.info("The Advent of Code has started or will start soon, starting countdown status.") - # Trying to change status too early in the bot's startup sequence will fail - # the task because the websocket instance has not yet been created. Waiting - # for this event means that both the websocket instance has been initialized - # and that the connection to Discord is mature enough to change the presence - # of the bot. - await bot.wait_until_guild_available() - # Calculate when the task needs to stop running. To prevent the task from # sleeping for the entire year, it will only wait in the currently # configured year. This means that the task will only start hibernating once @@ -578,9 +571,6 @@ async def new_puzzle_notification(bot: Bot) -> None: log.info("The Advent of Code has started or will start soon, waking up notification task.") - # Ensure that the guild cache is loaded so we can get the Advent of Code - # channel and role. - await bot.wait_until_guild_available() aoc_channel = bot.get_channel(Channels.advent_of_code) aoc_role = aoc_channel.guild.get_role(AdventOfCode.role_id) diff --git a/bot/exts/holidays/easter/egg_facts.py b/bot/exts/holidays/easter/egg_facts.py index 2fb2041e..43b31c7b 100644 --- a/bot/exts/holidays/easter/egg_facts.py +++ b/bot/exts/holidays/easter/egg_facts.py @@ -29,8 +29,6 @@ class EasterFacts(commands.Cog): @seasonal_task(Month.APRIL) async def send_egg_fact_daily(self) -> None: """A background task that sends an easter egg fact in the event channel everyday.""" - await self.bot.wait_until_guild_available() - channel = self.bot.get_channel(Channels.sir_lancebot_playground) await channel.send(embed=self.make_embed()) diff --git a/bot/exts/holidays/halloween/spookynamerate.py b/bot/exts/holidays/halloween/spookynamerate.py index 5d41ce6d..a76e5e12 100644 --- a/bot/exts/holidays/halloween/spookynamerate.py +++ b/bot/exts/holidays/halloween/spookynamerate.py @@ -355,7 +355,6 @@ class SpookyNameRate(Cog): async def get_channel(self) -> Optional[TextChannel]: """Gets the sir-lancebot-channel after waiting until ready.""" - await self.bot.wait_until_ready() channel = self.bot.get_channel( Channels.sir_lancebot_playground ) or await self.bot.fetch_channel(Channels.sir_lancebot_playground) diff --git a/bot/exts/holidays/pride/pride_facts.py b/bot/exts/holidays/pride/pride_facts.py index ae025ae7..36a9415e 100644 --- a/bot/exts/holidays/pride/pride_facts.py +++ b/bot/exts/holidays/pride/pride_facts.py @@ -28,8 +28,6 @@ class PrideFacts(commands.Cog): @seasonal_task(Month.JUNE) async def send_pride_fact_daily(self) -> None: """Background task to post the daily pride fact every day.""" - await self.bot.wait_until_guild_available() - channel = self.bot.get_channel(Channels.sir_lancebot_playground) await self.send_select_fact(channel, datetime.utcnow()) diff --git a/bot/exts/utilities/logging.py b/bot/exts/utilities/logging.py index e1d09fdf..83b7025f 100644 --- a/bot/exts/utilities/logging.py +++ b/bot/exts/utilities/logging.py @@ -23,8 +23,6 @@ class Logging(Cog): async def check_channels(self) -> None: """Verifies that all channel constants refer to channels which exist.""" - await self.bot.wait_until_guild_available() - if constants.Client.debug: log.info("Skipping Channels Check.") return diff --git a/bot/exts/utilities/reddit.py b/bot/exts/utilities/reddit.py index 07222d79..028c16bc 100644 --- a/bot/exts/utilities/reddit.py +++ b/bot/exts/utilities/reddit.py @@ -48,9 +48,7 @@ class Reddit(Cog): async def cog_load(self) -> None: """Sets the reddit webhook when the cog is loaded.""" - await self.bot.wait_until_guild_available() - if not self.webhook: - self.webhook = await self.bot.fetch_webhook(RedditConfig.webhook) + self.webhook = await self.bot.fetch_webhook(RedditConfig.webhook) @property def channel(self) -> TextChannel: @@ -256,7 +254,6 @@ class Reddit(Cog): await sleep_until(midnight_tomorrow) - await self.bot.wait_until_guild_available() if not self.webhook: await self.bot.fetch_webhook(RedditConfig.webhook) -- cgit v1.2.3