【问题标题】:Discord.py Reaction Roles with custom emojis带有自定义表情符号的 Discord.py 反应角色
【发布时间】:2021-12-02 22:51:24
【问题描述】:

我一直在为反应角色开发一个不和谐的机器人。想法是将消息、角色和表情符号注册到一个 .JSON 文件,并在 on_raw_reaction_removeon_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


    【解决方案1】:

    您没有将 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 文件中的 &lt;:custom:898198216611344424&gt;str(emoji) 打印出整个内容,就像在您的文件中一样。

    【讨论】:

    • 哇!有效!但我不明白的是,您的代码不会改变它在 JSON 文件中存储数据的方式。为了让我明白这一点,payload.emoji.name 是否在 data.json 文件中存储了“emoj”的特定部分?
    • 不,使用您在上面编写的代码,您将使用 ID 和所有内容存储在 json 文件中的“整个表情符号”。因此,在您的检查中,您必须将整个表情符号字符串与整个表情符号字符串进行比较。在上面的代码中,您基本上将整个表情符号字符串与仅表情符号名称进行了比较。您的代码所做的示例:if "&lt;:custom:898198216611344424&gt;" == "custom"。所以你的 if 语句永远不会触发。我的 sn-p 可以:if "&lt;:custom:898198216611344424&gt;" == "&lt;:custom:898198216611344424&gt;"。希望这是有道理的
    猜你喜欢
    • 2021-05-15
    • 2021-08-09
    • 2022-07-14
    • 2022-01-19
    • 2021-04-27
    • 2021-09-29
    • 2020-03-20
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多