比较对表情符号的反应 returns false 即使它是相同的表情符号

Comparing a reaction to an emoji returns false even though its the same emoji

我是 discord.py 的新手,我无法让它工作,我到处搜索但找不到答案。

我想做的事情:如果用户对特定的表情符号做出反应,则获得一个角色,如果他们对另一个表情符号做出反应,则获得另一个角色。

发生了什么:没有给出任何角色。

这是我的代码

@bot.event
async def on_reaction_add(reaction, user):
    channel = bot.get_channel(780094535711719465)

    if user == bot.user:
        return

    print("Reaction detected") #DEBUG
    print(reaction) #DEBUG. Returns  or  respectively
    print(user) #DEBUG. Returns whoever reacted
    print(reaction == "") #DEBUG. Returns False always
    print(reaction == "}") #DEBUG. Returns False always

    if reaction == "\N{TABLE TENNIS PADDLE AND BALL}": #Using "" also returns False
        print("Detected tennis padle") #DEBUG
        Role = discord.utils.get(user.guild.roles, name="test")
        await user.add_roles(Role)
        print("Added test role") #DEBUG

    elif reaction == "\N{RUNNER}": #Using "" also returns false
        print("Detected runner") #DEBUG
        Role = discord.utils.get(user.guild.roles, name="test2")
        await user.add_roles(Role)
        print("Added test2 role") #DEBUG

提前致谢。

由于 reactiondiscord.reaction.Reaction 类型的对象,因此您不能将它与字符串进行比较。您需要从对象访问表情符号字符串,所以它可能是这样的:

if reaction["emoji"]["name"] == "":

也许 Discord.js 文档中有关 Awaiting reactions 的部分可能会有所帮助。

(我知道你在用 Python 编程,所以你需要弄清楚确切的 Python 语法)