def bot_check(self, ctx): if isinstance(ctx.command, Group) or not ctx.guild: return True member = get(ctx.guild.members, id=ctx.author.id) if member.guild_permissions.administrator: return True allowed = False session = Session() obj = to_sql(session, member, Member) data = self.get_data(obj, DEFAULT_ROLE_USER_DATA) attributes = [] if ctx.cog: attributes.append(ctx.cog.qualified_name.lower()) if ctx.invoked_subcommand: attributes.append(ctx.invoked_subcommand.parent.name.lower()) attributes.append(ctx.command.name.lower()) permission = '.'.join(attributes) if not any([node == permission or permission.startswith(node) and permission[len(node)] == '.' for node in data]): for role in member.roles: obj = to_sql(session, role, Role) data = self.get_data(obj, DEFAULT_ROLE_USER_DATA) if any([node == permission or permission.startswith(node) and permission[len(node)] == '.' for node in data]): allowed = True break else: allowed = True session.close() if not allowed: raise MissingPermissions() return True
async def add(self, ctx, target: typing.Union[DiscordRole, DiscordMember], permission: str): session = Session() if isinstance(target, DiscordMember): obj = to_sql(session, target, Member) target_type = 'Member' else: obj = to_sql(session, target, Role) target_type = 'Role' data = self.get_data(obj, DEFAULT_ROLE_USER_DATA) if permission in data: session.close() raise CommandError('{} `{}` already has permission `{}`'.format(target_type, str(target), permission)) else: data.append(permission) self.set_data(session, obj, data) embed = generate_embed_template(ctx, 'Permissions Binding Added Successfully') set_permissions_embed(embed, target_type, target, permission) session.close() await ctx.send(embed=embed)
async def reset(self, ctx): session = Session() obj = to_sql(session, ctx.guild, Guild) for member in obj.members: self.set_data(session, member, DEFAULT_ROLE_USER_DATA) for role in obj.roles: self.set_data(session, role, DEFAULT_ROLE_USER_DATA) embed = generate_embed_template(ctx, 'Permissions Bindings Reset Successfully') embed.description = 'You may now set up your permissions again.' await ctx.send(embed=embed)
async def remove(self, ctx, target: typing.Union[DiscordMember, DiscordRole], permission: str): session = Session() if isinstance(target, DiscordMember): obj = to_sql(session, target, Member) target_type = 'Member' else: obj = to_sql(session, target, Role) target_type = 'Role' data = self.get_data(obj, DEFAULT_ROLE_USER_DATA) if permission in data: data.remove(permission) self.set_data(session, obj, data) embed = generate_embed_template(ctx, 'Permissions Binding Removed Successfully') set_permissions_embed(embed, target_type, target, permission) session.close() await ctx.send(embed=embed) else: session.close() raise CommandError('Could not find {} `{}` with permission `{}`'.format(target_type.lower(), str(target), permission))
async def allow(self, ctx, role: DiscordRole): session = Session() obj = to_sql(session, role, Role) data = self.get_data(obj, DEFAULT_ROLE_DATA) if data: session.close() raise CommandError('Role `{}` is already allowed'.format( str(role))) self.set_data(session, obj, True) session.close() embed = generate_embed_template(ctx, 'Role Allowed Successfully') embed.description = str(role) await ctx.send(embed=embed)
async def add(self, ctx, role: DiscordRole): session = Session() obj = to_sql(session, role, Role) data = self.get_data(obj, DEFAULT_ROLE_DATA) session.close() if data: if role in ctx.author.roles: raise CommandError('You already have the role `{}`'.format( str(role))) await ctx.author.add_roles(role, reason='Added via command') embed = generate_embed_template(ctx, 'Role Add Successfully') embed.description = str(role) await ctx.send(embed=embed) return raise CommandError('You are not allowed to add role {}'.format( str(role)))