-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
410 lines (360 loc) · 12 KB
/
index.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const Discord = require('discord.js');
const client = new Discord.Client();
const { token } = require('./config.json');
client.login(token);
const fetch = require("node-fetch");
// let func = require('./functions.js')
// Image url (Format)
// https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png
// 895 is maximum. Then jumps to 10001 => 10116
// 1011 total
let maxPokemon = 1011;
// =====================================================================
// =====================================================================
// ======================CHANGE WHEN SWITCHING SERVERS==================
let catchPokemonChannelId = '766560627954941963'
let pokemonInfoId = '766560317735829534'
// =====================================================================
// =====================================================================
let booted = false; // whether or not sprites have been loaded
let notStarted = false; // whether or not the player has started the game
let caught = false; // whether or not the last pokemon was caught
let guessed = false; // whether or not a player has guessed
let currentLevel = 0; // the current pokemon's level
let lastPokemonName = ''; // name of last sent pokemon by bot
let pokemonNames = []; // list of all pokemon names in db (loaded)
// let pokemonUrls = []; // list of all pokemon objects in db
// let pokemonSprites = []; // list of all pokemon sprites in db
// 18 Pokemon Types
let pokemonTypes = [
'normal',
'fire',
'water',
'electric',
'grass',
'ice',
'fighting',
'poison',
'ground',
'flying',
'psychic',
'bug',
'rock',
'ghost',
'dragon',
'dark',
'steel',
'fairy'
];
// Sprites are sent at minInterval * 1 min
let minInterval = 0.2;
// min, 60 seconds in min, 1000 milliseconds
const imgInterval = minInterval * 60 * 1000;
/*
TODO:
1. Battles AI (pick random pokemon from pokedex)
2. Figure out how to create player profiles and add
caught pokemon to a database.
3. Add '!pokedex' function.
4. Add modular api calls for fetchUrls.
5. Battle from own pokedex (random pokemon)
6. Battle with other players (1 pokemon)
*/
bootup();
// Event listener for incoming messages
client.on('message', async (message) => {
if (message.author.bot) {
return;
}
// adjusts message for easier parsing
msg = message.content.toLowerCase();
msg = msg.trim();
let splitMsg = msg.split(" ");
// First channel: channel set for catching pokemon
// Second channel: channel for pokemon info
if (message.channel.id === catchPokemonChannelId) {
// boots game: loads sprites and names list. else if trying to
// catch pokemon and pokemon hasn't been caught, check for user
// input and name equality
if (splitMsg[0] === '!start' && booted && !notStarted) {
notStarted = true;
console.log('executed');
// sends messages at the specified interval above
let interval = client.setInterval(async function() {
try {
caught = false;
guessed = false;
await sendRandomSprite(message);
} catch (error) {
console.log(error.stack);
}
}, imgInterval);
} else if (booted && splitMsg[0] === '!catch') {
if (!guessed) {
currentLevel = randomLevel();
guessed = true;
}
if (caught) {
message.channel.send('The pokemon has already been caught!');
} else if (splitMsg[1] === lastPokemonName) {
sendCaughtMessage(message);
} else {
sendFailedMessage(message);
}
}
} else if (message.channel.id === pokemonInfoId) {
// either print tutorial or checks if second user input is actually
// a pokemon type. otherwise, print incorrect format.
if (splitMsg[0] === '!p') {
if (msg === '!p') {
message.channel.send('-------------------------------------------------------------------------------------------------------' +
'\nUse me to find the strengths and weaknesses of Pokemon types!\n' +
'Type **`!p type [w]`** to get started.\n' +
'Pro tip: `!p type` will automatically display strengths!\n' +
'------------------------------------------------------------------------------------------------------------------------------');
} else if (pokemonTypes.includes(splitMsg[1])) {
if (splitMsg[2] == 'w' && splitMsg[2] == 'weakness') {
message.channel.send('.\n' + findWeaknesses(splitMsg[1]));
} else {
message.channel.send('.\n'+ findStrengths(splitMsg[1]));
}
} else {
message.channel.send('Format not recognized. Please use ' +
'`!p type [w]`');
}
}
}
});
/**
* Boots up catch channel: fetch api call will load pokemon names.
*/
async function bootup() {
await fetch('https://pokeapi.co/api/v2/pokemon?limit=1009')
.then(response => response.json())
.then(async function(allpokemon) {
await allpokemon.results.forEach(async function(pokemon) {
pokemonNames.push(pokemon.name);
});
});
console.log('booted');
booted = true;
}
/**
* Chooses and sends a random pokemon sprite from url with a randomly
* generated index. The index also corresponds to a name in the pokemon
* names list. The url will be autoadjusted (since the api is weird) by
* a constant so we do not make any bad requests.
* @param {Message} message Discord.js Message object for send call.
*/
async function sendRandomSprite(message) {
//let index = randomNumber(pokemonSprites.length);
let index = randomNumber(maxPokemon);
console.log(index);
lastPokemonName = pokemonNames[index];
console.log(pokemonNames[index]);
if (index > 895) {
index += 9107;
}
index++;
let sprite = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${index}.png`;
//message.channel.send('Who\'s that Pokemon??');
message.channel.send(sprite);
//message.channel.send('```Who\'s that Pokemon?```\n' +
// pokemonSprites[index]);
}
/**
* Generates a random index for sendRandomSprite use.
* @param {int} max Size of pokemon sprites list.
* @return Random index for accessing into sendRandomSprite.
*/
function randomNumber(max) {
return Math.floor(Math.random() * max);
}
/**
* Sends a congradulations message for catching a pokemon with a randomly
* generated level.
* @param {Message} message Discord.js Message object for send call.
*/
function sendCaughtMessage(message) {
let name = lastPokemonName.charAt(0).toUpperCase() +
lastPokemonName.slice(1);
message.channel.send(`Congratulations! You caught a level
${currentLevel} ${name}`);
caught = true;
}
/**
* Sends a failure message for incorrectly guessing the pokemon name
* with a randomly generated level.
* @param {Message} message Discord.js Message object for send call.
*/
function sendFailedMessage(message) {
let name = lastPokemonName.charAt(0).toUpperCase() +
lastPokemonName.slice(1);
message.channel.send('That\'s not right! You missed a level ' +
currentLevel + ' pokemon!');
caught = false;
}
/**
* Generates a random level for the generated pokemon.
*/
function randomLevel() {
return Math.floor(Math.random() * 100);
}
/**
* Returns a list of pokemon weaknesses for a given type.
* @param {String} type Input pokemon type.
* @return A concatenated list of all of type's weaknesses.
*/
function findWeaknesses(type) {
let text = '';
switch(type) {
case 'normal':
text = 'fighting\n'
break;
case 'fire':
text = 'water\nground\nrock'
break;
case 'water':
text = 'electric\ngrass'
break;
case 'electric':
text = 'ground'
break;
case 'ice':
text = 'fire\nfighting\nrock\nsteel'
break;
case 'fighting':
text = 'flying\npsychic\nfairy'
break;
case 'poison':
text = 'ground\npsychic'
break;
case 'ground':
text = 'water\ngrass\nice'
break;
case 'flying':
text = 'electric\nice\nrock'
break;
case 'psychic':
text = 'bug\nghost\ndark'
break;
case 'bug':
text = 'fire\nflying\nrock'
break;
case 'rock':
text = 'water\ngrass\nfighting\nground\nsteel'
break;
case 'ghost':
text = 'ghost\ndark'
break;
case 'dragon':
text = 'ice\ndragon\nfairy'
break;
case 'dark':
text = 'fighting\nbug\nfairy'
break;
case 'steel':
text = 'fire\nfighting\nground'
break;
case 'fairy':
text = 'poison\nsteel'
break;
case 'grass':
text = 'fire\nice\npoison\nflying\nbug'
break;
}
return text;
}
/**
* Returns a list of pokemon strengths for a given type.
* @param {String} type Input pokemon type.
* @return A concatenated list of all of type's strengths.
*/
function findStrengths(type) {
let text = '';
switch(type) {
case 'normal':
text = 'none'
break;
case 'fire':
text = 'grass\nice\nbug\nsteel'
break;
case 'water':
text = 'fire\nground\nrock'
break;
case 'electric':
text = 'water\nflying'
break;
case 'ice':
text = 'grass\nground\nflying\ndragon'
break;
case 'fighting':
text = 'normal\nice\nground\nflying\ndragon'
break;
case 'poison':
text = 'grass\nfairy'
break;
case 'ground':
text = 'fire\nelectric\npoison\nrock\nsteel'
break;
case 'flying':
text = 'grass\nfighting\nbug'
break;
case 'psychic':
text = 'fighting\npoison'
break;
case 'bug':
text = 'grass\npsychic\ndark'
break;
case 'rock':
text = 'fire\nice\nflying\nbug'
break;
case 'ghost':
text = 'psychic\nrock'
break;
case 'dragon':
text = 'dragon'
break;
case 'dark':
text = 'psychic\nghost'
break;
case 'steel':
text = 'ice\nrock\nfairy'
break;
case 'fairy':
text = 'fighting\ndragon\ndark'
break;
case 'grass':
text = 'water\nground\nrock'
break;
}
return text;
}
/*
Deprecated Functions. Found improvements in runtime.
async function bootup() {
await fetchNames();
for (let i = 0; i < pokemonUrls.length; i++) {
console.log(pokemonUrls[i]);
await fetchSprites(pokemonUrls[i]);
}
booted = true;
}
async function fetchNames() {
await fetch('https://pokeapi.co/api/v2/pokemon?limit=1009')
.then(response => response.json())
.then(async function(allpokemon) {
await allpokemon.results.forEach(async function(pokemon) {
pokemonNames.push(pokemon.name);
pokemonUrls.push(pokemon.url);
});
});
}
async function fetchSprites(url) {
await fetch(url)
.then(async (response) => await response.json())
.then(function(pokeData) {
pokemonSprites.push(pokeData.sprites.front_default);
});
}
*/