Discord.JS Unknown Command not working correctly

时间秒杀一切 提交于 2020-07-31 05:50:30

问题


I am making a Discord bot, and I need an unknown command feature. I am trying to make one, and it works, but it also sends when you do normal commands. Below is my code.

client.on('message', message=>{
    const prefix = '&'

    if (!message.content.startsWith(prefix)) return
    let [command, ...args] = message.content.slice(prefix).split(/\s+/g)
    
    switch(command) {
        case "help": 
               if(msg.startsWith(`${prefix}help`)) {
        let embedhelp = new Discord.MessageEmbed()
        .setAuthor('Test', 'https://cdn.discordapp.com/attachments/421820457587703812/736328588500140102/264855.png')
        .setTitle('Stock of ABC!')
        .addFields(
            { name: '&gen A:', value: ("Generates A!"), inline: true},
            { name: '&gen B:', value: ("Generates B!"), inline: true},
            { name: '&gen C:', value: ("Generates C!"), inline: true},
            { name: '&gen D:', value: ("Generates D!"), inline: true},
            { name: '&gen E:', value: ("Generates E!"), inline: true},
            { name: '&gen F:', value: ("Generates F!"), inline: true},
            { name: '&gen G:', value: ("Generates G!"), inline: true},
            { name: '&gen H:', value: ("Generates H!"), inline: true},
            { name: '&stock:', value: ("Shows the stock of every Account!"), inline: true},
            { name: '&help:', value: ("Brings this menu up!"), inline: true},
        )
        .setDescription("Test Bot")
        .setFooter("Test Bot")
        .setTimestamp();
    message.channel.send(embedhelp)
    }
        break;
        case "test": 
              message.channel.send('test')
        break;
        default:
              message.channel.send(`run ${prefix}help to get a list of commands`)
        break;
    }
})

Here is an image of the problem: https://imgur.com/a/Ya82TeA.


回答1:


Unfortunately, I couldn't reproduce the issue. I've rewritten your code and it seems to be working as expected. Please let me know if the problem persists.

client.on("message", message => {
    const prefix = "&";

    if (!message.content.startsWith(prefix)) return false;
    let [command, ...args] = message.content.slice(prefix.length).split(/\s+/g);

    switch(command) {
        case "help":
            const Embed = new Discord.MessageEmbed()
            .setAuthor('Test', 'https://cdn.discordapp.com/attachments/421820457587703812/736328588500140102/264855.png')
            .setTitle("Stock of ABC!")
            .addFields(
                {name: "&gen A:", value: ("Generates A!"), inline: true},
                {name: "&gen B:", value: ("Generates B!"), inline: true},
                {name: "&gen C:", value: ("Generates C!"), inline: true},
                {name: "&gen D:", value: ("Generates D!"), inline: true},
                {name: "&gen E:", value: ("Generates E!"), inline: true},
                {name: "&gen F:", value: ("Generates F!"), inline: true},
                {name: "&gen G:", value: ("Generates G!"), inline: true},
                {name: "&gen H:", value: ("Generates H!"), inline: true},
                {name: "&stock", value: ("Shows the stock of every account!"), inline: true},
                {name: "&help", value: "Brings this menu up!", inline: true}
            )
            .setDescription("Test Bot")
            .setFooter("Test Bot")
            .setTimestamp();
            message.channel.send(Embed);
            break;
        
        case "test":
            message.channel.send("test");
            break;
        default:
            message.channel.send(`Run ${prefix}help to get a list of commands.`);
            // Note: You don't need to use break in the default case.
    };
});


来源:https://stackoverflow.com/questions/63097897/discord-js-unknown-command-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!