为什么我的机器人不会使用 discord.js 发送消息

时间:2021-02-23 06:50:39

标签: javascript node.js discord.js

我的 bot 代码在其他聊天中工作,但是当我尝试让它在咖啡馆聊天中发送时,它不会发送它,我在日志中得到了这个:

enter image description here

这是不起作用的代码:

bot.on('guildMemberAdd', member => {
  const channel = member.guild.channels.cache.find(channel => channel.name.includes('☕cafe'));
  if (!channel) return;
  
  const welcome = {
    color: 0xF2CC0D,
    title: `**WELCOME TO THE SERVER, ${member.user.tag} !!**`,
    description: `I hope you will enjoy it here! \n A place full of chaos and wonder!`,
    thumbnail: {
      url: member.user.avatarURL(),
    },
    fields:[
    {
      name: 'welcome',
      value: 'Welcome again to Star City, !! Feel free to go grab some self roles in #?self-roles and go choose a color in #?mall !!!',
      inline: false
    },
    {
      name: 'commands!',
      value: 'Do: `!help` for all the bot commands!',
      inline: false
    }
   ],
   timestamp: new Date(),
  };

  channel.send({ embed: welcome });
})

2 个答案:

答案 0 :(得分:0)

https://discord.js.org/#/docs/main/master/class/Channel 正如您在文档中看到的,通道类没有发送方法 改为寻找文字频道https://discord.js.org/#/docs/main/master/class/TextChannel?scrollTo=send

答案 1 :(得分:0)

您可能有一个名称中包含 ☕cafe 的非基于文本的频道。

member.guild.channels.cache.find() 将返回 guild channel。它可以是任何东西; TextChannel、VoiceChannel、CategoryChannel、NewsChannel 等。只有 TextChannelNewsChannelsend() 方法。如果您的 channel 是其他东西,您将收到一个 TypeError: channel.send is not a function

幸运的是,频道有一个 isText() 方法来指示频道是否基于文本。您可以在 find() 的回调中使用它:

bot.on('guildMemberAdd', member => {
  const channel = member.guild.channels.cache.find(
    (channel) => channel.name.includes('☕cafe') && channel.isText()
  );
  if (!channel) return;
  // ...
});