我正在制造Discord机器人,并且我需要未知的命令功能。我正在尝试制作一个,它可以工作,但是当您执行普通命令时它也会发送。下面是我的代码。
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;
}
})
这是问题的图像:https://imgur.com/a/Ya82TeA。
答案 0 :(得分:1)
很遗憾,我无法重现该问题。我已经重写了您的代码,它似乎按预期工作。请让我知道问题是否仍然存在。
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.
};
});