discord.py on_raw_reaction_remove(payload) 将 user_id 传递为 none

Posted

技术标签:

【中文标题】discord.py on_raw_reaction_remove(payload) 将 user_id 传递为 none【英文标题】:discord.py on_raw_reaction_remove(payload) passing user_id as none 【发布时间】:2021-01-16 14:53:09 【问题描述】:

我编写了一个脚本,用于在服务器成员使用某个表情符号做出反应时分配角色,但在 Heroku 或 ibm cloud 中托管代码时,user_id 的值没有。

特别是,当使用来自on_raw_reaction_remove() 的有效负载时,member = guild.get_member(payload.user_id) 似乎没有为成员变量赋值。

使用on_raw_reaction_add() 添加角色按预期工作。

#import discord lib
import discord
#import commands from discord lib
from discord.ext import commands

#set client variable to use disocrd commands
client = commands.Bot(command_prefix = ".")

#start event and check Bot is ready
@client.event
async def on_ready():
    print("Bot is ready.")

#upon detecting a reaction event assign the role which matches the reaction emoji
#see https://discordpy.readthedocs.io/en/latest/api.html?#rawreactionactionevent
@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id

    #check if the message id matches the message id of the "assign your role!" message in discord
    if message_id == 123456789:
        #get the server name from the payload
        guild = client.get_guild(payload.guild_id)
        #get the member name from the payload
        member=(payload.member)

        #Debugging
        Post_Message = "Add(1/3) - Message ID matches."
        Post_Message = Post_Message+"'"+str(guild)+"'"+" is the Guild ID."
        Post_Message = Post_Message+"'"+str(member)+"'"+" is the Member ID."
        print(str(Post_Message))

        #if the name of the reaction emoji is "League" theen assign the "League bois discord role
        if payload.emoji.name == "League":
            role = discord.utils.get(guild.roles, name="League bois")
        else:
        #set role variable to the name of the reaction emoji
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            
            #debugging
            print("Add(2/3) - Attempting to assign Role...")
            
            if member is not None:
                #add the role
                await member.add_roles(role)
                #Debugging
                print("Add(3/3) - Role has been assigned.")
            else:
                print("Member not found.")
                print("Member returned is: "+str(member))
        else:
            print("Role not found.")
            print("Role Returned is: "+str(role))

但在 Heroku 或 ibmcloud 中托管时,使用 on_raw_reaction_remove() 删除角色不起作用。在本地运行时成功。

#upon detecting a a reaction emoji being removed, remove the appropriate role on discord
@client.event
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id

    # check if the message id matches the message id of the "assign your role!" message in discord
    if message_id == 123456789:
        #get the server name from the payload
        guild = client.get_guild(payload.guild_id)
        # payload.member is not availible for REACTION_REMOVE event type
        member = guild.get_member(payload.user_id)

        #Debugging
        Post_Message = "Remove(1/3) - Message ID matches."
        Post_Message = Post_Message+"'"+str(guild)+"'"+" is the Guild ID."
        Post_Message = Post_Message+"'"+str(member)+"'"+" is the Member ID."
        print(str(Post_Message))

        # if the name of the reaction emoji is "League" theen assign the "League bois discord role
        if payload.emoji.name == "League":
            role = discord.utils.get(guild.roles, name="League bois")

        else:
            # set role variable to the name of the reaction emoji
            role = discord.utils.get(guild.roles, name=payload.emoji.name)

        if role is not None:
            
                    
            #Debugging
            print("Remove(2/3) - Attempting to remove Role...")
            

            if member is not None:
                #remove the role
                await member.remove_roles(role)
                #Debugging
                print("Remove(3/3) - Role has been removed.")
            else:
                print("Member not found.")
                print("Member returned is: "+str(member))
        else:
            print("Role not found.")
            print("Role Returned is: "+str(role))

#bot token goes here
client.run("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

如果我添加然后删除一个反应,我会在本地得到以下内容

Add(1/3) - Message ID matches.'test big XD haha lol' is the Guild ID.'daz#6949' is the Member ID.
Add(2/3) - Attempting to assign Role...
Add(3/3) - Role has been assigned.
Remove(1/3) - Message ID matches.'test big XD haha lol' is the Guild ID.'daz#6949' is the Member ID.
Remove(2/3) - Attempting to remove Role...
Remove(3/3) - Role has been removed.

但这在 IBM Cloud 和 Heroku 中

APP/PROC/WEB    0   Add(1/3) - Message ID matches.'test server' is the Guild ID.'daz#6949' is the Member ID.    Sep 30, 2020, 9:55:07 PM
APP/PROC/WEB    0   Add(2/3) - Attempting to assign Role... Sep 30, 2020, 9:55:07 PM
APP/PROC/WEB    0   Add(3/3) - Role has been assigned.  Sep 30, 2020, 9:55:07 PM
APP/PROC/WEB    0   Remove(1/3) - Message ID matches.'test server' is the Guild ID.'None' is the Member ID. Sep 30, 2020, 9:55:08 PM
APP/PROC/WEB    0   Remove(2/3) - Attempting to remove Role...  Sep 30, 2020, 9:55:08 PM
APP/PROC/WEB    0   Member not found.   Sep 30, 2020, 9:55:08 PM
APP/PROC/WEB    0   Member returned is: None    Sep 30, 2020, 9:55:08 PM

有什么想法吗?

【问题讨论】:

你在使用 discord.py 1.5.0 吗? 我打赌你在本地安装了 1.4.x,heroku 正在拉取最新版本,1.5。您需要将 discord.py 冻结到 1.4.2 版本,或者如果您想使用 1.5,请参阅我在网关意图上的回答 here。 @derw 你是 100% 正确的。我在本地安装了 1.4.1。安装 1.5.0 并从 Heroku/IBM 复制结果。在您的链接中遵循您的答案,现在正在工作。谢谢! 【参考方案1】:

derw 是正确的。

我在本地安装了 discord.py 1.4.1。 跑py -3 -m pip install -U discord.py 并安装了1.5.0。安装后,我的结果在本地和远程(IBM/Heroku)之间是一致的。 按照 derw 的回答 here 并在 Discord 开发者门户上启用特权网关意图(Presence IntentServer Members Intent),然后替换它

client = commands.Bot(command_prefix = ".")

有了这个

intents = discord.Intents.all()
client = discord.Client(intents=intents)

谢谢你!

【讨论】:

以上是关于discord.py on_raw_reaction_remove(payload) 将 user_id 传递为 none的主要内容,如果未能解决你的问题,请参考以下文章

Discord嵌入图像在discord.py中不起作用

discord.Embed 不被视为嵌入 discord.py

Discord bot 添加对消息 discord.py 的反应(无自定义表情符号)

Discord 机器人帮助命令 [discord.py]

discord.py,同时使用斜杠命令和前缀

Discord 音乐机器人 - 队列 (discord.py)