我如何检查对不和谐消息的反应? [英] How do I check the reactions on a Discord message?

查看:20
本文介绍了我如何检查对不和谐消息的反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在创建一个赠品命令。应该检查谁对来自机器人的消息做出反应。如果没有人对该消息做出反应,则机器人应该输出没有人对该消息作出反应。但对我来说,这总是表明,即使有反应,也没有人对信息做出反应。这里有人看到错误吗?

        my_msg = await ctx.send(embed=e)

        await my_msg.add_reaction("🎉")

        await asyncio.sleep(mins * 60)

        new_msg = await ctx.channel.fetch_message(my_msg.id)

        if len(new_msg.reactions and my_msg.reactions) == 0:
            await ctx.send("No one reacted.")
        else:
            winner = random.choice([u for u in await my_msg.reactions[0].users().flatten() if not u.bot])
            e = discord.Embed(color=self.bot.color)
            e.title = "Giveaway ended!"
            e.description = f"You won: {prize}"
            e.timestamp = datetime.datetime.utcnow()
            e.set_footer(text=f'{self.bot.user.name}',
                         icon_url=self.bot.user.avatar_url)
            await ctx.send(f"{winner.mention}", embed=e)

推荐答案

无需勾选my_msg。它是处于发送状态的消息对象,即使在对其添加反应或对不一致进行更改之后,代码中的my_msg对象也不会包含该信息。

这就是new_msg = await ctx.channel.fetch_message(my_msg.id)存在的原因。New_msg包含所有信息。

将我们带到len(new_msg.reactions)。记住,它永远不会是0。你的机器人对它做出了反应,而这种反应算在内,所以if len(new_msg.reactions) == 0不是你能得到的东西。

在执行检查之前获取对消息做出反应的用户列表,确保不将机器人包括在该列表中。

    ...
        my_msg = await ctx.send(embed=e)
        await my_msg.add_reaction("🎉")
        await asyncio.sleep(set_time)
        new_msg = await ctx.channel.fetch_message(my_msg.id)

        user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != self.bot.user]

        # After we have the list, we can check if any users reacted
        if len(user_list) == 0:
            await ctx.send("No one reacted.")
        else:
            winner = random.choice(user_list)
            e = discord.Embed()
            e.title = "Giveaway ended!"
            e.description = f"You won:"
            e.timestamp = datetime.datetime.utcnow()
            e.set_footer(text=f'{self.bot.user.name}',
                         icon_url=self.bot.user.avatar_url)
            await ctx.send(f"{winner.mention}", embed=e)

这篇关于我如何检查对不和谐消息的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆