为什么我的频道未定义?

时间:2018-05-17 00:37:45

标签: discord discord.io

我正在使用Discord机器人,该机器人将通过DM投诉并将其发布在聊天室中以供主持人查看。出于某种原因,该类型被视为未定义。确切的错误文本是:

TypeError: Cannot read property '44640...' of undefined

错误来自这一行:

if (bot.channel[channelID].type == 1) 

如果通道ID本身被认为是未定义的,那么我认为这意味着通道本身未定义。

以下是该函数的完整代码:

bot.on('message', function (user, userID, channelID, message, evt) {
// Our bot needs to know if it will execute a command
// It will listen for messages that will start with `s!`
if (message.substring(0, 2) == 's!') {
    var args = message.substring(2).split(' ');
    var cmd = args[0];

    args = args.splice(2);
    switch(cmd) {
        // s!ping
        case 'ping':
            bot.sendMessage({
                to: channelID,
                message: 'Pong!'
            });
        break;

        case 'problem':
        if (bot.channel[channelID].type == 1) 
        {
            var embed = new Discord.RichEmbed()
            .setAuthor(message.author.username, message.author.avatarURL)
            .setDescription(message.content)
            .setTimestamp(new Date())
            .setColor('#C735D4');
            //stores the sent message, along with the sender and the time of the message

            client.channels.get(446393832066514947).send(embed);
            //sends message to channel
        }
        // Just add any case commands if you want to..
     }
 }
});

1 个答案:

答案 0 :(得分:0)

如果这是您的整个代码,我假设this.channel永远不会设置在任何地方,这意味着当您尝试访问它时它将是undefined

您的Client对象包含Channel个对象的集合,其中包含频道ID。

因此,在您的代码段中,您应该可以说

bot.channels[channelID].type