首页 > 解决方案 > 有什么方法可以将表情符号与不和谐进行比较?

问题描述

我正在尝试制作一个包含两个选项(两个反应)的选择屏幕,用户可以对其做出反应,机器人现在将用户选择什么反应

我尝试过使用 unicode 比较
反应!=“”或反应!=“✋”;还尝试过:反应!= u“\U0001F44C”或反应!= u“\u270B”使用unicode。还尝试了与反应相同的代码。 emoji, str(reaction / reaction.emoji). 还尝试比较 emojis 的 id 但reaction.emoji.id 引发异常说reaction.emoji 是str 并且字符串没有id(因为idk 为什么它返回一个str 而不是表情符号对象)我已经阅读了文档,它说它支持 != 操作,但我不知道要比较什么

@bot.event
async def on_reaction_add(reaction,user):
     print(reaction) #It prints the two emojis on my console ( and ✋)
     if user.bot:
        print('I am a bot')
        return
     if reaction != "" or reaction != "✋":
        print('Did not found the emoji')
        return
     else:
        print('Found the emoji')
#And then some code which will decide if the user that reacted is valid and what to do with it
    

#The embed the user have to react to if this helps
embed = discord.Embed(title = 'VS',color=0x00fff5)
        embed.set_author(name = message.author.name,icon_url=message.author.avatar_url)
        embed.set_footer(text = message.mentions[0].name , icon_url = mensaje.mentions[0].avatar_url)
        response = await message.channel.send(embed = embed)
        await response.add_reaction("") #OK emoji
        await response.add_reaction("✋") #STOP emoji

我希望机器人能够识别表情符号,但不知道如何识别。

标签: python-3.xdiscorddiscord.py

解决方案


TL;博士

  1. 切换orand
  2. 使用str(reaction.emoji)(参见 discordpy文档中的示例)

解释:

德摩根定律会这样说

if str(reaction.emoji) != "" or str(reaction.emoji) != "✋":

和写一样

if not (str(reaction.emoji) ==  "" and str(reaction.emoji) == "✋"):

并且由于反应不能同时是 OKSTOP,因此该if语句总是会返回True并且总是打印“未找到表情符号”。

就像是

     if str(reaction.emoji) != "" and str(reaction.emoji) != "✋":
        print('Did not found the emoji')
        return
     else:
        print('Found the emoji')

会工作。

编辑:恕我直言,一个更易读的解决方案是检查set中是否存在表情符号。

     if str(reaction.emoji) not in { "", "✋"}:
        print('Did not find the emoji')
        return
     else:
        print('Found the emoji')

推荐阅读