diff --git a/pyproject.toml b/pyproject.toml index 2d4eb89..963068c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rflying_tower_bot" -version = "0.1.0" +version = "0.2.0" description = "" authors = ["Kris Knigga "] diff --git a/rflying_tower_bot/__main__.py b/rflying_tower_bot/__main__.py index cb2d0b0..50cbbbb 100644 --- a/rflying_tower_bot/__main__.py +++ b/rflying_tower_bot/__main__.py @@ -7,6 +7,7 @@ from rflying_tower_bot.config import BotConfig, PRAWConfig from rflying_tower_bot.modlog import ModLog +from rflying_tower_bot.post_stream import PostStream logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -34,10 +35,9 @@ async def main() -> None: await bot_config.update_rules() modlog = ModLog(bot_config) + post_stream = PostStream(bot_config) - # The gather here is currently unnessisary, but is here to be ready for when - # we want to watch things other than the modlog. - await asyncio.gather(modlog.watch_modlog()) + await asyncio.gather(modlog.watch_modlog(), post_stream.watch_poststream()) try: diff --git a/rflying_tower_bot/post_stream.py b/rflying_tower_bot/post_stream.py new file mode 100644 index 0000000..91ca6fc --- /dev/null +++ b/rflying_tower_bot/post_stream.py @@ -0,0 +1,50 @@ +"""A module to react to new posts.""" + +import logging + +from asyncpraw.models import Comment, Subreddit + +from rflying_tower_bot.config import BotConfig +from rflying_tower_bot.utilities import Utilities + + +class PostStream: + """A class to react to new posts.""" + + def __init__(self, config: BotConfig) -> None: + """ + Create an instance of PostStream. + + Args: + ---- + config (BotConfig): See config.BotConfig + + """ + self.log: logging.Logger = logging.getLogger( + f"{__name__}.{self.__class__.__name__}" + ) + self.config = config + self.utilities = Utilities(config) + + async def watch_poststream(self) -> None: + """Watch the post stream and react to new posts.""" + subreddit: Subreddit = await self.config.reddit.subreddit( + self.config.subreddit_name + ) + self.log.info("Watching the post stream for new posts in %s", subreddit) + async for post in subreddit.stream.submissions(): + self.log.info("New post from %s: %s", post.author, post.permalink) + if post.selftext != "": + comment_text = f"This is a copy of the original post body for posterity:\n\n --- \n{post.selftext}" + c: Comment | None = await post.reply( + self.utilities.format_comment(comment_text) + ) + if not c: + self.log.error( + "Making comment on %s seems to have failed", str(post) + ) + return + await c.mod.distinguish(sticky=False) + self.log.info( + "Comment created with post body for posterity: %s", c.permalink + )