forked from mavrixwk/quagbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquadgoals.js
133 lines (122 loc) · 5.36 KB
/
squadgoals.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
//Template for new modules
//Author: Roger Lampe [email protected]
var sf = require('./sharedFunctions.js');
var gw2api = require('./api.js');
var colors = require('./colors.js');
var dungeonFreqenter = require('./dungeonFrequenter.js');
var debug = false;
module.exports = function() {
var ret = {
addResponses: function(controller) {
controller.hears(['^squadgoals(.*)', '^sg(.*)', '^dp(.*)', '^dungeonparty(.*)'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
//pre-reqs
if (!gw2api.loaded.achievements || !gw2api.loaded.achievementsCategories) {
bot.reply(message, "I'm still loading achievement data. Please check back in a couple of minutes. If this keeps happening, try 'db reload'.");
sf.setGlobalMessage(message);
return;
}
//get dungeon frequenter achievement
var dungeonFrequenterCheevo = gw2api.findInData('name', 'Dungeon Frequenter', 'achievements');
if (!dungeonFrequenterCheevo) {
bot.reply(message, "I couldn't find the Dungeon Frequenter achievement in my loaded data. Try 'db reload'.");
return;
}
sf.setGlobalMessage(message);
//establish everyone or just current user.
var matches = message.text.match(/(?:squadgoals|dungeonparty|dp|sg)(?: (\w+)$)?/i);
if (debug) sf.log("dp matches: " + JSON.stringify(matches));
if (!matches) {
sf.replyWith("I didn't quite get that. Try 'help dungeonparty'.");
return;
}
var userSelectString = matches[1] || null;
//fetch all color permissions from disc
sf.storageUsersGetSynch()
.then(function(users) {
return sf.userHasPermissionsAndReply(users, 'unlocks', 'progression');
})
//get selected/valid users
.then(function(validUsers) {
if (userSelectString) {
var requesterName = 'Friend';
var selectedUsers = [];
for (var c in validUsers) {
if (validUsers[c].id == message.user)
requesterName = "Yo, " + validUsers[c].dfid + sf.randomHonoriffic(validUsers[c].dfid, validUsers[c].id) + ". ";
if (userSelectString && userSelectString.indexOf(validUsers[c].dfid) > -1)
selectedUsers.push(validUsers[c]);
}
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 squadgoalers. 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, requesterName + ", There are " + selectedUsers.length + " players with correct permissions.");
//remove doubles
validUsers = selectedUsers;
}
return validUsers;
})
.then(function(validUsers) {
return Promise.all([
//get colors
colors.getColorsForUsers(validUsers)
.then(colors.getCommonColors)
.then(function(commonColors) {
var colorText = [];
var colorRGB = [];
colors.colorLookups(commonColors, colorText, colorRGB);
var text = colors.generateColorScheme(colorText, colorRGB);
bot.reply(message, "*Wear:*\n" + text);
}),
//get dungeon
dungeonFreqenter.getCheevosForUsers(validUsers)
.then(function(jsonData) {
if (debug) sf.log("userCheevos size:" + jsonData.length);
var bitsArrays = [];
for (var u in jsonData) {
for (var c in jsonData[u]) { //since an APi update, jsonData will always be only one long. Leaving this in in case of reversion.
if (jsonData[u][c].id && jsonData[u][c].id == dungeonFrequenterCheevo.id && jsonData[u][c].bits && jsonData[u][c].bits.length > 0) {
if (debug) sf.log("user " + u + "'s bits array:" + JSON.stringify(jsonData[u][c].bits));
bitsArrays = bitsArrays.concat(jsonData[u][c].bits);
break;
}
}
}
//reduce to bits in common
bitsArrays = sf.arrayUnique(bitsArrays);
if (debug) sf.log("final bits array:" + JSON.stringify(bitsArrays));
var candidates = [];
for (var bit in dungeonFrequenterCheevo.bits) {
if (bitsArrays.indexOf(bit) < 0)
candidates.push(dungeonFrequenterCheevo.bits[bit]);
}
//pick a random dungeon, and spit it out
bot.reply(message, "*Where:*\n" + dungeonFreqenter.dungeonNames[sf.randomOneOf(candidates).text]);
})
]);
})
.then(function() {
colors.joanColorCommentary();
})
.catch(function(error) {
sf.replyWith("I got an error that says " + error);
});
});
},
addHelp: function(helpFile) {
//helpFile.command = ...
helpFile.dungeonparty = "Recommend a dungeon party. Takes input like dungeonfreqenter and uses access token data to give you a dungeon everyone could use and a color pallete everyone can wear. Example \'dp ahrj\'";
helpFile.dp = "Alias for dungeonparty. " + JSON.stringify(helpFile.dungeonparty);
helpFile.squadgoals = "Alias for dungeonparty. " + JSON.stringify(helpFile.dungeonparty);
helpFile.sg = "Alias for dungeonparty. " + JSON.stringify(helpFile.dungeonparty);
}
};
return ret;
}();
//'private' functions