在机器人加入上发送消息不工作discord.js

时间:2018-01-09 02:54:03

标签: discord discord.js

我试图让机器人在加入服务器时发送消息。

到目前为止,这是我的代码:

client.on('guildCreate', guild => {
  guild.systemChannel.send(`Hello, I'm LMAOBOT. Thanks for inviting me, here are a list of all my commands! :alien:`, {
    embed:{
        title: ':x: Prefix',
        color: 0x2471a3, 
        description: "The prefix for all my commands is \'lmao\', e.g: \'lmao help\'.",
        fields:[
            {
                name: ':tada: Fun',
                value: 'agree, dankrate, gayrate, 8ball, meme, pun, roll, coinflip, doge, kappa, lenny, lol, megusta, pepe, sanic, spiderman, spooderman, troll, wat, dolan, notsure, alone, pupper, kitty'
            },     
            {
                name: ':tools: Utilities',
                value: 'help, ping, invite'
            },
            {
                name: ':loud_sound: Sound Board - WARNING (EARRAPE)',
                value: 'reee, airhorn, momgetthecamera, 20thcenturyfox, dedotatedwam, friendzoned, gofuckyourself, gottagofast, illuminati, ohmygod, pussy, sadviolin, smokeweed, ohbabyatriple, wombocombo, wow'
            } 
        ],

        footer: {
            text: 'LMAOBot created and developed by Pete#4164.'
        }
    }
  });
});

我的代码不一致,因为他们删除了.defaultChannel命令,我正在使用systemChannel。但如果#general频道被删除,它将输出:

  

TypeError:无法读取未定义

的属性'send'

在控制台中。

那么我将如何制作它以便机器人发送到它有权限的第一个频道?因为现在只要#general不存在就会崩溃。

2 个答案:

答案 0 :(得分:0)

您可以通过这段代码将机器人发送到第一个频道:

let defaultChannel = "";
guild.channels.forEach((channel) => {
  if(channel.type == "text" && defaultChannel == "") {
    if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
      defaultChannel = channel;
    }
  }
})
//defaultChannel will be the channel object that it first finds the bot has permissions for
defaultChannel.send(`Hello, I'm LMAOBOT. Thanks for inviting me, here are a list of all my commands! :alien:`, {
embed:{
    title: ':x: Prefix',
    color: 0x2471a3, 
    description: "The prefix for all my commands is \'lmao\', e.g: \'lmao help\'.",
    fields:[
        {
            name: ':tada: Fun',
            value: 'agree, dankrate, gayrate, 8ball, meme, pun, roll, coinflip, doge, kappa, lenny, lol, megusta, pepe, sanic, spiderman, spooderman, troll, wat, dolan, notsure, alone, pupper, kitty'
        },     
        {
            name: ':tools: Utilities',
            value: 'help, ping, invite'
        },
        {
            name: ':loud_sound: Sound Board - WARNING (EARRAPE)',
            value: 'reee, airhorn, momgetthecamera, 20thcenturyfox, dedotatedwam, friendzoned, gofuckyourself, gottagofast, illuminati, ohmygod, pussy, sadviolin, smokeweed, ohbabyatriple, wombocombo, wow'
        } 
    ],

    footer: {
        text: 'LMAOBot created and developed by Pete#4164.'
    }
}

});

编辑:哎呦我搞错了! ):

答案 1 :(得分:0)

要添加到Raymond的答案中,您需要使用guild.channels.cache.forEach()而不是guild.channels.forEach