From 1baede16eaa34601c061311b9593ae303c75fa82 Mon Sep 17 00:00:00 2001 From: David Paiva Date: Sat, 16 Oct 2021 18:43:01 +0100 Subject: [PATCH 1/2] Added character command for Genshin Impact --- src/apis/GenshinImpact.js | 39 ++++++ src/commands/games/genshinimpact.js | 16 +++ src/commands/games/genshinimpact/character.js | 130 ++++++++++++++++++ src/locales/en-US/commands.json | 11 ++ 4 files changed, 196 insertions(+) create mode 100644 src/apis/GenshinImpact.js create mode 100644 src/commands/games/genshinimpact.js create mode 100644 src/commands/games/genshinimpact/character.js diff --git a/src/apis/GenshinImpact.js b/src/apis/GenshinImpact.js new file mode 100644 index 000000000..080a94e40 --- /dev/null +++ b/src/apis/GenshinImpact.js @@ -0,0 +1,39 @@ +const { APIWrapper } = require('../') +const axios = require('axios') + +const API_URL = 'https://api.genshin.dev' + +module.exports = class GenshinImpact extends APIWrapper { + constructor () { + super({ + name: 'genshinimpact' + }) + + this.characters = [] + } + + load () { + this.request('characters', 'all').then(res => { + this.characters = res.data + }) + + return this + } + + async getCharacter (character) { + return this.characters.find(char => + char.name.toLowerCase().includes(character.toLowerCase()) + ) + } + + async getCharacterId (character) { + const { data } = await this.request('characters') + return data.find(char => + character.toLowerCase().includes(char.toLowerCase()) + ) + } + + request (endpoint, query = '') { + return axios.get(encodeURI(`${API_URL}/${endpoint}/${query}`)) + } +} diff --git a/src/commands/games/genshinimpact.js b/src/commands/games/genshinimpact.js new file mode 100644 index 000000000..d236e8213 --- /dev/null +++ b/src/commands/games/genshinimpact.js @@ -0,0 +1,16 @@ +const { SubcommandListCommand } = require('../../') + +module.exports = class GenshinImpact extends SubcommandListCommand { + constructor (client) { + super({ + name: 'genshinimpact', + aliases: ['genshin', 'gi'], + category: 'games', + requirements: { apis: ['genshinimpact'] }, + authorString: 'commands:genshinimpact.gameName', + authorImage: 'https://i.imgur.com/z6h1q5R.jpg', + authorURL: 'https://genshin.mihoyo.com/en/', + embedColor: '#ffffff' + }, client) + } +} diff --git a/src/commands/games/genshinimpact/character.js b/src/commands/games/genshinimpact/character.js new file mode 100644 index 000000000..f0b4f53d6 --- /dev/null +++ b/src/commands/games/genshinimpact/character.js @@ -0,0 +1,130 @@ +const { + Command, + SwitchbladeEmbed, + PaginatedEmbed, + CommandError, + Constants +} = require('../../../') + +const moment = require('moment') + +module.exports = class GenshinImpactCharacter extends Command { + constructor (client) { + super( + { + name: 'character', + aliases: ['char', 'c'], + parent: 'genshinimpact', + botPermissions: ['MANAGE_MESSAGES'], + parameters: [ + { + type: 'string', + full: true, + missingError: + 'commands:genshinimpact.subcommands.character.noCharacter' + } + ] + }, + client + ) + } + + async run ({ t, author, channel, language }, character) { + channel.startTyping() + moment.locale(language) + + const { + embedColor, + authorString, + authorImage, + authorURL + } = this.parentCommand + try { + const { + name, + vision, + weapon, + birthday, + nation, + affiliation, + rarity, + constellation, + description, + skillTalents, + passiveTalents, + constellations + } = await this.client.apis.genshinimpact.getCharacter(character) + const characterId = await this.client.apis.genshinimpact.getCharacterId( + name + ) + + const paginatedEmbed = new PaginatedEmbed(t, author) + + paginatedEmbed.addPage( + new SwitchbladeEmbed(author) + .setColor(embedColor) + .setAuthor(t(authorString), authorImage, authorURL) + .setTitle(`${name} ${'✦'.repeat(rarity)} | ${vision} • ${weapon}`) + .setDescriptionFromBlockArray([ + [description ? description : ''], + [ + nation ? `**Nation**: ${nation}` : '', + affiliation ? `**Affiliation**: ${affiliation}` : '', + constellation ? `**Constellation**: ${constellation}` : '', + birthday + ? `**Birthday**: ${moment(birthday).format('MMMM Do')}` + : '' + ] + ]) + .setThumbnail( + `https://api.genshin.dev/characters/${characterId}/gacha-splash.png` + ) + ) + + paginatedEmbed.addPage( + new SwitchbladeEmbed(author) + .setColor(embedColor) + .setAuthor(t(authorString), authorImage, authorURL) + .setTitle(`${name} - Skill Talents`) + .setDescriptionFromBlockArray([ + skillTalents.map(s => { + return `**${s.name}** (${s.unlock})\n${s.description}\n` + }) + ]) + ) + + if (passiveTalents) { + paginatedEmbed.addPage( + new SwitchbladeEmbed(author) + .setColor(embedColor) + .setAuthor(t(authorString), authorImage, authorURL) + .setTitle(`${name} - Passive Talents`) + .setDescriptionFromBlockArray([ + passiveTalents.map(s => { + return `**${s.name}** (${s.unlock})\n${s.description}\n` + }) + ]) + ) + } + + if (constellations) { + paginatedEmbed.addPage( + new SwitchbladeEmbed(author) + .setColor(embedColor) + .setAuthor(t(authorString), authorImage, authorURL) + .setTitle(`${name} - Constellations`) + .setDescriptionFromBlockArray([ + constellations.map(s => { + return `**${s.name}** (${s.unlock})\n${s.description}\n` + }) + ]) + ) + } + + paginatedEmbed.run(await channel.send(Constants.EMPTY_SPACE)) + channel.stopTyping() + } catch (e) { + throw new CommandError(t('errors:generic')) + } + } +} diff --git a/src/locales/en-US/commands.json b/src/locales/en-US/commands.json index 7e9b7e8aa..cb170badc 100644 --- a/src/locales/en-US/commands.json +++ b/src/locales/en-US/commands.json @@ -1142,6 +1142,17 @@ "notLive": "I can't change livestreams' positions!", "positionSet": "Player position set to `{{position}}`." }, + "genshinimpact": { + "commandDescription": "Shows information about Genshin Impact characters, artifacts, events and more.", + "commandUsage": " ", + "gameName": "Genshin Impact ©️ miHoYo Co Ltd", + "subcommands": { + "character": { + "commandDescription": "Looks up a Genshin Impact character's information.", + "commandUsage": "" + } + } + }, "leagueoflegends": { "commandDescription": "Shows the current issues of a League of Legends server or information about a champion.", "commandUsage": " ", From 182afa8ff7f9f1441d52f499256fe757302507f6 Mon Sep 17 00:00:00 2001 From: David Paiva Date: Sat, 16 Oct 2021 18:56:05 +0100 Subject: [PATCH 2/2] Forgor testing exists --- src/commands/games/genshinimpact.js | 2 +- src/commands/games/genshinimpact/character.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/games/genshinimpact.js b/src/commands/games/genshinimpact.js index d236e8213..03a0ac413 100644 --- a/src/commands/games/genshinimpact.js +++ b/src/commands/games/genshinimpact.js @@ -4,7 +4,7 @@ module.exports = class GenshinImpact extends SubcommandListCommand { constructor (client) { super({ name: 'genshinimpact', - aliases: ['genshin', 'gi'], + aliases: ['genshin'], category: 'games', requirements: { apis: ['genshinimpact'] }, authorString: 'commands:genshinimpact.gameName', diff --git a/src/commands/games/genshinimpact/character.js b/src/commands/games/genshinimpact/character.js index f0b4f53d6..b2ea010a0 100644 --- a/src/commands/games/genshinimpact/character.js +++ b/src/commands/games/genshinimpact/character.js @@ -66,7 +66,7 @@ module.exports = class GenshinImpactCharacter extends Command { .setAuthor(t(authorString), authorImage, authorURL) .setTitle(`${name} ${'✦'.repeat(rarity)} | ${vision} • ${weapon}`) .setDescriptionFromBlockArray([ - [description ? description : ''], + [description || ''], [ nation ? `**Nation**: ${nation}` : '', affiliation ? `**Affiliation**: ${affiliation}` : '',