Skip to content

Commit

Permalink
Truncate posterity comments to fit the max comment size, and catch Re…
Browse files Browse the repository at this point in the history
…ddit API errors when commenting
  • Loading branch information
kdknigga committed Sep 7, 2024
1 parent 1c3cd4f commit 3d4d1df
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions rflying_tower_bot/post_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import time

from asyncpraw.exceptions import RedditAPIException
from asyncpraw.models import Comment, Subreddit
from asyncprawcore.exceptions import RequestException, ServerError

Expand Down Expand Up @@ -68,10 +69,26 @@ async def watch_poststream(self) -> None:

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)
# Reddit comments have a max length of 10,000 characters, so truncate the post body if it's too long
# Leave room for the bot header and footer
truncated_original_post_body: str = post.selftext[:9500] + (
post.selftext[9500:] and "..."
)
comment_text = f"This is a copy of the original post body for posterity:\n\n --- \n{truncated_original_post_body}"
c: Comment | None = None
try:
c = await post.reply(
self.utilities.format_comment(comment_text)
)
except RedditAPIException as e:
self.log.error(
"API error making comment on %s: %s. Logging the comment as successful to prevent constant retrying.",
post.permalink,
e,
)
await self.config.history.add(
post.permalink, "save_post_body"
)
if not c:
self.log.error(
"Making comment on %s seems to have failed", str(post)
Expand Down

0 comments on commit 3d4d1df

Please sign in to comment.