我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用discord.ext.commands.has_permissions()。
def add_perms(self, ctx, *msg: str): """Sets up custom permissions on the provided command Format must be 'perms add <command> <permission>' If you want to open the command to everyone, provide 'none' as the permission EXAMPLE: !perms add skip ban_members RESULT: No more random people voting to skip a song""" # Since subcommands exist, base the last word in the list as the permission, and the rest of it as the command command = " ".join(msg[0:len(msg) - 1]) try: permissions = msg[len(msg) - 1] except IndexError: await self.bot.say("Please provide the permissions you want to setup, the format for this must be in:\n" "`perms add <command> <permission>`") return cmd = utils.find_command(self.bot, command) if cmd is None: await self.bot.say( "That command does not exist! You can't have custom permissions on a non-existant command....") return # If a user can run a command, they have to have send_messages permissions; so use this as the base if permissions.lower() == "none": permissions = "send_messages" # Convert the string to an int value of the permissions object, based on the required permission # If we hit an attribute error, that means the permission given was not correct perm_obj = discord.Permissions.none() try: setattr(perm_obj, permissions, True) except AttributeError: await self.bot.say("{} does not appear to be a valid permission! Valid permissions are: ```\n{}```" .format(permissions, "\n".join(valid_perms))) return perm_value = perm_obj.value # Two cases I use should never have custom permissions setup on them, is_owner for obvious reasons # The other case is if I'm using the default has_permissions case # Which means I do not want to check custom permissions at all # Currently the second case is only on adding and removing permissions, to avoid abuse on these for check in cmd.checks: if "is_owner" == check.__name__ or "has_permissions" in str(check): await self.bot.say("This command cannot have custom permissions setup!") return key = ctx.message.server.id entry = {'server_id': key, 'permissions': {cmd.qualified_name: perm_value}} if not await utils.update_content('server_settings', entry, key): await utils.add_content('server_settings', entry) await self.bot.say("I have just added your custom permissions; " "you now need to have `{}` permissions to use the command `{}`".format(permissions, command))