-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
321 lines (297 loc) · 11 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
import { config } from "dotenv";
import * as discord from "discord.js";
import { main } from "./commands/commandBuilder.js";
import { typeMap } from "./data/icons.js";
import { typeWS, typeWS2 } from "./data/stren_weak.js";
import { InteractionResponseType } from "discord.js";
config();
const BOT_TOKEN = process.env.BOT_TOKEN;
const client = new discord.Client({
intents: [
discord.GatewayIntentBits.Guilds,
discord.GatewayIntentBits.GuildMessages,
discord.GatewayIntentBits.MessageContent
]
});
const base = `https://pokeapi.co/api/v2/pokemon/`;
const base2 = `https://pokeapi.co/api/v2/pokemon-species/`;
function start() {
client.login(BOT_TOKEN);
console.log(` DEX.IO is successfully logged ┃✅`)
}
start();
client.on("interactionCreate", (interaction) => {
try {
if (interaction.isChatInputCommand()) {
const pokename = interaction.options.getString("name").toLowerCase();
const getpokemon = async () => {
const newbase = base + pokename;
const newbase2 = base2 + pokename;
const pokemonData = await fetch(newbase);
const pokemonData2 = await fetch(newbase2);
const pokemon = await pokemonData.json();
const pokemon2 = await pokemonData2.json();
botreply(pokemon, pokemon2);
};
getpokemon(pokename)
.then((response) => {
return response;
})
.catch((error) => {
if (error.message.includes("404")) {
interaction.reply({
content: `${interaction.user} OOPS❗please make sure you have spelled your prompt correctly`
});
}
});
function botreply(pokemon, pokemon2) {
const name = pokemon.name;
let color = "";
let iconID = "";
let iconID2 = "";
let type = "";
let type2 = "";
let bar = "";
let spacing = "ㅤㅤ";
let flavor_text = "";
if (pokemon.types.length === 2) {
type = pokemon.types[0].type.name;
type2 = pokemon.types[1].type.name;
} else {
type = `${pokemon.types[0].type.name}`;
type2 = "ㅤ";
spacing = "ㅤㅤㅤㅤㅤㅤㅤㅤ";
}
const typeData = typeMap[type.toLowerCase()] || {};
iconID = typeData.iconID || "";
color = typeData.color || "";
bar = typeData.bar || "";
const typeData2 = typeMap[type2.toLowerCase()] || {};
iconID2 = typeData2.iconID || "";
try{
interaction.reply({
content: `${interaction.user}`,
embeds: [
new discord.EmbedBuilder()
.setColor(color)
.setTitle(`${name.charAt(0).toUpperCase() + name.slice(1)}`)
.setURL(
`https://bulbapedia.bulbagarden.net/wiki/${pokemon.name}_(Pokémon)`
)
.setDescription(`${pokemon2.flavor_text_entries[0].flavor_text}`)
.setImage(
`${pokemon.sprites.other["official-artwork"].front_default}`
)
.addFields(
{
name: "ㅤ",
value: `${spacing}${iconID} **${type}**`,
inline: true
},
{
name: "ㅤ",
value: `ㅤㅤ${iconID2} **${type2}**`,
inline: true
}
)
.setFooter({ text: `#${pokemon.id}` })
],
components: [
{
type: 1,
components: [
{
type: 2,
style: 2,
label: "📊 Stats",
custom_id: "statistics"
},
{
type: 2,
style: 2,
label: "⚡️ Evolutions",
custom_id: "evolutions"
},
{
type: 2,
style: 2,
label: "👍 Strengths/👎 Weaknesses",
custom_id: "sw"
}
]
}
]
})
}catch(err) {
console.log(`this is the error ${interaction.message}`)
}
}
return;
} else if (interaction.isButton()) {
const custom_id = interaction.component.customId;
const title = interaction.message.embeds[0].data.title;
fetch(
`https://pokeapi.co/api/v2/pokemon/${title.toLocaleLowerCase()}`
)
.then((response) => response.json())
.then((data) => {
const type = data.types[0].type.name;
const type2 = data.types[1].type.name;
const typeData = typeMap[type.toLowerCase()] || {};
let color = typeData.color || "";
let iconID = typeData.iconID || "";
switch (custom_id) {
case "statistics":
interaction.reply({
embeds: [
new discord.EmbedBuilder()
.setColor(color)
.setThumbnail(data.sprites.front_default)
.setDescription("ㅤ")
.addFields(
{
name: "HP",
value: `${data.stats[0].base_stat}\n\ㅤ`,
inline: true
},
{
name: "ATK",
value: `${data.stats[1].base_stat}\n\ㅤ`,
inline: true
},
{
name: "DEF",
value: `${data.stats[2].base_stat}\n\ㅤ`,
inline: true
},
{
name: "SP.ATK",
value: `${data.stats[3].base_stat}\n\ㅤ`,
inline: true
},
{
name: "SP.DEF",
value: `${data.stats[4].base_stat}\n\ㅤ`,
inline: true
},
{
name: "SPD",
value: `${data.stats[5].base_stat}\n\ㅤ`,
inline: true
}
)
]
});
break;
case "evolutions":
fetch(`https://pokeapi.co/api/v2/evolution-chain/${data.id}`)
.then(response => response.json()).then(pokemon => {
interaction.reply({content: `${pokemon.chain.species.name}`})
}).catch(err => console.log("evolustion",err));
break;
case "sw":
let strengths;
let weaknesses;
if (data.types.length === 2) {
let dualTypes
for (let key in typeWS2){
if (key === `${type}/${type2}`){
dualTypes = `${type}/${type2}`
}else if (key ===`${type2}/${type}`) {
let arr = Array.from(interaction.message.embeds[0].data.fields[0].value)
let arr2 = arr.filter((letter , index)=> index > 32 && index< 38)
let singleType = arr2.join("")
dualTypes = `${type2}/${type}`
}
}
let affinData = typeWS2[dualTypes.toLowerCase()];
strengths = affinData.strength ;
weaknesses = affinData.weakness ;
} else if (data.types.length===1){
let affinData = typeWS[singleType];
strengths = affinData.strength;
weaknesses = affinData.weakness;
}
interaction.reply({
content: `**${title} | ${data.generation} | strengths and weaknesses** ${interaction.user}`,
embeds: [
new discord.EmbedBuilder().setImage(data.sprites.front_default).setColor(color).setFields(
{
name: "ㅤ",
value: `<:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643> **Strengths** <:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643>\n\ㅤ`,
inline: false
},
{
name: `${strengths[0]}`,
value: "ㅤ",
inline: true
},
{
name: `${strengths[1]}`,
value: "ㅤ",
inline: true
},
{
name: `${strengths[2]}`,
value: "ㅤ",
inline: true
},
{
name: `${strengths[3]}`,
value: "ㅤ",
inline: true
},
{
name: `${strengths[4]}`,
value: "ㅤ",
inline: true
},
{
name: "ㅤ",
value: `<:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643> **Weaknesses** <:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643><:grass2:1071586457174679643>\n\ㅤ`,
inline: false
},
{
name: `${weaknesses[0]}`,
value: "ㅤ",
inline: true
},
{
name: `${weaknesses[1]}`,
value: "ㅤ",
inline: true
},
{
name: `${weaknesses[2]}`,
value: "ㅤ",
inline: true
},
{
name: `${weaknesses[3]}`,
value: "ㅤ",
inline: true
},
{
name: `${weaknesses[4]}`,
value: "ㅤ",
inline: true
}
)
]
});
break;
default:
console.log("Unknown");
break;
}
})
.catch((error) =>
interaction.reply({content:` OOPs!!⚠️ this is the error comming from fetching data in the buttons interaction ⚠️ : ${error} ${typeMap.type2}`})
);
}
} catch (e) {
console.log(e);
console.log(discord.Client.captureRejections);
}
});
main();