[英]Discord.js Bot disregards prefix and will respond to anything infront
我已經讓這個機器人工作了一段時間,由於某種原因,該機器人現在會響應它前面的任何前綴,而不是設置的前綴。
const PREFIX = '$';
bot.on('message', message => {
let argus = message.content.substring(PREFIX.length).split(" ");
switch (argus[0]) {
case 'yeet':
message.channel.send("yeet")
break;
}
});
在您的代碼中,您無需檢查消息是否以前綴開頭。 因此,您的代碼針對每條消息執行,並且如果命令在子字符串之后具有相同的PREFIX
長度,它將觸發該命令。
更正的代碼:
// Example prefix.
const PREFIX = '!';
bot.on('message', message => {
// Ignore the message if it's from a bot or doesn't start with the prefix.
if (message.author.bot || !message.content.startsWith(PREFIX)) return;
// Take off the prefix and split the message into arguments by any number of spaces.
const args = message.content.slice(PREFIX.length).split(/ +/g);
// Switching the case of the command allows case iNsEnSiTiViTy.
switch(args[0].toLowerCase()) {
case 'yeet':
message.channel.send('yeet')
// Make sure to handle any rejected promises.
.catch(console.error);
break;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.