我的 Python 代码:
@bot.command(aliases = ["crole","cr"])
@commands.has_permissions(manage_roles = True)
async def createrole(ctx, *, name):
guild = ctx.guild
await guild. create_role(name=name)
await ctx.send(f'Role `{name}` has been created')
#create role error
@createrole.error
async def createrole_error(self, ctx, error):
if isinstance(error, MissingRequiredArgument):
createrole= discord.Embed(
title = f'Command: {prefix}createrole',
colour = discord.Colour.from_rgb(0,0,0)
)
createrole.add_field(name= 'Description:', value= 'Create a Role',inline=False)
createrole.add_field(name= 'Usage:', value = f'`{prefix}createrole [rolename] [color]`',inline=False)
createrole.add_field(name='Example:', value= f'`{prefix}createrole @parth normal`',inline=False)
await ctx.send(f"{createrole}")
elif isinstance(error,commands.BotMissingPermissions):
permission_error = discord.Embed(description = f"❌ I Am Missing **MANAGE ROLES** Permisson"
,colour = discord.Colour.from_rgb(0,0,0))
await ctx.send(embed = permission_error)
通过发送不完整的命令获取错误 >createrole 以检查我的 MissingRequiredArgument 消息是否已发送但收到此错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 847, in invoke
await self.prepare(ctx)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 784, in prepare
await self._parse_arguments(ctx)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 699, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 535, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: name is a required argument that is missing.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 71, in wrapped
ret = await coro(*args, **kwargs)
TypeError: createrole_error() missing 1 required positional argument: 'error'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Mridul Folder\python\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\bot.py", line 943, in on_message
await self.process_commands(message)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\bot.py", line 940, in process_commands
await self.invoke(ctx)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\bot.py", line 907, in invoke
await ctx.command.dispatch_error(ctx, exc)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 417, in dispatch_error
await injected(ctx, error)
File "D:\Mridul Folder\python\lib\site-packages\discord\ext\commands\core.py", line 77, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: createrole_error() missing 1 required positional argument: 'error'