I'm trying to redo my events into an fs event system to have separate files for each event, and I'm having some issues with converting the code to Discord.JS v12.
Can someone please help me figure out what's going wrong with my message.js
file?
Here is my code:
index.js
const { Client } = require('discord.js-commando');
const path = require('path');
const fs = require('fs');
const server_invite = (process.env.INVITE_URL);
const owner_id = (process.env.BOT_OWNER);
const prefix = (process.env.BOT_PREFIX);
const stripIndents = require('common-tags').stripIndents;
require('dotenv').config();
const client = new Client({
commandPrefix: prefix,
unknownCommandResponse: false,
disableMentions: 'everyone',
owner: owner_id,
invite: server_invite
})
client.registry
.registerDefaultTypes()
.registerGroups([
['admin', 'Administration'],
['mod', 'Moderation'],
['fun', 'Fun'],
['misc', 'Miscellanious'],
['util', 'Utility']
])
.registerDefaultGroups()
.registerDefaultCommands()
.registerCommandsIn(path.join(__dirname, 'commands'))
fs.readdir('./events/', (err, files) => {
if (err) return console.error;
files.forEach(file => {
if (!file.endsWith('.js')) return;
const evt = require(`./events/${file}`);
let evtName = file.split('.')[0];
console.log(`Loaded event '${evtName}'`);
client.on(evtName, evt.bind(null, client));
});
});
client.on('error', console.error)
client.login(process.env.BOT_TOKEN);
message.js
const discord = require("discord.js");
const dotenv = require('dotenv').config;
const prefix = (process.env.BOT_PREFIX);
const fs = require('fs');
module.exports = (client, message) => {
if (message.author.bot) return;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.cache.get(command);
if (!cmd) return;
cmd.run(client, message, args);
};
Basically every time I run a command it crashes the bot. Other than that my ready.js
event seems to work flawlessly.
Here is the error my message.js
file throws:
/app/events/message.js:13
const cmd = client.commands.cache.get(command);
^
TypeError: Cannot read property 'cache' of undefined
at module.exports (/app/events/message.js:13:33)
at CommandoClient.emit (events.js:326:22)
at MessageCreateAction.handle (/app/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/app/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:125:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:797:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! yuzuki@1.0.0 start: `node --harmony index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yuzuki@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /app/.npm/_logs/2020-10-07T08_08_09_863Z-debug.log
Process exited with status 1
State changed from up to crashed
I'm running Discord.JS ^12.0.0
with discord.js-commando discord.js/Commando
and node ^12.16.4
if it helps.
Thanks for the help from all who responded. Was able to fix my message.js
file with the response provided.
Here is my working message.js
file if anyone wants to use it:
const client = require('discord.js-commando');
const prefix = (process.env.BOT_PREFIX);
require('dotenv').config;
module.exports = (message) => {
if (message.author.client) return;
if (message.content.indexOf(prefix) !== 0) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.cache.get(command);
if (!cmd) return;
cmd.run(client, message, args);
};
Because you're using Commando, you don't need another listener for the message
event. The library already does that for you. Deleting message.js
should remove the error.
The error is occurring because client.commands
is undefined
. You seem to be using code from the main guide, which is intended for bots that are not using Commando and assumes you have already set client.commands
to a Collection
of your commands. You may want to take a look at the Commando guide.
Please don't ask multiple questions in one post. If you have a different issue with your other event handlers, please ask a new question.