如何正确定义我的机器人的前缀,使其不响应任何单字母前缀?

How can I properly define my bot's prefix so it doesn't respond to any one-letter prefix?

我正在制作一个机器人并在出现故障时托管它。我希望前缀为 'a',但机器人会响应任何单个字母前缀。

{
 "prefix": "a",
 "devID": "443992049746968586"
}

这是我的 config.json 包含的内容。


//cmd handler
client.commands = new Discord.Collection();

fs.readdir("./commands/", (err, files) => {
    if (err) console.log(err);

    let jsfile = files.filter(f => f.split(".").pop() === "js")
    if(jsfile.length <= 0){
        console.log("Couldn't find commands")
        return;
    }
    jsfile.forEach((f, i) =>{
let props = require(`./commands/${f}`);
console.log(`${f} loaded`);
client.commands.set(props.help.name, props);
    });
});


client.on("message", msg =>{
    let messageArray = msg.content.split(" ");
    let cmd = messageArray[0];
    let args = messageArray.slice(1);
    let commandfile = client.commands.get(cmd.slice(config.prefix.length));
    if(commandfile) commandfile.run(client,msg,args);
})

这就是我的 index.js 包含的内容,删除了所有不相关的部分。
当我使用我的 bot 时会发生什么,我可以去 aping,它会发出 ping 信号。然后,我可以去 bping,它会 ping,无需我指定 'b' 是一个前缀。我该如何应对?

我的做法是检查消息内容是否以前缀开头。下面我粘贴了一些用于我的机器人的代码。主线是

if (message.content.indexOf(config.prefix) !== 0) return;

这里我检查消息是否包含我的前缀,如果是,它是否在消息的开头。如果不是这种情况,我只是 return 出了这个方法。

我的代码:

client.on("message", async message =>
{
    // Ignore messages from all bots
    if (message.author.bot) return;

    // Ignore messages which don't start with the given prefix
    if (message.content.indexOf(config.prefix) !== 0) return;

    // Split the message into the command and the remaining arguments
    const args = message.content.slice(config.prefix.length).trim().split(' ');
    const cmd = args.shift().toLowerCase();

    // Do stuff with your input here
});

最后一点,我强烈建议您在代码中也包含行 if (message.author.bot) return;。这可以防止您的机器人响应其他可能创建某种无限消息循环的机器人