我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用discord.ext.commands.CommandError()。
def skip(self, ctx, force: str = None): # if the skip is forced, check the privilege and do it if force and force.lower() in ['f', 'force']: if not self._bot.is_operator(ctx.message.author): raise dec.CommandError('You don\'t have a permission to force the skip') await self._bot.player.force_skip() await self._bot.message('Skip forced by {}'.format(ctx.message.author.mention)) return # if the argument is not none, raise an error if force is not None: raise dec.UserInputError('*force* is the only argument allowed for the *skip* command') # now do the "normal voting" await self._bot.player.skip_vote(int(ctx.message.author.id)) await self._bot.log('User {} has voted to skip'.format(ctx.message.author))
def member_by_substring(ctx: commands.Context, substring: str): """This searches for a member by substrings.""" try: return await memberconverter.convert(ctx, substring) except commands.CommandError: pass substring = substring.lower() for member in ctx.guild.members: if substring in member.name.lower() or substring in member.display_name.lower(): return member raise commands.BadArgument(f"No user with substring `{substring}` was found.")
def role_by_substring(ctx: commands.Context, substring: str): """This searches for a role by substrings.""" try: return await roleconverter.convert(ctx, substring) except commands.CommandError: pass substring = substring.lower() for role in ctx.guild.roles: if substring in role.name.lower(): return role raise commands.BadArgument(f"No role with substring `{substring}` was found.")
def _can_run(command, ctx): try: return await command.can_run(ctx) except commands.CommandError: return False
def _command_check(self, ctx): # if the channel is not private, delete the command immediately regardless of the response if not isinstance(ctx.message.channel, discord.PrivateChannel): self._bot.loop.create_task(self._bot.client.delete_message(ctx.message)) # if privileged, check the member role if hasattr(ctx.command, 'privileged'): if not self._bot.is_operator(ctx.message.author): raise dec.CommandError('You don\'t have a permission to use the *{}* command'.format(ctx.command)) return True # # Listeners #
def role_or_permissions(ctx, check, **perms): if check_permissions(ctx, perms): return True ch = ctx.message.channel author = ctx.message.author if isinstance(ch, (discord.DMChannel, discord.GroupChannel)): return False # can't have roles in PMs role = discord.utils.find(check, author.roles) if role is None: raise commands.CommandError("You need a special role to do this! (A discord role with the name \"Bot Mod\" or \"Bot Admin\")") return True
def filter_command_list(self, command): """Returns a filtered list of commands based on the two attributes provided, :attr:`show_check_failure` and :attr:`show_hidden`. Also filters based on if :meth:`is_cog` is valid. Returns -------- iterable An iterable with the filter being applied. The resulting value is a (key, value) tuple of the command name and the command itself. """ def sane_no_suspension_point_predicate(tup): cmd = tup[1] if self.is_cog(command): # filter commands that don't exist to this cog. if cmd.instance is not command: return False if cmd.hidden and not self.show_hidden: return False return True async def predicate(tup): if sane_no_suspension_point_predicate(tup) is False: return False cmd = tup[1] try: return await cmd.can_run(self.context) except commands.CommandError: return False iterator = command.all_commands.items() if not self.is_cog(command) else self.context.bot.all_commands.items() if self.show_check_failure: return filter(sane_no_suspension_point_predicate, iterator) # Gotta run every check and verify it ret = [] for elem in iterator: valid = await predicate(elem) if valid: ret.append(elem) return ret