python-3.x - 有什么方法可以将表情符号与不和谐进行比较?
问题描述
我正在尝试制作一个包含两个选项(两个反应)的选择屏幕,用户可以对其做出反应,机器人现在将用户选择什么反应
我尝试过使用 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
我希望机器人能够识别表情符号,但不知道如何识别。
解决方案
TL;博士
- 切换
or
为and
- 使用
str(reaction.emoji)
(参见 discordpy文档中的示例)
解释:
德摩根定律会这样说
if str(reaction.emoji) != "" or str(reaction.emoji) != "✋":
和写一样
if not (str(reaction.emoji) == "" and str(reaction.emoji) == "✋"):
并且由于反应不能同时是 OK和STOP,因此该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')
推荐阅读
- javascript - 收到笑话:序列化为相同的字符串
- kotlin - 尽管内联 lambda 具有返回值,但在具有块体的函数中需要一个“返回”表达式
- linux - OpenOCD:调试模块未激活。dmcontrol=0x0
- javascript - 当我使用 php 变量时,JQuery 无法加载
- python - Selenium 索引表以使用 python 搜索多个数据点
- android - Recycler View添加Item Animator没有动画
- php - 如何使用 PHP 在 JSON 中获取这个嵌套字符串?
- python - 检查字符串是否是列表的有序子列表
- ruby-on-rails - ActiveRecord.where 在 rake 任务中无效,在 Rails 控制台中工作
- android - ColdFusion 读取移动设备文件并写入移动设备文件 - 从本地设备