-
Notifications
You must be signed in to change notification settings - Fork 0
/
scryfall.js
37 lines (32 loc) · 1.17 KB
/
scryfall.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
const request = require('request-promise')
const scryfallUrl = 'https://api.scryfall.com/cards/search?include_multilingual=true&'
exports.findCards = (query) => {
query = scryfallUrl + `q=${encodeURI(query)}`
return new Promise((resolve, reject) => {
request(query)
.then((response) => {
let possibleCards = JSON.parse(response)["data"]
let cardNames = getCardNamesFromLanguages(possibleCards)
resolve(cardNames)
})
.catch((err) => {
if(err.statusCode == 404) {
reject ({
success: false,
message: 'Não foram encontradas cartas com esse nome no banco de dados do Scryfall. Por favor, tente novamente com outros termos.'
})
} else {
console.log(err)
reject({
success: false
})
}
})
})
}
let getCardNamesFromLanguages = (cards, languages = ['en', 'pt', 'es']) =>
filterCardsFromLanguages(cards, languages).map(card =>
[{ "text": (card.printed_name || card.name), "callback_data": card.name, "id": card.id }]
)
let filterCardsFromLanguages = (cards, languages) =>
cards.filter((card) => languages.includes(card.lang))