-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (82 loc) · 2.67 KB
/
Copy pathapp.js
File metadata and controls
100 lines (82 loc) · 2.67 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const { ALLOW_FALLBACKS, ALLOW_RATING_OVERRIDE } = require("./constants")
const getSettings = require("./settings")
const getPossibleTargets = async (target, targetRating) => {
const { ratings } = await getSettings()
if (!ratings[targetRating]) throw new Error(`Invalid targetRating ${targetRating}!`)
let possibleTargets = []
for (let [key] of Object.entries(target)) {
if (ratings[targetRating].includes(key)) {
possibleTargets = possibleTargets.concat(target[key])
}
}
if (possibleTargets.length === 0) {
if (ALLOW_FALLBACKS) {
possibleTargets = target.general
} else {
throw new Error(`No values found!`)
}
}
return possibleTargets
}
const getRandomFrom = async (target, rating) => {
const possibleTargets = await getPossibleTargets(target, rating)
return possibleTargets[Math.floor(Math.random() * possibleTargets.length)]
}
const getSentence = async (rating) => {
const { sentences } = await getSettings()
return getRandomFrom(sentences, rating)
}
const buildFromVerbObjectCombo = async (rating) => {
const { names, verbObjectCombo } = await getSettings()
return `Well, ${await getRandomFrom(verbObjectCombo, rating)} and call me ${await getRandomFrom(
names,
rating
)}`
}
const buildFromVerbAndObject = async (rating) => {
const { verbs, objects, names } = await getSettings()
return `Well, ${await getRandomFrom(verbs, rating)} ${await getRandomFrom(
objects,
rating
)} and call me ${await getRandomFrom(names, rating)}`
}
const probabilities = [
{
weight: 10,
action: getSentence,
},
{
weight: 2,
action: buildFromVerbObjectCombo,
},
{
weight: 50,
action: buildFromVerbAndObject,
},
]
const makeEndpoint = (rating = "general") => async (req, res) => {
const { ratings } = await getSettings()
// If "text" parameter exists and is valid, override rating
if (ALLOW_RATING_OVERRIDE && req.body.text) {
const passedText = req.body.text.toLowerCase()
if (ratings[passedText]) {
rating = passedText
}
}
if (probabilities === undefined || probabilities[0] === undefined) {
throw new Error("Could not find probabilities!")
}
const totalWeight = probabilities.reduce((acc, probability) => acc + probability.weight, 0)
const targetWeight = Math.floor(Math.random() * totalWeight) // from 0 to (probabilties.length - 1)
for (let i = 0; i < totalWeight; i++) {
if (probabilities[i + 1] === undefined || targetWeight < probabilities[i].weight) {
res.json({
response_type: "in_channel",
text: await probabilities[i].action(rating),
})
return
}
}
throw new Error("Unknown error")
}
module.exports = makeEndpoint