discord.py 重写 |为我的命令犯错误
discord.py rewrite | Making errors for my commands
现在我 [大部分] 完成了我的审核命令,我正在尝试添加错误。我已经犯了 "please specify a member" 错误,但我无法让机器人说 "this member does not exist"输入的名称无效。
@client.command(name='kick',
brief='Kicks user',
aliases=['Kick'],
pass_context=True)
async def kick(context, member:discord.Member=None):
# Errors
if not member:
await context.send('Please specify a member.')
return
# Actual Kicking
if context.author.guild_permissions.kick_members == True:
await member.kick()
await context.send(f"{member.mention} was kicked ")
else:
await context.send(context.message.author.mention + ", you don't have permission")
这是我的命令之一,一切正常。如果该成员显然不存在,我想要一个显示 "User not found" 的错误。例如,k!kick ijhguiserb
会让机器人说 "Member not found," 而不是在 shell.
中给我一个错误
不胜感激,谢谢!
您必须定义一个 error handler 来处理 ConversionError
from discord.ext.commands import ConversionError
@kick.error
async def kick_error(ctx, error):
if isinstance(error, (ConversionError, BadArgument)):
await ctx.send("Member not found")
else:
raise error
现在我 [大部分] 完成了我的审核命令,我正在尝试添加错误。我已经犯了 "please specify a member" 错误,但我无法让机器人说 "this member does not exist"输入的名称无效。
@client.command(name='kick',
brief='Kicks user',
aliases=['Kick'],
pass_context=True)
async def kick(context, member:discord.Member=None):
# Errors
if not member:
await context.send('Please specify a member.')
return
# Actual Kicking
if context.author.guild_permissions.kick_members == True:
await member.kick()
await context.send(f"{member.mention} was kicked ")
else:
await context.send(context.message.author.mention + ", you don't have permission")
这是我的命令之一,一切正常。如果该成员显然不存在,我想要一个显示 "User not found" 的错误。例如,k!kick ijhguiserb
会让机器人说 "Member not found," 而不是在 shell.
不胜感激,谢谢!
您必须定义一个 error handler 来处理 ConversionError
from discord.ext.commands import ConversionError
@kick.error
async def kick_error(ctx, error):
if isinstance(error, (ConversionError, BadArgument)):
await ctx.send("Member not found")
else:
raise error