discord.js报错:GuildTextBasedChannel不存在createMessageComponentCollector属性
解决discord.js v14中
createMessageComponentCollector类型错误 问题原因
discord.js v14已经移除了Channel实例的createMessageComponentCollector方法,这个方法现在属于Message实例。你遇到的类型错误是因为尝试在频道对象上调用一个不存在的方法,而另一台机器能运行大概率是版本实际为v13(v13中Channel支持该方法),或者存在类型定义异常。
正确用法
要创建消息组件收集器,必须先获取到带组件的消息实例,再调用该方法:
// 示例:发送带组件的回复并创建收集器 const replyMsg = await int.reply({ content: "请点击下方按钮", components: [ { type: 1, components: [ { type: 2, style: 1, label: "测试按钮", customId: "test_btn" } ] } ], fetchReply: true // 必须开启才能获取到回复的消息实例 }); // 在消息实例上创建收集器 const collector = replyMsg.createMessageComponentCollector({ time: 60000, // 收集器超时时间,单位毫秒 filter: i => i.user.id === int.user.id // 只收集发起命令用户的交互 }); // 监听收集器事件 collector.on('collect', async i => { await i.deferUpdate(); await i.editReply({ content: `你点击了${i.customId}按钮` }); }); collector.on('end', collected => { console.log(`收集到了${collected.size}次交互`); });
频道级组件监听替代方案
如果需要监听整个频道内的所有组件交互,不要用收集器,直接通过客户端事件监听:
int.client.on('interactionCreate', async i => { if (!i.isButton()) return; // 只处理按钮交互 if (i.channelId !== int.channelId) return; // 只处理当前频道的交互 // 处理交互逻辑 await i.reply({ content: `你在频道${i.channel.name}点击了按钮`, ephemeral: true }); });
内容的提问来源于stack exchange,提问作者clapmemereview
相关产品推荐
相关产品推荐

