-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeHelp.js
142 lines (124 loc) · 4.66 KB
/
theHelp.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/** ============================================================================ *
== == == == == == == == == == HELPER FUNCTIONS == == == == == == == == == ==
** ============================================================================ */
/**
* Retreives just the first name of the user sending a chat's message.
* @param {object} event The event object from Hangouts Chat.
* @return {string} The first name from the user's chat display name.
*/
const _getFirstName = (event) => event.user.displayName.split(' ', 2)[0]
/**
* Returns a random element from an array.
* @param {array} choices An array holding the possible choices to return.
* @return {any} An array element from choices.
*/
const choose = (choices) => choices[Math.floor(Math.random() * choices.length)]
/**
* Generates a random integer between min and max.
* @param {integer} min The minimum included in the range.
* @param {integer} max The maximum excluded from the range.
* @return {integer} The number on a roll, or series of rolls.
*/
const getRandomInt = (min, max) => {
// Round up/down:
mini = Math.ceil(min)
maxi = Math.floor(max)
// The maximum is exclusive and the minimum is inclusive:
return Math.floor(Math.random() * (maxi - mini) + mini)
}
/**
* Returns a string in titleCase.
* @param {string} str The string to format to titleCase.
* @return {string} The sentence in titleCase.
*/
const toTitleCase = (str) => {
if((str===null) || (str==='')) {
return false
}
else {
str = str.toString()
}
return str.replace(/\w\S*/g, (x) => x.charAt(0).toUpperCase() + x.substr(1).toLowerCase())
}
/**
* Creates a Chat's message card with a clickable image widget.
* @param {string=} imgUrl An image link for the image to display.
* @param {string} link The URL link that gets launched when the image is clicked.
* @return {object} A chat bot card response message.
*/
const createImgCard = (imgUrl="https://example.com/kitten.png",
link="https://example.com/",
headerTitle='Boop Bop Bot',
headerSubtitle='beep booboo bop',
headerImgUrl="") => {
const card = { "cards": [
{
"header": {
"title": `<u><b>${headerTitle}</b></u>`,
"subtitle": headerSubtitle,
"imageUrl": headerImgUrl
},
"sections": [
{
"widgets": [{
"image": {
"imageUrl": imgUrl,
"onClick": {
"openLink": {
"url": link
}
}
}
}]
}
]
}
]
}
return card
}
/**
* Generates a random number to simulate a dice roll. The 'roll' slash command also accepts
* additional arguments: number of dice and type of die. (i.e. /roll 2d3)
* @param {object} event The Google Chat event object.
* @return {string} A response with the total of the roll, and if multiple rows a list of
* each result.
*/
const rollDice = (event) => {
// The string we'll return:
let result = ""
// Grab sender's first name for return message:
const firstName = _getFirstName(event)
// Boolean of whether the event object has the 'argumentText' property:
const hasArgs = event.hasOwnProperty('argumentText')
// [CASE] No Arguments provided --> Create fake one:
if (!hasArgs) {
result = `${firstName} rolled a ${getRandomInt(1, 7)}`
}
// [CASE] Argument provided:
else {
// Remove whitespace from arguments:
let args = event.message.argumentText.trim()
// Split at every character to look for 'd'
// [CASE] Contains 'd':
if (args.split('').includes('d')) {
let numOf = 0, numSides = 0, sum = 0, rollsList = Array()
let splitAtD = args.split('d')
numOf = parseInt(splitAtD[0])
numSides = parseInt(splitAtD[1])
// Roll all those dice!
for (let i = 0; i < numOf; i++) {
let rollNum = getRandomInt(1, numSides+1)
sum += rollNum
rollsList.push(rollNum)
}
result = `\`\`\`${firstName} rolled: [${rollsList.toString()}]\n` +
`Total: ${sum}\`\`\``
}
// [CASE] No 'd' --> Argument text not formatted correctly:
else {
result = `Sorry, I don't recognize that. Try '/roll 3d10' to roll three 10-sided dice.`
}
}
return result
}