Skip to content

Commit

Permalink
fixing spellIndex and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Oct 13, 2023
1 parent 1bace41 commit f36fe1b
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 187 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 57 additions & 0 deletions autocomplete.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
154 changes: 45 additions & 109 deletions champion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"strconv"
"strings"

"github.com/bwmarrin/discordgo"
Expand All @@ -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,
},
})

Expand All @@ -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)
Expand All @@ -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"}
Expand All @@ -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}
Expand Down
37 changes: 30 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand Down
Loading

0 comments on commit f36fe1b

Please sign in to comment.