Node.js Discord Bot:类型错误:无法读取未定义的“发送”属性

时间:2021-05-08 06:19:27

标签: javascript node.js discord.js

编辑:向帮助提供反馈的人大喊大叫,他们对现场仍然很陌生,但对它非常着迷,所以我一直在努力。 我启用了一个新的命令处理程序,它似乎刚刚提升了我拥有的一切。这是我整个代码中的一个普遍问题,但如果我能得到有关此命令的帮助,我将能够解决所有问题。

我的删除命令目前有问题。它有效,但它会引发错误并且我遗漏了一些明显的东西。任何帮助是极大的赞赏。我似乎有一个问题,没有定义我想要它做什么,当我有一个基本的命令处理程序时,它曾经完美无缺地工作,但我后来转向了一个更干净的,我知道我只是遗漏了一些明显的东西。该死的几乎破坏了整个代码试图弄清楚

错误是:

TypeError: Cannot read property 'send' of undefined
    at Object.execute (C:\Users\Admin\Desktop\discord bot\commands\delete.js:9:56)
    at module.exports (C:\Users\Admin\Desktop\discord bot\events\guild\message.js:17:17)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\Admin\Desktop\discord bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Admin\Desktop\discord bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Admin\Desktop\discord bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Admin\Desktop\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Admin\Desktop\discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\discord bot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)

main.js

const Discord = require('discord.js');
require('dotenv').config();
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });
const fs = require('fs');


client.commands = new Discord.Collection();
client.events = new Discord.Collection();

['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord)
})

client.login(process.env.DISCORD_TOKEN);

命令处理程序

const fs = require('fs');

module.exports = (client, Discord) =>{
    const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('js'))

    for(const file of command_files){
        const command = require(`../commands/${file}`);
        if(command.name){
            client.commands.set(command.name, command);
        } else {
            continue;
        }
    }
}

我认为这是我的执行命令。也称为我的 message.js

require('dotenv').config();
module.exports = (Discord, client, message) => {
    const prefix = process.env.PREFIX;
    const send = require ('discord.js');

    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    const command = client.commands.get(cmd) || 
        client.commands.find(a => a.aliases && a.aliases.includes(cmd));

    if(command) command.execute(client, message, args, Discord);  

    try {
        command.execute(message, args, cmd, client, Discord);
    } catch (err) {
        message.reply("whoops, shit got fuckity on my backend");
        console.log(err);
    }
}

删除命令


const message = require("../events/guild/message");
module.exports = {
    name: 'delete',
    description: "delete messages",
        execute(client, message, args, Discord) {
            try {
            if (!args[0]) return message.channel.send("type a number with the command doofus");
            if (isNaN(args[0])) return message.channel.send("bruh enter a real number");

            if (args[0] > 100) return message.channel.send("way too much");
            if (args[0] < 1) return message.channel.send("stop tryna be a funny guy")

            return message.channel.messages.fetch({ limit: args[0] }).then(messages => {
                message.channel.bulkDelete(messages);


                message.channel.send("you saw nothing");
            })
     } catch (err) {
            console.log(err);
     }
    }
}

1 个答案:

答案 0 :(得分:0)

您执行的函数是 execute(client, message, args, Discord)。因此,第一个参数应始终为 client

但是,在您的“message.js”文件中,您写道:

try {
        command.execute(message, args, cmd, client, Discord);
} catch (err) {
        **some code here**
}

在这里,您首先传入 message,因此您的参数顺序混乱了。通过重新排列参数来解决这个问题,代码应该可以工作。