diff options
author | 2020-12-13 11:20:13 +0100 | |
---|---|---|
committer | 2020-12-13 11:20:13 +0100 | |
commit | fb7838c6165b6b32f6561a871428330f9d8f0c7c (patch) | |
tree | 2d76758970c94a744b3465948df7acc8e975a01d | |
parent | Merge branch 'master' into sebastiaan/advent-of-code/refactor-background-tasks (diff) |
Clarify comment on AoC Status Task startup delay
The Advent of Code Status Countdown task needs to wait for two things to
happen to prevent it from failing during the startup sequence:
1. The Websocket instance discord.py creates needs to be available as
an attribute of the bot, otherwise discord.py fails internally:
Traceback (most recent call last):
File "discord/client.py", line 1049, in change_presence
await self.ws.change_presence(
activity=activity, status=status, afk=afk
)
File "advent_of_code/_cog.py", line 52, in countdown_status
await bot.change_presence(activity=discord.Game(playing))
AttributeError: 'NoneType' object has no attribute 'change_presence'
2. Allegedly, according to the discord.py community, trying to change
the status too early in the sequence to establish a connection with
Discord may result ub the Discord API aborting the connection.
To solve this, I've added a `wait_until_guild_available` waiter, as it
guarantees that the websocket is available and the connections is
mature.
Kaizen: I've changed the name `new_puzzle_announcement` to
`new_puzzle_notification` to better reflect its function.
-rw-r--r-- | bot/exts/christmas/advent_of_code/_cog.py | 2 | ||||
-rw-r--r-- | bot/exts/christmas/advent_of_code/_helpers.py | 7 |
2 files changed, 6 insertions, 3 deletions
diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 8c07cdb4..c3b87f96 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -39,7 +39,7 @@ class AdventOfCode(commands.Cog): self.countdown_task = None self.status_task = None - notification_coro = _helpers.new_puzzle_announcement(self.bot) + notification_coro = _helpers.new_puzzle_notification(self.bot) self.notification_task = self.bot.loop.create_task(notification_coro) self.notification_task.set_name("Daily AoC Notification") self.notification_task.add_done_callback(_helpers.background_task_callback) diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index f8c0dc22..b7adc895 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -464,7 +464,10 @@ async def countdown_status(bot: Bot) -> None: 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. Waiting until this event seems to work well. + # 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 @@ -501,7 +504,7 @@ async def countdown_status(bot: Bot) -> None: await asyncio.sleep(delay) -async def new_puzzle_announcement(bot: Bot) -> None: +async def new_puzzle_notification(bot: Bot) -> None: """ Announce the release of a new Advent of Code puzzle. |