Skip to content

Commit

Permalink
Add post body saving feature, bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdknigga committed Jul 13, 2024
1 parent 08d8523 commit 3f8c7e3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "rflying_tower_bot"
version = "0.1.0"
version = "0.2.0"
description = ""
authors = ["Kris Knigga <[email protected]>"]

Expand Down
6 changes: 3 additions & 3 deletions rflying_tower_bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
50 changes: 50 additions & 0 deletions rflying_tower_bot/post_stream.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 3f8c7e3

Please sign in to comment.