問題描述
現在我完成了我的審核命令 [大部分],我正在嘗試添加錯誤.我已經犯了請指定一個成員"錯誤,但我無法讓機器人說這個成員不存在"輸入無效名稱時.
Now that I finished my moderation commands [mostly], I am trying to add errors in. I already made the "please specify a member" error, but I cannot manage to make the bot say "this member does not exist" when an invalid name is input.
@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")
這是我的命令之一,一切正常.如果該成員顯然不存在,我想要一個顯示找不到用戶"的錯誤.例如,k!kick ijhguiserb
會讓機器人說未找到成員",而不是在 shell 中給我一個錯誤.
This is one of my commands, where everything is working. I would like an error which says "User not found" if the member, obviously, doesn't exist. For example, k!kick ijhguiserb
would make the bot say, "Member not found," rather than giving me an error in the shell.
我們將不勝感激,謝謝!
Help would be appreciated, thanks!
推薦答案
你必須定義一個 錯誤處理程序來處理ConversionError
You'll have to define an error handler to handle the 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
這篇關于discord.py 重寫 |為我的命令出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!