【问题标题】:discord.js no messages pass through the message collector filterdiscord.js 没有消息通过消息收集器过滤器
【发布时间】:2022-11-17 04:25:27
【问题描述】:

我正在尝试做一个音乐测验,机器人播放一首歌并问“这首歌的名字是什么?”,然后给用户一个 30 秒的时间段,它可以输入歌曲名称,否则它会说没有人能及时得到正确答案。现在,当我尝试执行此命令并给出正确答案时,机器人只是忽略它并等待 30 秒用完并说没有人及时正确。

const filter = m => m.content.toLowerCase() === item.title.toLowerCase(); // the filter
interaction.reply({ content: "What is the name of this song?"})      
const collector = interaction.channel.createMessageCollector({ filter, time: 30000 }); // creates a collector with the filter and 30 second time period
collector.on('collect', m => { // this is the event that gets triggered if it gets collected
  console.log(`Collected ${m.content}`);
  interaction.followUp(`${m.author} got the correct answer!`)
  queue.skip()
});

collector.on('end', collected => { // this is the even that gets triggered when the time runs out
  console.log(`Collected ${collected.size} items`);
  interaction.followUp(`${collected.size} people got the right answer!`)
  queue.skip()
});

item 对象只是一个 JSON 文件,其中包含当前歌曲的数据:艺术家、URL 和标题。因此,对于此示例,我们假设这是给定的信息:

{
  "title": "Uptown Funk",
  "url": "https://www.youtube.com/watch?v=OPf0YbXqDm0",
  "singers": ["Mark Ronson", "Bruno Mars"]
},

然后即使用户说 uptown funk,也不会被接受。

【问题讨论】:

  • 你觉得这个有什么作用? m.content.includes(item.title.toLowerCase() === item.title.toLowerCase())。特别是这个:item.title.toLowerCase() === item.title.toLowerCase()

标签: javascript discord.js


【解决方案1】:

这是因为您的过滤器定义不正确。

目前,您正在检查您收集的消息是否包含一个 item.title,它等于同一个 item.title。这会打乱过滤器的逻辑,并返回 false。

相反,您可以执行以下两种操作之一:

const filter = m => m.content.toLowerCase() === item.title.toLowerCase();

或者

const filter = m => m.content.toLowerCase().includes(item.title.toLowerCase());

【讨论】:

  • 我把它改成了那个,但它仍然没有解决问题
猜你喜欢
  • 2020-12-30
  • 1970-01-01
  • 2021-03-31
  • 2019-11-25
  • 2020-10-25
  • 1970-01-01
  • 2021-03-17
  • 2013-03-04
  • 2022-01-21
相关资源
最近更新 更多