Updated GenerateDialogFunction()

This commit is contained in:
2018-10-21 02:50:09 +11:00
parent 1867eb3e3e
commit 9f7eb52ba5
2 changed files with 10 additions and 8 deletions

View File

@@ -53,14 +53,13 @@ client.on("message", function(msg) {
//DEBUGGING //DEBUGGING
let dialogJson = { let dialogJson = {
"hello": [ "hello": [
"Hi there {1}!", "Hi there {1} {2} {3} {2} {1}!"
"Howdy {1}!"
], ],
"goodbye": "See ya!" "goodbye": "See ya!"
} }
let dialog = shared.GenerateDialogFunction(dialogJson); //eventually be require("./dialog.json") let dialog = shared.GenerateDialogFunction(dialogJson); //eventually be require("./dialog.json")
console.log(dialog("hello", "Kayne")); console.log(dialog("hello", "Kayne", "Matthew", "Ruse"));
//actually log in //actually log in
client.login(process.env.DEVELOPER_TOKEN); client.login(process.env.DEVELOPER_TOKEN);

View File

@@ -23,7 +23,7 @@ exports.CloneArray = function(arg) {
//GenerateDialogFunction //GenerateDialogFunction
//dialogJson - the json object containing the bot's dialog //dialogJson - the json object containing the bot's dialog
exports.GenerateDialogFunction = function(dialogJson) { exports.GenerateDialogFunction = function(dialogJson) {
return function(key, data1 = "", data2 = "", data3 = "") { return function(key, ...data) {
let result; let result;
if (Array.isArray(dialogJson[key])) { if (Array.isArray(dialogJson[key])) {
@@ -32,10 +32,13 @@ exports.GenerateDialogFunction = function(dialogJson) {
result = dialogJson[key]; result = dialogJson[key];
} }
return result let counter = 0;
.replace(/\{1\}/g, data1) data.map((dat) => {
.replace(/\{2\}/g, data2) counter++;
.replace(/\{3\}/g, data3); result = result.replace(/\{([1-9][0-9]*)\}/g, a => a === "{" + counter + "}" ? dat : a);
});
return result;
} }
} }