Start & cancel tasks in cog_[un]load instead

pull/128/head
stijndcl 2022-08-29 14:21:21 +02:00
parent 1752d651a9
commit dc2262b246
1 changed files with 16 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import random
import traceback import traceback
from discord.ext import commands, tasks # type: ignore # Strange & incorrect Mypy error from discord.ext import commands, tasks # type: ignore # Strange & incorrect Mypy error
from overrides import overrides
import settings import settings
from database import enums from database import enums
@ -38,6 +39,15 @@ class Tasks(commands.Cog):
def __init__(self, client: Didier): def __init__(self, client: Didier):
self.client = client self.client = client
self._tasks = {
"birthdays": self.check_birthdays,
"ufora": self.pull_ufora_announcements,
"remove_ufora": self.remove_old_ufora_announcements,
"wordle": self.reset_wordle_word,
}
@overrides
def cog_load(self) -> None:
# Only check birthdays if there's a channel to send it to # Only check birthdays if there's a channel to send it to
if settings.BIRTHDAY_ANNOUNCEMENT_CHANNEL is not None: if settings.BIRTHDAY_ANNOUNCEMENT_CHANNEL is not None:
self.check_birthdays.start() self.check_birthdays.start()
@ -50,11 +60,12 @@ class Tasks(commands.Cog):
# Start other tasks # Start other tasks
self.reset_wordle_word.start() self.reset_wordle_word.start()
self._tasks = { @overrides
"birthdays": self.check_birthdays, def cog_unload(self) -> None:
"ufora": self.pull_ufora_announcements, # Cancel all pending tasks
"wordle": self.reset_wordle_word, for task in self._tasks.values():
} if task.is_running():
task.stop()
@commands.group(name="Tasks", aliases=["Task"], case_insensitive=True, invoke_without_command=True) @commands.group(name="Tasks", aliases=["Task"], case_insensitive=True, invoke_without_command=True)
@commands.check(is_owner) @commands.check(is_owner)