forked from mavrixwk/quagbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
characters.js
172 lines (159 loc) · 7.56 KB
/
characters.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
//Characters replated functions for lessdremoth
//Author: Roger Lampe [email protected]
var gw2api = require('./api.js');
var sf = require('./sharedFunctions.js');
var debug = false;
module.exports = function() {
var ret = {
addResponses: function(controller) {
controller.hears(['^deaths$', '^characters$'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (!user || !user.access_token || !sf.userHasPermission(user, 'characters')) {
bot.botkit.log('ERROR: characters: no access token: ' + JSON.stringify(user) + "err: " + JSON.stringify(err));
bot.reply(message, "Sorry, I don't have your access token " + (user && user.access_token && !sf.userHasPermission(user, 'characters') ? "with correct 'characters' permissions " : "") + "on file. Direct message me the phrase \'access token help\' for help.");
return;
}
gw2api.characters(function(jsonList) {
if (jsonList.text || jsonList.error) {
bot.reply(message, "Oops. I got this error when asking about characters: " + (jsonList.text ? jsonList.text : jsonList.error));
} else {
bot.reply(message, "I found " + Object.keys(jsonList).length + ' characters, ' + user.dfid + sf.randomHonoriffic(user.dfid, user.id));
var attachments = [];
var attachment = {
fallback: 'A character death report' + (user.name ? " for " + user.name : '') + '.',
color: '#000000',
thumb_url: "https://cdn4.iconfinder.com/data/icons/proglyphs-signs-and-symbols/512/Poison-512.png",
fields: [],
};
var totalDeaths = 0;
for (var n in jsonList) {
if (debug) bot.botkit.log("char :" + jsonList[n]);
attachment.fields.push({
value: jsonList[n].name + '\n' + (jsonList[n].race == 'Charr' ? 'Filthy Charr' : jsonList[n].race) + ' ' + jsonList[n].profession + ' ' + jsonList[n].level,
title: jsonList[n].deaths,
short: true,
});
totalDeaths += jsonList[n].deaths;
}
attachment.title = 'Death Report: ' + totalDeaths + ' total deaths.';
attachments.push(attachment);
bot.reply(message, {
attachments: attachments,
}, function(err, resp) {
if (err || debug) bot.botkit.log(err, resp);
});
}
}, {
access_token: user.access_token,
ids: "all"
}, true);
});
});
////PROFESSION REPORT
controller.hears(['^professionReport$', '^pr$'], 'direct_message,direct_mention,mention,ambient', function(bot, message) {
//Setup variables
var num = 0;
var goodUsers = [];
var classes = [];
//once all users are loaded, correlate their professions.
var professionReportCallback = function(jsonData, headers) {
var name;
//save this user classes and name
for (var z in goodUsers) {
if (headers && headers.options && headers.options.access_token && headers.options.access_token == goodUsers[z].access_token && goodUsers[z].name) {
name = goodUsers[z].name;
if (jsonData.error || jsonData.text) {
sf.replyWith("I got an error looking up the data for " + name + ". They will be omitted from the results.", true);
bot.botkit.log("error: " + jsonData.error + "\ntext: " + jsonData.text);
//no need to exit. it will find nothing in jsonData and exit, unless this is the last one, then it will assemble the report.
goodUsers[z].error = true;
}
break;
}
}
for (var c in jsonData) {
if (jsonData[c].profession) {
if (!classes[jsonData[c].profession])
classes[jsonData[c].profession] = {
num: 0,
user: []
};
classes[jsonData[c].profession].num++;
classes[jsonData[c].profession].user.push(name);
} else bot.botkit.log("Unknown profession?" + JSON.stringify(jsonData[c]));
}
//after all users are done, spit out report
if (++num == goodUsers.length) {
var acceptableQuaggans = [
"https://static.staticwars.com/quaggans/helmut.jpg",
"https://static.staticwars.com/quaggans/knight.jpg",
"https://static.staticwars.com/quaggans/hoodie-up.jpg",
"https://static.staticwars.com/quaggans/lollipop.jpg"
];
acceptableQuaggans = sf.arrayUnique(acceptableQuaggans);
//remove errored users
for (var e in goodUsers)
if (goodUsers[e].error)
goodUsers.splice(e, 1);
var pretextString = '';
len = goodUsers.length;
for (var i = 0; i < len; i++) {
pretextString += goodUsers[i].name;
if (i == len - 2) pretextString += " and ";
else if (i !== len - 1) pretextString += ", ";
}
if (len == 1) pretextString += " (all alone)";
var fieldsFormatted = [];
for (var s in classes) {
var plural = (s == 'Thief' ? 'Thieves' : s + 's');
fieldsFormatted.push({
"title": classes[s].num + ' ' + (classes[s].num != 1 ? plural : s),
"value": classes[s].user.join(", "),
"short": true
});
}
var attachments = [];
var attachment = { //assemble attachment
fallback: 'A Profession Report',
color: '#000000',
thumb_url: sf.randomOneOf(acceptableQuaggans),
fields: fieldsFormatted,
};
attachments.push(attachment);
sf.replyWith({
text: "Collating the professions of: " + pretextString + ".",
attachments: attachments,
}, false);
}
};
//fetch access tokens from storage
controller.storage.users.all(function(err, userData) {
for (var u in userData) {
//remove those without permissions
if (userData[u].access_token && sf.userHasPermission(userData[u], 'characters')) {
goodUsers.push(userData[u]);
}
}
//goodUsers is now a list of users with good access tokens
bot.botkit.log(goodUsers.length + " of " + userData.length + " users were elegible for profession report.");
//If no user id argument or only invalid arguments, print list and return
bot.reply(message, "Professions? Hang on.");
sf.setGlobalMessage(message);
for (var g in goodUsers) {
gw2api.characters(professionReportCallback, {
access_token: goodUsers[g].access_token,
ids: 'all'
}, true);
}
});
});
},
addHelp: function(helpFile) {
helpFile.professionReport = "Collate all known accounts characters by profession";
helpFile.pr = "Alias for professionReport. " + JSON.stringify(helpFile.professionReport);
helpFile.deaths = "Display a report of characters on your account, and their career deaths.";
helpFile.characters = 'Alias for character deaths. ' + JSON.stringify(helpFile.characterDeaths);
}
};
return ret;
}();