我想发送一个对我的消息做出反应的用户列表,但列表总是空的。

huangapple go评论55阅读模式
英文:

I want to send a list of users who reacted to my message but the list always shows empty

问题

我查了一下看到有人这样做但它总是返回一个空列表

```python
@bot.command()
async def spinthewheel(ctx,msg:discord.Message=None):
  guild=ctx.guild
  if msg==None:
    em=discord.Embed(title="SPIN THE WHEEL!!", description="React to this message with ⚡ to enter!",color=discord.Color.from_str('#ff00ff'))
    msg=await ctx.send(embed=em)
    await msg.add_reaction('⚡')

  else:
    await msg.add_reaction('⚡')
    await ctx.send("Reaction has been added",delete_after=10)

  await asyncio.sleep(5)

  users = list()
  for reaction in msg.reactions:
    if reaction.emoji == '⚡':
      async for user in reaction.users():
        if user != bot.user:
          users.append(user.name)
  user_list="\n".join(user.name for user in users)
  await ctx.send(f"users: {user_list}")

我也尝试了这个,但结果一样

users = [user async for user in reaction.users()]

你能告诉我如何修复这个问题吗?谢谢 <3


<details>
<summary>英文:</summary>

I searched it up and saw someone do it like this but it always returns an empty list

@bot.command()
async def spinthewheel(ctx,msg:discord.Message=None):
guild=ctx.guild
if msg==None:
em=discord.Embed(title="SPIN THE WHEEL!!", description="React to this message with ⚡ to enter!",color=discord.Color.from_str('#ff00ff'))
msg=await ctx.send(embed=em)
await msg.add_reaction('⚡')

else:
await msg.add_reaction('⚡')
await ctx.send("Reaction has been added",delete_after=10)

await asyncio.sleep(5)

users = list()
for reaction in msg.reactions:
if reaction.emoji == '⚡':
async for user in reaction.users():
if user != bot.user:
users.append(user.name)
user_list="\n".join(user.name for user in users)
await ctx.send(f"users: {user_list}")



I tried using this one too but same results

users = [user async for user in reaction.users()]


Can you pls tell me how to fix this? Thanks &lt;3

</details>


# 答案1
**得分**: 1

问题在于`msg.reactions`为空 - 这是因为它是在创建时的消息表示,并且自那时以来没有更新过与反应相关的信息。这很容易通过以下方式来修复:

```python
msg = await msg.fetch()

这将从频道中重新获取消息。只需将它放在asyncio.sleep之后,然后再循环遍历反应之前。

英文:

The issue is that msg.reactions is empty - this is because it's the message representation at the time it was created and hasn't been updated since with the reaction information. This is easily fixable with a:

msg = await msg.fetch()

This will fetch the message anew from the channel. Just put it after your asyncio.sleep and before you loop over the reactions.

huangapple
  • 本文由 发表于 2023年2月6日 15:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定