python - discord.py 1.3.2:如何在用户按下表情符号反应时检测和执行代码
问题描述
在我制作的不和谐机器人中,我在调用函数时将此字段编码为输出,$track_win
并在该字段下方添加了许多反应。
@bot.command()
async def track_win(ctx):
embed = discord.Embed(title = "Individual Win Tracker", description = "Click on any of the following emojis corresponding to the winner", color =0xeee657)
embed.add_field(name = "Afif:", value = ":joy:")
embed.add_field(name = "Faisal:", value = ":rage:")
embed.add_field(name = "Irfan:", value = ":open_mouth:")
embed.add_field(name = "Mahdi:", value = ":pensive:")
embed.add_field(name = "Maruf:", value = ":unamused:")
embed.add_field(name = "Nasseef:", value = ":sob:")
message = await ctx.send(embed = embed)
await message.add_reaction('\U0001F602')
await message.add_reaction('\U0001F621')
await message.add_reaction('\U0001F62E')
await message.add_reaction('\U0001F614')
await message.add_reaction('\U0001F612')
await message.add_reaction('\U0001F62D')
现在,一旦用户按下其中一个反应,我想执行一组功能。你能告诉我我能做到这一点的方法吗?
看到这个 Stack Overflow question中的答案,我这样做了:
@client.event
async def on_reaction_add(reaction,user):
if reaction.emoji == '\U0001F602': #all of the functions that I wanted to execute are given below
fo = open("/storage/emulated/0/afif.txt", "r")
wins = fo.read()
afif = int(str(wins)) + 1
fo.close()
fo = open("/storage/emulated/0/afif.txt", "w+")
fo.write(str(afif))
fo.close()
print(afif)
然而,虽然这种方法似乎不起作用,但也没有抛出异常。我无法完全理解discord.py 文档
那么你能给我一个例子来说明我如何做到这一点吗?
提前致谢!
解决方案
推荐阅读
- ios - 调试信息“DWARF”和崩溃日志
- javascript - 如何添加指向 Google GeoChart 地图的链接?
- javascript - MongoDB 的聚合函数有问题,得到错误消息不明白为什么
- typescript - 打字稿:如何将字符串转换为类型
- python - 如何平均熊猫数据框中的前瞻性数据
- javascript - 日历调度程序类似控件以指定值范围
- url-rewriting - 在 NetCore 2 重写中间件中修改 RewriteContext Host
- php - PHP - 如何重构代码以便我不必为每个表单实例复制和粘贴它?
- git - git 和 sourcetree user.name 混淆
- python - 不能在 NAO 上移动手臂关节