这是我的代码,每当我运行 $dm 命令时,它都会运行它,但也会运行 $help 命令。 我知道这可能是一个新手问题,但如果有人可以帮助我,我将不胜感激
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
const { MessageEmbed } = require('discord.js');
client.once('ready', () => { client.user.setActivity('Use $help for a list of commands!', { type: "PLAYING" }); });
console.log('Ready!');
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (message.content.startsWith(`${prefix}ping`)) {
message.channel.send('Pong.');
} else if (message.content.startsWith(`${prefix}beep`)) {
message.channel.send('Boop.');
} else if (message.content === `${prefix}server`) {
message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
} else if (message.content === `${prefix}user-info`) {
message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
console.log(message.content);
} else if (message.content === (`${prefix}dm`)) {
message.author.send("string");
} else if (message.content === "$help") {}
let embed = new MessageEmbed()
.setTitle("Command List")
.setDescription("$help, $ping, $beep, $server, $user-info, $dm")
.setColor("RANDOM")
message.channel.send(embed)
}
,
)
client.login(token)
答案 0 :(得分:2)
else if (message.content === "$help") {}
<- 问题
您过早关闭大括号,因此下面代码的执行实际上并不依赖于 if 语句。
应该是这样的
else if (message.content === "$help") {
let embed = new MessageEmbed()
.setTitle("Command List")
.setDescription("$help, $ping, $beep, $server, $user-info, $dm")
.setColor("RANDOM")
message.channel.send(embed)
}
答案 1 :(得分:1)
您必须放置此代码:
let embed = new MessageEmbed()
.setTitle("Command List")
.setDescription("$help, $ping, $beep, $server, $user-info, $dm")
.setColor("RANDOM")
message.channel.send(embed)
在这些花括号内:
} else if (message.content === "$help") {}
提示:改用 switch 语句