当他们对某种反应做出反应时,如何将 Discord 用户 ID 添加到机器人嵌入消息中?

时间:2021-07-24 01:55:33

标签: discord discord.py bots

我刚刚学习了 python 并在互联网、discord py API 和 StackOverflow 上进行了一些搜索,但我仍然不确定如何让我的 discord bot 正常工作。

当用户对机器人嵌入消息上的复选标记作出反应时,有没有办法获取不和谐的用户 ID?

这是我的代码: 这部分有效:

#new dely ubjectiv
  if msg.startswith('$new_dobj'):
    
    ubj = msg.replace('$new_dobj ','')

    embed = discord.Embed(title = "Dely Ubjectiv:", description = ubj +"\n\n**ppl hoo compleeted it:**", colour = 0x99dfff)
    m = await message.channel.send(embed=embed)
    await m.add_reaction('✅')
  • 下面的代码似乎没有任何作用。目标:当人们点击复选标记反应他们 用户 id 被添加到“ppl hoo 完成它”下 在消息中。

    user_reaction = await client.wait_for('reaction_add')
      u = user_reaction.message.id
    
      if user_reaction == '✅':
        m = m.append(">>> " + u + "\n")
        await msg.edit(m)
    

这是到目前为止在 Discord 中的样子: screenshot_of_delyubjectivbot

1 个答案:

答案 0 :(得分:0)

由于 on_reaction_add 有两个参数,所以它返回一个元组。 client.wait_for 的返回值反映了事件引用的参数,见此处:https://discordpy.readthedocs.io/en/stable/api.html#discord-api-events

这个例子应该给你一个很好的起点:

reaction, user = await client.wait_for('reaction_add')
# Just access `user.id` for the id

if str(reaction.emoji) == '✅':
    e = m.embeds[0]
    e.description = f'{e.description}\n> {user}'
    await m.edit(embed=e)

不清楚 msg 在您的代码中是什么意思。它看起来像一个 str,但您也调用了 msg.edit,它仅适用于 discord.Message 对象。

更新:

async def on_message(m):

    # put your msg content and etc here

    def check(reaction, user):
        return m.id == reaction.message.id and str(reaction.emoji) == '✅'

    description = m.embeds[0].description

    while not client.is_closed():
        try:
            reaction, user = await client.wait_for('reaction_add', check=check, timeout=35)
        except asyncio.TimeoutError:
            break
        else:
            e = m.embeds[0]
            description += f'\n> {user.mention}'
            e.description = description
            await m.edit(embed=e)