FS 事件 Message.js 在 Discord.js v12 中不工作
FS Events Message.js not working in Discord.js v12
我正在尝试将我的事件重做到一个 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);
};
因为您使用的是 Commando,所以 message
事件不需要另一个侦听器。图书馆已经为您做到了。删除 message.js
应该会消除错误。
错误发生是因为 client.commands
是 undefined
。您似乎在使用 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.
请不要在一个问题中提出多个问题post。如果您对其他事件处理程序有不同的问题,请ask a new question。
我正在尝试将我的事件重做到一个 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);
};
因为您使用的是 Commando,所以 message
事件不需要另一个侦听器。图书馆已经为您做到了。删除 message.js
应该会消除错误。
错误发生是因为 client.commands
是 undefined
。您似乎在使用 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.
请不要在一个问题中提出多个问题post。如果您对其他事件处理程序有不同的问题,请ask a new question。