问题
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