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..03a0ac413 --- /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'], + 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..b2ea010a0 --- /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 || ''], + [ + 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": " ",