我的代码有问题 命令引发异常:NameError:未定义名称'message'
async def delrole(ctx, *,role_name):
author = discord.Guild
server = message.guild
role = discord.utils.get(ctx.send_message.server.roles, name=role_name)
if role:
try:
await bot.delete_role(ctx.send_message.server, role)
await bot.say("The role {} has been deleted!".format(role.name))
except discord.Forbidden:
await bot.say("Missing Permissions to delete this role!")
else:
await bot.say("The role doesn't exist!")
答案 0 :(得分:0)
您可以使用role converter进行很多清理工作,尽管这需要将一些代码移至error handler
@bot.command()
async def delrole(ctx, *, role: discord.Role):
name = role.name
try:
await role.delete()
await ctx.send(f"The role {name} has been deleted")
except discord.Forbidden:
await ctx.send(f"I could not delete the role {name}")
@delrole.error
async def delrole_error(ctx, error):
if isinstance(error, commands.ConversionError):
await ctx.send("I do not recognize that role!")
else:
raise error