From 0e208d5fa31ef630f19ef070196091e3c3270411 Mon Sep 17 00:00:00 2001 From: "Bruno R.Novais" Date: Wed, 27 Oct 2021 22:51:56 -0300 Subject: [PATCH 1/3] Added HearthStone Api Wrapper and configured envs and git ignore files --- .env.example | 1 + .gitignore | 6 +++++ src/apis/HearthStone.js | 51 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/apis/HearthStone.js diff --git a/.env.example b/.env.example index b2b0157e9..53514b2b1 100644 --- a/.env.example +++ b/.env.example @@ -42,3 +42,4 @@ MERRIAM_WEBSTER_API_KEY=1293a33-4731-34cb-69a3-178a39b7c23 GITHUB_USER=SwitchbladeBot GITHUB_REPOSITORY=switchblade GITHUB_BRANCH=master +HEARTHSTONE_RAPIDAPI_KEY=183320a28emsh45e274654308a94p1664b2jsn7440338857cf diff --git a/.gitignore b/.gitignore index 94e24f1dd..9f07cb0ac 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,12 @@ typings/ # next.js build output .next +# yarn files +yarn.lock + +#prettier files +.prettierrc + .vscode .idea/ diff --git a/src/apis/HearthStone.js b/src/apis/HearthStone.js new file mode 100644 index 000000000..45d84358c --- /dev/null +++ b/src/apis/HearthStone.js @@ -0,0 +1,51 @@ +const { APIWrapper } = require('../') +const axios = require('axios') + +const endpoint = 'https://omgvamp-hearthstone-v1.p.rapidapi.com' + +module.exports = class HearthStone extends APIWrapper { + constructor() { + super({ + name: 'hearthstone', + envVars: ['HEARTHSTONE_RAPIDAPI_KEY'] + }) + } + + // Get Card by Name + /** + * @param {string} name the name of the card + */ + + async getCardByName(name) { + const options = { + method: 'GET', + url: `${endpoint}/cards/${name}`, + headers: { + 'x-rapidapi-host': `${endpoint}`, + 'x-rapidapi-key': `${process.env.HEARTHSTONE_RAPIDAPI_KEY}` + } + } + return await axios(options) + .then((res) => res.data) + .catch((err) => console.error(err)) + } + + // Get Card by Id + /** + * @param {int} id the integer of the card + */ + + async getCardById(id) { + const options = { + method: 'GET', + url: `${endpoint}/cards/${id}`, + headers: { + 'x-rapidapi-host': `${endpoint}`, + 'x-rapidapi-key': `${process.env.HEARTHSTONE_RAPIDAPI_KEY}` + } + } + return await axios(options) + .then((res) => res.data) + .catch((err) => console.error(err)) + } +} From b17063f569cb4a785916ec7cbca75fc90f06a01e Mon Sep 17 00:00:00 2001 From: "Bruno R.Novais" Date: Sun, 6 Feb 2022 11:45:38 -0300 Subject: [PATCH 2/3] Added HearthStone command and get cards by id --- src/apis/HearthStone.js | 4 ++-- src/commands/games/hearthstone.js | 16 ++++++++++++++++ src/commands/games/hearthstone/cards.js | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/commands/games/hearthstone.js create mode 100644 src/commands/games/hearthstone/cards.js diff --git a/src/apis/HearthStone.js b/src/apis/HearthStone.js index 45d84358c..d16a726ab 100644 --- a/src/apis/HearthStone.js +++ b/src/apis/HearthStone.js @@ -3,10 +3,10 @@ const axios = require('axios') const endpoint = 'https://omgvamp-hearthstone-v1.p.rapidapi.com' -module.exports = class HearthStone extends APIWrapper { +module.exports = class HearthStoneAPI extends APIWrapper { constructor() { super({ - name: 'hearthstone', + name: 'hearthstoneapi', envVars: ['HEARTHSTONE_RAPIDAPI_KEY'] }) } diff --git a/src/commands/games/hearthstone.js b/src/commands/games/hearthstone.js new file mode 100644 index 000000000..211cb8873 --- /dev/null +++ b/src/commands/games/hearthstone.js @@ -0,0 +1,16 @@ +const { SubcommandListCommand } = require('../../') + +module.exports = class HearthStone extends SubcommandListCommand { + constructor(client) { + super( + { + name: 'hearthstone', + aliases: ['hstone'], + category: 'games', + requirements: { apis: ['hearthstoneapi'] }, + embedColor: '#00BFFF' + }, + client + ) + } +} diff --git a/src/commands/games/hearthstone/cards.js b/src/commands/games/hearthstone/cards.js new file mode 100644 index 000000000..09ef904f8 --- /dev/null +++ b/src/commands/games/hearthstone/cards.js @@ -0,0 +1,14 @@ +const { Command, SwitchbladeEmbed, CommandError } = require('../../../') + +module.exports = class HearthStoneCardCommands extends Command { + constructor(client) { + super( + { + name: 'cards', + aliases: ['cards', 'c'], + parent: 'hearthstone' + }, + client + ) + } +} From 90e1ba4a4e82b467b2e85fb14ffab37b7e807514 Mon Sep 17 00:00:00 2001 From: "Bruno R.Novais" Date: Sun, 6 Feb 2022 12:45:15 -0300 Subject: [PATCH 3/3] Added feature of searching cards by id --- .gitignore | 1 + src/apis/HearthStone.js | 8 ++-- src/commands/games/hearthstone.js | 2 +- src/commands/games/hearthstone/cards.js | 14 ------- src/commands/games/hearthstone/cardsById.js | 41 +++++++++++++++++++++ 5 files changed, 48 insertions(+), 18 deletions(-) delete mode 100644 src/commands/games/hearthstone/cards.js create mode 100644 src/commands/games/hearthstone/cardsById.js diff --git a/.gitignore b/.gitignore index 9f07cb0ac..ede9195a9 100644 --- a/.gitignore +++ b/.gitignore @@ -65,6 +65,7 @@ yarn.lock #prettier files .prettierrc +.prettierignore .vscode diff --git a/src/apis/HearthStone.js b/src/apis/HearthStone.js index d16a726ab..bb42475ac 100644 --- a/src/apis/HearthStone.js +++ b/src/apis/HearthStone.js @@ -4,11 +4,12 @@ const axios = require('axios') const endpoint = 'https://omgvamp-hearthstone-v1.p.rapidapi.com' module.exports = class HearthStoneAPI extends APIWrapper { - constructor() { + constructor () { super({ name: 'hearthstoneapi', envVars: ['HEARTHSTONE_RAPIDAPI_KEY'] }) + this.languages = ['ptBR,enUS'] } // Get Card by Name @@ -16,7 +17,7 @@ module.exports = class HearthStoneAPI extends APIWrapper { * @param {string} name the name of the card */ - async getCardByName(name) { + async getCardByName (name) { const options = { method: 'GET', url: `${endpoint}/cards/${name}`, @@ -35,10 +36,11 @@ module.exports = class HearthStoneAPI extends APIWrapper { * @param {int} id the integer of the card */ - async getCardById(id) { + async getCardById (id, lang) { const options = { method: 'GET', url: `${endpoint}/cards/${id}`, + params: { locale: `${lang}` }, headers: { 'x-rapidapi-host': `${endpoint}`, 'x-rapidapi-key': `${process.env.HEARTHSTONE_RAPIDAPI_KEY}` diff --git a/src/commands/games/hearthstone.js b/src/commands/games/hearthstone.js index 211cb8873..aeb3f7fee 100644 --- a/src/commands/games/hearthstone.js +++ b/src/commands/games/hearthstone.js @@ -1,7 +1,7 @@ const { SubcommandListCommand } = require('../../') module.exports = class HearthStone extends SubcommandListCommand { - constructor(client) { + constructor (client) { super( { name: 'hearthstone', diff --git a/src/commands/games/hearthstone/cards.js b/src/commands/games/hearthstone/cards.js deleted file mode 100644 index 09ef904f8..000000000 --- a/src/commands/games/hearthstone/cards.js +++ /dev/null @@ -1,14 +0,0 @@ -const { Command, SwitchbladeEmbed, CommandError } = require('../../../') - -module.exports = class HearthStoneCardCommands extends Command { - constructor(client) { - super( - { - name: 'cards', - aliases: ['cards', 'c'], - parent: 'hearthstone' - }, - client - ) - } -} diff --git a/src/commands/games/hearthstone/cardsById.js b/src/commands/games/hearthstone/cardsById.js new file mode 100644 index 000000000..2c3a9d1b6 --- /dev/null +++ b/src/commands/games/hearthstone/cardsById.js @@ -0,0 +1,41 @@ +const { Command, SwitchbladeEmbed } = require('../../../') + +module.exports = class HearthStoneCardCommands extends Command { + constructor (client) { + super( + { + name: 'cards', + aliases: ['cards', 'c'], + parent: 'hearthstone' + }, + client + ) + } + + async run ({ t, author, channel, prefix, language }, cardId) { + channel.startTyping() + const embed = new SwitchbladeEmbed(author) + const { embedColor } = this.parentCommand + try { + const { name, cardSet, type, faction, rarity, cost, atack, health, text, img } = await this.client.apis.hearthstoneapi.getCardsById(cardId) + channel.send( + embed.setAuthor(author) + .setColor(embedColor) + .setImage(img) + .setTitle(name) + .setDescriptionFromBlockArray([` -/ CardSet: ${cardSet} + -/ Type: ${type} + -/ Faction: ${faction} + -/ Rarity: ${rarity} + -/ Cost: ${cost} + -/ Atack: ${atack} + -/ Heallth: ${health} points + -/ Text `, + `${text}`]) + ) + } catch (e) { + channel.send(new SwitchbladeEmbed().setTitle('Card not Found !').setDescription('Search for a valid card Id !')).then(() => channel.stopTyping()) + } + channel.send(embed).then(() => channel.stopTyping()) + } +}