首页 > 解决方案 > discord py“未知角色”

问题描述

我正在尝试创建一个命令,您必须使用它来验证并获得服务器上的角色。我在网上搜索,但找不到任何对我有帮助的东西。这是我的代码:

    @commands.command(name="verify")
    async def verify(self, ctx: commands.Context):
        failembed = discord.Embed(title="Verification failed", description="Took too long to verify",
                                  timestamp=datetime.utcnow(), colour=0x206694)
        verificationword = secrets.randbelow(1000000)
        verificationembed = discord.Embed(title="Verification", description=f"Type `" + str(
            verificationword) + "`to verify and get access to the server \nYou have  60sek",
                                          timestamp=datetime.utcnow(), colour=0x206694)
        verificationembed.set_footer(text="Made by Ruko",
                                     icon_url="https://cdn.discordapp.com/emojis/861768133149065217.png?v=1")
        verificationdoneembed = discord.Embed(title="Verfication accompplished!",
                                              description=f"You are now verified on the server and have access to the channels",
                                              timestamp=datetime.utcnow(),
                                              colour=0x206694)
        verificationdoneembed.set_footer(text="Made by Ruko",
                                         icon_url="https://cdn.discordapp.com/emojis/861768133149065217.png?v=1")
        message = await ctx.send(embed=verificationembed)
        check = lambda m: m.author == ctx.message.author and m.channel == ctx.channel
        member = ctx.message.author
        try:
            confirm = await self.bot.wait_for("message", check=check, timeout=60)
        except asyncio.TimeoutError:
            await message.edit(embed=failembed)
            return

        if confirm.content == str(verificationword):
            await message.edit(embed=verificationdoneembed)
            role = get(member.guild.roles, name="verified")
            await member.add_roles(member, role)
            return

        await message.edit(content="verification canceled")

但是当我运行命令时,它给了我这个错误:

  File "C:\Users\Laurenz\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Laurenz\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Laurenz\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

但我确实有一个名为“已验证”的角色,我仔细检查了它,但它不起作用。

标签: pythondiscorddiscord.py

解决方案


根据文档, add_roles 不将成员 obj 作为参数。

代替await member.add_roles(member, role)

尝试await member.add_roles(role)

如果它仍然不起作用,请确保您的机器人具有 manage_roles 权限。


推荐阅读