python - Discord wait_for() 如何添加作者的多个回复?
问题描述
目前我的不和谐机器人回复了一条消息。
- !添加 12345
- 机器人回应“你想添加这个项目吗?是/否
- 用户写入 y 或 n(目前仅适用于 1 个响应
- 如果用户写 y(机器人响应“它被添加”),如果用户写 n(机器人响应“它没有被添加”)
我将如何添加“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))
解决方案
要回答这个问题,我认为您只需要多了解一下 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()
电话,因为这里没有操作
推荐阅读
- java - 未定义的标签:firestore 内的 loop3
- javascript - 自定义 Google 表格图表的单元格
- python - 为什么列表不保留它的价值?
- python - 将特定于类别的值分配为新列中的观察值
- firebase - 使用 Spring Boot 将图像上传到 Firebase
- redux - 使用(Material-ui + React Redux + SSR 中的 Next.js)重新渲染未更新传递的属性
- windows - 显示内存内容时,windbg 是否会自行处理内核中的分页代码?
- python - 当特定语句需要更多时间执行时,如何在 Python for 循环中跳过迭代?
- csv - 用于搜索(大)未排序数组的索引(类型)
- python - Scrapy CrawlSpider 无法抓取首页