コード例 #1
0
    def command_assert(client, arg, is_staff=None, is_mod=None, parameters=None,
                       split_spaces=None, split_commas=False):
        """
        Kept for backwards compatibility. Use assert_command.
        """
        message = ('Code is using old command_assert syntax. Please change it (or ask your server '
                   'developer) so that it uses Constants.assert_command instead.')
        warnings.warn(message, category=UserWarning, stacklevel=2)

        if is_staff is not None:
            if is_staff is True and not client.is_staff():
                raise ClientError.UnauthorizedError('You must be authorized to do that.')
            if is_staff is False and client.is_staff():
                raise ClientError.UnauthorizedError('You have too high a rank to do that.')

        if is_mod is not None:
            if is_mod is True and not client.is_mod:
                raise ClientError.UnauthorizedError('You must be authorized to do that.')
            if is_mod is False and client.is_mod():
                raise ClientError.UnauthorizedError('You have too high a rank to do that.')

        if parameters is not None:
            symbol, num = parameters[0], [int(i) for i in parameters[1:].split('-')]
            # Set up default values
            if (num[0] > 0 or symbol == '&') and split_spaces is None and split_commas is False:
                split_spaces = True
            elif split_spaces is None:
                split_spaces = False

            if split_spaces:
                arg = arg.split(' ')
            elif split_commas:
                arg = arg.split(', ')

            if arg == ['']:
                arg = list()

            error = None
            if symbol == '=':
                expect = num[0]
                if len(arg) != expect:
                    if expect == 0:
                        expect = 'no'
                    error = ('This command has {} argument{}.', expect)
            elif symbol == '<':
                expect = num[0] - 1
                if len(arg) > expect:
                    error = ('This command has at most {} argument{}.', expect)
            elif symbol == '>':
                expect = num[0] + 1
                if len(arg) < expect:
                    error = ('This command has at least {} argument{}.', expect)
            elif symbol == '&':
                expect = num
                if not expect[0] <= len(arg) <= expect[1]:
                    expect = '{} to {}'.format(expect[0], expect[1])
                    error = ('This command has from {} argument{}.', expect)

            if error:
                raise ArgumentError(error[0].format(error[1], 's' if error[1] != 1 else ''))
コード例 #2
0
    def assert_command(client, arg, is_staff=None, is_officer=None, is_mod=None, parameters=None,
                       split_spaces=None, split_commas=False):
        if is_staff is not None:
            if is_staff is True and not client.is_staff():
                raise ClientError.UnauthorizedError('You must be authorized to do that.')
            if is_staff is False and client.is_staff():
                raise ClientError.UnauthorizedError('You have too high a rank to do that.')

        if is_officer is not None:
            if is_officer is True and not (client.is_mod or client.is_cm):
                raise ClientError.UnauthorizedError('You must be authorized to do that.')
            if is_officer is False and (client.is_mod or client.is_cm):
                raise ClientError.UnauthorizedError('You have too high a rank to do that.')

        if is_mod is not None:
            if is_mod is True and not client.is_mod:
                raise ClientError.UnauthorizedError('You must be authorized to do that.')
            if is_mod is False and client.is_mod():
                raise ClientError.UnauthorizedError('You have too high a rank to do that.')

        if parameters is not None:
            symbol, num = parameters[0], [int(i) for i in parameters[1:].split('-')]
            # Set up default values
            if (num[0] > 0 or symbol == '&') and split_spaces is None and split_commas is False:
                split_spaces = True
            elif split_spaces is None:
                split_spaces = False

            if split_spaces:
                arg = arg.split(' ')
            elif split_commas:
                arg = arg.split(', ')

            if arg == ['']:
                arg = list()

            error = None
            if symbol == '=':
                expect = num[0]
                if len(arg) != expect:
                    if expect == 0:
                        expect = 'no'
                    error = ('This command has {} argument{}.', expect)
            elif symbol == '<':
                expect = num[0] - 1
                if len(arg) > expect:
                    error = ('This command has at most {} argument{}.', expect)
            elif symbol == '>':
                expect = num[0] + 1
                if len(arg) < expect:
                    error = ('This command has at least {} argument{}.', expect)
            elif symbol == '&':
                expect = num
                if not expect[0] <= len(arg) <= expect[1]:
                    expect = '{} to {}'.format(expect[0], expect[1])
                    error = ('This command has from {} argument{}.', expect)

            if error:
                raise ArgumentError(error[0].format(error[1], 's' if error[1] != 1 else ''))