From 6f88c2c9a2cf392caa69a5004f5cba491e59d698 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Wed, 24 May 2017 02:25:01 +0200 Subject: [PATCH] bots: Dedup rate limiting error code. --- contrib_bots/bot_lib.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/contrib_bots/bot_lib.py b/contrib_bots/bot_lib.py index a7100f8..744e061 100644 --- a/contrib_bots/bot_lib.py +++ b/contrib_bots/bot_lib.py @@ -23,6 +23,8 @@ class RateLimit(object): self.message_limit = message_limit self.interval_limit = interval_limit self.message_list = [] + self.error_message = '-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n' + 'Is your bot trapped in an infinite loop by reacting to its own messages?' def is_legal(self): self.message_list.append(time.time()) @@ -33,6 +35,11 @@ class RateLimit(object): else: return True + def show_error_and_exit(self): + logging.error(self.error_message) + sys.exit(1) + + class BotHandlerApi(object): def __init__(self, client): # Only expose a subset of our Client's functionality @@ -51,19 +58,13 @@ class BotHandlerApi(object): if self._rate_limit.is_legal(): return self._client.send_message(*args, **kwargs) else: - logging.error('-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n' - 'Is your bot trapped in an infinite loop by reacting to' - ' its own messages?') - sys.exit(1) + self._rate_limit.show_error_and_exit() def update_message(self, *args, **kwargs): if self._rate_limit.is_legal(): return self._client.update_message(*args, **kwargs) else: - logging.error('-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n' - 'Is your bot trapped in an infinite loop by reacting to' - ' its own messages?') - sys.exit(1) + self._rate_limit.show_error_and_exit() def send_reply(self, message, response): if message['type'] == 'private':