Я создаю бот Discord, который позволит пользователям создавать собственные команды.
Он работает таким образом, что пользователь вводит "! Addcommand! Commandname: command value".Затем программа разделит строку и добавит! Commandname: command value в текстовый файл.Всякий раз, когда кто-то вводит! Commandname в разногласия, бот выводит «значение команды» в чат.
Программа должна проверять, существует ли новая команда при каждом запуске оператора if.Однако, похоже, что это проверка только при первом запуске программы, что приводит к тому, что новые команды не распознаются, пока программа не будет перезапущена.
Примечание:
Client.on прослушивает канал, и его содержимое запускается каждый раз, когда кто-то что-то говорит в чате.
! Команда addcommand работает правильно, и я могу подтвердить, что строкизаписывается в файл по назначению.
Не знаю, что еще попробовать.
Основной файл:
//Assume that requires etc are included
client.on('message', message => {
const pingCheck = message.content.charAt(0);
const commandCheck = message.content.split(" ");
if (pingCheck === "!") {
//Populates the list of custom commands. Must be done on every check, or new commands will not be recognized.
//Currently, this seems to only update once the service/program is restarted
var commandList = customCommands.returnPhrase();
//If the commandList object contains the correct key (stored in commandCheck[0]) such as !commandname, the bot will send the value "command value" as a string to the discord chat.
if (commandList.hasOwnProperty(commandCheck[0])) {
message.channel.send(commandList[commandCheck[0]]);
}
//If the key does not exist, the program then checks predefined commands. Other commands exist here, but for the purposes of this question I'll show only the !addcommand, which is used to create a new custom command.
else {
switch (commandCheck[0]) {
case "!addcommand":
//This checks that the command is formatted properly, "!commandname:commandvalue". If it does not start with ! or contain : somewhere in the string, it's probably an invalid format.
//Technically this still allows for a !:commandvalue format. I haven't implemented a check for this yet.
if (commandCheck[1].startsWith("!") && commandCheck[1].includes(":")) {
//While loop reconstructs the command key to be passed in, ignores slot 0 as this is the !addcommand
var gs = "";
var x = 1;
while (x < commandCheck.length) {
gs += gs +commandCheck[x] + " ";
x++;
}
gs = gs.slice(0,-1)+"\r\n"; //removes the last " " from the input string, and adds line-break
addCommands.addPhrase(gs);//passes reconstructed command to be added to commandlist.txt
message.channel.send("I have added " + commandCheck[1] + " to the command list.");
break;
}
default:
message.channel.send("I dont recognize that command.");
}
}
}
});
Модуль, которыйдобавляет команды:
const fs = require('fs');
var createCommand = {
addPhrase: function(x) {
fs.appendFile("commandlist.txt", x, function(err){
if(err) throw err;
console.log(err)
});
}
}
module.exports = createCommand;
Модуль, который заполняет список пользовательских команд:
const fs = require('fs');
var commandFile = fs.readFileSync('commandlist.txt','utf8');
var dataSplit = commandFile.split("\r\n");
var readCommand = {
returnPhrase: function(){
var splitSplit = {};
var i = 0;
//populates splitSplit with keys and values based on text file
while (i<dataSplit.length){
var newKey = dataSplit[i].split(':');
splitSplit[newKey[0]] = newKey[1];
i++
};
return splitSplit;
},
};
module.exports = readCommand;
Лучшая читаемость: https://repl.it/repls/DarkvioletDeafeningAutomaticparallelization
Ожидается: commandList заполняется каждый раз, если операторtriggered
Actual: commandList заполняется при первом запуске оператора