繁体   English   中英

Discord.js v13 在消息收集器中返回

[英]Discord.js v13 Return in Message Collector

我尝试在Discord.js v13 中使用message.channel.createMessageCollector创建一个“设置”命令。 我希望用户键入quit以取消/中止命令。 我正在尝试使用此代码,但在收集器中使用return并不能阻止它:

const filter = (m) => {
  return m.author.id === message.author.id;
};

const collector = message.channel.createMessageCollector({
  filter,
  max: 5,
  time: 1000 * 20,
});

collector.on("collect", (collect) => {
  if (collect.content.toLowerCase() === "quit") return message.reply("Bye!"); // The return is doesn't work
});

我怎样才能让它工作?

一个简单的return语句不会停止收集器。 它只是使 function 里面的代码的 rest 不能运行。 但是,如果有新的传入消息,回调 function 将再次执行。

但是,您可以使用Collector#stop()方法来停止收集器并发出end事件。 您还可以添加收集器结束的原因。

查看下面的代码:

const filter = (m) => m.author.id === message.author.id;

const collector = message.channel.createMessageCollector({
  filter,
  max: 5,
  time: 1000 * 20,
});

collector.on('collect', (collected) => {
  if (collected.content.toLowerCase() === 'quit') {
    // collector stops and emits the end event
    collector.stop('user cancelled');
    // although the collector stopped this line is still executed
    return message.reply('Bye!');
  }

  // this line only runs if the above if statement is false
  message.reply(`You said _"${collected.content}"_`);
});

// listening for the end event
collector.on('end', (collected, reason) => {
  // reason is the one you passed above with the stop() method
  message.reply(`I'm no longer collecting messages. Reason: ${reason}`);
});

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM