From d09a9422d78cd4139723f57a6114eb485581a4e8 Mon Sep 17 00:00:00 2001 From: Kyagara Date: Mon, 27 May 2024 15:15:39 -0300 Subject: [PATCH] fixing vlrggapi, removing unused parameters, run betteralign --- README.md | 12 ++-- data.go | 10 ++-- esports.go | 2 +- go.mod | 6 +- go.sum | 14 ++--- lol.go | 75 +++++++++++++------------ models/champion.go | 34 ++++++------ normalize/lol_models.go | 118 ++++++++++++++++++++-------------------- spell.go | 4 +- utils.go | 2 +- val.go | 95 +++++++++++++------------------- 11 files changed, 179 insertions(+), 193 deletions(-) diff --git a/README.md b/README.md index cad3b36..99db99d 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,13 @@ This bot periodically sends information about upcoming League of Legends and Val - `news` command for latest val/lol esports news. - `recent` command for latest val/lol esports games results. - `item` command for lol items. -- Cache requests to esports apis in a file to avoid requests when starting up unless its past the post time. (Partially complete) -- Disable button when pressed by a user. (To avoid errors for now, buttons are sent but they are disabled) +- Disable button when pressed by a user. (To avoid errors for now, buttons sent are disabled) ## What data does this bot provide and how? ### Champions and spells (stats, modifiers and notes) -Data from [DataDragon](https://developer.riotgames.com/docs/lol#data-dragon) provided by riot can be sometimes... interesting, take Miss Fortune for example, her ultimate, `Bullet Time`, has apparently a range of 25000, the same range as Ashe's ultimate, a global spell. [CommunityDragon](https://github.com/CommunityDragon/)'s alternative to the DataDragon champion endpoint provides more data, but runs into the same issue in the case of Miss Fortune's ultimate. This leaves the [League of Legends Wiki](https://leagueoflegends.fandom.com/wiki/League_of_Legends_Wiki) as a pretty good source of information. +Data from [DataDragon](https://developer.riotgames.com/docs/lol#data-dragon) provided by riot can be sometimes... interesting, take Miss Fortune for example, her ultimate, `Bullet Time`, has apparently a range of 25000, the same range as Ashe's ultimate, a global spell. [CommunityDragon](https://github.com/CommunityDragon/)'s alternative to the DataDragon champion endpoint provides more data, but runs into the same issue. This leaves the [League of Legends Wiki](https://leagueoflegends.fandom.com/wiki/League_of_Legends_Wiki) as a pretty good source of information. Using the [lolstaticdata](https://github.com/meraki-analytics/lolstaticdata) tool to gather data from the wiki and normalizing it for this bot's use case (numbers can be string since it will be thrown into Discord Embeds anyway) and the CommunityDragon's CDN to get links for some static data, we can get some pretty good information, nothing ground breaking or that "discards" a visit to the wiki (which is why we keep links to the wiki everywhere). @@ -42,6 +41,9 @@ For League of Legends, this bot uses the unofficial [LolEsports](https://lolespo This bot requires the data provided from [lolstaticdata](https://github.com/meraki-analytics/lolstaticdata). For now, only champion data is needed, so you can just run `python -m lolstaticdata.champions`. +> [!WARNING] +> When a new League of Legends patch is out, please try to not constantly update the data, when a new champion is out for example, check if the wiki has enough information on it to justify gathering all data again. + After copying the champions data folder to the root of the project, a champion should have a path like `./data/champions/Aatrox.json`. Run `go run ./normalize`, this will create a folder with a path for a champion like `./data/champions/normalized/Aatrox.json`. @@ -69,10 +71,6 @@ If mod_roles is empty, anyone will be able to use the `update` option from the ` Now run the bot with the flag `-register` once, this will **overwrite all guild commands** to the specified guild in the config file, after that you can run the bot without specifying any flag. -## Updating - -When a new League of Legends patch is out, just repeat the process of gathering and normalizing the data, please try to not constantly update the data, when a new champion is out for example, check if the wiki has enough information on it to justify gathering all data again. - ## Disclaimer discord-esports isn't endorsed by Riot Games and doesn't reflect the views or opinions of Riot Games or anyone officially involved in producing or managing Riot Games properties. Riot Games, and all associated properties are trademarks or registered trademarks of Riot Games, Inc. diff --git a/data.go b/data.go index 61953a1..d5ec89e 100644 --- a/data.go +++ b/data.go @@ -83,27 +83,27 @@ func loadWikiData() error { 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")) + spellsEmbeds[champion.Key]["P"] = append(spellsEmbeds[champion.Key]["P"], createChampionSpellEmbed(&champion, &spell)) } 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")) + spellsEmbeds[champion.Key]["Q"] = append(spellsEmbeds[champion.Key]["Q"], createChampionSpellEmbed(&champion, &spell)) } 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")) + spellsEmbeds[champion.Key]["W"] = append(spellsEmbeds[champion.Key]["W"], createChampionSpellEmbed(&champion, &spell)) } 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")) + spellsEmbeds[champion.Key]["E"] = append(spellsEmbeds[champion.Key]["E"], createChampionSpellEmbed(&champion, &spell)) } 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")) + spellsEmbeds[champion.Key]["R"] = append(spellsEmbeds[champion.Key]["R"], createChampionSpellEmbed(&champion, &spell)) } championsEmbeds[champion.Key] = createChampionEmbed(&champion) diff --git a/esports.go b/esports.go index da8a2d9..9374fbd 100644 --- a/esports.go +++ b/esports.go @@ -23,7 +23,7 @@ func EsportsCommand(session *discordgo.Session, interaction *discordgo.Interacti game := optionMap["game"].StringValue() if optionMap["update"] != nil { - if !hasPermissions(session, interaction) { + if !hasPermissions(interaction) { return } diff --git a/go.mod b/go.mod index f661b89..19e8ebe 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module discord-esports go 1.20 require ( - github.com/bwmarrin/discordgo v0.27.1 - go.uber.org/zap v1.26.0 - golang.org/x/text v0.13.0 + github.com/bwmarrin/discordgo v0.28.1 + go.uber.org/zap v1.27.0 + golang.org/x/text v0.15.0 ) require ( diff --git a/go.sum b/go.sum index ded9cb6..789403e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY= -github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= +github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= @@ -7,11 +7,11 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= -go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= @@ -21,7 +21,7 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/lol.go b/lol.go index a6a45c2..9f5560d 100644 --- a/lol.go +++ b/lol.go @@ -11,49 +11,54 @@ import ( ) type LOLEsportsLeagueResponse struct { - Data struct { - Leagues []struct { - ID string `json:"id"` - Slug string `json:"slug"` - Name string `json:"name"` - Region string `json:"region"` - Image string `json:"image"` - Priority int `json:"priority"` - DisplayPriority struct { - Position int `json:"position"` - Status string `json:"status"` - } `json:"displayPriority"` - } `json:"leagues"` - } `json:"data"` + Data LOLEsportsLeagueData `json:"data"` +} + +type LOLEsportsLeagueData struct { + Leagues []LOLEsportsLeague `json:"leagues"` +} + +type LOLEsportsLeague struct { + ID string `json:"id"` } type LOLEsportsEventListResponse struct { - Data struct { - Esports struct { - Events []struct { - StartTime string `json:"startTime"` - Match struct { - Teams []struct { - Code string `json:"code"` - Image string `json:"image"` - } `json:"teams"` - } `json:"match"` - League struct { - ID string `json:"id"` - Slug string `json:"slug"` - Name string `json:"name"` - } `json:"league"` - } `json:"events"` - } `json:"esports"` - } `json:"data"` + Data LOLEsportsEventData `json:"data"` +} + +type LOLEsportsEventData struct { + Esports LOLEsportsEventEsports `json:"esports"` +} + +type LOLEsportsEventEsports struct { + Events []LOLEsportsEvent `json:"events"` +} + +type LOLEsportsEvent struct { + StartTime string `json:"startTime"` + League LOLEsportsEventLeague `json:"league"` + Match LOLEsportsEventMatch `json:"match"` +} + +type LOLEsportsEventLeague struct { + Slug string `json:"slug"` + Name string `json:"name"` +} + +type LOLEsportsEventMatch struct { + Teams []LOLEsportsEventMatchTeam `json:"teams"` +} + +type LOLEsportsEventMatchTeam struct { + Code string `json:"code"` } type LOLEsportsLeagueSchedule struct { - League string - URL string - Time string TeamA string TeamB string + League string + Time string + URL string } func updateLOLEsportsData() error { diff --git a/models/champion.go b/models/champion.go index 540ba23..e5a485a 100644 --- a/models/champion.go +++ b/models/champion.go @@ -2,22 +2,22 @@ package models type Champion struct { - ID int `json:"id"` - Key string `json:"key"` - Name string `json:"name"` - FullTitle string `json:"fullTitle"` - Icon string `json:"icon"` - Lore string `json:"lore"` - Resource string `json:"resource"` - AttackType string `json:"attackType"` - AdaptiveType string `json:"adaptiveType"` - Roles []string `json:"roles"` - PatchLastChanged string `json:"patchLastChanged"` - OfficialPage string `json:"officialPage"` - WikiPage string `json:"wikiPage"` // ChampionStats also include per level number when available - Stats ChampionStats `json:"stats"` - Spells ChampionSpells `json:"spells"` + Stats ChampionStats `json:"stats"` + Key string `json:"key"` + Name string `json:"name"` + FullTitle string `json:"fullTitle"` + Icon string `json:"icon"` + Lore string `json:"lore"` + Resource string `json:"resource"` + AttackType string `json:"attackType"` + AdaptiveType string `json:"adaptiveType"` + PatchLastChanged string `json:"patchLastChanged"` + OfficialPage string `json:"officialPage"` + WikiPage string `json:"wikiPage"` + Spells ChampionSpells `json:"spells"` + Roles []string `json:"roles"` + ID int `json:"id"` } type ChampionStats struct { @@ -46,7 +46,6 @@ type ChampionSpell struct { Icon string `json:"icon"` Video string `json:"video"` WikiPage string `json:"wikiPage"` - Effects []ChampionSpellEffect `json:"effects"` AffectedByCDR string `json:"affectedByCDR"` Cost string `json:"cost"` Cooldown string `json:"cooldown"` @@ -56,13 +55,14 @@ type ChampionSpell struct { Resource string `json:"resource"` DamageType string `json:"damageType"` Projectile string `json:"projectile"` - Notes []string `json:"notes"` Speed string `json:"speed"` Width string `json:"width"` Angle string `json:"angle"` CastTime string `json:"castTime"` EffectRadius string `json:"effectRadius"` TargetRange string `json:"targetRange"` + Effects []ChampionSpellEffect `json:"effects"` + Notes []string `json:"notes"` } type ChampionSpellEffect struct { diff --git a/normalize/lol_models.go b/normalize/lol_models.go index 841c5df..3585ab5 100644 --- a/normalize/lol_models.go +++ b/normalize/lol_models.go @@ -1,49 +1,36 @@ package main type WikiChampion struct { - ID int `json:"id"` - Key string `json:"key"` - Name string `json:"name"` - Title string `json:"title"` - Icon string `json:"icon"` - Resource string `json:"resource"` - AttackType string `json:"attackType"` - AdaptiveType string `json:"adaptiveType"` - Stats WikiChampionStats `json:"stats"` - Roles []string `json:"roles"` - AttributeRatings struct { - Damage int `json:"damage"` - Toughness int `json:"toughness"` - Control int `json:"control"` - Mobility int `json:"mobility"` - Utility int `json:"utility"` - AbilityReliance int `json:"abilityReliance"` - Difficulty int `json:"difficulty"` - } `json:"attributeRatings"` - Spells WikiChampionSpells `json:"abilities"` + Key string `json:"key"` + Name string `json:"name"` + Title string `json:"title"` + Icon string `json:"icon"` + Resource string `json:"resource"` + AttackType string `json:"attackType"` + AdaptiveType string `json:"adaptiveType"` ReleaseDate string `json:"releaseDate"` ReleasePatch string `json:"releasePatch"` PatchLastChanged string `json:"patchLastChanged"` - Price struct { - BlueEssence int `json:"blueEssence"` - RP int `json:"rp"` - SaleRP int `json:"saleRp"` - } `json:"price"` - Lore string `json:"lore"` - Skins []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 { + Lore string `json:"lore"` + Spells WikiChampionSpells `json:"abilities"` + Roles []string `json:"roles"` + Skins []struct { + Cost any `json:"cost"` + Sale any `json:"sale"` + Name string `json:"name"` + Availability string `json:"availability"` + FormatName string `json:"formatName"` + Distribution string `json:"distribution"` + Rarity string `json:"rarity"` + Lore string `json:"lore"` + Release string `json:"release"` + SplashPath string `json:"splashPath"` + UncenteredSplashPath string `json:"uncenteredSplashPath"` + TilePath string `json:"tilePath"` + LoadScreenPath string `json:"loadScreenPath"` + LoadScreenVintagePath string `json:"loadScreenVintagePath"` + Chromas []struct { Name string `json:"name"` - ID int `json:"id"` ChromaPath string `json:"chromaPath"` Colors []string `json:"colors"` Descriptions []struct { @@ -51,26 +38,39 @@ type WikiChampion struct { Region string `json:"region"` } `json:"descriptions"` Rarities []struct { - Rarity int `json:"rarity"` Region string `json:"region"` + Rarity int `json:"rarity"` } `json:"rarities"` + ID int `json:"id"` } `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"` + Set []string `json:"set"` + VoiceActor []string `json:"voiceActor"` + SplashArtist []string `json:"splashArtist"` + ID int `json:"id"` + IsBase bool `json:"isBase"` + LootEligible bool `json:"lootEligible"` + NewEffects bool `json:"newEffects"` + NewAnimations bool `json:"newAnimations"` + NewRecall bool `json:"newRecall"` + NewVoice bool `json:"newVoice"` + NewQuotes bool `json:"newQuotes"` } `json:"skins"` + Stats WikiChampionStats `json:"stats"` + AttributeRatings struct { + Damage int `json:"damage"` + Toughness int `json:"toughness"` + Control int `json:"control"` + Mobility int `json:"mobility"` + Utility int `json:"utility"` + AbilityReliance int `json:"abilityReliance"` + Difficulty int `json:"difficulty"` + } `json:"attributeRatings"` + Price struct { + BlueEssence int `json:"blueEssence"` + RP int `json:"rp"` + SaleRP int `json:"saleRp"` + } `json:"price"` + ID int `json:"id"` } type WikiChampionSpells struct { @@ -130,9 +130,6 @@ type WikiSpellCooldown struct { type WikiSpell struct { Name string `json:"name"` Icon string `json:"icon"` - Effects []WikiSpellEffect `json:"effects"` - Cost WikiSpellCost `json:"cost"` - Cooldown WikiSpellCooldown `json:"cooldown"` Targeting string `json:"targeting"` Affects string `json:"affects"` SpellShieldable string `json:"spellshieldable"` @@ -145,7 +142,6 @@ type WikiSpell struct { Notes string `json:"notes"` Blurb string `json:"blurb"` MissileSpeed string `json:"missileSpeed"` - RechargeRate []float64 `json:"rechargeRate"` CollisionRadius string `json:"collisionRadius"` TetherRadius string `json:"tetherRadius"` OnTargetCDStatic string `json:"onTargetCdStatic"` @@ -156,4 +152,8 @@ type WikiSpell struct { CastTime string `json:"castTime"` EffectRadius string `json:"effectRadius"` TargetRange string `json:"targetRange"` + Effects []WikiSpellEffect `json:"effects"` + Cost WikiSpellCost `json:"cost"` + RechargeRate []float64 `json:"rechargeRate"` + Cooldown WikiSpellCooldown `json:"cooldown"` } diff --git a/spell.go b/spell.go index 60b6b18..23edf5b 100644 --- a/spell.go +++ b/spell.go @@ -16,9 +16,9 @@ type SpellEmbeds struct { } type SpellInfo struct { - Index int Key string FullName string + Index int } func SpellCommand(session *discordgo.Session, interaction *discordgo.InteractionCreate) { @@ -111,7 +111,7 @@ func SpellCommand(session *discordgo.Session, interaction *discordgo.Interaction } } -func createChampionSpellEmbed(champion *models.Champion, spell *models.ChampionSpell, key string) SpellEmbeds { +func createChampionSpellEmbed(champion *models.Champion, spell *models.ChampionSpell) SpellEmbeds { description := fmt.Sprintf("[Wiki](%v) - [Video](%v)\n\n", spell.WikiPage, spell.Video) for _, effects := range spell.Effects { description += fmt.Sprintf("%v\n\n", effects.Description) diff --git a/utils.go b/utils.go index 6df2313..4e30714 100644 --- a/utils.go +++ b/utils.go @@ -16,7 +16,7 @@ func newRequest(endpoint string) (*http.Request, error) { return req, nil } -func hasPermissions(session *discordgo.Session, interaction *discordgo.InteractionCreate) bool { +func hasPermissions(interaction *discordgo.InteractionCreate) bool { if len(client.config.ModRoles) == 0 { return true } diff --git a/val.go b/val.go index 90246e8..508f215 100644 --- a/val.go +++ b/val.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "strings" "time" @@ -12,36 +11,33 @@ import ( ) type VLRGGUpcomingResponse struct { - Data struct { - Status int `json:"status"` - Segments []struct { - Team1 string `json:"team1"` - Team2 string `json:"team2"` - Flag1 string `json:"flag1"` - Flag2 string `json:"flag2"` - Score1 string `json:"score1"` - Score2 string `json:"score2"` - TimeUntilMatch string `json:"time_until_match"` - RoundInfo string `json:"round_info"` - TournamentName string `json:"tournament_name"` - UnixTimestamp int `json:"unix_timestamp"` - MatchPage string `json:"match_page"` - } `json:"segments"` - } `json:"data"` + Data VLRGGUpcomingData `json:"data"` +} + +type VLRGGUpcomingData struct { + Segments []VLRGGUpcomingSegments `json:"segments"` +} + +type VLRGGUpcomingSegments struct { + Team1 string `json:"team1"` + Team2 string `json:"team2"` + UnixTimestamp string `json:"unix_timestamp"` + Tournament string `json:"match_event"` + Series string `json:"match_series"` } type VALEsportsTournamentSchedule struct { - Tournament string - URL string - RoundInfo string - Time int TeamA string TeamB string + Time time.Time + Tournament string + Series string + URL string } func updateVALEsportsData() error { http := http.DefaultClient - req, err := newRequest("https://vlrggapi.vercel.app/match/upcoming_index") + req, err := newRequest("https://vlrggapi.vercel.app/match?q=upcoming") if err != nil { return err } @@ -59,60 +55,54 @@ func updateVALEsportsData() error { return err } - var schedule = make(map[string]map[string][]VALEsportsTournamentSchedule) + var schedule = make(map[time.Time]map[string][]VALEsportsTournamentSchedule, 10) for _, segment := range upcoming.Data.Segments { + time, err := time.Parse("2006-01-02 15:04:05", segment.UnixTimestamp) + if err != nil { + return err + } + gameData := VALEsportsTournamentSchedule{ - Tournament: segment.TournamentName, - RoundInfo: segment.RoundInfo, - Time: segment.UnixTimestamp, TeamA: segment.Team1, - URL: segment.MatchPage, TeamB: segment.Team2, + Tournament: segment.Tournament, + Series: segment.Series, + Time: time, } if gameData.TeamA == "TBD" && gameData.TeamB == "TBD" { continue } - date := time.Unix(int64(gameData.Time), 0) - realDate := fmt.Sprintf("%v %v %v", date.Year(), int(date.Month()), date.Day()) - - if schedule[realDate] == nil { - schedule[realDate] = make(map[string][]VALEsportsTournamentSchedule) + if schedule[time] == nil { + schedule[time] = make(map[string][]VALEsportsTournamentSchedule) } - tournament := fmt.Sprintf("%v - %v", segment.RoundInfo, segment.TournamentName) + tournament := fmt.Sprintf("%v - %v", segment.Tournament, segment.Series) - if schedule[realDate] != nil && schedule[realDate][tournament] == nil { - schedule[realDate][tournament] = make([]VALEsportsTournamentSchedule, 0) + if schedule[time] != nil && schedule[time][tournament] == nil { + schedule[time][tournament] = make([]VALEsportsTournamentSchedule, 0) } - schedule[realDate][tournament] = append(schedule[realDate][tournament], gameData) + schedule[time][tournament] = append(schedule[time][tournament], gameData) } tempSchedule := make(map[string][]VALEsportsTournamentSchedule, 0) - for dateKey, entries := range schedule { - parsedTime, err := time.Parse("2006 1 2", dateKey) - if err != nil { - client.logger.Error(fmt.Sprintf("Error parsing date key: %v", err)) - continue - } - - if parsedTime.After(tomorrow) { + for time, entries := range schedule { + if time.After(tomorrow) { continue } for tournament, entryList := range entries { for _, item := range entryList { - parsedTime := time.Unix(int64(item.Time), 0) - if parsedTime.Before(now) { + if time.Before(now) { continue } if tempSchedule[tournament] == nil { - tempSchedule[tournament] = make([]VALEsportsTournamentSchedule, 0) + tempSchedule[tournament] = make([]VALEsportsTournamentSchedule, 0, 10) } tempSchedule[tournament] = append(tempSchedule[tournament], item) @@ -140,15 +130,8 @@ func createVALMessageEmbed() *discordgo.MessageEmbed { } for _, game := range games { - i, err := strconv.ParseInt(fmt.Sprintf("%v", game.Time), 10, 64) - if err != nil { - client.logger.Error(fmt.Sprintf("Error parsing game time: %v", err)) - continue - } - - date := time.Unix(i, 0) - if date.After(now) { - output += fmt.Sprintf("%v vs %v, .\n", game.TeamA, game.TeamB, date.UnixMilli()/1000) + if game.Time.After(now) { + output += fmt.Sprintf("%v vs %v, .\n", game.TeamA, game.TeamB, game.Time.UnixMilli()/1000) } }