【问题标题】:Discord.JS TypeError: Cannot read properties of undefined (reading 'get')Discord.JS TypeError:无法读取未定义的属性(读取'get')
【发布时间】:2022-02-08 14:28:55
【问题描述】:

早安!我目前正在开发一个不和谐的机器人,但我遇到了 事件处理程序 的问题。 “get”命令似乎有问题,但我似乎无法找出问题所在 是,我已将以下代码提供给我的 message.js

module.exports = (Discord, client, msg) => {

    const prefix = 'e!';

    if (!msg.content.startsWith(prefix) || msg.author.bot) return;

    const args = msg.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    const cmd = client.commands.get(command)

    if (!cmd){
        msg.channel.send("That is not an available command!")
    };

    if(command) command.execute(client, msg, args, Discord);
};

下面的代码是我的index.js

const Discord = require("discord.js")
const client = new Discord.Client({intents : ["GUILDS", "GUILD_MESSAGES"]});
const button = require('discord-buttons')(client)
const { MessageButton } = require("discord-buttons")
 
client.commands = new Discord.Collection();
client.events = new Discord.Collection();

['command_handler', 'event_handler'].forEach(handler =>{
    require(`./handlers/${handler}`)(client, Discord);
})

client.login(process.env.token)

任何帮助将不胜感激!

【问题讨论】:

  • 这意味着client.commandsundefined。如果没有看到您的 client.commands 是什么,我们真的无法提供帮助。
  • 我编辑了我的问题并添加了 index.js 文件,希望对您有所帮助
  • 我应该在 message.js 中引用 client.commands 还是 index.js 文件?
  • 也许在 index.js 中尝试 module.exports = client 或在 message.js 文件中定义 client

标签: events discord discord.js typeerror handler


【解决方案1】:

我修好了!似乎 Discord 和客户的顺序(在这里)。

module.exports = (Discord, client, msg)

错了!相反,我将Discordclient 交换,它似乎有效!

module.exports = (client, Discord, msg)

如果有人能告诉我为什么会发生这种情况(因为我很想了解更多) 你可以评论或回答!

执行命令也有错误,我也修复了

if(cmd) {
        cmd.execute(client, msg, args, Discord);
}

感谢大家的贡献!真的需要你的帮助!

【讨论】: