简体   繁体   English

“AttributeError: 'str' object has no attribute 'id'” discord.py 上的错误

[英]“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_rolesdiscord.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 passedpass_context重写中也不需要 pass_context kwarg,上下文总是通过

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 discord.py AttributeError: 'str' 对象没有属性 'id' - discord.py AttributeError: 'str' object has no attribute 'id' Discord.py“AttributeError:‘str’对象没有‘channel’属性” - Discord.py "AttributeError: 'str' object has no attribute 'channel'" Discord.py AttributeError: 'str' object 没有属性 'server' - Discord.py AttributeError: 'str' object has no attribute 'server' AttributeError: 'str' object 没有属性 'get' — Discord.py - AttributeError: 'str' object has no attribute 'get' — Discord.py 我收到 discord.py 错误,说 AttributeError: 'str' object has no attribute 'read' - I'm getting a discord.py error saying AttributeError: 'str' object has no attribute 'read' “ AttributeError: 'User' object 没有属性 'edit' ” discord.py 错误 - “ AttributeError: 'User' object has no attribute 'edit' ” discord.py error 收到错误:'str' object 没有属性 'url' discord.py - Getting the error: 'str' object has no attribute 'url' discord.py Discord.py 属性错误(&#39;dict&#39; 对象没有属性&#39;id&#39;) - Discord.py Attribute Error ('dict' object has no attribute 'id') 为什么我得到 discord.py AttributeError: 'str' object has no attribute 'trigger_typing' - Why am I getting discord.py AttributeError: 'str' object has no attribute 'trigger_typing' 错误 discord.py NoneType object 没有属性“id” - error with discord.py NoneType object has no attribute 'id'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM