我正在尝试将我的事件重做到 fs 事件系统中,以便为每个事件提供单独的文件,并且在将代码转换为 Discord.JS v12 时遇到了一些问题。
有人可以帮我弄清楚我的message.js
文件出了什么问题吗?
这是我的代码:
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);
};
基本上每次我运行命令时都会使机器人崩溃。除此之外,我的ready.js
活动似乎完美无缺。
这是我的message.js
文件抛出的错误:
/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
我正在跑步Discord.JS ^12.0.0
,如果有帮助discord.js-commando discord.js/Commando
的话。node ^12.16.4
感谢所有回复的人的帮助。能够message.js
使用提供的响应修复我的文件。
message.js
如果有人想使用它,这是我的工作文件:
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);
};