python discord.py React角色属性错误:“int”对象没有属性“guild”

f87krz0w  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(131)

抱歉,如果这是一个糟糕的问题,但我无法找到一个解决方案,这在任何地方,我已经尝试了两天,使React角色为我的机器人工作,它给出的输出:

AttributeError: 'int' object has no attribute 'guild'
@commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        reaction_message_id = 1067231301452701748
        user = payload.user_id
        user_reaction = str(payload.emoji)
        if reaction_message_id != payload.message_id:
            return`
        if user_reaction == '<a:rose-1:1066240347769942066>':
            role = discord.utils.get(user.guild.roles, name="Red")
            await user.add_roles(role)

这应该会改变我在React上的角色。我不确定我需要做什么才能让这条线工作

role = discord.utils.get(user.guild.roles, name="Red")
s3fp2yjn

s3fp2yjn1#

所以问题是user = payload.user_id,所以现在user是用户id,是int,后来当你试图得到user.guild时,用户是id(也是int),没有像guild这样的属性。
你做了什么:

@commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        reaction_message_id = 1067231301452701748
        user = payload.user_id # returns 1234567890
        user_reaction = str(payload.emoji)
        if reaction_message_id != payload.message_id:
            return`
        if user_reaction == '<a:rose-1:1066240347769942066>':
            role = discord.utils.get(user.guild.roles, name="Red") # here its the 123456789
            await user.add_roles(role)

正确的方法

@commands.Cog.listener()
    async def on_raw_reaction_add(self, payload):
        reaction_message_id = 1067231301452701748
        user = payload.member # here it returns memebr object 
        user_reaction = str(payload.emoji)
        if reaction_message_id != payload.message_id:
            return`
        if user_reaction == '<a:rose-1:1066240347769942066>':
            role = discord.utils.get(user.guild.roles, name="Red")
            await user.add_roles(role)

相关问题