示例#1
0
文件: bot.py 项目: gflerm/seejoo
    def _command(self, user, command, channel=None):
        '''Internal function that handles the processing of commands.
        Returns the result of processing as a text response to be "said" by the bot,
        or None if it wasn't actually a command.

        :param channel: Channel where the command was issued
                        (can be None if it was private message command)
        '''
        m = COMMAND_RE.match(command)
        if not m:
            return

        cmd = m.group('cmd')
        args = m.groupdict().get('args')

        # Poll plugins for command result
        resp = ext.notify(self, 'command',
                          channel=channel, user=user, cmd=cmd, args=args)
        if resp:
            return resp

        # Plugins didn't care so find a command and invoke it if present
        cmd_object = ext.get_command(cmd)
        if cmd_object:
            if callable(cmd_object):
                try:
                    resp = cmd_object(args)
                except Exception, e:
                    resp = type(e).__name__ + ": " + str(e)
                resp = [resp]  # Since we expect response to be iterable
            else:
                return ["Invalid command '%s'; likely indicates faulty plugin" % cmd]
示例#2
0
文件: bot.py 项目: gosuwachu/seejoo
 def _command(self, user, command):
     '''
     Internal function that handles the processing of commands. 
     Returns the result of processing as a text response to be "said" by the bot,
     or None if it wasn't actually a command.
     '''
     m = COMMAND_RE.match(command)
     if not m:   return
     
     cmd = m.group('cmd')
     args = m.groupdict().get('args')
     
     # Poll plugins for command result
     resp = ext.notify(self, 'command', user = user, cmd = cmd, args = args)
     if resp:    return resp
         
     # Plugins didn't care so find a command and invoke it if present
     cmd_object = ext.get_command(cmd)
     if cmd_object:
         if callable(cmd_object):    
             try:                    resp = cmd_object(args)
             except Exception, e:    resp = type(e).__name__ + ": " + str(e)
             resp = [resp] # Since we expect response to be iterable
         else:
             return ["Invalid command '%s'; likely indicates faulty plugin" % cmd]
     else:
         # Check whether the command can be unambiguously resolved
         completions = ext._commands.search(cmd).keys()
         if len(completions) == 1:
             command = completions[0]
             if args:    command += " %s" % args
             return self._command(user, command)
         
         # Otherwise, suggest other variants
         suggestions = set()
         for i in range(1, len(cmd) + 1):
             completions = ext._commands.search(cmd[:i]).keys()
             suggestions = suggestions.union(set(completions))
             
         if len(suggestions) == 0:
             resp = ["Unrecognized command '%s'." % cmd]
         else:
             # If there are too many suggestions, filter them out
             MAX_SUGGESTIONS = 5
             suggestions = filter(None, suggestions)
             more = None
             if len(suggestions) > MAX_SUGGESTIONS:
                 more = len(suggestions) - MAX_SUGGESTIONS
                 suggestions = suggestions[:MAX_SUGGESTIONS]
             
             # Format them
             if config.cmd_prefix:
                 suggestions = map(lambda s: config.cmd_prefix + s, suggestions)
             suggestions = str.join(" ", suggestions)
             if more:    suggestions += " ... (%s more)" % more
             
             resp = ["Did you mean one of: %s ?" % suggestions]
                 
     return resp