【问题标题】:Problems with discord.py (collecting reactions and then iterating through the users who reacted)discord.py 的问题(收集反应,然后遍历做出反应的用户)
【发布时间】:2021-08-25 14:19:05
【问题描述】:

所以我在 discord.py 中制作了一个机器人,但我偶然发现了一个问题。

这是我的代码:

async def make_event(ctx):
    with open('stats.json', "r") as f:
        users = json.load(f)
    event_embed = discord.Embed(title="AN EVENT IS HAPPENING!", description="React to the message to gain some coins!", color=discord.Colour.red())
    
    event_message = await ctx.send(embed=event_embed)
    await event_message.add_reaction("????")
    await asyncio.sleep(4)
    reactors = []
    for reaction in event_message.reactions:
        
        reactorsForThatReaction = await reaction.users().flatten()
        
        for user in reactorsForThatReaction:
            if str(user.id) not in reactors:
                reactors.append(str(user.id))
    resultEmbed = discord.Embed(title="Event results",  description="The following users won coins:")
    for user in reactors:
        gain = random.randint(5000, 100000)
        users[user]["cash"] += gain
        formattedGain = "{:,}".format(gain)
        resultEmbed.add_field(name=client.fetch_user(int(user)).name, value=f"{formattedGain} cash.")

    await ctx.send(embed=resultEmbed)

基本上,这段代码的作用是制作和发送嵌入(用户对其做出反应),然后机器人处理这些反应并分发硬币。我遇到的问题是机器人根本没有注册event_message.reactions。当我尝试打印出event_message.reactions 时,它返回一个空列表。我不知道这里发生了什么。

有人可以帮忙吗?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您必须再次获取消息,因为现在您使用的是睡眠前的旧消息信息。

    使用reactions之前取消息是这样的

    event_message = await ctx.fetch_message(event_message.id)
    

    Context.fetch_message

    【讨论】:

    • 哦,我现在明白了。我有一个暗示,这可能是原因,但你解释它使它更容易理解。非常感谢!
    【解决方案2】:

    我发现 this thread 可能会对您有所帮助。基本上,您可以使用ctx.message.reactions 获得反应,而不是使用event_message

    【讨论】:

    • 这也很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 2018-04-03
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2021-12-31
    • 2021-03-18
    相关资源
    最近更新 更多