You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Discord机器人等待用户的多个指定回复之一?

实现Discord机器人等待多选项回复

你只需要修改check函数的判断条件,把单一匹配改成判断回复内容是否在允许的选项集合中,同时建议限制回复者为发起命令的用户,避免无关消息干扰。

修改后的完整代码:

@client.event
async def on_message(message):
    if message.content.startswith('$choose'):
        channel = message.channel
        author = message.author  # 记录发起命令的用户
        await channel.send('选择A、B或C')

        def check(m):
            # 判断回复者是原用户、同一频道,且内容是A/B/C(忽略大小写)
            return m.author == author and m.channel == channel and m.content.lower() in {'a', 'b', 'c'}

        msg = await client.wait_for('message', check=check)
        await channel.send(f'你选择了{msg.content},{msg.author}!')

关键修改说明:

  • 新增author = message.author记录发起命令的用户,确保只有该用户的回复会被机器人识别。
  • 将check函数的内容判断从单一的m.content == 'hello'改为m.content.lower() in {'a', 'b', 'c'},用集合存储允许的选项,同时通过lower()让匹配不区分大小写(用户输入a/A都能被识别)。
  • 命令触发词改为$choose,更贴合功能场景,你也可以改回原来的$greet。

如果需要扩展更多选项,只需要在集合里添加对应的元素即可,比如{'a', 'b', 'c', 'd'}。

内容的提问来源于stack exchange,提问作者Scar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 20:50:29