微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类型错误:无法读取未定义的属性“通道”

如何解决类型错误:无法读取未定义的属性“通道”

我正在使用 discord.js 开发一个不和谐的机器人,但我遇到了这个我不知道如何解决的问题 我做了一个命令来测试机器人是否在线,这是它的代码 就在我展示任何代码之前,我想说我对 javascript 有点陌生,所以这个问题让我很困惑

module.exports = {
    name : "onstatus",description : "",execute(message,args) {
        message.channel.send("online");
    }
}

所以当我输入 !-onstatus 我期望发生的是机器人响应。但它没有,它在 vs 代码显示了这个错误

TypeError: Cannot read property 'channel' of undefined

我有一个有效的命令,但由于某种原因,这不是有效的代码

module.exports = {
    name : 'ping',description : '',args){
        message.channel.send('pong');
        
    }
}

当我运行这个并输入 !-ping 时它会起作用

如果你想要整个机器人的代码,你去

const discord = require('discord.js');

const client = new discord.Client();

const prefix = "!-";

const fs = require('fs');
 
client.commands = new discord.Collection();
 
const commandFiles = fs.readdirsync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name,command);
}
 
client.once('ready',() => {
    console.log('leobot is currently online ');
});

client.on('message',message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command == 'ping') {
        client.commands.get('ping').execute(message,args);
    } else if (command == 'developerofleobot'){
        client.commands.get('developerofleobot').execute(message.args);
    } else if (command == 'onstatus'){
        client.commands.get('onstatus').execute(message.args);
    }
});


client.login('######');

解决方法

您的代码

newArr

逗号将是消息而不是点之后的下一个

newArr

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。