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 45e0f77298
commit 6f88c2c9a2

View file

@ -23,6 +23,8 @@ class RateLimit(object):
self.message_limit = message_limit self.message_limit = message_limit
self.interval_limit = interval_limit self.interval_limit = interval_limit
self.message_list = [] 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): def is_legal(self):
self.message_list.append(time.time()) self.message_list.append(time.time())
@ -33,6 +35,11 @@ class RateLimit(object):
else: else:
return True return True
def show_error_and_exit(self):
logging.error(self.error_message)
sys.exit(1)
class BotHandlerApi(object): class BotHandlerApi(object):
def __init__(self, client): def __init__(self, client):
# Only expose a subset of our Client's functionality # Only expose a subset of our Client's functionality
@ -51,19 +58,13 @@ class BotHandlerApi(object):
if self._rate_limit.is_legal(): if self._rate_limit.is_legal():
return self._client.send_message(*args, **kwargs) return self._client.send_message(*args, **kwargs)
else: else:
logging.error('-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n' self._rate_limit.show_error_and_exit()
'Is your bot trapped in an infinite loop by reacting to'
' its own messages?')
sys.exit(1)
def update_message(self, *args, **kwargs): def update_message(self, *args, **kwargs):
if self._rate_limit.is_legal(): if self._rate_limit.is_legal():
return self._client.update_message(*args, **kwargs) return self._client.update_message(*args, **kwargs)
else: else:
logging.error('-----> !*!*!*MESSAGE RATE LIMIT REACHED, EXITING*!*!*! <-----\n' self._rate_limit.show_error_and_exit()
'Is your bot trapped in an infinite loop by reacting to'
' its own messages?')
sys.exit(1)
def send_reply(self, message, response): def send_reply(self, message, response):
if message['type'] == 'private': if message['type'] == 'private':