【发布时间】:2021-12-02 22:51:24
【问题描述】:
我一直在为反应角色开发一个不和谐的机器人。想法是将消息、角色和表情符号注册到一个 .JSON 文件,并在 on_raw_reaction_remove 或 on_raw_reaction_add 上执行角色更新
现在,问题是它确实将自定义反应添加到指定消息。但是,它并没有在reaction_add 上赋予受人尊敬的角色。我找不到解决此问题的方法。我的观点是问题可能出在机器人注册自定义表情符号的方式上。这是一个例子:
[
{
"role_id": 897227510574616656,
"emoji": "\ud83d\ude04",
"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)```
【问题讨论】:
标签: python python-3.x discord discord.py