Пример #1
0
    def test_command_grouping_greadyness(self):
        plugin = Object()
        plugin.bot = self.bot

        self.bot._commands = [
            Command(plugin, None, 'a', group='test'),
            Command(plugin, None, 'b', group='test')
        ]

        self.bot.recompute()
        self.assertNotEqual(self.bot.command_matches_re.match('test a'), None)
        self.assertNotEqual(self.bot.command_matches_re.match('te a'), None)
        self.assertNotEqual(self.bot.command_matches_re.match('t b'), None)
        self.assertEqual(self.bot.command_matches_re.match('testing b'), None)
        self.assertEqual(self.bot.command_matches_re.match('testlmao a'), None)
Пример #2
0
    def test_many_commands(self):
        self.bot._commands = [
            Command(None, None, 'test{}'.format(i), '<test:str>')
            for i in range(1000)
        ]

        self.bot.compute_command_matches_re()
        match = self.bot.command_matches_re.match('test5 123')
        self.assertNotEqual(match, None)

        match = self.bot._commands[0].compiled_regex.match('test0 123 456')
        self.assertEqual(match.group(1).strip(), 'test0')
        self.assertEqual(match.group(2).strip(), '123 456')
Пример #3
0
    def register_command(self, func, *args, **kwargs):
        """
        Registers a command.

        Parameters
        ----------
        func : function
            The function to be registered.
        args
            Arguments to pass onto the :class:`disco.bot.command.Command` object.
        kwargs
            Keyword arguments to pass onto the :class:`disco.bot.command.Command`
            object.
        """
        self.commands.append(Command(self, func, *args, **kwargs))
Пример #4
0
    def test_group_and_command(self):
        plugin = Object()
        plugin.bot = self.bot

        self.bot._commands = [
            Command(plugin, None, 'test'),
            Command(plugin, None, 'a', group='test'),
            Command(plugin, None, 'b', group='test'),
        ]

        self.bot.recompute()

        msg = Object()

        msg.content = '!test a'
        commands = list(
            self.bot.get_commands_for_message(False, None, '!', msg))
        self.assertEqual(commands[0][0], self.bot._commands[1])
        self.assertEqual(commands[1][0], self.bot._commands[0])

        msg.content = '!test'
        commands = list(
            self.bot.get_commands_for_message(False, None, '!', msg))
        self.assertEqual(commands[0][0], self.bot._commands[0])
Пример #5
0
    def register_command(self, func, *args, **kwargs):
        """
        Registers a command.

        Parameters
        ----------
        func : function
            The function to be registered.
        args
            Arguments to pass onto the :class:`disco.bot.command.Command` object.
        kwargs
            Keyword arguments to pass onto the :class:`disco.bot.command.Command`
            object.
        """
        if kwargs.pop('update', False) and func.__name__ in self.commands:
            self.commands[func.__name__].update(*args, **kwargs)
        else:
            wrapped = functools.partial(self._dispatch, 'command', func)
            self.commands[func.__name__] = Command(self, wrapped, *args,
                                                   **kwargs)
Пример #6
0
 def deco(func):
     if kwargs.pop('admin', False) is True:
         kwargs['level'] = -1
     func._cmd_obj = Command(func, *args, **kwargs)
     return func