Ejemplo n.º 1
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config

        while True:

            data = sock.recv(config['socket_buffer_size']).rstrip()

            if len(data) == 0:
                pp('Connection was lost, reconnecting.')
                sock = self.irc.get_irc_socket_object()

            if config['debug']:
                print data

            # check for ping, reply with pong
            irc.check_for_ping(data)

            if irc.check_for_message(data):
                message_dict = irc.get_message(data)

                channel = message_dict['channel']
                message = message_dict['message']
                username = message_dict['username']

                ppi(channel, message, username)

                # check if message is a command with no arguments
                if commands.is_valid_command(
                        message) or commands.is_valid_command(
                            message.split(' ')[0]):
                    command = message

                    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):
                                pbot(
                                    'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                    % (command, username,
                                       commands.get_cooldown_remaining(
                                           command, channel)), channel)
                            else:
                                pbot(
                                    'Command is valid an not on cooldown. (%s) (%s)'
                                    % (command, username), channel)

                                result = commands.pass_to_function(
                                    command, args, username)
                                commands.update_last_used(command, channel)

                                if result:
                                    resp = '@%s - %s' % (username, result)
                                    pbot(resp, channel)
                                    irc.send_message(channel, resp)

                    else:
                        if commands.is_on_cooldown(command, channel):
                            pbot(
                                'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                % (command, username,
                                   commands.get_cooldown_remaining(
                                       command, channel)), channel)
                        elif commands.check_has_return(command):
                            pbot(
                                'Command is valid and not on cooldown. (%s) (%s)'
                                % (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)
                            irc.send_message(channel, resp)
Ejemplo n.º 2
0
	def run(self):
		irc = self.irc
		sock = self.socket
		config = self.config

		tasks.run_tasks()

		while True:
			data = sock.recv(config['socket_buffer_size']).rstrip()

			if len(data) == 0:
				pp('Connection was lost, reconnecting.')
				sock = self.irc.get_irc_socket_object()

			if config['debug']:
				print(data)

			# check for ping, reply with pong
			irc.check_for_ping(data)

			if irc.check_for_message(data):
				message_dict = irc.get_message(data)

				channel = message_dict['channel']
				message = message_dict['message']
				username = message_dict['username']

				ppi(channel, message, username)

				# check if message is a command with no arguments
				if commands.is_valid_command(message) or commands.is_valid_command(message.split(' ')[0]):
					command = message

					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):
								pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
									command, username, commands.get_cooldown_remaining(command, channel)), 
									channel
								)
							else:
								pbot('Command is valid an not on cooldown. (%s) (%s)' % (
									command, username), 
									channel
								)
								
								result = commands.pass_to_function(command, args)
								commands.update_last_used(command, channel)

								if result:
									# resp = '(%s) > %s' % (username, result)
									resp = '%s' % (result)
									pbot(resp, channel)
									irc.send_message(channel, resp)

					else:
						if commands.is_on_cooldown(command, channel):
							pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
									command, username, commands.get_cooldown_remaining(command, channel)), 
									channel
							)
						elif commands.check_has_return(command):
							pbot('Command is valid and not on cooldown. (%s) (%s)' % (
								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)
							irc.send_message(channel, resp)
Ejemplo n.º 3
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config

        while True:
            data = sock.recv(config['socket_buffer_size']).rstrip()

            if len(data) == 0:
                pp('Connection was lost, reconnecting.')
                sock = self.irc.get_irc_socket_object()

            if config['debug']:
                print data

            # check for ping, reply with pong
            irc.check_for_ping(data)

            for channel in config['channels'] :
                channel_runtime.of(channel)

            if irc.check_for_message(data):
                message_dict = irc.get_message(data)

                channel = message_dict['channel']
                message = message_dict['message']
                username = message_dict['username']

                ppi(channel, message, username)

                # check if message is a command with no arguments
                if commands.is_valid_command(message) or commands.is_valid_command(message.split(' ')[0]):
                    command = message

                    if commands.is_protected(message.split(' ')[0]) \
                                    and not username in channel_runtime.of(channel).moderators:
                                continue

                    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):
                                pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
                                    command, username, commands.get_cooldown_remaining(command, channel)),
                                    channel
                                )
                            else:
                                pbot('Command is valid an not on cooldown. (%s) (%s)' % (
                                    command, username),
                                    channel
                                )"""

                            #args.append(username)
                            result = commands.pass_to_function(command, args, username, channel_runtime.of(channel))
                            commands.update_last_used(command, channel)

                            if result:
                                resp = '%s' % result
                                pbot(resp, channel)
                                irc.send_message(channel, resp)

                    else:
                        """if commands.is_on_cooldown(command, channel):
                            pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
                                    command, username, commands.get_cooldown_remaining(command, channel)),
                                    channel
                            )
                        elif commands.check_has_return(command):
                            pbot('Command is valid and not on cooldown. (%s) (%s)' % (
                                command, username),
                                channel
                            )"""
                        commands.update_last_used(command, channel)

                        resp = '%s' % commands.get_return(command)
                        commands.update_last_used(command, channel)
                        if resp != "nothing":
                            pbot(resp, channel)
                            irc.send_message(channel, resp)
Ejemplo n.º 4
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config
        
        while True:
            data = sock.recv(config['socket_buffer_size']).rstrip()
            
            if len(data) == 0:
                pp('Connection was lost, reconnecting.')
                sock = self.irc.get_irc_socket_object()

            if config['debug']:
                print data

            # check for ping, reply with pong
            irc.check_for_ping(data)
            
            if irc.check_for_message(data):
                message_dict = irc.get_message(data)

                channel = message_dict['channel']
                message = message_dict['message']
                username = message_dict['username']
                
                ppi(channel, message, username)
                
                #custom triggers
                stop = re.match('stop ([0-9]+)', message)
                if stop:
                    time.sleep(int(stop.group(1)))
                    continue
                    
                #pause for some time to prevent hitting request limit
                time.sleep(randint(2,4))
                print "sending rando"
                irc.random_chat(config['channels'][0])
                
                # check if message is a command with no arguments
                if commands.is_valid_command(message) or commands.is_valid_command(message.split(' ')[0]):
                    command = message

                    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):
                                pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
                                    command, username, commands.get_cooldown_remaining(command, channel)), 
                                    channel
                                )
                            else:
                                pbot('Command is valid an not on cooldown. (%s) (%s)' % (
                                    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)
                                    irc.send_message(channel, resp)

                    else:
                        if commands.is_on_cooldown(command, channel):
                            pbot('Command is on cooldown. (%s) (%s) (%ss remaining)' % (
                                    command, username, commands.get_cooldown_remaining(command, channel)), 
                                    channel
                            )
                        elif commands.check_has_return(command):
                            pbot('Command is valid and not on cooldown. (%s) (%s)' % (
                                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)
                            irc.send_message(channel, resp)
Ejemplo n.º 5
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config
        ALWAYS_LEARN = False
        while True:
            data = sock.recv(config['socket_buffer_size']).rstrip()

            if len(data) == 0:
                pp('Connection was lost, reconnecting.')
                sock = self.irc.get_irc_socket_object()

            if config['debug']:
                print(data)

            # check for ping, reply with pong
            irc.check_for_ping(data)

            if irc.check_for_message(data):
                message_dict = irc.get_message(data)

                channel = message_dict['channel']
                message = message_dict['message']
                username = message_dict['username']
                botCommand = ('@ShrikeChanBot' in message) or ('!learn'
                                                               in message)
                if username is 'shrikechanbot':
                    print('skip bot messages')
                else:

                    if ALWAYS_LEARN and not botCommand and self.last_message != '':
                        clean_message = message.replace('@ShrikeChanBot',
                                                        '').replace(
                                                            '!learn',
                                                            '').strip()
                        chatbot.learn_response(
                            Statement(text=clean_message),
                            Statement(text=self.last_message))
                        print('LEARNING', clean_message, '>',
                              self.last_message, '=',
                              chatbot.get_response(clean_message))
                        self.last_message = clean_message
                    ppi(channel, message, username)
                    # check if message is a command with no arguments
                    if commands.is_valid_command(
                            message) or commands.is_valid_command(
                                message.split(' ')[0]):
                        command = message

                        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):
                                    pbot(
                                        'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                        % (command, username,
                                           commands.get_cooldown_remaining(
                                               command, channel)), channel)
                                else:
                                    pbot(
                                        'Command is valid and not on cooldown. (%s) (%s)'
                                        % (command, username), channel)
                                    if botCommand:
                                        args = message
                                    result = commands.pass_to_function(
                                        command, args, chatbot,
                                        self.last_message)
                                    commands.update_last_used(command, channel)

                                    if result:
                                        resp = '%s @%s' % (result, username)
                                        pbot(resp, channel)
                                        irc.send_message(channel, resp)

                        else:
                            if commands.is_on_cooldown(command, channel):
                                pbot(
                                    'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                    % (command, username,
                                       commands.get_cooldown_remaining(
                                           command, channel)), channel)
                            elif commands.check_has_return(command):
                                pbot(
                                    'Command is valid and not on cooldown. (%s) (%s)'
                                    % (command, username), channel)
                                commands.update_last_used(command, channel)

                                resp = '%s @%s' % (
                                    commands.get_return(command), username)
                                commands.update_last_used(command, channel)

                                pbot(resp, channel)
                                irc.send_message(channel, resp)
                    if ALWAYS_LEARN or botCommand:
                        self.last_message = message.replace(
                            '@ShrikeChanBot', '').replace('!learn',
                                                          '').strip()
Ejemplo n.º 6
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config
        for rank in self.ranks:
            self.ranks[rank].start()
            resp = u'! 98bot進入聊天室,計算聊天等級開始。'
            irc.send_message(rank, resp)

        while True:
            data = sock.recv(config['socket_buffer_size']).rstrip()

            if len(data) == 0:
                pp('Connection was lost, reconnecting.')
                sock = self.irc.get_irc_socket_object()

            if config['debug']:
                print data

            # check for ping, reply with pong
            irc.check_for_ping(data)

            if irc.check_for_message(data):
                message_dict = irc.get_message(data)

                channel = message_dict['channel']
                message = message_dict['message']
                username = message_dict['username']

                if username.lower() in self.config['ignore']: continue

                # give XP to this user
                rks = self.ranks.get(channel, None)
                if rks is not None and rks.live == True: rks.newchat(username)
                if rks.quiet: continue

                #I'm lazy to write a module
                msg = message.strip().split(' ')
                target = username
                if len(msg) > 1: target = msg[1].lower()
                if msg[0].lower() == '!rank':
                    if username == channel[1:] and len(
                            msg) == 1:  #broadcaster don't have level
                        u = rks.top()
                        if u is not None:
                            if u[4].lower() == u[1].lower():
                                resp = u'! 報告台主,目前講話最多的是 %s,%s級,經驗值%s/%s。' % (
                                    u[4], str(u[3]), str(
                                        u[2]), str(threshold[u[3]]))
                            else:
                                resp = u'! 報告台主,目前講話最多的是%s (%s),%s級,經驗值%s/%s。' % (
                                    u[4], u[1], str(u[3]), str(
                                        u[2]), str(threshold[u[3]]))
                        else:
                            resp = u'! 報告台主,你到現在都沒有半個觀眾講過話,幫QQ'
                    else:
                        u = rks.find(target)
                        if u is not None:
                            if u[4].lower() == u[1].lower():
                                resp = u'! %s 目前%s級,排第%s名,經驗值%s/%s。' % (
                                    u[4], str(u[3]), str(u[0]), str(
                                        u[2]), str(threshold[u[3]]))
                            else:
                                resp = u'! %s (%s) 目前%s級,排第%s名,經驗值%s/%s。' % (
                                    u[4], u[1], str(u[3]), str(u[0]), str(
                                        u[2]), str(threshold[u[3]]))
                        else:
                            resp = u'! 找不到 %s 的等級,大概還沒講過話吧。' % target
                    irc.send_message(channel, resp)
                    continue

                # for checking the bot is online
                if message.strip().lower() == '!98bot' or message.strip(
                ).split(' ')[0].lower() == '!98bot':
                    if rks.live:
                        resp = u'! 正在努力記錄大家的經驗值。'
                    else:
                        resp = u'! 目前沒開台沒經驗值,但我不會阻止你講話~'
                    irc.send_message(channel, resp)
                    continue

                # check if message is a command with no arguments
                if commands.is_valid_command(
                        message) or commands.is_valid_command(
                            message.split(' ')[0]):
                    command = message

                    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):
                                pbot(
                                    'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                    % (command, username,
                                       commands.get_cooldown_remaining(
                                           command, channel)), channel)
                            else:
                                pbot(
                                    'Command is valid an not on cooldown. (%s) (%s)'
                                    % (command, username), channel)

                                result = commands.pass_to_function(
                                    command, args)
                                commands.update_last_used(command, channel)

                                if result:
                                    resp = '%s' % result
                                    pbot(resp, channel)
                                    irc.send_message(channel, resp)

                    else:
                        if commands.is_on_cooldown(command, channel):
                            pbot(
                                'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                % (command, username,
                                   commands.get_cooldown_remaining(
                                       command, channel)), channel)
                        elif commands.check_has_return(command):
                            pbot(
                                'Command is valid and not on cooldown. (%s) (%s)'
                                % (command, username), channel)
                            commands.update_last_used(command, channel)

                            resp = '%s' % commands.get_return(command)
                            commands.update_last_used(command, channel)

                            pbot(resp, channel)
                            irc.send_message(channel, resp)
Ejemplo n.º 7
0
    def run(self):
        irc = self.irc
        sock = self.socket
        config = self.config

        while True:
            data_raw = sock.recv(config['socket_buffer_size']).rstrip()

            for data in data_raw.split('\r'):
                data = data.strip()
                if len(data) == 0:
                    pp('Connection was lost, reconnecting.')
                    sock = self.irc.get_irc_socket_object()

                if config['debug']:
                    print data

                # check for ping, reply with pong
                if irc.check_for_ping(data):
                    continue

                if irc.check_for_join(data):
                    user, channel = irc.get_join(data)
                    self.users[channel].add(user.lower())
                    pp("User {0} joined {1}".format(user, channel))
                    continue

                if irc.check_for_part(data):
                    user, channel = irc.get_part(data)
                    try:
                        self.users[channel].remove(user.lower())
                    except KeyError:
                        pass
                    pp("User {0} left {1}".format(user, channel))
                    continue

                if irc.check_for_message(data):
                    message_dict = irc.get_message(data)

                    channel = message_dict['channel']
                    message = message_dict['message']
                    username = message_dict['username']

                    ppi(channel, message, username)

                    # check if message is a command with no arguments
                    if commands.is_valid_command(
                            message) or commands.is_valid_command(
                                message.split(' ')[0]):
                        command = message

                        if commands.check_returns_function(
                                command.split(' ')[0]):
                            if commands.check_has_correct_args(
                                    command,
                                    command.split(' ')[0]):
                                command_name = command.split(' ')[0]
                                args = command.split(' ')[1:]

                                if commands.is_on_cooldown(
                                        command_name, channel):
                                    pbot(
                                        'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                        % (command, username,
                                           commands.get_cooldown_remaining(
                                               command_name, channel)),
                                        channel)
                                else:
                                    pbot(
                                        'Command is valid an not on cooldown. (%s) (%s)'
                                        % (command, username), channel)

                                    result = commands.pass_to_function(
                                        command_name[1:], args, username,
                                        self.users[channel])
                                    commands.update_last_used(
                                        command_name, channel)

                                    if result:
                                        for r in result:
                                            resp = '(%s) > %s' % (username, r)
                                            pbot(resp, channel)
                                            irc.send_message(channel, resp)

                        else:
                            if commands.is_on_cooldown(command, channel):
                                pbot(
                                    'Command is on cooldown. (%s) (%s) (%ss remaining)'
                                    % (command, username,
                                       commands.get_cooldown_remaining(
                                           command, channel)), channel)
                            elif commands.check_has_return(command):
                                pbot(
                                    'Command is valid and not on cooldown. (%s) (%s)'
                                    % (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)
                                irc.send_message(channel, resp)