我正在制造一个机器人并将其托管在故障中。我希望前缀为“ 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包含的内容,所有不相关的部分都被删去了。
当我使用我的漫游器时会发生什么,我可以ping通,然后ping通。然后,我可以进行bping并且它将进行ping操作,而无需指定'b'是前缀。我该如何应对?
答案 0 :(得分:2)
我这样做的方法是检查邮件内容是否以前缀开头。下面,我粘贴了一些我的机器人使用的代码。主线是
D1
在这里,我检查邮件中是否包含我的前缀,如果包含,请检查它是否在邮件的开头。如果不是这种情况,我就退出该方法。
我的代码:
if (message.content.indexOf(config.prefix) !== 0) 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
});
行。这样可以防止您的漫游器对可能创建某种无限消息循环的其他漫游器做出响应