到目前为止,我已经收到一条消息,新用户可以在某个频道中做出不协调的反应,这将根据他们选择的反应为他们分配角色(此部分正在工作)。我还希望如果用户删除对该消息的响应,则从用户中删除该角色(这是行不通的)。我收到一条错误消息:on_raw_reaction_remove中的第23行 角色= discord.utils.get(payload.member.guild.roles,name ='War Thunder') AttributeError:“ NoneType”对象没有属性“行会”
@client.event
# this works to assign a role
async def on_raw_reaction_add(payload):
# channel and message IDs should be integer:
if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
if str(payload.emoji) == "<:WarThunder:745425772944162907>":
role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
await payload.member.add_roles(role)
# this doesn't work in removing the role
async def on_raw_reaction_remove(self, payload):
if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
if str(payload.emoji) != "<:WarThunder:745425772944162907>":
role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
await payload.member.remove_roles(role)
答案 0 :(得分:0)
正如documentation所说,payload.member
仅在event_type
为REACTION_ADD
时可用。因此,要获得公会,您必须使用payload.guild_id
和以下任何一种方式:
client.fetch_guild()
:
async def on_raw_reaction_remove(payload):
if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
if str(payload.emoji) == "<:WarThunder:745425772944162907>":
guild = await client.fetch_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name='War Thunder')
await payload.member.remove_roles(role)
discord.utils.get()
:
async def on_raw_reaction_remove(payload):
if payload.channel_id == 700895165665247325 and payload.message_id == 756577133165543555:
if str(payload.emoji) == "<:WarThunder:745425772944162907>":
guild = discord.utils.get(client.guilds, id=payload.guild_id)
role = discord.utils.get(guild.roles, name='War Thunder')
await payload.member.remove_roles(role)
PS:您不必每次都写discord.utils.get()
,而是可以在导入文件中写from discord.utils import get
并写get(iterable, **attrs)
。