我想这样做,以便如果我做[prefix] [command]
会产生与[mention bot] [command]
相同的效果,但是我创建命令和参数的方式使这一工作变得困难:
前缀存储为var prefix = '!3';
这就是我创建命令的方式:
bot.on('message', msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot)
return;
//the first message after '!13 '
//!
let args = msg.content.toLowerCase().substring(prefix.length).split(" ");
//^
//any capitalisation is allowed (ping,Ping,pIng etc.)
switch(args[1]) {
case 'ping': //if user inputs '!3 ping'
msg.channel.send('Pong!') //send a message to the channel 'Pong!'
}//switch (command) ends here
};//event listener ends here
答案 0 :(得分:2)
您可以有一个预定义的前缀列表,并在该列表上循环以确定msg是否具有该列表中的前缀。
let prefixList = ['!31 ', '!asdf ', `<@${bot.user.id}> `, `<@!${bot.user.id}> `]
function hasPrefix(str) {
for(let pre of prefixList)
if(str.startsWith(pre))
return true;
return false;
}
<@${bot.user.id}>
,<@!${bot.user.id}>
将设置机器人提及作为前缀。
答案 1 :(得分:0)
这是secretlyrice's
答案的简短版本:
const startsWithPrefix = (command) =>
['!prefix1 ', '!prefix2', <@botId>, <@!botId>].some(p => command.startsWith(p))