From f36fe1b6bf5a697911fbf3d3baddc82fdf696139 Mon Sep 17 00:00:00 2001 From: Kyagara Date: Fri, 13 Oct 2023 14:55:41 -0300 Subject: [PATCH] fixing spellIndex and refactor --- README.md | 1 + autocomplete.go | 57 ++++++++++++++++++ champion.go | 154 ++++++++++++++---------------------------------- client.go | 37 +++++++++--- events.go | 2 +- main.go | 20 +++---- models.go | 45 +++++++++++++- spell.go | 97 ++++++++++++------------------ 8 files changed, 226 insertions(+), 187 deletions(-) create mode 100644 autocomplete.go diff --git a/README.md b/README.md index f5eca8f..9bd9066 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This bot uses the Lolesports api, [vlrggapi](https://github.com/axsddlr/vlrggapi - Script to normalize the data from lolstaticdata. - `item` command. +- Disable button when pressed by a user. ## Setup diff --git a/autocomplete.go b/autocomplete.go new file mode 100644 index 0000000..b63b7cb --- /dev/null +++ b/autocomplete.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "strings" + + "github.com/bwmarrin/discordgo" +) + +func autoCompleteChampionName(session *discordgo.Session, interaction *discordgo.InteractionCreate, userInput string) { + filteredNames := make(map[string]string) + for id, name := range championsNames { + if strings.HasPrefix(strings.ToLower(name), strings.ToLower(userInput)) { + filteredNames[name] = id + } + + if len(filteredNames) == 20 { + break + } + } + + var choices []*discordgo.ApplicationCommandOptionChoice + for id, name := range filteredNames { + choices = append(choices, &discordgo.ApplicationCommandOptionChoice{Name: name, Value: id}) + } + + err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: choices, + }, + }) + + if err != nil { + client.logger.Error(fmt.Sprintf("Error sending champion autocomplete: %v", err)) + } +} + +func autoCompleteSpell(session *discordgo.Session, interaction *discordgo.InteractionCreate, championName string) { + spells := spellsInfo[championName] + + var choices []*discordgo.ApplicationCommandOptionChoice + for _, spell := range spells { + choices = append(choices, &discordgo.ApplicationCommandOptionChoice{Name: spell.FullName, Value: fmt.Sprintf("%v,%v", spell.Key, spell.Index)}) + } + + err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionApplicationCommandAutocompleteResult, + Data: &discordgo.InteractionResponseData{ + Choices: choices, + }, + }) + + if err != nil { + client.logger.Error(fmt.Sprintf("Error sending spell autocomplete: %v", err)) + } +} diff --git a/champion.go b/champion.go index 80550e5..23d1cfe 100644 --- a/champion.go +++ b/champion.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "strconv" "strings" "github.com/bwmarrin/discordgo" @@ -16,96 +15,45 @@ func ChampionCommand(session *discordgo.Session, interaction *discordgo.Interact } if interaction.Type == discordgo.InteractionApplicationCommandAutocomplete { - if ok := optionMap["champion"].Focused; ok { - filteredNames := make(map[string]string) - for id, name := range championsNames { - if strings.HasPrefix(strings.ToLower(name), strings.ToLower(optionMap["champion"].StringValue())) { - filteredNames[name] = id - } - - if len(filteredNames) == 20 { - break - } - } - - var choices []*discordgo.ApplicationCommandOptionChoice - for id, name := range filteredNames { - choices = append(choices, &discordgo.ApplicationCommandOptionChoice{Name: name, Value: id}) - } - - err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionApplicationCommandAutocompleteResult, - Data: &discordgo.InteractionResponseData{ - Choices: choices, - }, - }) - - if err != nil { - client.logger.Error(fmt.Sprintf("Error sending champion autocomplete: %v", err)) - } - - return - } - - if ok := optionMap["spell"].Focused; ok { - spells := spellsInfo[optionMap["champion"].StringValue()] - - var choices []*discordgo.ApplicationCommandOptionChoice - for _, spell := range spells { - choices = append(choices, &discordgo.ApplicationCommandOptionChoice{Name: spell.FullName, Value: fmt.Sprintf("%v,%v", spell.Key, spell.Index)}) - } - - err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionApplicationCommandAutocompleteResult, - Data: &discordgo.InteractionResponseData{ - Choices: choices, - }, - }) - - if err != nil { - client.logger.Error(fmt.Sprintf("Error sending spell autocomplete: %v", err)) - } + champion, ok := optionMap["champion"] + if ok && champion.Focused { + autoCompleteChampionName(session, interaction, champion.StringValue()) } return } - key := optionMap["champion"].StringValue() - - if ok := optionMap["spell"].StringValue() != ""; ok { - spell := strings.Split(optionMap["spell"].StringValue(), ",") - spellIndex, err := strconv.Atoi(spell[1]) - if err != nil { - client.logger.Error(fmt.Sprintf("error converting spell index to int: %v", err)) + championKey := optionMap["champion"].StringValue() + + embed := &discordgo.MessageEmbed{} + components := []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ + discordgo.Button{Label: "Spells", CustomID: fmt.Sprintf("spells_%v", championKey)}, + discordgo.Button{Label: "Skins", CustomID: fmt.Sprintf("skins_%v", championKey)}, + }}} + + champion := championsEmbeds[championKey] + + embedType, ok := optionMap["type"] + if !ok || embedType.StringValue() == "" { + embed = &champion.General + } else { + switch embedType.StringValue() { + case "spells": + embed = &champion.Spells + components = []discordgo.MessageComponent{} + case "skins": + embed = &champion.Skins + components = []discordgo.MessageComponent{} + default: + embed = &champion.General } - - err = client.session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{&spellsEmbeds[key][spell[0]][spellIndex].General}, - Components: []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ - discordgo.Button{Label: "Modifiers", CustomID: fmt.Sprintf("modifiers_%v_%v_%v", key, spell[0], spellIndex)}, - discordgo.Button{Label: "Notes", CustomID: fmt.Sprintf("notes_%v_%v_%v", key, spell[0], spellIndex)}, - }}}, - }, - }) - - if err != nil { - client.logger.Error(fmt.Sprintf("Error responding with embed: %v", err)) - } - - return } - embed := championsEmbeds[key].General err := client.session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{&embed}, - Components: []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ - discordgo.Button{Label: "Spells", CustomID: fmt.Sprintf("spells_%v", key)}, - discordgo.Button{Label: "Skins", CustomID: fmt.Sprintf("skins_%v", key)}, - }}}, + Embeds: []*discordgo.MessageEmbed{embed}, + Components: components, }, }) @@ -120,27 +68,6 @@ func createChampionEmbed(champion *WikiChampion) ChampionEmbeds { resource = "None" } - fields := []*discordgo.MessageEmbedField{ - {Name: "HP | Regen", Value: fmt.Sprintf("``%v (+ %v)``\n``%v (+ %v)``", champion.Stats.Health.Flat, champion.Stats.Health.PerLevel, champion.Stats.HealthRegen.Flat, champion.Stats.HealthRegen.PerLevel), Inline: true}, - } - - if champion.Stats.Mana.Flat != 0 && champion.Stats.Mana.PerLevel != 0 { - fields = append(fields, &discordgo.MessageEmbedField{Name: "MP | Regen", Value: fmt.Sprintf("``%v (+ %v)``\n``%v (+ %v)``", champion.Stats.Mana.Flat, champion.Stats.Mana.PerLevel, champion.Stats.ManaRegen.Flat, champion.Stats.ManaRegen.PerLevel), Inline: true}) - } - - fields = append(fields, &discordgo.MessageEmbedField{Name: "Armor | MR", Value: fmt.Sprintf("``%v (+ %v)``\n``%v (+ %v)``", champion.Stats.Armor.Flat, champion.Stats.Armor.PerLevel, champion.Stats.MagicResistance.Flat, champion.Stats.MagicResistance.PerLevel), Inline: true}) - - fields = append(fields, &discordgo.MessageEmbedField{Name: "", Value: ""}) - fields = append(fields, &discordgo.MessageEmbedField{Name: "Attack Damage", Value: fmt.Sprintf("``%v (+ %v)``", champion.Stats.AttackDamage.Flat, champion.Stats.AttackDamage.PerLevel), Inline: true}) - - if champion.Stats.AttackSpeed.Flat != 0 && champion.Stats.AttackSpeed.PerLevel != 0 { - fields = append(fields, &discordgo.MessageEmbedField{Name: "Attack Speed", Value: fmt.Sprintf("``%v (+ %v)``", champion.Stats.AttackSpeed.Flat, champion.Stats.AttackSpeed.PerLevel), Inline: true}) - } - - /* if champion.Stats.Crit != 0 && champion.Stats.CritPerLevel != 0 { - fields = append(fields, &discordgo.MessageEmbedField{Name: "Crit", Value: fmt.Sprintf("``%v (+ %v)``", champion.Stats.CriticalStrikeDamage, champion.Stats.CritPerLevel), Inline: true}) - } */ - /* cds := fmt.Sprintf("``Q - %v\nW - %v\nE - %v\nR - %v``", champion.Spells.[0].CooldownBurn, data.Spells[1].CooldownBurn, data.Spells[2].CooldownBurn, data.Spells[3].CooldownBurn) costs := fmt.Sprintf("``Q - %v\nW - %v\nE - %v\nR - %v``", data.Spells[0].CostBurn, data.Spells[1].CostBurn, data.Spells[2].CostBurn, data.Spells[3].CostBurn) @@ -152,11 +79,6 @@ func createChampionEmbed(champion *WikiChampion) ChampionEmbeds { fields = append(fields, &discordgo.MessageEmbedField{Name: "Spell Cost", Value: costs, Inline: true}) fields = append(fields, &discordgo.MessageEmbedField{Name: "Spell Range", Value: ranges, Inline: true}) */ - fields = append(fields, &discordgo.MessageEmbedField{Name: "", Value: ""}) - fields = append(fields, &discordgo.MessageEmbedField{Name: "Range", Value: fmt.Sprintf("``%v``", champion.Stats.AttackRange.Flat), Inline: true}) - fields = append(fields, &discordgo.MessageEmbedField{Name: "Movement", Value: fmt.Sprintf("``%v``", champion.Stats.MovementSpeed.Flat), Inline: true}) - fields = append(fields, &discordgo.MessageEmbedField{Name: "Resource", Value: resource, Inline: true}) - tags := champion.Roles if len(tags) == 0 || tags[0] == "" { tags = []string{"None"} @@ -169,9 +91,23 @@ func createChampionEmbed(champion *WikiChampion) ChampionEmbeds { Thumbnail: &discordgo.MessageEmbedThumbnail{ URL: champion.Icon, }, - Description: fmt.Sprintf("[Wiki](https://leagueoflegends.fandom.com/wiki/%s/LoL) - [LoLalytics](https://lolalytics.com/lol/%s/build/)", strings.Replace(champion.Name, " ", "_", 1), strings.ToLower(champion.Key)), - Fields: fields, - Footer: &discordgo.MessageEmbedFooter{Text: strings.Join(tags, ", ")}, + Description: fmt.Sprintf("[Wiki](https://leagueoflegends.fandom.com/wiki/%s/LoL) - [LoLalytics](https://lolalytics.com/lol/%s/build/)\n\n%v", strings.Replace(champion.Name, " ", "_", 1), strings.ToLower(champion.Key), champion.Lore), + Fields: []*discordgo.MessageEmbedField{ + {Name: "HP | Regen", Value: fmt.Sprintf("%v (+ %v)\n%v (+ %v)", champion.Stats.Health.Flat, champion.Stats.Health.PerLevel, champion.Stats.HealthRegen.Flat, champion.Stats.HealthRegen.PerLevel), Inline: true}, + {Name: "MP | Regen", Value: fmt.Sprintf("%v (+ %v)\n%v (+ %v)", champion.Stats.Mana.Flat, champion.Stats.Mana.PerLevel, champion.Stats.ManaRegen.Flat, champion.Stats.ManaRegen.PerLevel), Inline: true}, + {Name: "Armor | MR", Value: fmt.Sprintf("%v (+ %v)\n%v (+ %v)", champion.Stats.Armor.Flat, champion.Stats.Armor.PerLevel, champion.Stats.MagicResistance.Flat, champion.Stats.MagicResistance.PerLevel), Inline: true}, + {Name: "", Value: ""}, + {Name: "Attack Range", Value: fmt.Sprintf("%v", champion.Stats.AttackRange.Flat), Inline: true}, + {Name: "Attack Damage", Value: fmt.Sprintf("%v (+ %v)", champion.Stats.AttackDamage.Flat, champion.Stats.AttackDamage.PerLevel), Inline: true}, + {Name: "Attack Speed", Value: fmt.Sprintf("%v (+ %v)", champion.Stats.AttackSpeed.Flat, champion.Stats.AttackSpeed.PerLevel), Inline: true}, + {Name: "", Value: ""}, + {Name: "Movement", Value: fmt.Sprintf("%v", champion.Stats.MovementSpeed.Flat), Inline: true}, + {Name: "Adaptive Type", Value: fmt.Sprintf("%v", champion.AdaptiveType), Inline: true}, + {Name: "Resource", Value: resource, Inline: true}, + {Name: "", Value: ""}, + {Name: "Patch last changed", Value: champion.PatchLastChanged, Inline: true}, + }, + Footer: &discordgo.MessageEmbedFooter{Text: strings.Join(tags, ", ")}, } return ChampionEmbeds{General: championEmbed} diff --git a/client.go b/client.go index e99c0d4..f7c6dfa 100644 --- a/client.go +++ b/client.go @@ -163,12 +163,15 @@ func (c *Client) loadEnabledCommands() { DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Nome do champion."}, }, { - Name: "spell", - Autocomplete: true, - NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "habilidade"}, - Description: "The champion's spell.", - DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "A habilidade do champion."}, + Name: "type", + NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "tipo"}, + Description: "Choose the type of information you want from the champion.", + DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Escolha o tipo de informação que você quer desse champion."}, Type: discordgo.ApplicationCommandOptionString, + Choices: []*discordgo.ApplicationCommandOptionChoice{ + {Name: "Spells", Value: "spells", NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Geral"}}, + {Name: "Skins", Value: "Skins", NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Modificadores"}}, + }, }, }, }, Handler: ChampionCommand} @@ -182,11 +185,31 @@ func (c *Client) loadEnabledCommands() { Options: []*discordgo.ApplicationCommandOption{ { Name: "champion", + Description: "Champion name.", + DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Nome do champion."}, Required: true, Autocomplete: true, Type: discordgo.ApplicationCommandOptionString, - Description: "Champion name.", - DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Nome do champion."}, + }, + { + Name: "spell", + NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "habilidade"}, + Description: "Spell name.", + DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Nome da habilidade."}, + Required: true, + Autocomplete: true, + Type: discordgo.ApplicationCommandOptionString, + }, + { + Name: "type", + NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "tipo"}, + Description: "Choose the type of information you want from the spell.", + DescriptionLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Escolha o tipo de informação que você quer dessa habilidade."}, + Type: discordgo.ApplicationCommandOptionString, + Choices: []*discordgo.ApplicationCommandOptionChoice{ + {Name: "Modifiers", Value: "modifiers", NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Modificadores"}}, + {Name: "Notes", Value: "notes", NameLocalizations: map[discordgo.Locale]string{discordgo.PortugueseBR: "Notas"}}, + }, }, }, }, Handler: SpellCommand} diff --git a/events.go b/events.go index cdd34b8..a85fed4 100644 --- a/events.go +++ b/events.go @@ -38,7 +38,7 @@ func interactionsEvent(session *discordgo.Session, interaction *discordgo.Intera if interaction.Type == discordgo.InteractionMessageComponent { id := strings.Split(interaction.MessageComponentData().CustomID, "_") - if contains(commandButtonsID, id[0]) { + if !contains(commandButtonsID, id[0]) { respondWithError(interaction.Interaction, fmt.Errorf("message component id '%s' not found", id[0])) return } diff --git a/main.go b/main.go index 52a0052..5ccbe00 100644 --- a/main.go +++ b/main.go @@ -76,24 +76,24 @@ func main() { spellsInfo[champion.Key] = make([]SpellInfo, 0) spellsEmbeds[champion.Key] = make(map[string][]SpellEmbeds) - for _, spell := range champion.Spells.Passive { - spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "P", -1)) + for i, spell := range champion.Spells.Passive { + spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "P", i)) spellsEmbeds[champion.Key]["P"] = append(spellsEmbeds[champion.Key]["P"], createChampionSpellEmbed(&champion, &spell, "P")) } - for _, spell := range champion.Spells.Q { - spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "Q", 0)) + for i, spell := range champion.Spells.Q { + spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "Q", i)) spellsEmbeds[champion.Key]["Q"] = append(spellsEmbeds[champion.Key]["Q"], createChampionSpellEmbed(&champion, &spell, "Q")) } - for _, spell := range champion.Spells.W { - spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "W", 1)) + for i, spell := range champion.Spells.W { + spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "W", i)) spellsEmbeds[champion.Key]["W"] = append(spellsEmbeds[champion.Key]["W"], createChampionSpellEmbed(&champion, &spell, "W")) } - for _, spell := range champion.Spells.E { - spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "E", 2)) + for i, spell := range champion.Spells.E { + spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "E", i)) spellsEmbeds[champion.Key]["E"] = append(spellsEmbeds[champion.Key]["E"], createChampionSpellEmbed(&champion, &spell, "E")) } - for _, spell := range champion.Spells.R { - spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "R", 3)) + for i, spell := range champion.Spells.R { + spellsInfo[champion.Key] = append(spellsInfo[champion.Key], createSpellInfo(&spell, "R", i)) spellsEmbeds[champion.Key]["R"] = append(spellsEmbeds[champion.Key]["R"], createChampionSpellEmbed(&champion, &spell, "R")) } diff --git a/models.go b/models.go index 861d93e..891d18f 100644 --- a/models.go +++ b/models.go @@ -68,7 +68,50 @@ type WikiChampion struct { RP int `json:"rp"` SaleRP int `json:"saleRp"` } `json:"price"` - Lore string `json:"lore"` + Lore string `json:"lore"` + Skins []WikiSkins `json:"skins"` +} + +type WikiSkins struct { + Name string `json:"name"` + ID int `json:"id"` + IsBase bool `json:"isBase"` + Availability string `json:"availability"` + FormatName string `json:"formatName"` + LootEligible bool `json:"lootEligible"` + Cost any `json:"cost"` + Sale any `json:"sale"` + Distribution string `json:"distribution"` + Rarity string `json:"rarity"` + Chromas []struct { + Name string `json:"name"` + ID int `json:"id"` + ChromaPath string `json:"chromaPath"` + Colors []string `json:"colors"` + Descriptions []struct { + Description string `json:"description"` + Region string `json:"region"` + } `json:"descriptions"` + Rarities []struct { + Rarity int `json:"rarity"` + Region string `json:"region"` + } `json:"rarities"` + } `json:"chromas"` + Lore string `json:"lore"` + Release string `json:"release"` + Set []string `json:"set"` + SplashPath string `json:"splashPath"` + UncenteredSplashPath string `json:"uncenteredSplashPath"` + TilePath string `json:"tilePath"` + LoadScreenPath string `json:"loadScreenPath"` + LoadScreenVintagePath string `json:"loadScreenVintagePath"` + NewEffects bool `json:"newEffects"` + NewAnimations bool `json:"newAnimations"` + NewRecall bool `json:"newRecall"` + NewVoice bool `json:"newVoice"` + NewQuotes bool `json:"newQuotes"` + VoiceActor []string `json:"voiceActor"` + SplashArtist []string `json:"splashArtist"` } type Stat struct { diff --git a/spell.go b/spell.go index bc5623f..aed94e4 100644 --- a/spell.go +++ b/spell.go @@ -18,76 +18,55 @@ func SpellCommand(session *discordgo.Session, interaction *discordgo.Interaction } if interaction.Type == discordgo.InteractionApplicationCommandAutocomplete { - if ok := optionMap["champion"].Focused; ok { - filteredNames := make(map[string]string) - for id, name := range championsNames { - if strings.HasPrefix(strings.ToLower(name), strings.ToLower(optionMap["champion"].StringValue())) { - filteredNames[name] = id - } - - if len(filteredNames) == 20 { - break - } - } - - var choices []*discordgo.ApplicationCommandOptionChoice - for id, name := range filteredNames { - choices = append(choices, &discordgo.ApplicationCommandOptionChoice{Name: name, Value: id}) - } - - err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionApplicationCommandAutocompleteResult, - Data: &discordgo.InteractionResponseData{ - Choices: choices, - }, - }) - - if err != nil { - client.logger.Error(fmt.Sprintf("Error sending champion autocomplete: %v", err)) - } - + champion, ok := optionMap["champion"] + if ok && champion.Focused { + autoCompleteChampionName(session, interaction, champion.StringValue()) return } + spell, ok := optionMap["spell"] + if ok && spell.Focused { + autoCompleteSpell(session, interaction, optionMap["champion"].StringValue()) + } + return } - key := optionMap["champion"].StringValue() + championKey := optionMap["champion"].StringValue() - if ok := optionMap["spells"].StringValue() != ""; ok { - spell := strings.Split(optionMap["spell"].StringValue(), ",") - spellIndex, err := strconv.Atoi(spell[1]) - if err != nil { - client.logger.Error(fmt.Sprintf("error converting spell index to int: %v", err)) - } + spell := strings.Split(optionMap["spell"].StringValue(), ",") + spellIndex, err := strconv.Atoi(spell[1]) + if err != nil { + client.logger.Error(fmt.Sprintf("error converting spell index to int: %v", err)) + } - err = client.session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{&spellsEmbeds[key][spell[0]][spellIndex].General}, - Components: []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ - discordgo.Button{Label: "Modifiers", CustomID: fmt.Sprintf("modifiers_%v_%v_%v", key, spell[0], spellIndex)}, - discordgo.Button{Label: "Notes", CustomID: fmt.Sprintf("notes_%v_%v_%v", key, spell[0], spellIndex)}, - }}}, - }, - }) - - if err != nil { - client.logger.Error(fmt.Sprintf("Error responding with embed: %v", err)) - } + embed := &discordgo.MessageEmbed{} + components := []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ + discordgo.Button{Label: "Modifiers", CustomID: fmt.Sprintf("modifiers_%v_%v_%v", championKey, spell[0], spellIndex)}, + discordgo.Button{Label: "Notes", CustomID: fmt.Sprintf("notes_%v_%v_%v", championKey, spell[0], spellIndex)}, + }}} - return + embedType, ok := optionMap["type"] + if !ok || embedType.StringValue() == "" { + embed = &spellsEmbeds[championKey][spell[0]][spellIndex].General + } else { + switch embedType.StringValue() { + case "modifiers": + embed = &spellsEmbeds[championKey][spell[0]][spellIndex].Modifiers + components = []discordgo.MessageComponent{} + case "notes": + embed = &spellsEmbeds[championKey][spell[0]][spellIndex].Notes + components = []discordgo.MessageComponent{} + default: + embed = &spellsEmbeds[championKey][spell[0]][spellIndex].General + } } - embed := championsEmbeds[key].General - err := client.session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ + err = client.session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{&embed}, - Components: []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{ - discordgo.Button{Label: "Spells", CustomID: fmt.Sprintf("spells_%v", key)}, - discordgo.Button{Label: "Skins", CustomID: fmt.Sprintf("skins_%v", key)}, - }}}, + Embeds: []*discordgo.MessageEmbed{embed}, + Components: components, }, }) @@ -226,7 +205,7 @@ func createChampionSpellEmbed(champion *WikiChampion, spell *WikiSpell, key stri func createSpellInfo(spell *WikiSpell, key string, index int) SpellInfo { var spellFullName string - if index == -1 { + if key == "P" { spellFullName = fmt.Sprintf("Passive - %v", spell.Name) } else { spellFullName = fmt.Sprintf("%v - %v", key, spell.Name) @@ -291,5 +270,5 @@ func getSpellRangeOrRadius(rangeOrRadius string) string { return strings.Replace(burn, "/(basedonlevel)", " (based on level)", -1) } - return strings.Replace(rangeOrRadius, " ", "/", -1) + return strings.Replace(rangeOrRadius, " / ", "/", -1) }