bots: Dedup rate limiting error code.

This commit is contained in:
derAnfaenger 2017-05-24 02:25:01 +02:00 committed by Tim Abbott
parent 188f313e15
commit 0b2969a363
1 changed files with 9 additions and 8 deletions

View File

@ -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':