I am trying to create a simple discord bot which sends random pokemon names with their images with a specific command. I was successful in sending the image and name but only one is of them is sent. This is the code that I am using
var random= Math.floor( Math.random() * 800 ) <br>
function getPoke() {<br>
return fetch("https://pokeapi.co/api/v2/pokemon/" + random)
.then( res => { <br>
return res.json() <br>
}
)
<br>
.then(data => {<br>
return data ["name"] +data["sprites"]["front_default"]<br>
}
)
<br>
}
if (msg.content === "$poke") { <br>
getPoke().then (pokemon=> msg.channel.send ( pokemon) );
If I only try with -
return data["sprites"]["front_default"]
It sends the image in the chat. Please help
Doing
return data["name"] +data["sprites"]["front_default"]
returns the name and the URL together, or combines them.
I think you would want to return the name and the URL with a space next to each other, so you would need a space in between them.
You can fix this by either doing return `${data.name} ${data.sprites.front_default}` or return data.name + " " + data.sprites.front_default.
I solved this problem by ->
var random= Math.floor( Math.random() * 800 )
function getPoke() {
return fetch("https://pokeapi.co/api/v2/pokemon/" + random)
.then( res => {
return res.json()
}
)
.then(data => {
return data ["name"]
}
)
}
if (msg.content === "$poke") {
getPoke().then(poek => msg.channel.send(poek,{
files:[
'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/'+random+'.png'
]
}
).then(
random=Math.floor(Math.random() *898)
));
}