gpt4 book ai didi

python - discord.py等待 “AttributeError: '列表的对象在使用add_roles时没有属性 'id'”

转载 作者:行者123 更新时间:2023-12-03 07:56:04 26 4
gpt4 key购买 nike

我试图使用discord.py创建一个机器人,并试图使用discord.ext命令使用add_roles成员函数。
这是我的代码:

import os

import discord
from dotenv import load_dotenv

from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)


@client.event


async def on_ready():
guild = discord.utils.get(client.guilds, name=GUILD)

print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name} (id: {guild.id})'
)
members = guild.members
for member in members:
role = discord.utils.get(member.guild.roles, name='test 2')
await member.add_roles([role],False)
当它调用“await member.add_roles([role],False)”时,会出现以下错误:
    Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/Users/tanay/Documents/Python CYOA/bot.py", line 31, in on_ready
await member.add_roles([role],False)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/discord/member.py", line 676, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'list' object has no attribute 'id'
我一直在进行大量互联网搜索,但是找不到解决方法。有人知道我做错了吗?
非常感谢!

最佳答案

添加一个角色时,不必将其放在[]中。这就是为什么您会收到此错误。因此,您只需要卸下括号。

...
for member in members:
role = discord.utils.get(member.guild.roles, name='test 2')
await member.add_roles(role)

关于python - discord.py等待 “AttributeError: '列表的对象在使用add_roles时没有属性 'id'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65706563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com