-
Notifications
You must be signed in to change notification settings - Fork 4
/
Raid.js
313 lines (283 loc) · 11.5 KB
/
Raid.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
const { pokelist } = require('./constant.json');
const pokemon = require('./pokemon.js');
const Discord = require('discord.js');
const Attendee = require('./Attendee.js');
const BotMessage = require('./BotMessage.js');
let config = {};
// Check for config file
if (process.argv[2]) {
try {
let configfile = './' + process.argv[2];
config = require(configfile);
} catch (error) {
config = require('./tester.json');
}
} else {
config = require('./tester.json');
console.error("No config file given. start with node Raider.js configFileName");
}
//config = require('./tester.json')
class Raid {
/**
* Raid object that represents a raid event. Creating one of these implies that a group of people will be heading to a gym to take on a raid.
* @param {String} id 2-Character ID that will represent the raid in the ActiveRaids array in the main program
* @param {String} time The time value to display for when the raid starts. Intentionally not actually a time.
* @param {String | Number} poke The pokemon identifier. Could be the name, could be the number.
* @param {String} location Where the raid is supposed to take place
* @param {Discord.User} owner The user who created the raid
* @param {Number} timeout Number of milliseconds the raid should exist. It will autodelete after the time expires.
* @param {Number} guests How many people the creator is bringing
*/
constructor(id, time, poke, location, owner, guests = 1) {
this.id = id;
this.time = time;
this.location = location;
this.gym = location;
this.locationComment = "";
this.poke = {};
this.setPokemon(pokemon.interpretPoke(poke));
this.poke.id = pokemon.interpretPoke(poke);
this.poke.name = pokelist[this.poke.id - 1] ? pokelist[this.poke.id - 1] : poke;
this.owner = owner;
this.expires = config.EXMon.includes(this.poke.id) ? Date.now() + config.timeoutEX : Date.now() + config.timeoutNormal;
/** @type {BotMessage[]} */
this.channels = [];
/** @type {Discord.Collection<string,Attendee>} */
this.attendees = new Discord.Collection();
// this.potential = {}; //TODO: "Maybe" a raid; potentially joining
// this.comments = {}; //TODO: Add in a way for users to add comments to the raid
this.addUserToRaid(owner, isFinite(guests) && guests > 0 ? guests : 1);
}
/**
* @returns {FlattenedRaid} Returns a "flattened raid" for saving to the disk. Doesn't save any of the actual discord stuff
*/
flatten() {
let raid = {
id: this.id,
time: this.time,
location: this.location,
gym: this.gym,
locationComment: this.locationComment,
poke: this.poke,
owner: { id: this.owner.id },
expires: this.expires,
channels: this.channels.map((bm) => bm.flatten()),
attendees: this.attendees.map((attendee) => {
return {
id: attendee.id,
count: attendee.count,
mention: attendee.mention,
here: attendee.here,
username: attendee.username
};
})
};
return raid;
}
/**
*
* @param {Discord.Channel} channel
* @param {Discord.Message} message
* @param {"info" | "reply" | "unknown" } type
*/
addMessage(channel, message, type = "unknown") {
this.channels.push(new BotMessage(channel, message, type));
};
setPokemon(poke) {
this.poke.id = pokemon.interpretPoke(poke);
this.poke.name = pokelist[this.poke.id - 1] ? pokelist[this.poke.id - 1] : poke;
}
/**
* @param {Discord.User} user
*/
userInRaid(user) {
return this.attendees.get(user.id);
};
/**
* Checks if the user owns the raid (or is me). Used in things like transfering, merging, and inactivating raids
* @returns True if the message is owned by this raid's owner, if the user is this raid's owner, or if it's from the admin.
* @param {Discord.Message | Discord.User} messageOrUser
*/
authorized(messageOrUser) {
if (messageOrUser instanceof Discord.Message) {
return (this.owner.id === messageOrUser.author.id || messageOrUser.author.id === '218550507659067392'); // Admin :)
} else if (messageOrUser instanceof Discord.User) {
return (this.owner.id === messageOrUser.id || messageOrUser.id === '218550507659067392');
}
};
/**
* Adds a user and guests to the raid
* @param {Discord.User | {id: string, username: string, count: number, mention?: string}} user
* @param {number} count
* @returns 0 if fail, user's count if success
*/
addUserToRaid(user, count = 1) {
// check if the user is already in the raid
let att = this.attendees.get(user.id);
if (att) {
// if the count is different
if (att.count != count) {
if (count == 0) {
this.removeFromRaid(user);
} else {
att.count = count;
}
this.updateInfo();
} else count = 0;
} else if (parseInt(count) > 0) {
this.attendees.set(user.id, new Attendee(user.id, user.username, user.mention || `<@${user.id}>`, count));
this.updateInfo();
}
return count;
};
updateInfo() {
this.channels.filter((botChan) => { return botChan.type === "info"; }).forEach((botChan) => {
botChan.message.edit({ embed: this.embed() }).catch((error) => console.error(error));
}, this);
}
/**
* Removes the user from the raid
* @param {Discord.User} user user to remove from the raid
*/
removeFromRaid(user) {
return this.attendees.delete(user.id);
}
/**
* Calculates how many people are in the raid
* @returns {Number} the total count of attendees registered for the raid
*/
total() {
var sum = 0;
this.attendees.forEach((val) => { sum += parseInt(val.count); });
return sum;
//return this.attendees.reduce((acc, val) => { return acc + val.count }, 0);
}
/**
* writes out a string for the raid
* @returns {String} stringified version of the Raid
*/
toString() {
let str = "";
str += `**Raid ID: ${this.id}**\n`;
str += `\tTime: ${this.time}\n`;
str += `\tPokemon: #${ this.poke.id} ${this.poke.name}\n`;
str += `\tLocation: ${this.location}\n`;
if (this.gym != this.location) {
str += `\tGym:${this.gym}\n`;
}
str += `\tOrganizer: ${this.owner}\n`;
str += `\tTotal Attendees: ${this.total()}`;
str += `\tAttendees:`;
str += this.listAttendees();
return str;
}
/** @returns {string} the roster of attendees with icon for here or not */
listAttendees() {
let str = "";
this.attendees.forEach((attendee) => {
str += `\t\t${attendee.here ? "✅" : "❓" }${attendee.count}\t - ${attendee.username}\t - ${attendee.mention}\n`;
});
return str;
}
/** @returns {string} the @mentions of all the attendees as a string. */
atAttendees() {
return this.attendees.map((attendee) => attendee.mention).join(", ");
}
/**
*
* @param {Discord.Client} client
* @param {Attendee | Discord.User} attendee
*/
toggleHere(client, attendeeOrUser) {
/** @type {Attendee} */
let attendee;
if (attendeeOrUser instanceof Discord.User) {
attendee = this.attendees.get(attendeeOrUser.id);
} else {
attendee = attendeeOrUser;
}
if (attendee && attendee.id && this.attendees.has(attendee.id)) {
attendee.here = !attendee.here;
this.updateInfo();
} else {
this.addUserToRaid(attendeeOrUser);
this.toggleHere(client, attendeeOrUser);
}
}
getUniqueChannelList() {
let botChannels = [];
for (const botChan of this.channels) {
botChannels = botChan.buildlist(botChannels);
}
return botChannels;
}
/**
*
* @param {Discord.Client} client
*/
sendStart(client, user) {
if (this.authorized(user)) {
this.messageRaid(`${user} has signaled to start the ${this.time} ${this.poke.name} raid at ${this.location}!`, client, true);
}
}
embed() {
let emb = new Discord.RichEmbed();
emb.setTitle("Raid Information");
emb.setColor(0xEE6600).setTimestamp();
if (pokelist[this.poke.id]) {
emb.setAuthor("RaiderBot_" + this.poke.name, "https://raw.githubusercontent.com/vutran/alfred-pokedex/master/data/sprites/" + (+this.poke.id) + ".png");
emb.setThumbnail("https://raw.githubusercontent.com/vutran/alfred-pokedex/master/data/sprites/" + (+this.poke.id) + ".png");
} else {
emb.setAuthor("RaiderBot", "https://s-media-cache-ak0.pinimg.com/originals/ca/4d/a5/ca4da5848311d9a21361f7adfe3bbf55.jpg");
emb.setThumbnail("https://s-media-cache-ak0.pinimg.com/originals/ca/4d/a5/ca4da5848311d9a21361f7adfe3bbf55.jpg");
}
let str = `**Time: ** ${this.time}
**Location: **${this.location}\n`;
if (this.gym != this.location) {
str += `**Gym: **: ${this.gym}\n`;
}
str += `**Pokemon: **[${this.poke.name}](https://pokemongo.gamepress.gg/raid-boss-counter/${this.poke.name}-raid-counter-guide)
**Total Attendees: **${this.total()}`;
//**Links:**`
emb.addField("Raid " + this.id, str, true);
emb.addField("Instructions", "#⃣: How many are you bringing?\n❌: leave the raid\n✅: \"Here!\"\n▶: Send start (only the raid's creator)", true);
//TODO: Fix listattendees for if the string is too long.
emb.addField("Attendees:", this.listAttendees() || "None");
return emb;
};
async messageRaid(fwdmessage, client, alert = false) {
try {
this.getUniqueChannelList().forEach(async(channel) => {
let message = await channel.send(`${this.atAttendees()}\n\n${fwdmessage}`);
message.delete(5 * 60 * 1000); // delete the message after 5 minutes
});
} catch (error) {
console.log(error);
}
if (alert) {
this.attendees.forEach((attendee) => {
client.users.get(attendee.id).createDM()
.then(
(dm) => {
dm.send(`Message from raid ${this.id} in the ${this.channels.map((botchan) => botchan.channel.toString()).join(", ")} channel(s)`).catch(error => console.log(error));
})
.catch((error) => console.log(error));
});
}
};
}
module.exports = Raid;
/**
* A flattened raid object
* @typedef {object} FlattenedRaid
* @property {string} id
* @property {string} time
* @property {string} location
* @property {string} gym
* @property {string} locationComment
* @property {{id: number, name: string}} poke
* @property {{id: string}} owner
* @property {number} expires
* @property {{channel:string, message:string, type: string}[]} channels
* @property {{id: string, count: number, mention:string , here: boolean, username: string}[]} attendees
*/