I am trying to make a discord.js snipe command and I keep getting an error message: Cannot read properties of undefined (reading 'get'). I created command handlers and event handlers and everything works fine, even the messageDelete event. I get this message every time I run the command
Snipe command:
module.exports = {
commands: ['snipe','sn','s'],
requiredRoles: ['Members'],
callback : (client, message, args) => {
const msg = client.snipes.get(message.channel.id)
if(!msg) return message.channel.send("There's nothing to snipe!")
message.channel.send(`${msg.author} deleted: ${msg.content}`)
}
}
Index file:
const path = require('path')
const fs = require('fs')
const { Client, Intents, Collection } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const config = require('./config.json');
require('./util/loadEvents')(client);
client.snipes = new Collection()
Event Handler:
const fs = require('fs');
module.exports = client => {
fs.readdir("events/", (_err, files) => {
files.forEach((file) => {
if (!file.endsWith(".js")) return;
const event = require(`../events/${file}`);
let eventName = file.split(".")[0];
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`../events/${file}`)];
});
});
}
And messageDelete Event:
module.exports = async (client,message) => {
if(message.author.client) return;
client.snipes.set(message.channel.id, {
content: message.content,
author: message.author.tag,
})
}
All other commands work perfectly
Make sure you pass the arguments in the correct order.
If you run the callback like this:
callback(message, arguments, arguments.join(' '), client);
The order should be like this:
module.exports = {
commands: ['snipe', 'sn', 's'],
requiredRoles: ['Members'],
callback: (message, arguments, joinedArguments, client) => {
const msg = client.snipes.get(message.channel.id);
if (!msg) return message.channel.send("There's nothing to snipe!");
message.channel.send(`${msg.author} deleted: ${msg.content}`);
}
};