Skip to content

Commit

Permalink
Add better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kdknigga committed Jul 22, 2024
1 parent 92c2119 commit ddd922c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 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.3.0"
version = "0.3.1"
description = ""
authors = ["Kris Knigga <[email protected]>"]

Expand Down
2 changes: 1 addition & 1 deletion rflying_tower_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""rflying_tower_bot package."""

__version__ = "0.3.0"
__version__ = "0.3.1"
54 changes: 32 additions & 22 deletions rflying_tower_bot/modlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,35 @@ async def watch_modlog(self) -> None:
self.config.subreddit_name
)
self.log.info("Starting watch of %s's mod log", subreddit)
async for modlog_entry in subreddit.mod.stream.log(skip_existing=True):
self.log.debug(
"Found new modlog entry: %s did %s (%s) to target %s",
modlog_entry.mod,
modlog_entry.action,
modlog_entry.details,
modlog_entry.target_permalink,
)
if (
modlog_entry.action == "editflair"
and modlog_entry.target_fullname
and modlog_entry.target_fullname[:2] == "t3"
):
target_name: str = modlog_entry.target_fullname[3:]
post: Submission = await self.config.reddit.submission(id=target_name)
await self.check_post_flair(post)

if (
modlog_entry.action == "wikirevise"
and modlog_entry.details == f"Page {self.config.rules_wiki_page} edited"
):
await self.config.update_rules()
while True:
try:
async for modlog_entry in subreddit.mod.stream.log(skip_existing=True):
self.log.debug(
"Found new modlog entry: %s did %s (%s) to target %s",
modlog_entry.mod,
modlog_entry.action,
modlog_entry.details,
modlog_entry.target_permalink,
)
if (
modlog_entry.action == "editflair"
and modlog_entry.target_fullname
and modlog_entry.target_fullname[:2] == "t3"
):
target_name: str = modlog_entry.target_fullname[3:]
post: Submission = await self.config.reddit.submission(
id=target_name
)
await self.check_post_flair(post)

if (
modlog_entry.action == "wikirevise"
and modlog_entry.details
== f"Page {self.config.rules_wiki_page} edited"
):
await self.config.update_rules()
except KeyboardInterrupt:
self.log.info("Caught keyboard interrupt, exiting modlog watcher")
break
except Exception as e:
self.log.error("Error in modlog watcher: %s", e)
57 changes: 36 additions & 21 deletions rflying_tower_bot/post_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,39 @@ async def watch_poststream(self) -> None:
self.config.subreddit_name
)
self.log.info("Watching the post stream for new posts in %s", subreddit)
async for post in subreddit.stream.submissions():
if await self.config.history.check(post.permalink, "save_post_body") > 0:
self.log.info("Skipping post %s, already processed", post.permalink)
continue

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)
await self.config.history.add(post.permalink, "save_post_body")
self.log.info(
"Comment created with post body for posterity: %s", c.permalink
)
while True:
try:
async for post in subreddit.stream.submissions():
if (
await self.config.history.check(
post.permalink, "save_post_body"
)
> 0
):
self.log.info(
"Skipping post %s, already processed", post.permalink
)
continue

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)
await self.config.history.add(post.permalink, "save_post_body")
self.log.info(
"Comment created with post body for posterity: %s",
c.permalink,
)
except KeyboardInterrupt:
self.log.info("Caught keyboard interrupt, exiting post stream watcher")
break
except Exception as e:
self.log.error("Error in post stream watcher: %s", e)
2 changes: 1 addition & 1 deletion tests/test_rflying_tower_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

def test_version():
"""Test the version of the rflying_tower_bot package."""
assert __version__ == "0.3.0"
assert __version__ == "0.3.1"

0 comments on commit ddd922c

Please sign in to comment.