Exemplo n.º 1
0
    async def format_command_help(self, ctx, prefix, cmd):
        guild_config = await self.bot.db.get_guild_config(ctx.guild.id)
        cmd_level = get_command_level(cmd, guild_config)

        if isinstance(cmd, RainCommand):
            if await self.can_run(ctx, cmd) and cmd.enabled:
                em = discord.Embed(
                    title=prefix + cmd.signature,
                    description=f'{cmd.help}\n\nPermission level: {cmd_level}',
                    color=0x7289da)
                return em

        elif isinstance(cmd, RainGroup):
            em = discord.Embed(
                title=prefix + cmd.signature,
                description=f'{cmd.help}\n\nPermission level: {cmd_level}',
                color=0x7289da)
            subcommands = ''
            commands = []

            for i in cmd.commands:
                if await self.can_run(ctx, i):
                    commands.append(i)

            for i in commands:
                subcommands += f"`{i.name}` {i.short_doc}\n"

            em.add_field(name='Subcommands', value=subcommands)
            return em
Exemplo n.º 2
0
    async def setcommandlevel(self, ctx, perm_level: typing.Union[int, str], *,
                              command: lower):
        """Changes a command's required permission level

        Examples:
        - !!setcommandlevel reset ban
        - !!setcommandlevel 8 warn add
        """
        if isinstance(perm_level, int) and (perm_level < 0 or perm_level > 15):
            raise commands.BadArgument(
                f'{perm_level} is an invalid level, valid levels: 0-15 or reset'
            )

        if isinstance(perm_level, str) and perm_level != 'reset':
            raise commands.BadArgument(
                f'{perm_level} is an invalid level, valid levels: 0-15 or reset'
            )

        cmd = self.bot.get_command(command)
        if not cmd:
            raise commands.BadArgument(
                f'No command with name "{command}" found')

        if isinstance(cmd, RainGroup):
            raise commands.BadArgument('Cannot override a command group')

        name = cmd.qualified_name.replace(' ', '_')

        if perm_level == 'reset':
            perm_level = cmd.perm_level

        levels = [{'command': name, 'level': perm_level}]
        action = "pull" if perm_level == cmd.perm_level else "push"
        guild_config = await self.bot.db.get_guild_config(ctx.guild.id)

        if cmd.parent:
            parent_level = get_command_level(cmd.parent, guild_config)
            if perm_level < parent_level:
                levels.append({
                    'command': cmd.parent.name.replace(' ', '_'),
                    'level': perm_level
                })
            elif perm_level > parent_level:
                cmd_level = get_command_level(cmd, guild_config)
                all_levels = [
                    get_command_level(c, guild_config)
                    for c in cmd.parent.commands
                ]

                all_levels.remove(cmd_level)
                all_levels.append(perm_level)

                lowest = min(all_levels)
                if lowest > parent_level:
                    levels.append({
                        'command': cmd.parent.name.replace(' ', '_'),
                        'level': lowest
                    })

        if action == 'push':
            levels = {'$each': levels}
        elif action == 'pull':
            for i in levels:
                i['level'] = get_command_level(
                    self.bot.get_command(i['command']), guild_config)
            levels = {'$in': levels}

        await self.bot.db.update_guild_config(
            ctx.guild.id, {f'${action}': {
                'command_levels': levels
            }})

        await ctx.send(self.bot.accept)