python - 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
但我确实有一个名为“已验证”的角色,我仔细检查了它,但它不起作用。
解决方案
根据文档, add_roles 不将成员 obj 作为参数。
代替await member.add_roles(member, role)
尝试await member.add_roles(role)
如果它仍然不起作用,请确保您的机器人具有 manage_roles 权限。
推荐阅读
- webpack - 压缩发生在 __WB_MANIFEST 注入之前
- android - 如何使用 fl_chart 显示工具提示指示器?
- java - 如何检查字符串是否仅包含运算符?
- python - 不可能在 Python 中请求 WCF,而 C# 可以
- matplotlib - 无法实现 matplotlib.pyplot.canvas.draw() 进行实时绘图
- google-chrome - DevTool,无法隐藏网络选项卡中的列
- javascript - MySQL JavaScript 查询用 0 替换数字的最后一位
- kendo-grid - 剑道网格中的 ClientTemplate 没有发生 DataBind
- azure - 使用 Set-AzureADApplicationProxyApplication 设置 ConnectorGroupId 的问题
- javascript - React Native(Android)未处理的承诺拒绝 - 网络请求