didier/didier/cogs/tasks.py

61 lines
2.3 KiB
Python
Raw Normal View History

2022-06-19 00:23:25 +02:00
import traceback
2022-06-21 18:58:33 +02:00
from discord.ext import commands, tasks # type: ignore # Strange & incorrect Mypy error
2022-06-19 00:23:25 +02:00
import settings
2022-06-19 00:51:24 +02:00
from database.crud.ufora_announcements import remove_old_announcements
2022-06-19 00:23:25 +02:00
from didier import Didier
from didier.data.embeds.ufora.announcements import fetch_ufora_announcements
class Tasks(commands.Cog):
"""Task loops that run periodically"""
client: Didier
def __init__(self, client: Didier):
# pylint: disable=no-member
2022-06-19 00:23:25 +02:00
self.client = client
# Only pull announcements if a token was provided
if settings.UFORA_RSS_TOKEN is not None and settings.UFORA_ANNOUNCEMENTS_CHANNEL is not None:
2022-06-19 00:51:24 +02:00
self.pull_ufora_announcements.start()
self.remove_old_ufora_announcements.start()
2022-06-19 00:23:25 +02:00
@tasks.loop(minutes=10)
async def pull_ufora_announcements(self):
"""Task that checks for new Ufora announcements & logs them in a channel"""
# In theory this shouldn't happen but just to please Mypy
if settings.UFORA_RSS_TOKEN is None or settings.UFORA_ANNOUNCEMENTS_CHANNEL is None:
return
announcements_channel = self.client.get_channel(settings.UFORA_ANNOUNCEMENTS_CHANNEL)
announcements = await fetch_ufora_announcements(self.client.db_session)
for announcement in announcements:
await announcements_channel.send(embed=announcement.to_embed())
@pull_ufora_announcements.before_loop
async def _before_ufora_announcements(self):
"""Don't try to get announcements if the bot isn't ready yet"""
await self.client.wait_until_ready()
@pull_ufora_announcements.error
async def _on_announcements_error(self, error: BaseException):
"""Error handler for the Ufora Announcements task"""
print("".join(traceback.format_exception(type(error), error, error.__traceback__)))
2022-06-19 00:51:24 +02:00
@tasks.loop(hours=24)
async def remove_old_ufora_announcements(self):
"""Remove all announcements that are over 1 week old, once per day"""
await remove_old_announcements(self.client.db_session)
@remove_old_ufora_announcements.before_loop
async def _before_remove_old_ufora_announcements(self):
await self.client.wait_until_ready()
2022-06-19 00:23:25 +02:00
async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Tasks(client))