【问题标题】:Cannot read property 'send' of undefined(Discord.js)无法读取未定义的属性“发送”(Discord.js)
【发布时间】:2021-04-16 21:41:04
【问题描述】:

抱歉打扰各位了。我写了一段代码,但我认为这段代码有问题,因为我是 js 和 discord.js 的新手。 感谢任何帮助 (我也看了一些其他问题,但找不到像我这样的例子)

const Discord = require('discord.js');
module.exports = {
    name: "ping",
    description: "Returns latency and API ping",
    execute: 
    async (client, message, args) => 
    {
        try
        {
        const msg = await message.channel.send(`???? Pinging....`);

        msg.edit(`???? Pong!
        latency ${Math.floor(msg.createdTimestap - message.createdTimestap)}ms
        API latency ${Math.round(client.ping)}ms`);
        }catch(e){console.log(e)}```

    }
}

这是控制台:

    at Object.execute (C:\Users\berke\Desktop\RedBot\commands\ping.js:10:43)
    at Client.<anonymous> (C:\Users\berke\Desktop\RedBot\bot.js:77:35)
    at Client.emit (events.js:327:22)
    at MessageCreateAction.handle (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\berke\Desktop\RedBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\berke\Desktop\RedBot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)

【问题讨论】:

  • 错误准确地说明了问题所在。由于某些原因,message.channel 为空。
  • 如错误所示:channel 属性为 undefined,无论出于何种原因。如果您不希望您的应用程序中断,您可以检查是否定义了 channel 对象属性,然后使用它的键,如下所示:const msg = await (message.channel &amp;&amp; message.channel.send('???? Pinging....'));
  • 您是否将正确的东西传递给message 参数?

标签: javascript discord.js


【解决方案1】:

正如评论者所指出的,您在函数中收到的消息没有频道属性。

您可以调试您的应用程序并在执行时查看参数的值,或者您可以console.log() 它们并对其进行测试以更好地了解正在发生的事情。

如果您遵循 Discord.js' 文档中的指南,您可能需要查看此 repository,它显示了动态执行命令的工作示例。

出于 stackoverflow 的目的,我将从下面的仓库中粘贴相关代码:

index.js

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
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('Ready!');
});

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

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

    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

commands/ping.js

module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message) {
        message.channel.send('Pong.');
    }

config.json

{
    "prefix": "!",
    "token": "your-token-goes-here"
}

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2021-04-26
    • 1970-01-01
    • 2020-06-17
    • 2018-12-01
    • 2021-11-02
    • 1970-01-01
    • 2021-04-12
    • 2021-06-27
    相关资源
    最近更新 更多