config.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:Excalibot 作者: endreman0 项目源码 文件源码
def permissions(self, ctx, target_name, command_name, allow: bool=None):
        """Sets permissions for a user, role, or everyone.
        You can mention a user or role, or use their name or ID, to set permissions for them.
        Use "everyone" to affect everyone's permissions in this guild.
        Allow can be true/false, yes/no, or on/off.
        """
        target = await self.find_target(ctx, target_name)
        if target is None:
            await ctx.send('BAKA! Target must be a member, role, or "everyone"!')

        command, command_name = self.find_command(ctx, command_name)
        if command is None:
            await ctx.send('BAKA! Command must be command name, category name, or "all"!')

        if allow is None: # Get
            allowed = config.allowed(ctx, user=target, command=command)
            perm_str = 'allowed' if allowed else 'not allowed'
        elif config.allowed(ctx, command=command): # Check the caller has permission to give/take this command
            with db.Session() as session:
                attrs = {'guild_id': ctx.guild.id, 'target_id': target.id, 'command_name': command_name}
                record = session.get(config.Permission, **attrs).one_or_none()
                if record is None:
                    record = session.add(config.Permission(**attrs, allowed=allow))
                else:
                    record.allowed = allow
            perm_str = 'now allowed' if allow else 'no longer allowed'
        else:
            return await ctx.send('BAKA! You can only control permissions for commands you can use!')

        if isinstance(target, discord.Member):
            target_str = target.name
        elif isinstance(target, discord.Role):
            target_str = 'The {} role'.format(target.name.capitalize())
        else:
            target_str = 'Everyone'

        if command_name == '_all':
            command_str = 'all commands'
        elif command_name.startswith('_'):
            command_str = 'the {} category'.format(command_name[1:].capitalize())
        else:
            command_str = 'the {} command'.format(command_name)

        await ctx.send('{0} is {1} to use {2}.'.format(target_str, perm_str, command_str))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号