discord.py 1.3.2:如何在用户按下表情符号反应时检测并执行代码

时间:2020-03-31 15:05:42

标签: python event-handling discord discord.py discord.py-rewrite

在我制作的discord机器人中,我在调用$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')

现在,我想在用户按下其中一个反应后执行一组功能。你能告诉我我可以做到的方式吗?

看到this 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 documentation

那么您能举例说明如何实现这一目标吗?

谢谢!

0 个答案:

没有答案