【问题标题】:FS Events Message.js not working in Discord.js v12FS 事件 Message.js 在 Discord.js v12 中不起作用
【发布时间】:2021-01-18 15:27:22
【问题描述】:

我正在尝试将我的事件重做到一个 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.0discord.js-commando discord.js/Commandonode ^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);
};

【问题讨论】:

  • 'it crash the bot' 发生了什么错误?能否提供完整的错误信息?
  • 抱歉,回复晚了。我会用它现在给出的错误来更新我的问题。

标签: discord.js fs commando


【解决方案1】:

因为您使用的是 Commando,所以您不需要另一个侦听器来处理 message 事件。图书馆已经为您做到了。删除 message.js 应该可以消除错误。

发生错误是因为client.commandsundefined。您似乎正在使用code from the main guide,它适用于未使用 Commando 的机器人,并假设您已经使用了set client.commands to a Collection of your commands。你可能想看看Commando guide

请不要在一篇文章中提出多个问题。如果您对其他事件处理程序有不同的问题,请ask a new question

【讨论】:

  • 啊,好吧。我没有意识到它不需要这个。
猜你喜欢
  • 2020-12-25
  • 2021-03-02
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 2020-10-11
  • 2022-06-25
  • 2021-05-19
相关资源
最近更新 更多