discord.py on_raw_reaction_remove(payload)将user_id传递为none [英] discord.py on_raw_reaction_remove(payload) passing user_id as none
问题描述
当服务器成员使用某种表情符号做出反应时,我编写了一个脚本来分配角色,但是在Heroku或ibm云中托管代码时,user_id的值都不是。
I've written a script to assign roles when a server member reacts using a certain emoji, but when hosting the code in Heroku or ibm cloud none is the value for user_id.
单独地,当使用 on_raw_reaction_remove()的有效负载时,似乎
member = guild.get_member(payload.user_id)
没有将任何内容分配给成员变量code>。
Sepcifically, it seems like member = guild.get_member(payload.user_id)
is assigning none into member variable when using the payload from on_raw_reaction_remove()
.
使用 on_raw_reaction_add()
添加角色可以正常工作。
Adding a role using on_raw_reaction_add()
works as expected.
#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))
但是使用<$ c $删除角色c> on_raw_reaction_remove()在Heroku或ibmcloud中托管时不起作用。在本地运行时,它可以成功运行。
But removing a role using on_raw_reaction_remove()
doesn't work when hosted in Heroku or ibmcloud. It works successfully when ran locally.
#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")
如果我添加然后删除了一个反应,我会在本地得到以下信息
If I add and then remove a reaction I get the following locally
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中
but this in IBM Cloud and 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
有什么想法吗?
推荐答案
derw是正确的。
我在本地安装了discord.py 1.4.1。
Ran py -3 -m pip install -U discord.py
并安装1.5.0。安装后,我的结果在本地和远程(IBM / Heroku)之间是一致的。
按照derw的回答此处,并在Discord开发人员门户上启用特权网关意图(存在意图和服务器成员意图),然后替换此
I had discord.py 1.4.1 installed locally.
Ran py -3 -m pip install -U discord.py
and installed 1.5.0. After install my results were consistent between local and remote (IBM/Heroku).
Followed derw's answer here and enabled Privileged Gateway Intents on the Discord Developer Portal (Presence Intent and Server Members Intent), then replaced this
client = commands.Bot(command_prefix = ".")
使用此
intents = discord.Intents.all()
client = discord.Client(intents=intents)
感谢derw!
这篇关于discord.py on_raw_reaction_remove(payload)将user_id传递为none的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!