[英]“AttributeError: 'str' object has no attribute 'id'” error on discord.py
I'm making a bot for my Discord server using discord.py.我正在使用 discord.py 为我的 Discord 服务器制作一个机器人。 I'm trying to make a command that gives a user a role named "Muted".我正在尝试创建一个命令,为用户提供一个名为“静音”的角色。 But when I try and mute the user (by sending "0mute @user#0000"), I get errors.但是当我尝试使用户静音时(通过发送“0mute @user#0000”),我得到了错误。 My code for giving the role is this:我赋予角色的代码是这样的:
@client.command(pass_context=True)
@commands.has_any_role("Admin")
async def mute(ctx, user: discord.Member):
await user.add_roles("Muted", atomic=False)
Note: I'm using regular discord.py, NOT discord.py rewrite.注意:我使用的是常规 discord.py,而不是discord.py 重写。
Edit: Recently found out I'm not allowed to post images of errors or code, as such I'll past the errors here as text instead of an image.编辑:最近发现我不允许发布错误或代码的图像,因此我将在此处将错误作为文本而不是图像传递。
Ignoring exception in command mute:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 43, in mute
await user.add_roles("Muted", atomic=False)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 669, in add_roles
new_roles = utils._unique(Object(id=r.id) for s in (self.roles[1:], roles) for r in s)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 287, in _unique
return [x for x in iterable if not (x in seen or adder(x))]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 287, in <listcomp>
return [x for x in iterable if not (x in seen or adder(x))]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 669, in <genexpr>
new_roles = utils._unique(Object(id=r.id) for s in (self.roles[1:], roles) for r in s)
AttributeError: 'str' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/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: AttributeError: 'str' object has no attribute 'id'
Member.add_roles
takes discord.Role
instances as the arguments, you're passing a string. Member.add_roles
将discord.Role
实例作为 arguments,您正在传递一个字符串。
@client.command()
@commands.has_any_role("Admin")
async def mute(ctx, user: discord.Member):
role = discord.utils.get(ctx.guild.roles, name="Muted") # Getting the role
await user.add_roles(role, atomic=False)
Also the pass_context
kwarg is not necessary in discord.py rewrite, the context is always passed在pass_context
重写中也不需要 pass_context kwarg,上下文总是通过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.