【发布时间】:2021-04-19 08:37:55
【问题描述】:
我正在尝试创建一个简单的不和谐机器人,目前正在使用 nodeJS。 我正在创建只有特定用户才能使用的特定命令,并且每当无权使用此类命令的人可以得到回复“您没有权限”。 (我希望你能明白。抱歉措辞不好)。
这是我当前的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '!';
client.once('ready', () => {
console.log('Bot is online');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ban' && message.author.id === "123456789"){
message.channel.send('suspended');
}
else{message.channel.send('no permission.')
;}
if(command === 'chat' && message.author.id === "123456789"){
message.channel.send('chat-restricted');
}
else{message.channel.send('no permission.')
;}
if(command === 'coins' && message.author.id === "123456789"){
message.channel.send('balance updated.');
}
else{message.channel.send('no permission.')
;}
if(command === 'coins 2' && message.author.id === "123456789"){
message.channel.send('balance updated.');
}
else{message.channel.send('no permission.')
;}
});
但实际情况是,每当有人使用命令时,是或否条件将显示 4 次,因为有 4 个命令。
因此,如果用户尝试使用 !ban 命令,输出将是
no permission
no permission
no permission
no permission
我很确定我在 if/else 条件下搞砸了一些东西,但我不确定它是什么.. 非常感谢您的帮助,我很抱歉措辞不好..
【问题讨论】:
标签: javascript node.js if-statement discord discord.js