forked from Reginald-Gillespie/HackPackHelpBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmermaidParse.js
More file actions
69 lines (53 loc) · 2.36 KB
/
mermaidParse.js
File metadata and controls
69 lines (53 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Helper module to manage flowchart parsing
// If my flowcharts start using more advanced stuff, this file will need to be changed to support more.
const fs = require("fs")
function postProcessForDiscord(message, guild) {
message = String(message || "This answer appears to be undefined. Please try again later.");
message = message.replaceAll("#quot;", '"')
message = message.replaceAll("https:\\/\\/", "https://")
// Snowflake channel mentions
const channelMentionRegex = /#(\w[\w-]*)/g;
message = message.replace(channelMentionRegex, (match, channelName) => {
const channel = guild.channels.cache.find(c => c.name === channelName);
return channel ? `<#${channel.id}>` : match;
});
return message;
}
function matchMultipleGroups(content, regex=/./) {
// returns [ [match array], [match array] ]
const matchFinder = new RegExp(regex.source, "gm");
const matchParser = new RegExp(regex.source, "");
const results = [];
content.match(matchFinder)?.forEach(match => {
const parsed = match.match(matchParser);
results.push(parsed);
})
return results;
}
function getQuestionAndAnswers(chartJSON, currentQuestionID, currentAnswerID) {
// returns [{questionID, question}, [ answer1, answer2, ...]]
try {
let nextNodeId = null;
if (!currentQuestionID) {
// If no current ID was specified, find the entry point
nextNodeId = "Title";
} else {
// If there was an ID, find the next node along this path
const currentNode = chartJSON[currentQuestionID];
const selectedAnswer = currentNode.answers.find(answerObj => answerObj?.answer === currentAnswerID);
nextNodeId = selectedAnswer?.nextStep;
}
// If no question is provided, find the flowchart entrypoint
const nextNode = chartJSON[nextNodeId];
if (!nextNode) throw new Error("No entry node found");
// Format question and answer data as needed
const questionData = {
questionID: nextNodeId,
question: nextNode?.question
}
return [questionData, nextNode?.answers?.filter(answerObj => answerObj?.answer).map(answerObj => answerObj?.answer) ]
} catch {
return [ {}, [] ]
}
}
module.exports = { getQuestionAndAnswers, postProcessForDiscord }