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

运行Discord.js命令如何等待用户响应?v13版collector的filter怎么用

Discord.js v13+ 等待用户回复功能修复方案

现有代码的问题点

  • 未给awaitMessages方法添加await关键字,导致程序不会等待消息收集完成就直接执行后续逻辑
  • Discord.js v13及以上版本调整了awaitMessages的传参规则:filter不再作为独立的第一个参数传入,需放到配置对象中作为属性配置
  • filter逻辑错误:m.author.target为不存在的属性,你需要的是校验回复消息的发送者是被@的目标用户
  • 未对收集结果做异常捕获,超时未收到符合条件的回复时会抛出未处理的Promise错误
  • 原有代码存在拼写错误:提示语里的You e应为You and

修正后的可运行代码

const target = message.mentions.users.first();
if (!target) return message.reply("you have to mention somebody");

await message.reply(
  `you proposed to ${target}! Is it **YES** or **NO**?`
);

// 修正filter逻辑:校验回复者是被@的目标、不是机器人、回复内容开头为yes
const filter = (m) =>
  m.content.toLowerCase().startsWith("yes") &&
  !m.author.bot &&
  m.author.id === target.id;

const main = message.guild.channels.resolve("706263571562102835");

// 修正传参规则,添加await等待收集结果,增加异常捕获处理超时场景
try {
  const collected = await main.awaitMessages({
    filter,
    errors: ["time"],
    max: 1,
    time: 900000,
  });
  // 收集到符合要求的回复,发送成功提示
  await message.reply(
    `You and ${target} are now married!`
  );
} catch (e) {
  // 超时未收到符合要求的回复,发送超时提示
  await message.reply("Proposal timed out, no valid response received.");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 14:06:06