discord.js报错:message.createMessageComponentCollector不是函数,如何解决?
问题解决:message.createMessageComponentCollector 不是函数
错误原因
你直接把channel.send()返回的Promise赋值给了message变量,但Promise本身没有createMessageComponentCollector方法,只有当Promise resolve后拿到的实际Message对象才拥有这个方法。
修复方案
方法1:使用 async/await(推荐)
确保代码所在函数为异步函数,用await等待消息发送完成,拿到真实的Message实例:
const channel = client.channels.cache.get(channelIDD); // 需确保当前函数被async修饰 const message = await channel.send({ content: 'I think you should,', ephemeral: true, embeds: [embed], components: [row] }); const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15000 }); collector.on('collect', i => { i.reply(`${i.user.id} clicked on the ${i.customId} button.`); }); collector.on('end', collected => { console.log(`Collected ${collected.size} interactions.`); });
方法2:使用 .then() 链式调用
如果无法使用async/await,通过then处理Promise获取Message对象:
const channel = client.channels.cache.get(channelIDD); channel.send({ content: 'I think you should,', ephemeral: true, embeds: [embed], components: [row] }) .then(message => { const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button, time: 15000 }); collector.on('collect', i => { i.reply(`${i.user.id} clicked on the ${i.customId} button.`); }); collector.on('end', collected => { console.log(`Collected ${collected.size} interactions.`); }); }) .catch(err => console.error('发送消息失败:', err));
额外提示:你定义的filter变量未被使用,可直接删除;若需要过滤特定用户或交互,可将其加入收集器配置项中。
内容的提问来源于stack exchange,提问作者SashimiDude
相关产品推荐
相关产品推荐

