def __init__(self, config): self.config = config self.irc = IRC(config) self.socket = self.irc.get_irc_socket_object()
class Roboraj(object): """ Main class for Twitch Bot """ COMMAND_ON_COOLDOWN = u'Command is on cooldown. ({0}) ({1}) ({2} remaining)' COMMAND_VALID = u'Command is valid and not on cooldown. ({0}) ({1})' config = None irc = None socket = None def __init__(self, config): self.config = config self.irc = IRC(config) self.socket = self.irc.get_irc_socket_object() def _handle_commands(self, channel, username, command): """ Internal function to handle commands """ if commands.check_returns_function(command.split(' ')[0]): if commands.check_has_correct_args(command, command.split(' ')[0]): args = command.split(' ') del args[0] command = command.split(' ')[0] if commands.is_on_cooldown(command, channel): cooldown = commands.get_cooldown_remaining(command, channel) pbot(self.COMMAND_ON_COOLDOWN.format(command, username, cooldown), channel) else: pbot(self.COMMAND_VALID.format(command, username), channel) result = commands.pass_to_function(command, args) commands.update_last_used(command, channel) if result: resp = '(%s) > %s' % (username, result) pbot(resp, channel) self.irc.send_message(channel, resp) else: if commands.is_on_cooldown(command, channel): cooldown = commands.get_cooldown_remaining(command, channel) pbot(self.COMMAND_ON_COOLDOWN.format(command, username, cooldown), channel) elif commands.check_has_return(command): pbot(self.COMMAND_VALID.format(command, username), channel) commands.update_last_used(command, channel) resp = '(%s) > %s' % (username, commands.get_return(command)) commands.update_last_used(command, channel) pbot(resp, channel) self.irc.send_message(channel, resp) def run(self): """ Runs Twitch bot """ while True: data = self.socket.recv(self.config['socket_buffer_size']).rstrip() if len(data) == 0: plog('Connection was lost, reconnecting.', 'warn') self.socket = self.irc.get_irc_socket_object() plog(str(data, 'utf-8'), 'debug') # check for ping, reply with pong self.irc.check_for_ping(data) if self.irc.check_for_message(data): message_dict = self.irc.get_message(data) channel = message_dict['channel'] message = message_dict['message'] username = message_dict['username'] pchat(channel, message, username) # check if message is a command with no arguments if commands.is_valid_command(message.split(' ')[0]): self._handle_commands(channel, username, message)