我试图通过吐出You do not have the permissions {}
让我的机器人告诉用户是否有错误但是当我尝试使用此代码时:
@client.command(pass_context = True)
async def ban(ctx, member : discord.Member, *, content: str):
if ctx.message.author == client.user:
return
if ctx.message.author.server_permissions.administrator:
msg = (str(member) + "has been banned for" + str(content)).format(ctx.message)
await client.send_message(member, content)
await client.ban(member)
await client.send_message(ctx.message.channel, msg)
@ban.error
async def ban_error(error, ctx):
if isinstance(error, CheckFailure):
msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)
await client.send_message(ctx.message.channel, msg)
discord bot dms用户并且python控制台中没有错误,如果我删除@ban.error
片段,我得到的权限错误太低。
如果CheckFailure
失败,check只会被提升。您的代码中没有检查,因此这种情况永远不会发生。您可以使用if
将commands.has_permission
语句转换为非常简单的检查:
@client.command(pass_context = True)
@has_permissions(administrator=True)
async def ban(ctx, member : discord.Member, *, content: str):
msg = "{} has been banned for {}".format(ctx.message.author.mention, content)
await client.send_message(member, content)
await client.ban(member)
await client.send_message(ctx.message.channel, msg)
情况可能是错误处理程序正在抑制有价值的错误信息。我们可以调用内置错误处理的机器人来处理任何其他不是CheckFailure
s的错误
from discord.ext.commands import CheckFailure
from discord import Forbidden
@ban.error
async def ban_error(error, ctx):
if isinstance(error, CheckFailure):
msg = "Sorry but you do not have the permissions {}".format(ctx.message.author.mention)
await client.send_message(ctx.message.channel, msg)
elif isinstance(error, Forbidden):
await client.send_message(ctx.message.channel, "I do not have the correct permissions")
else:
print(error)
await client.on_command_error(error, ctx)