简体   繁体   English

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

[英]Why will my bot not send the message using discord.js

My bot's code works in the other chats but when I try to make it send it in the cafe chat, it wont send it and I get this in the log:我的机器人代码在其他聊天中工作,但是当我尝试让它在咖啡馆聊天中发送时,它不会发送它,我在日志中得到了这个:

在此处输入图像描述

Here is the code that is not working:这是不起作用的代码:

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 });
})

https://discord.js.org/#/docs/main/master/class/Channel As you can see in the docs the channel class does not have a send method Look for a text channel instead https://discord.js.org/#/docs/main/master/class/TextChannel?scrollTo=send https://discord.js.org/#/docs/main/master/class/Channel正如您在文档中看到的那样,频道 class 没有发送方法 寻找文本频道而不是Z5E056C://discord.jsDjsD8B6A7110BAD50。 .org/#/docs/main/master/class/TextChannel?scrollTo=send

Chances are you have a non-text-based channel with a name that includes ☕cafe .您可能有一个名称包含☕cafe的非基于文本的频道。

member.guild.channels.cache.find() will return a guild channel . member.guild.channels.cache.find()将返回一个公会频道 It can be anything;它可以是任何东西; a TextChannel, a VoiceChannel, a CategoryChannel, a NewsChannel, etc. Only TextChannel and NewsChannel has a send() method. TextChannel、VoiceChannel、CategoryChannel、NewsChannel 等。只有TextChannelNewsChannelsend()方法。 If your channel is something else, you'll receive a TypeError: channel.send is not a function .如果您的channel是其他内容,您将收到TypeError: channel.send is not a function

Luckily, channels have an isText() method that indicates whether the channel is text-based.幸运的是,频道有一个isText()方法来指示频道是否基于文本。 You can use it in your find() 's callback:您可以在find()的回调中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM