From 73980ef1c2091758498104955714780c023ee336 Mon Sep 17 00:00:00 2001 From: "Karina J. Kwiatek" Date: Tue, 6 Aug 2024 13:36:38 +0200 Subject: [PATCH] Use discordpy's tasks to check RSS --- src/bot.py | 18 ++++++++++++++++++ src/main.py | 13 +------------ src/rss.py | 21 +++------------------ 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/bot.py b/src/bot.py index 0e6b5d5..2f8e77d 100644 --- a/src/bot.py +++ b/src/bot.py @@ -15,6 +15,7 @@ ANNOUNCE_CHANNEL_NAME, WELCOME_CATEGORY_NAME, PASSWORDS_CHANNEL_NAME, + FEED_CHECK_INTERVAL, ) from src.commands.join import join from src.commands.team import ( @@ -26,6 +27,10 @@ create_team_channel, ) +from discord.ext import tasks + +from src.rss import check_posts + class BotClient(discord.Client): logger: logging.Logger @@ -66,6 +71,9 @@ async def setup_hook(self) -> None: self.tree.copy_global_to(guild=self.guild) await self.tree.sync(guild=self.guild) + async def setup_hook(self) -> None: + self.check_for_new_blog_posts.start() + async def on_ready(self) -> None: self.logger.info(f"{self.user} has connected to Discord!") guild = self.get_guild(self.guild.id) @@ -134,6 +142,16 @@ async def on_member_remove(self, member: discord.Member) -> None: await channel.delete() self.logger.info(f"Deleted channel '{channel.name}', because it has no users.") + @tasks.loop(seconds=FEED_CHECK_INTERVAL) + async def check_for_new_blog_posts(self): + self.logger.info("Checking for new blog posts") + await check_posts(self.get_guild(int(os.getenv('DISCORD_GUILD_ID')))) + + @check_for_new_blog_posts.before_loop + async def before_check_for_new_blog_posts(self): + await self.wait_until_ready() + + async def load_passwords(self) -> AsyncGenerator[Tuple[str, str], None]: """ Returns a mapping from role name to the password for that role. diff --git a/src/main.py b/src/main.py index ffb5d27..9e48628 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,3 @@ -import asyncio import os import sys import logging @@ -7,7 +6,6 @@ from discord import Intents from src.bot import BotClient -from rss import post_check_timer logger = logging.getLogger("srbot") logger.setLevel(logging.INFO) @@ -26,13 +24,4 @@ exit(1) bot = BotClient(logger=logger, intents=intents) - loop = asyncio.get_event_loop() - -try: - loop.create_task(post_check_timer(bot)) - loop.run_until_complete(bot.start(token)) -except KeyboardInterrupt: - loop.run_until_complete(bot.close()) - # cancel all tasks lingering -finally: - loop.close() + bot.run(token) diff --git a/src/rss.py b/src/rss.py index d181482..2acb5a3 100644 --- a/src/rss.py +++ b/src/rss.py @@ -1,4 +1,3 @@ -import asyncio import os from typing import List @@ -7,14 +6,7 @@ from bs4 import BeautifulSoup from feedparser import FeedParserDict -from src.bot import BotClient -from src.constants import FEED_URL, FEED_CHECK_INTERVAL, FEED_CHANNEL_NAME - - -def get_feed_channel(bot: BotClient) -> discord.TextChannel: - for channel in bot.get_all_channels(): - if channel.name == FEED_CHANNEL_NAME: - return channel +from src.constants import FEED_URL, FEED_CHANNEL_NAME def get_seen_posts() -> List[str]: @@ -30,9 +22,9 @@ def add_seen_post(post_id: str) -> None: f.write(post_id + '\n') -async def check_posts(bot: BotClient): +async def check_posts(guild: discord.Guild) -> None: feed = feedparser.parse(FEED_URL) - channel = get_feed_channel(bot) + channel = discord.utils.get(guild.channels, name=FEED_CHANNEL_NAME) post = feed.entries[0] if post.id + "\n" not in get_seen_posts(): @@ -54,10 +46,3 @@ def create_embed(post: FeedParserDict) -> discord.Embed: embed.set_image(url=post.media_thumbnail[0]['url']) return embed - - -async def post_check_timer(bot: BotClient): - await bot.wait_until_ready() - while True: - await check_posts(bot) - await asyncio.sleep(FEED_CHECK_INTERVAL)