python 在原始React添加时抛出错误:discord.py

nuypyhwy  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(107)

我正在为我的discord机器人编写一个“自我React”脚本,所以我使用on_reaction_add(),它工作得很好,但我意识到当我重新启动我的机器人时,他无法注册对第一条消息的React,所以我做了一些研究,我发现我必须使用on_raw_reaction_add()我尝试了一下,我工作得很好,但最后一行代码抛出了一个错误:

@Client.event
async def on_raw_reaction_add(payload):
    if payload.user_id == Client.user.id:
        return

    if str(payload.emoji) == "🍪" and payload.channel_id == 1078702317386524170:
        guild =Client.get_guild(1075533700804530239)
        role = guild.get_role(1078715150363795417)
        await discord.User(payload.user_id).add_roles(role)

错误:TypeError: BaseUser.__init__() takes 1 positional argument but 2 were given整个错误:here
谢谢!:)

t30tvxxf

t30tvxxf1#

问题是你正在尝试自己示例化一个discord.User,你已经得到了guild,让我们使用get_member来得到用户,这样我们就可以添加角色了。

@Client.event
async def on_raw_reaction_add(payload):
    if payload.user_id == Client.user.id:
        return

    if str(payload.emoji) == "🍪" and payload.channel_id == 1078702317386524170:
        guild = Client.get_guild(1075533700804530239)
        role = guild.get_role(1078715150363795417)
        user = guild.get_member(payload.user_id)
        if not user:
            # incase they are not cached
            user = await guild.fetch_member(payload.user_id)
        await user.add_roles(role)

相关问题