forked from mavrixwk/quagbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.js
340 lines (322 loc) · 14.3 KB
/
colors.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
///Template for new modules
//Author: Roger Lampe roger.lampe@gmail.com
var sf = require('./sharedFunctions.js');
var gw2api = require('./api.js');
var debug = false;
module.exports = function() {
var ret = {
addResponses: function(controller) {
controller.hears(['^colorpreview(.*)', '^cp(.*)', '^preview(.*)'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
var color = message.text.substring(message.text.indexOf(' ') + 1);
if (debug) sf.log("preview matches: " + JSON.stringify(color));
var cleanSearch = sf.removePunctuationAndToLower(color).replace(/\s+/g, '');
if (!color || cleanSearch.length === 0) {
bot.reply(message, "I didn't quite get that. Try 'help preview'.");
return;
}
if (debug) sf.log("find color: " + cleanSearch);
var colorsFound = [];
var exactMatch = [];
for (var i in gw2api.data.colors) {
var cleanColor = sf.removePunctuationAndToLower(gw2api.data.colors[i].name).replace(/\s+/g, '');
if (cleanColor.includes(cleanSearch)) {
colorsFound.push(gw2api.data.colors[i]);
}
if (cleanColor == cleanSearch) { //exact match cutout (for short names)
if (debug) sf.log('exact match color ' + cleanSearch);
exactMatch.push(gw2api.data.colors[i]);
}
}
if (exactMatch.length > 0) colorsFound = exactMatch;
var previewResponse = function(color) {
return rgbToHex(color.cloth.rgb) + " " + color.name;
};
if (debug) sf.log(colorsFound.length + " matches found for search string");
if (colorsFound.length === 0) { //no match
bot.reply(message, "No color by that name. Please check the spelling and try again.");
} else if (colorsFound.length == 1) { //exactly one. Ship it.
bot.reply(message, previewResponse(colorsFound[0]));
} else if (colorsFound.length > 10) { //too many matches in our 'contains' search, notify and give examples.
var itemNameList = [];
for (var n in colorsFound) {
itemNameList.push(colorsFound[n].name);
}
bot.reply(message, {
attachments: {
attachment: {
fallback: 'Too many items found in search.',
text: "Criminey. I found " + colorsFound.length + ' items. Get more specific.\n' + itemNameList.join("\n")
}
}
});
} else { //10 items or less, allow user to colorsFound
bot.startConversation(message, function(err, convo) {
var listofItems = '';
colorsFound.sort(function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
for (var i in colorsFound) {
listofItems += '\n' + [i] + ": " + colorsFound[i].name;
}
convo.ask('I found multiple colors with that name. Which number you mean? (say no to quit)' + listofItems, [{
//number, no, or repeat
pattern: new RegExp(/^(\d{1,2})/i),
callback: function(response, convo) {
//if it's a number, and that number is within our search results, print it
var matches = response.text.match(/^(\d{1,2})/i);
var selection = matches[0];
if (selection < colorsFound.length) {
convo.say(previewResponse(colorsFound[selection]));
} else convo.repeat(); //invalid number. repeat choices.
convo.next();
}
}, {
//negative response. Stop repeating the list.
pattern: bot.utterances.no,
callback: function(response, convo) {
convo.say('¯\\_(ツ)_/¯');
convo.next();
}
}, {
default: true,
callback: function(response, convo) {
// loop back, user needs to pick or say no.
convo.say("Hum, that doesn't look right. Next time choose a number of the color you'd like to see.");
convo.next();
}
}]);
});
}
});
controller.hears(['^color(.*)', '^mycolor(.*)', '^dye(.*)', '^mydye(.*)', '^joan$', '^joanrivers$'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
bot.reply(message, {
"text": "colors go!"
});
sf.setGlobalMessage(message);
//establish everyone or just current user.
var matches = message.text.match(/(my|joan)?(?:colors?|dyes?)?(cheme|filter)?(?: (.+)$)?/i);
if (debug) sf.log("Color matches: " + JSON.stringify(matches));
if (!matches) {
sf.replyWith("I didn't quite get that. Try 'help color'.");
return;
}
var usersToFetch;
var isJoan = (matches[1] && matches[1].toLowerCase() == 'joan');
//if "cheme" i.e. colorscheme set isScheme to true
var isScheme = ((matches[2] && matches[2].toLowerCase() == 'cheme') || isJoan);
var isFilter = ((matches[2] && matches[2].toLowerCase() == 'filter'));
var userSelectString = matches[3] || null;
//If single user, make usersToFetch a list of that user, otherwise leave blank to fetch all users
if ((matches[1] && (matches[1].toLowerCase() == 'my') || isJoan || isFilter)) usersToFetch = [message.user];
var singleUser = true;
if (isFilter) {
userSelectString = sf.removePunctuationAndToLower(userSelectString).replace(/\s+/g, '');
if(debug) sf.log("input category: "+userSelectString);
ret.reloadColorCategories();
if (ret.colorCategories.indexOf(userSelectString) < 0) {
sf.replyWith("Looking for an exact match on color category, guy. Here's a list.\n" + ret.colorCategories.join(", "));
return;
} else {
if (debug) sf.log("Valid color category: " + userSelectString);
}
}
sf.storageUsersGetSynch(usersToFetch)
.then(function(users) {
return sf.userHasPermissionsAndReply(users, "unlocks");
})
.then(function(validUsers) {
//if there's a list of user codes, filter out matching users
if (userSelectString && !isFilter) {
var requesterName = '';
var selectedUsers = [];
for (var c in validUsers) {
if (validUsers[c].id == message.user)
requesterName = "Hey, " + validUsers[c].dfid + sf.randomHonoriffic(validUsers[c].dfid, validUsers[c].id) + ". ";
if (userSelectString && userSelectString.indexOf(validUsers[c].dfid) > -1)
selectedUsers.push(validUsers[c]);
}
//remove doubles
selectedUsers = sf.arrayUnique(selectedUsers);
//If no user id argument or only invalid arguments, print list and return
if (selectedUsers.length < 1) {
var replyString = '';
for (var k in validUsers) {
replyString += '\n' + validUsers[k].dfid + ': ' + validUsers[k].name;
}
sf.replyWith(requesterName + "Here's a list of eligible players of color. You can see a report by string together their codes like 'colors rsja'." + replyString + '\nTry colors <string> again.');
return Promise.resolve(null);
} else
bot.reply(message, "(Using colors for " + selectedUsers.length + " players.)");
validUsers = selectedUsers;
}
singleUser = (validUsers.length < 2);
return validUsers;
})
.then(ret.getColorsForUsers)
.then(ret.getCommonColors)
.then(function(commonColors) {
var title = "No Dyes Whatsoever";
var icon = "http://a1.mzstatic.com/us/r30/Purple3/v4/a9/3b/d3/a93bd379-6be6-c487-894c-7046c4481b9b/icon175x175.png";
var text = "";
var colorText = [];
var colorRGB = [];
ret.colorLookups(commonColors, colorText, colorRGB, isFilter, userSelectString);
if (colorText.length > 0) {
if (!isScheme) { //show list of dyes
title = singleUser ? "Your " + (isFilter?userSelectString:sf.randomOneOf(['Oscar Season', 'spring', 'summer', 'fall', 'winter'])) + " palette of " + colorText.length + " colors!" : "All of the beautiful people are wearing:";
icon = singleUser ? "https://render.guildwars2.com/file/109A6B04C4E577D9266EEDA21CC30E6B800DD452/66587.png" : "https://render.guildwars2.com/file/E3EAA9D80D4216D1E092915AFD90C069CEE8E470/222694.png";
text = colorText.sort().join(", ");
sf.replyWith({
"username": "Joan Rivers' Ghost",
"icon_url": "https://dwonnaknowwhatithink.files.wordpress.com/2014/09/joan-rivers-4.jpg",
attachments: {
attachment: {
fallback: 'Look, Melissa! ' + colorText.length + ' dyes.',
title: title,
text: text,
thumb_url: icon
}
}
});
} else {
title = (singleUser ? "Your" : "Our") + " new Color Scheme:";
text += ret.generateColorScheme(colorText, colorRGB);
sf.replyWith({
"text": "*" + title + "*\n" + text
}, true);
ret.joanColorCommentary();
}
} else {
sf.replyWith({
"username": "Joan Rivers' Ghost",
"icon_url": "http://cdn2.holytaco.com/wp-content/uploads/2014/07/joan-rivers.jpg",
attachments: {
attachment: {
fallback: 'No dyes!',
title: title,
text: "There are no colors here.\nShut off the fucking camera.",
thumb_url: icon
}
}
});
}
})
.catch(function(error) {
sf.replyWith("I got an error that says " + error);
});
});
},
addHelp: function(helpFile) {
helpFile.mycolors = "Returns a list of dyes you've discovered";
helpFile.colors = "Returns a list of dyes common to all known users.";
helpFile.mycolorscheme = "Randomly picks 3 colors from the list of dyes you've discovered";
helpFile.colorscheme = "Randomly picks 3 colors from the list of dyes common to all known users.";
helpFile.dye = "Alias for color. Can be substituted in all color commands, like mydyes and dyescheme.";
helpFile.colorfilter = "Show your colors by category. entering a nonexistant category will output a list. Usage: colorfilter metal";
helpFile.preview = "Preview a color with the very inaccurate swatch. Example: preview antique gold";
helpFile.colorpreview = "Alias for preview: " + JSON.stringify(helpFile.preview);
helpFile.cp = "Alias for preview: " + JSON.stringify(helpFile.preview);
},
getColorsForUsers: function(validUsers) {
var userColorPromises = [];
for (var usr in validUsers)
if (validUsers[usr] !== null) {
if (debug) sf.log(validUsers[usr].name + " is a valid user");
userColorPromises.push(gw2api.promise.accountDyes(["all"], validUsers[usr].access_token));
}
if (debug) sf.log(userColorPromises.length + " account dye lists to fetch");
if (userColorPromises.length === 0)
return Promise.reject("there were no users with correct permissions.");
else
return Promise.all(userColorPromises);
},
getCommonColors: function(colorLists) {
if (colorLists === null) return Promise.resolve();
if (debug) sf.log("colorLists : " + colorLists.length);
//sort lists. Reduce to only common elements
colorLists.sort(function(a, b) {
return a.length - b.length;
});
var commonColors = colorLists.shift().filter(function(v) {
return colorLists.every(function(a) {
return a.indexOf(v) !== -1;
});
});
return commonColors;
},
colorLookups: function(commonColors, colorText, colorRGB, isFilter, userSelectString) {
if (isFilter && debug) sf.log("filtering colors for category " + userSelectString);
if (isFilter && debug) sf.log("list before: " + commonColors.length);
for (var id in commonColors) {
var color = gw2api.findInData("id", commonColors[id], "colors");
if (color && color.name) {
var temp = [];
if (isFilter && color.categories) {
for (var cat in color.categories)
temp.push(sf.removePunctuationAndToLower(color.categories[cat]));
}
if (!isFilter || temp.indexOf(userSelectString) > -1) {
colorText.push(color.name);
if (color.cloth && color.cloth.rgb)
colorRGB.push(color.cloth.rgb);
else
colorRGB.push([0, 0, 0]);
}
} else sf.log("Invalid color id: " + commonColors[id]);
}
if (isFilter && debug) sf.log("list after: " + colorText.length);
},
joanColorCommentary: function() {
var fashionSpice = ["crashing Elton John's", 'sneaking into a hit', 'perking up your', 'sprucing up an old', 'spicing up that', 'giving some oomph to my', 'your', 'that', 'my', 'our'];
var fashionAdj = ['Oscar', 'spring', 'summer', 'fall', 'winter', 'lobster', 'fancy-ass', 'casual', 'black tie'];
var fashionNoun = ['season', 'pregnancy', 'outfit', 'night', 'evening', 'gala', 'costume party', 'vacation', 'seance', 'afterlife'];
text = "What great colors for ";
if ((Math.floor(Math.random() * 20) > 17))
text += "Red Lobster's Lobsterfest, now featuring Ceaseless Shrimp and Bottomless Margarita Blasters! Red Lobster: Come for the food, leave! Back to you";
else
text += sf.randomOneOf(fashionSpice) + " " + sf.randomOneOf(fashionAdj) + " " + sf.randomOneOf(fashionNoun);
text += sf.randomOneOf([", Mellis... Lessdremoth!", ", Lessdremoth.", ", Lessy!", ", people!", ", fashion fans!", ", bitches!"]);
sf.replyWith({
"username": "Joan Rivers' Ghost",
"icon_url": "https://dwonnaknowwhatithink.files.wordpress.com/2014/09/joan-rivers-4.jpg",
"text": text
});
},
generateColorScheme: function(colorText, colorRGB) {
var text = "";
var index = Math.floor(Math.random() * colorText.length);
text += rgbToHex(colorRGB.splice(index, 1)[0]) + " " + colorText.splice(index, 1) + '\n';
index = Math.floor(Math.random() * colorText.length);
text += rgbToHex(colorRGB.splice(index, 1)[0]) + " " + colorText.splice(index, 1) + '\n';
index = Math.floor(Math.random() * colorText.length);
text += rgbToHex(colorRGB[index]) + " " + colorText[index];
return text;
},
reloadColorCategories: function() {
if (ret.colorCategories.length > 0) {
if (debug) sf.log("Already Collated. (" + ret.colorCategories.length + " color categories)");
return;
}
for (var colorIndex in gw2api.data.colors) {
for (var catIndex in gw2api.data.colors[colorIndex].categories) {
ret.colorCategories.push(sf.removePunctuationAndToLower(gw2api.data.colors[colorIndex].categories[catIndex]));
}
}
ret.colorCategories = sf.arrayUnique(ret.colorCategories);
sf.log("Collated " + ret.colorCategories.length + " color categories.");
},
colorCategories: []
};
return ret;
}();
//'private' functions
function rgbToHex(rgb) {
var componentToHex = function(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
};
if (debug) sf.log("RGB is: " + JSON.stringify(rgb));
return "#" + componentToHex(rgb[0]) + componentToHex(rgb[1]) + componentToHex(rgb[2]);
}