【问题标题】:discord.js TypeError: Cannot read property 'send' of undefineddiscord.js TypeError:无法读取未定义的属性“发送”
【发布时间】:2020-06-17 23:39:21
【问题描述】:

我正在尝试使用命令处理程序为我的 Discord 机器人创建一个 ping 命令,但是当我尝试在 Discord 中运行该命令时,我收到以下错误:(我正在使用 PM2 运行我的机器人并记录它)
TypeError:无法读取未定义的属性“发送”
我的命令代码是:

module.exports.run = async (message) => {
    message.channel.send('pinging...').then(m => {
        var ping = m.createdTimestamp - message.createdTimestamp;
        m.edit(ping + 'ms' + ', pong!');
    });
};

module.exports.config = {
    name: 'ping',
    displayName: 'ping',
    aliases: ['p'],
    usage: 'ping',
    description: 'gets your ping (i hope it\'ts high)',
    accessableby: 'members',
    type: 'utility'
};

而我的 index.js 代码是:

const Discord = require('discord.js');
const client = new Discord.Client( {
    disableEveryone: true
});
const { prefix, token } = require('./config.json');

client.on('ready', async () => {
    console.log('bot is too ready for this.');
    let botstatus = ['for bot.help', `at ${client.guilds.size} servers`]
    client.user.setActivity(botstatus, {
        type: 'WATCHING'
    });
});

const fs = require('fs');

client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
let pathCMD ='./commands/';
fs.readdir(pathCMD, (err, files) => {
    if (err) return console.log(err);

    let jsfiles = files.filter(f => f.split('.').pop() === 'js');
    if (jsfiles.length < 1) return console.log('commands are nah');
    else console.log(`loading ${jsfiles.length} commands man`);

    jsfiles.forEach((f, i) => {
        delete require.cache[require.resolve(pathCMD + f)];
        var pull = require(pathCMD + f);
        console.log(`man, i loaded ${f} (${i+1}/${jsfiles.length})`);
        client.commands.set(pull.config.name, pull);
        pull.config.aliases.forEach(alias => {
            client.aliases.set(alias, pull.config.name);
        });
    });
});

client.on('message', async message => {
    if (message.author.bot) return;

    let messageArray = message.content.split(' ');
    let cmd = messageArray[0].substring(prefix.length).toLowerCase();
    let args = messageArray.slice(1);

    if(!message.content.startsWith(prefix)) return;
    else console.log(`[CMD] ${message.author.tag} used ${cmd}`);
    let commandfile = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd));
    if (commandfile) commandfile.run(client, message, args);
});

client.login(token);

我不知道这是否有帮助,但我在 Windows 10 上使用 Node 版本 13.7.0、NPM 版本 6.14.2 和 discord.js 版本 12.0.1。

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    尝试从这里更改您的 ping 命令文件

    module.exports.run = async (message) => {
        message.channel.send('pinging...').then(m => {
            var ping = m.createdTimestamp - message.createdTimestamp;
            m.edit(ping + 'ms' + ', pong!');
        });
    };
    

    收件人:

    module.exports.run = async (client, message, args) => {
        message.channel.send('pinging...').then(m => {
            var ping = m.createdTimestamp - message.createdTimestamp;
            m.edit(ping + 'ms' + ', pong!');
        });
    };
    

    【讨论】: