【问题标题】:Cannot Read 'execute' of undefined Discord.js无法读取未定义 Discord.js 的“执行”
【发布时间】:2021-07-06 21:27:16
【问题描述】:

我有一个错误: C:\Users\robcio\Desktop\BotFolder\index.js:33 client.commands.get('verify').execute(message, args, Discord, client); ^

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\robcio\Desktop\BotFolder\index.js:33:38)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\ws\lib\websocket.js:825:20)

我的代码是:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '!';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./command').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./command/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Ready');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    if(command === 'info'){
        message.channel.send('Author: ITS ME')
        message.channel.send('Version: Alpha')
    }
    if(command === 'verify'){
        client.commands.get('verify').execute(message, args, Discord, client);
    }
})

client.login('NOT FOR YOU!');

验证.js:

module.exports = {
    name: 'Verification',
    description: 'Verification',
    execute(message, args, Discord, client) {
        const channel = 'NOPE';
        const VerificatedRole = message.guild.roles.cache.find(role => role.name === "Verificated");

        const VerificatedEmoji = '✅';

        let embed = new Discord.MesssageEmbed()
        .setColor('#043927')
        .setTitle('Verificate')
        .SetDescription('Normal Verification')

        let messageEmbed = new message.channel.send(embed);
        messageEmbed.react(VerificatedEmoji);

        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.messsage.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === VerificatedEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(VerificatedRole);
                } else {
                    return;
                }
            }
        });

        client.on('messageReactionRemove', async (reaction, user) => {
            if (reaction.messsage.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === VerificatedEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(VerificatedRole);
                } else {
                    return;
                }
            }
        });
    }
}

我不知道如何修复这个我认为我必须使用 const 执行“执行”,但 IDK 如何做到这一点 我只想用验证和其他东西做一个非常简单的机器人

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    在 verify.js 文件中,命令名称为 Verification,但在您的主文件中,您尝试获取 verify (..get('verify')) 并执行它。由于找不到验证命令名称,因此会引发错误。这是因为您试图在命令处理程序中获取command.name。因此,在您的情况下,要么在 verify.js 中将名称更改为 verify,要么尝试在主文件中获取 Verification,因为命令名称为 Verification

    注意: JS 区分大小写,因此请确保两者相同。

    【讨论】:

    • 谢谢,但它引发了另一个错误:let embed = new Discord.MesssageEmbed() ^ TypeError: Discord.MesssageEmbed is not a constructor
    • 你有一个错字。 Discord.MessageEmbed()MessageEmbed 中有三个“s”。
    猜你喜欢
    • 2021-07-03
    • 2021-04-30
    • 2020-10-22
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2022-06-10
    • 2021-04-15
    • 2020-12-05
    相关资源
    最近更新 更多