【问题标题】:Discord.js/javascript issue with a error. if (command ==='clear'). ReferenceError: command not definedDiscord.js/javascript 出现错误。如果(命令 ==='清除')。 ReferenceError:命令未定义
【发布时间】:2022-02-23 22:43:40
【问题描述】:

每次弹出错误时说 if(command==='clear') referencerError: command is not defined, 尽管 bot 正在上线但没有响应 discord bot 中的任何指令。感谢您的帮助,提前! 我尝试删除 if(command==='clear') 整个部分,但它在下一个播放命令中显示相同的错误,这是音乐机器人的主要命令之一。 我还链接了我用作此代码参考的 youtube 视频,并且我尝试完全按照他所说的对我的不和谐机器人进行编码。 https://www.youtube.com/watch?v=3wJJDM7jUsk&list=RDCMUC08G-UJT58SbkdmcOYyOQVw&index=2

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

const client =  new Discord.Client();

const prefix = '!';

const fs = require('fs');
 
client.commands = new Discord.Collection();
 
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name, command);
}


client.once('ready',() =>{
    console.log('Tihadi.io is online!');

} );
client.on('guildMemberAdd', guildMember => {
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name ==='member');
    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('783308287571132417').send(`Welcome <@$<{guildMember.user.id}> malik, To our server!`)
});



 


client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ +/);
    const commands = args.shift().toLowerCase();
    if (command === 'clear') {
        client.commands.get('clear').execute(message ,args);
    }else if (command==='play') {
        client.commands.get('play').execute(message ,args);
    }else if (command==='leave') {
        client.command.get('leave').execute(messsage ,args);
    }

    
});
 
 



client.login('token');

【问题讨论】:

  • 这是commands (const commands = ...) 不是command

标签: javascript node.js discord.js


【解决方案1】:

在您的代码中,您将commands 定义为一个变量,然后您尝试检查command 与某些字符串。您的client.commands.get() 之一上也缺少s

要修复此错误,您可以执行以下操作:

if (commands == 'clear') {
    client.commands.get('clear').execute(message ,args);
}else if (commands == 'play') {
    client.commands.get('play').execute(message ,args);
}else if (commands == 'leave') {
    client.commands.get('leave').execute(messsage ,args);
}

【讨论】:

    【解决方案2】:

    这可能对你有帮助!

    client.commands = new Collection()
    
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'))
    for(const file of commandFiles){
     const commands = require(`./commands/${file}`)
     client.commands.set(commands.name, commands)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 2021-03-01
      • 2020-09-17
      • 2021-04-14
      • 2021-11-16
      • 2021-04-04
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多