Discord.js如何使用bot提和set前缀作为前缀

Discord.js如何使用bot提和set前缀作为前缀,discord,discord.js,prefix,mention,Discord,Discord.js,Prefix,Mention,我想这样做,如果我执行[prefix][command]操作,它将产生与[提及bot][command]相同的效果,但我创建命令和参数的方式使这变得很困难: 前缀存储为var prefix='!3'; 这就是我创建命令的方式: bot.on('message',msg=>{ 如果(!msg.content.startsWith(前缀)| | msg.author.bot) 回来 //“!13”之后的第一条消息 //! 设args=msg.content.toLowerCase().substri

我想这样做,如果我执行
[prefix][command]
操作,它将产生与
[提及bot][command]
相同的效果,但我创建命令和参数的方式使这变得很困难:

前缀存储为
var prefix='!3';

这就是我创建命令的方式:

bot.on('message',msg=>{
如果(!msg.content.startsWith(前缀)| | msg.author.bot)
回来
//“!13”之后的第一条消息
//!
设args=msg.content.toLowerCase().substring(prefix.length).split(“”);
//^
//允许任何资本化(ping、ping、ping等)
开关(参数[1]){
案例“ping”://如果用户输入“!3 ping”
msg.channel.send('Pong!')//向通道'Pong!'发送消息
}//开关(命令)到此结束
};//事件侦听器到此结束

您可以有一个预定义前缀的列表,并在该列表上循环,以确定消息是否具有该列表中的前缀

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;
}
let prefixList=['!31'、'!asdf'、``、`]
函数hasPrefix(str){
for(让prefixList的pre)
如果(str.startsWith(pre))
返回true;
返回false;
}

将设置bot-idention作为前缀。

您可以有一个预定义前缀列表,并循环该列表以确定消息是否具有该列表中的前缀

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;
}
let prefixList=['!31'、'!asdf'、``、`]
函数hasPrefix(str){
for(让prefixList的pre)
如果(str.startsWith(pre))
返回true;
返回false;
}

将设置bot-ntify作为前缀。

以下是
secretlyrice的
答案的简短版本:

const startsWithPrefix = (command) => 
['!prefix1 ', '!prefix2', <@botId>, <@!botId>].some(p => command.startsWith(p))
const startsWithPrefix=(命令)=>
['!prefix1','!prefix2',].some(p=>command.startsWith(p))

下面是
secretlyrice的回答的简短版本:

const startsWithPrefix = (command) => 
['!prefix1 ', '!prefix2', <@botId>, <@!botId>].some(p => command.startsWith(p))
const startsWithPrefix=(命令)=>
['!prefix1','!prefix2',].some(p=>command.startsWith(p))

回答很好:D帮了我的忙,Too我必须补充一点,我不建议使用bot-提提作为命令的主要前缀,因为显然Discord创建了一个与您的bot同名的角色。用户很容易无意中提到角色而不是bot。我上面提供的代码没有将role-提提作为前缀。这是我得到一个错误“`['!3','',”,]^TypeError:无法读取null的属性“id”``我建议您将这部分代码放在bot.on('message',…)或bot.on('read',…)中很好的回答:D帮了我的忙,我必须补充一点,我不建议使用bot-提提作为命令的主要前缀,因为显然Discord创建了一个与您的bot同名的角色。用户很容易无意中提到角色而不是bot。我上面提供的代码没有将role-提提作为前缀。这是一个错误r我得到了“`[”!3',“,”,]^TypeError:无法读取null的属性“id”``我建议您将这部分代码放在bot.on('message',…)或bot.on('read',…)中请为你的答案添加一些解释,以便其他人可以从中学习。请为你的答案添加一些解释,以便其他人可以从中学习