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

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

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

问题

  1. 我查了一下看到有人这样做但它总是返回一个空列表
  2. ```python
  3. @bot.command()
  4. async def spinthewheel(ctx,msg:discord.Message=None):
  5. guild=ctx.guild
  6. if msg==None:
  7. em=discord.Embed(title="SPIN THE WHEEL!!", description="React to this message with ⚡ to enter!",color=discord.Color.from_str('#ff00ff'))
  8. msg=await ctx.send(embed=em)
  9. await msg.add_reaction('⚡')
  10. else:
  11. await msg.add_reaction('⚡')
  12. await ctx.send("Reaction has been added",delete_after=10)
  13. await asyncio.sleep(5)
  14. users = list()
  15. for reaction in msg.reactions:
  16. if reaction.emoji == '⚡':
  17. async for user in reaction.users():
  18. if user != bot.user:
  19. users.append(user.name)
  20. user_list="\n".join(user.name for user in users)
  21. await ctx.send(f"users: {user_list}")

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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}")

  1. I tried using this one too but same results

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

  1. Can you pls tell me how to fix this? Thanks &lt;3
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 问题在于`msg.reactions`为空 - 这是因为它是在创建时的消息表示,并且自那时以来没有更新过与反应相关的信息。这很容易通过以下方式来修复:
  6. ```python
  7. 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:

  1. 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:

确定