在下面的代码中,每当他们对我的消息作出反应时,我试图给一个人一个角色,但这段代码给我一个错误,说'int' object has no attribute 'id'
代码说问题是这个代码:await user.add_roles(user.guild.id, user.id, role, reason='reason')
我如何解决这个问题?
import discord
from discord.ext import commands
import random
client = discord.Client()
@client.event
async def on_ready():
print('ready')
@client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.guild.roles, name = 'Test_Bot')
await user.add_roles(user.guild.id, user.id, role, reason='reason')
client.run('TOKEN')
正如Dominik所说,如果你说await user.add_roles
,你不需要user.guild.id, user.id
。发生的事情是,它试图使用你传递的user.id
(一个整数),而不是你在调用函数时使用的用户对象:await user.add_roles(...)
.
所以,去掉await user.add_roles(user.guild.id, user.id, role, reason='reason')
,改成:await user.add_roles(role)
.
固定代码应该是:
import discord
from discord.ext import commands
import random
client = discord.Client()
@client.event
async def on_ready():
print('ready')
@client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.guild.roles, name = 'Test_Bot')
await user.add_roles(role, reason='reason')
client.run('TOKEN')
当您使用add_roles()
时,TL;DR: Discord.py需要完整的成员对象。你给它成员ID的整数。