【问题标题】:Why is my channel undefined?为什么我的频道未定义?
【发布时间】:2018-10-27 03:36:39
【问题描述】:

我正在开发一个 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..
     }
 }
});

【问题讨论】:

  • 我相信您正在尝试使用 2 个不同的库。 bot.sendMessage() 不是 Discord.JS,但生成 Rich Embed 的方式看起来像 Discord.JS。您实际使用的是什么库?
  • @AndréPaulo 我正在使用 Discord.io

标签: discord discord.io


【解决方案1】:

如果这是您的全部代码,我假设 this.channel 从未在任何地方设置,这意味着当您尝试访问它时它将是 undefined。

您的Client 对象包含Channel 对象的集合,以通道ID 作为键。

所以在你的 sn-p 中你应该可以说

bot.channels[channelID].type

【讨论】:

  • if (bot.channel[channelID].type == 1) 产生一个错误,其中 ChannelID 是一个未定义的属性。
  • @MarkGradl bot.channels 持有频道集合,而不是bot.channel
猜你喜欢
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2014-07-27
  • 2016-01-28
相关资源
最近更新 更多