【问题标题】:Waiting for command author's reaction to edit to the help category page等待命令作者的反应来编辑帮助分类页面
【发布时间】:2023-03-24 06:42:01
【问题描述】:
message=await ctx.send(f"{ctx.author.mention}, after reacting to an emoji below the message will be edited to your choosen category)

        await message.add_reaction('????')
        await message.add_reaction('????')
        await message.add_reaction('????')
        await message.add_reaction('????')



        message = await client.wait_for('on_reaction_add', timeout= 10, check=lambda message: message.author == ctx.author)

        if message.reaction_add == '????':
            await ctx.send("You reacted!")

我正在发出使用帮助页面的命令,它将预先存在的帮助消息编辑为用户选择的与他们做出反应的表情符号相对应的类别。

我使用了一种没有检查的方法,如果他们碰巧对这些表情符号对另一条消息做出反应,这会惹恼其他人,有没有办法进行类似于我已经做过或接近的检查,消息在哪里的帮助可以编辑到用户想要的页面,并且只有在他们使用专门为可以使用表情符号的作者的命令时才有效?

【问题讨论】:

标签: python discord.py


【解决方案1】:

为了稍微简化一下,我将只使用提供的两个反应。

@client.command()
async def test(ctx):
    message = await ctx.send("Message that's getting reactions")
    paper = '?' # for the sake of "I'd rather not copy and paste these emojis a lot"
    hammer = '?'
    await message.add_reaction(paper) 
    await message.add_reaction(hammer)

    def check(reaction, user):#checking for reactions and user
        return user == ctx.author and str(reaction.emoji) in [paper, hammer]
    # in this case, the message author and the reaction emojis paper and hammer

    try:
        reaction, user = await client.wait_for("reaction_add", timeout = 3, check=check)
        # waiting for a reaction to be added
        if str(reaction.emoji) == paper:
            await ctx.send("You reacted!")

        if str(reaction.emoji) == hammer:
            await message.edit(content="Another reaction!")# this is for editing the message sent

    except: # Timeout error-handling
        await message.edit(content="Run command to use again!")

如果您愿意,还可以包含while True,这样用户就不必不断地执行命令。 工作测试如下图所示:


参考资料:

discord.Client.wait_for

discord.Message.edit

How to loop with a reaction embed menu (SO)

【讨论】:

  • 它看起来可以工作,但它似乎没有为我编辑消息并且没有给出任何错误
【解决方案2】:
reaction, user = await client.wait_for('reaction_add', timeout=10.0, check...)

如果您阅读有关 on_reaction_add 的文档,您会发现它需要 reaction 和 user 参数。

要编辑消息:

message = reaction.message
await message.edit(content='new content!')

【讨论】:

  • 但是它如何编辑它发送的消息呢?我不希望它向用户发送消息
  • reaction.message 并像任何其他消息一样简单地编辑它
  • 需要用什么支票? def check(reaction, user):return and str(reaction.emoji) == '?' 喜欢吗?
  • 这取决于你想做什么
  • 我只需要它作为作者的检查,因为我不希望机器人添加反应但它会改变消息
猜你喜欢
  • 2020-10-29
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多