Skip to content

Commit

Permalink
Rate limit errors on message sending are now handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
William Lam committed Dec 28, 2020
1 parent 52f47af commit 4b26e99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions hon_patch_notes_game_bot/communications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module contains functions related to communications across the Reddit platform
"""
import time
from praw.exceptions import RedditAPIException


Expand Down Expand Up @@ -67,11 +68,27 @@ def send_message_to_winners(reddit, winners_list, version_string, gold_coin_rewa
)
try:
reddit.redditor(recipient).message(subject=subject_line, message=message)
except RedditAPIException as redditError:

# Reddit API Exception
except RedditAPIException as redditException:
print(
f"{redditError}\n{recipient} was not sent a message, continuing to next recipient"
f"{redditException}\n{recipient} was not sent a message, continuing to next recipient"
)

# Rate limit error handling
# Reference: https://github.com/GrafeasGroup/tor/blob/5ee21af7cc752abc6e7ae6aa47228105e6ec2e05/tor/core/helpers.py#L160-L171
if redditException.error_type == "RATELIMIT":
# Sleep for the rate limit duration
totalLength = str(redditException.items[0].message).split(
"you are doing that too much. try again in ", 1
)[1]
minutesToSleep = totalLength[0].partition("minutes.")[0]
secondsToSleep = int(minutesToSleep) * 60
print("Sleeping for " + str(secondsToSleep) + " seconds")
time.sleep(secondsToSleep)

continue

except Exception as error:
print(
f"{error}\n{recipient} was not sent a message, continuing to next recipient"
Expand Down
2 changes: 2 additions & 0 deletions hon_patch_notes_game_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def main(): # noqa: C901
gold_coin_reward=gold_coin_reward,
)

print("Reddit bot script ended gracefully")


if __name__ == "__main__":
main()

0 comments on commit 4b26e99

Please sign in to comment.