首页 > 解决方案 > Discord wait_for() 如何添加作者的多个回复?

问题描述

目前我的不和谐机器人回复了一条消息。

我将如何添加“n”响应?

这是我当前的代码

@client.command()
async def target(ctx, arg1):
    sku = arg1
    channel = ctx.channel
    await ctx.channel.send('Do you wish to add this item? y/n')

    def check(m):
        return m.content == 'y' and m.channel == channel

    msg = await client.wait_for('message', check=check)
    await ctx.channel.send('It was added!'.format(msg))

标签: pythondiscord.py

解决方案


要回答这个问题,我认为您只需要多了解一下 discord.pyclient.wait_for()方法,特别是它的check回调和返回实际上是做什么的。从文档中:

  • check (Optional[Callable[…, bool]]) -- 检查等待什么的谓词。参数必须满足正在等待的事件的参数。

...

退货

不返回任何参数、单个参数或反映事件引用中传递的参数的多个参数的元组。

换句话说,check它只是一个过滤器,当您想停止等待并对等待的消息执行某些操作时,它必须返回 True。这种情况下的返回与传递给回调的消息相同。

因此,您可以简单地扩展逻辑check以过滤除“y”之外的“n”。(或者,如果您想在向频道发送任何消息后总是说些什么,您可以完全摆脱内容检查)

然后,一旦wait_for返回,您可以使用返回的Message()对象根据实际所说的内容进行分支:

@client.command()
async def target(ctx, arg1):
    sku = arg1
    channel = ctx.channel
    await ctx.channel.send('Do you wish to add this item? y/n')

    def check(m):
        return m.content in ['y', 'n'] and m.channel == channel

    msg = await client.wait_for('message', check=check)
    if msg.content == 'y':
        await ctx.channel.send('It was added!')
    elif msg.content == 'n':
        await ctx.channel.send('It was not added')

我也挂断了format()电话,因为这里没有操作


推荐阅读