首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法读取未定义(Discord.js)的属性'send‘

无法读取未定义(Discord.js)的属性'send‘
EN

Stack Overflow用户
提问于 2021-01-12 06:31:08
回答 1查看 47关注 0票数 0

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

代码语言:javascript
复制
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)}```

    }
}

这里是控制台:

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

发布于 2021-01-12 07:20:20

正如评论者所指出的那样,在函数中接收的消息没有通道属性。

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

如果你正在遵循Discord.js文档中的指南,你可能想看看这个repository,它展示了一个动态执行命令的工作示例。

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

index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message) {
        message.channel.send('Pong.');
    }

config.json

代码语言:javascript
复制
{
    "prefix": "!",
    "token": "your-token-goes-here"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65675462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档