0
votes

EDIT: big shout out to the guys who are helping with feedback, still very new to the scene, but just so fascinated by it so i keep trying. i have enabled a new command handler and it seemed to have just jacked up everything i have. its kind of a general problem throughout my code but if i can get help with this command i will be able to fix it all.

i am currently having a problem with my delete command. it works, but it throws errors and im missing something obvious. any help is greatly appreciated. i seem to have a problem with not defining what i want it to do, it used to work flawlessly when i had a basic command haandler, but i have since moved on to a cleaner one and i know i'm just missing something obvious. damn near wrecked the entire code trying to figure it out

The error is:

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);

COMMAND HANDLER

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;
        }
    }
}

I THINK THIS IS MY EXECUTE COMMAND. also known as my 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);
    }
}

DELETE COMMAND


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
What is the error returned? What's the code that throws the error?Don Diego
added it to original post, sorry for not clarifyingInnocence
Your async execute(client, message, args, Discord) should be non-async, with return message.channel.messages.fetch(...) instead of await message.channel.messages.fetch(...).Tomalak
The error says "Cannot read property 'send' of undefined" on line 6 in your "delete" command. So obviously message.channel is undefined, which would mean that message probably is not what you think it is.Tomalak
...and the UnhandledPromiseRejectionWarning comes from the missing try/catch in your delete command. In order to at least keep the bot from crashing, wrap the function body in a try/catch block and dump any errors to console.Tomalak

1 Answers

0
votes

You're execute functions is execute(client, message, args, Discord). So, the first parameter should always be client.

However, in your "message.js" file, you wrote:

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

Here, you're passing in message first, so your order of the parameters is messed up. Fix that by rearranging the parameters and the code should work.