Discord错误:命令引发了异常:NameError:未定义名称“消息”

时间:2019-07-19 06:37:11

标签: python discord.py

我的代码有问题 命令引发异常: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!")

1 个答案:

答案 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