编写discord bot时出现“ TypeError:无法读取未定义的属性'send'”

时间:2020-06-11 19:38:19

标签: javascript discord discord.js

我一直在尝试创建一个机器人,该机器人可以将消息从主机服务器中继到该机器人被邀请到的所有其他服务器。我已经与某个正确的方向进行了微调,有人给了我这段代码供您使用。

const channels = ['broadcast', 'start'];
 let announcement = args.slice(1).join(' ');
bot.guilds.cache.forEach(guild => {
let channel = guild.channels.cache.first();
 channels.forEach(c => {
  const ch = message.guild.channels.cache.find(channel => channel.name === c);
  ch.send(announcement);

不幸的是,控制台响应时显示错误,提示为send is undefinedch.send is not a real function。有人可以告诉我我做错了什么吗?我对编码还是很陌生。

编辑:这是类型错误

TypeError: Cannot read property 'send' of undefined

更新:感谢您帮助我现在接近解决方案。我现在通过

到达此错误
C:\Users\profile\Desktop\Discord Bot\node_modules\@discordjs\collection\dist\index.js:160
            if (fn(val, key, this))
                ^

TypeError: fn is not a function

2 个答案:

答案 0 :(得分:0)

感谢大家的帮助,但事实证明,我对此太过思索了。下面的代码实现了我的跨服务器中继思想。

if(command === "test" ) {

const Detail = args.join(" ");
bot.guilds.cache.forEach(guild => {
guild.channels.cache.find(t => t.name == 'association').send(Detail);

});

答案 1 :(得分:-1)

您在forEach循环中犯了一个小错误。看,c包含有关频道的所有信息(id,名称,行会等),因此,如果要查找它,则需要执行以下操作:

// Inside the forEach loop
const ch = message.guild.channels.cache.find(channel => channel.name === c.name);
if(ch) ch.send(announcement);

我还添加了if语句,以防止在找不到频道时弹出错误。

希望这会对您有所帮助:)