添加异常以告诉频道中的用户他们没有权限
Adding an exception to tell the user in the channel they dont have permission
我没有收到此代码的任何错误,但 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
在命令调用期间引发的错误,在调用回调之前(如 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
我没有收到此代码的任何错误,但 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
在命令调用期间引发的错误,在调用回调之前(如 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