diff --git a/README.md b/README.md index f196834..fbc6ee9 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,16 @@ This project is not affiliated with the ZUnivers's project. It's a community pro ## Features - ✅ `!daily` reminder -- ✅ Notify for new patchnotes -- ✅ Notify for configuration changes -- ✅ Notify for new webapp versions -- ✅ Notify for new items/item changes -- ✅ Notify for new packs/packs changes -- ✅ Notify for new banners/banners changes -- ✅ Notify for new "ascension" season -- ✅ Notify for new event/events changes -- ✅ Notify for new achievements/achievements changes +- ✅ Notifies for new patchnotes +- ✅ Notifies for configuration changes +- ✅ Notifies for new webapp versions +- ✅ Notifies for new items/item changes +- ✅ Notifies for new packs/packs changes +- ✅ Notifies for new banners/banners changes +- ✅ Notifies for new "ascension" season +- ✅ Notifies for new event/events changes +- ✅ Notifies for new achievements/achievements changes +- ✅ Notifies for new challenges - ✅ Multiple webhooks dispatching ## Usage diff --git a/config.default.json b/config.default.json index c762a3d..52d927a 100644 --- a/config.default.json +++ b/config.default.json @@ -21,11 +21,13 @@ "new_banner": "Une nouvelle bannière a été ajoutée !", "banner_changed": "Une bannière a été modifiée !", "new_event": "Un nouvel event a débuté !", - "event_changed": "Un event a été modifié", + "event_changed": "Un event a été modifié3", "event_removed": "Un event s'est terminé.", "new_achievement_category": "Une catégorie de succès a été ajoutée.", "achievement_category_changed": "Une catégorie de succès a été modifiée.", "new_achievement": "Un succès a été ajouté.", - "achievement_changed": "Un succès a été modifié." + "achievement_changed": "Un succès a été modifié.", + "challenge_changed": "Un challenge a été modifié.", + "new_challenge": "Un nouveau challenge a débuté3" } } \ No newline at end of file diff --git a/database/database.go b/database/database.go index 848af63..819a152 100644 --- a/database/database.go +++ b/database/database.go @@ -27,6 +27,7 @@ func Init() (db *gorm.DB) { &structures.Event{}, &structures.AchievementCategory{}, &structures.Achievement{}, + &structures.Challenge{}, ) if err != nil { diff --git a/services/webhookService.go b/services/webhookService.go index 006a35e..1cbb996 100644 --- a/services/webhookService.go +++ b/services/webhookService.go @@ -141,18 +141,25 @@ func fillEmbed(embed *discord.Embed, oldObject, newObject interface{}) { } func processDisplay(field *discord.EmbedField, oldValue, newValue interface{}, parts []string) { + split := strings.Split(parts[1], "|") + format := "`%v`" + if len(split) > 1 { + format = split[1] + } + oldValueText := "" if oldValue != nil { if utils.IsTime(oldValue) { if utils.TimeDifference(oldValue, newValue) { - oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprint(oldValue)) + oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue)) } } else if oldValue != newValue { - oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprint(oldValue)) + oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue)) } } - field.Value += fmt.Sprintf("__%s:__ %s`%s`\n", parts[1], oldValueText, fmt.Sprint(newValue)) + value := fmt.Sprintf("__%s:__ %s%s\n", split[0], oldValueText, fmt.Sprintf(format, newValue)) + field.Value += value } func processImage(embed *discord.Embed, newValue interface{}, parts []string) { diff --git a/services/zuniversService.go b/services/zuniversService.go index eefd954..2c4bc2a 100644 --- a/services/zuniversService.go +++ b/services/zuniversService.go @@ -60,3 +60,8 @@ func FetchAchievements(categoryId string) (achProgress []structures.AchievementP err = utils.Request("/public/achievement/Alex'Presso%235480/"+categoryId, "GET", nil, &achProgress) return } + +func FetchChallenges() (chProgress []structures.ChallengeProgress, err error) { + err = utils.Request("/public/challenge", "GET", nil, &chProgress) + return +} diff --git a/structures/Challenge.go b/structures/Challenge.go new file mode 100644 index 0000000..9ea35ff --- /dev/null +++ b/structures/Challenge.go @@ -0,0 +1,17 @@ +package structures + +import "gorm.io/gorm" + +type ChallengeProgress struct { + Challenge *Challenge `json:"challenge"` +} + +type Challenge struct { + gorm.Model + + ChallengeID string `json:"id"` + Description string `json:"description" zu:"display=Description"` + RewardLoreDust uint32 `json:"rewardLoreDust" zu:"display=Poussière de lore"` + Score uint32 `json:"score" zu:"display=Score"` + Type string `json:"type" zu:"display=Type"` +} diff --git a/structures/Event.go b/structures/Event.go index e180c90..b6bfc9e 100644 --- a/structures/Event.go +++ b/structures/Event.go @@ -10,5 +10,5 @@ type Event struct { EndDate *DateTime `json:"endDate" zu:"display=Fin"` ImageURL string `json:"imageUrl" zu:"imageUrl=%s"` Name string `json:"name" zu:"display=Nom"` - BalanceCost int `json:"balanceCost" zu:"Coût im"` + BalanceCost int `json:"balanceCost" zu:"display=Coût d'invocation"` } diff --git a/structures/Item.go b/structures/Item.go index afe632b..7fcf947 100644 --- a/structures/Item.go +++ b/structures/Item.go @@ -9,12 +9,12 @@ type Item struct { Genre string `json:"genre" zu:"display=Genre"` Name string `json:"name" zu:"display=Nom"` Rarity uint32 `json:"rarity" zu:"display=Rareté"` - Slug string `json:"slug" zu:"url=/item/%s"` + Slug string `json:"slug" zu:"url=/carte/%s"` } type Pack struct { gorm.Model PackID string `json:"id"` - Name string `json:"name" zu:"display=Nom"` + Name string `json:"name" zu:"display=Nom;url=/catalogue/%s"` } diff --git a/structures/Patchnote.go b/structures/Patchnote.go index a6439b5..fff270a 100644 --- a/structures/Patchnote.go +++ b/structures/Patchnote.go @@ -9,8 +9,8 @@ type Patchnote struct { PatchnoteID string `json:"id"` Title string `json:"title" zu:"display=Titre"` - CreatedBy string `json:"createdBy" zu:"display=Auteur"` + CreatedBy string `json:"createdBy" zu:"display=Auteur|<@%s>"` Date *DateTime `json:"date" zu:"display=Date"` ImageUrl string `json:"imageUrl" zu:"imageUrl=%s"` - Slug string `json:"slug" zu:"url=/post/%s"` + Slug string `json:"slug" zu:"url=/actu/%s"` } diff --git a/tasks/checkChallenges.go b/tasks/checkChallenges.go new file mode 100644 index 0000000..4a1bd4a --- /dev/null +++ b/tasks/checkChallenges.go @@ -0,0 +1,45 @@ +package tasks + +import ( + "github.com/alexpresso/zunivers-webhooks/services" + "github.com/alexpresso/zunivers-webhooks/structures" + "github.com/alexpresso/zunivers-webhooks/structures/discord" + "github.com/alexpresso/zunivers-webhooks/utils" + "gorm.io/gorm" +) + +func checkChallenges(db *gorm.DB, embeds *[]discord.Embed) { + chProgress, err := services.FetchChallenges() + if err != nil { + utils.Log("An error occurred while fetching challenges: " + err.Error()) + return + } + + var challenges []*structures.Challenge + var dbChallenges []structures.Challenge + dbChallengesMap := make(map[string]*structures.Challenge) + + db.Find(&dbChallenges) + for _, chall := range dbChallenges { + chall := chall + dbChallengesMap[chall.ChallengeID] = &chall + } + + for i := 0; i < len(chProgress); i++ { + challenge := &chProgress[i].Challenge + challenges = append(challenges, *challenge) + dbChallenge := dbChallengesMap[(*challenge).ChallengeID] + + if dbChallenge != nil { + (*challenge).ID = dbChallenge.ID + + if utils.AreDifferent(**challenge, *dbChallenge) { + *embeds = append(*embeds, *services.MakeEmbed("challenge_changed", *dbChallenge, **challenge)) + } + } else if len(dbChallengesMap) > 0 { + *embeds = append(*embeds, *services.MakeEmbed("new_challenge", nil, **challenge)) + } + } + + db.Save(&challenges) +} diff --git a/tasks/taskManager.go b/tasks/taskManager.go index 8e06187..6a69ccd 100644 --- a/tasks/taskManager.go +++ b/tasks/taskManager.go @@ -29,6 +29,7 @@ func checkInfos(db *gorm.DB) { checkEvents(db, embeds) checkAchievementCategories(db, embeds) checkSeason(db, embeds) + checkChallenges(db, embeds) utils.Log("Checked for infos.")