此代码我没有收到任何错误,但是bot仍然不会告诉非管理员用户他们没有权限,它只会停留在终端上,这是我在做什么错了,bot不会这么说或者我需要进行更改以解决此问题,也使用discord.py rewrite
bans a user with a reason
@client.command()
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban (ctx, member:discord.User=None, reason =None):
adminroles = ("Keyblade Master","Foretellers")
try:
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
elif reason == None:
reason = "being a jerk!"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member)
await ctx.channel.send(f"{member} is banned!")
except commands.errors.MissingAnyRole(adminroles):
await ctx.channel.send("You do not have permission to do that!")
return
答案 0 :(得分:0)
在调用回调期间,在调用回调之前引发的错误(如CheckFailure
,UserInputError
及其派生类)需要由单独的error handler处理,因为失败实际上并没有在您的try
区块中。
@client.command(name="ban")
@commands.has_any_role("Keyblade Master","Foretellers")
async def ban_command(ctx, member:discord.User=None, reason =None):
adminroles = ("Keyblade Master","Foretellers")
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
elif reason == None:
reason = "being a jerk!"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member)
await ctx.send(f"{member} is banned!")
@ban_command.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingAnyRole):
await ctx.send("You do not have permission to do that!")
else:
raise error