Discord.py反应角色自定义表情符号



我一直在为反应角色开发一个不和聊天机器人。这个想法是它注册消息,角色和表情符号到一个。json文件,并在on_raw_reaction_removeon_raw_reaction_add上执行角色更新

现在,问题是它确实将自定义反应添加到指定消息中。然而,它并没有在反动添加上给予应有的作用。我找不到解决这个问题的方法。我的观点是,问题可能是机器人注册自定义表情符号的方式。下面是一个例子:

[
{
"role_id": 897227510574616656,
"emoji": "ud83dude04",
"message_id": "898215881136570418"
},
{
"role_id": 897227510574616656,
"emoji": "<:custom:898198216611344424>",
"message_id": "898215881136570418"
}
]
@client.event
async def on_raw_reaction_add(payload):
if payload.member.bot:
pass
else:
with open('data.json') as react_file:
data = json.load(react_file)
for x in data:
if x['emoji'] == payload.emoji.name:
role = discord.utils.get(client.get_guild(
payload.guild_id).roles, id=x['role_id'])
await payload.member.add_roles(role)
@client.command()
@commands.has_permissions(administrator=True, manage_roles=True)
async def add(ctx, msg_id, emoji, role: discord.Role):
msg= await ctx.fetch_message(msg_id)
await msg.add_reaction(emoji)
with open('data.json') as json_file:
data = json.load(json_file)
new_react_role = {'role_id': role.id,
'emoji': emoji,
'message_id': msg_id}
data.append(new_react_role)
with open('data.json', 'w') as f:
json.dump(data, f, indent=4)```

您没有将emoji.name保存在json文件中,因此在您的检查中,您需要做一些更像if x['emoji'] == str(payload.emoji):的事情才能使其工作。

else:
with open('data.json') as react_file:
data = json.load(react_file)
for x in data:
if x['emoji'] == str(payload.emoji):
role = discord.utils.get(client.get_guild(
payload.guild_id).roles, id=x['role_id'])

emoji.name只会打印出类似custom的东西,而不是保存在json文件中的<:custom:898198216611344424>str(emoji)打印出整个内容,就像在您的文件中一样。

最新更新