diff --git a/_settings.json b/_settings.json index b7aeb0c..0ab98c5 100644 --- a/_settings.json +++ b/_settings.json @@ -7,5 +7,6 @@ ["3", "USER_ID"], ["4", "USER_ID", "USER_ID"], ["5", "USER_ID"] - ] + ], + "color": 16711935 } diff --git a/main.go b/main.go index 7707545..3cfa4e9 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,19 @@ import ( "log" "os" "os/signal" + "strconv" + "strings" dg "github.com/bwmarrin/discordgo" ) type Settings struct { - Token string `json:"token"` - Server string `json:"server"` - Ranking [][]string `json:"ranking"` + Token string `json:"token"` + Server string `json:"server"` + Leaderboard [][]string `json:"leaderboard"` + Color int `json:"color"` + students map[string]int + rankings []map[string]struct{} } var stg Settings @@ -25,6 +30,14 @@ func init() { if err := json.Unmarshal(file, &stg); err != nil { log.Fatalf("Error while reading settings.json : %s", err.Error()) } + stg.students = make(map[string]int, len(stg.Leaderboard)) + for i, r := range stg.Leaderboard { + stg.rankings = append(stg.rankings, make(map[string]struct{}, len(r))) + for _, s := range r { + stg.rankings[i][s] = struct{}{} + stg.students[s] = i + } + } } var descs = map[string]*dg.ApplicationCommand{ @@ -48,13 +61,40 @@ var descs = map[string]*dg.ApplicationCommand{ }, } +// Just in case... +var duplicateNames = []string{ + "bis", "ter", "quater", "quinquies", "sexies", "septies", "octies", "nonies", "decies", "undecies", "duodecies", "terdecies", "quaterdecies", "quindecies", "sexdecies", "septdecies", "octodecies", "novodecies", "vicies", +} + +func init() { + for rank, students := range stg.rankings { + rankName := strconv.Itoa(rank + 1) + for i := range len(students) { + opt := dg.ApplicationCommandOption{ + Type: dg.ApplicationCommandOptionUser, + Description: "Guess for student " + rankName, + DescriptionLocalizations: map[dg.Locale]string{ + dg.French: "Supposition pour l'élève " + rankName, + }, + Required: true, + } + if i == 0 { + opt.Name = rankName + } else { + opt.Name = rankName + "_" + duplicateNames[i-1] + } + descs["sub"].Options = append(descs["sub"].Options, &opt) + } + } +} + func send_error(s *dg.Session, i *dg.Interaction, reason string) { s.InteractionRespond(i, &dg.InteractionResponse{ Type: dg.InteractionResponseChannelMessageWithSource, Data: &dg.InteractionResponseData{ Embeds: []*dg.MessageEmbed{ { - Title: "⚠️ Something went wrong", + Title: "⚠️ Error", Description: reason, Color: 0xFF0000, }, @@ -74,8 +114,10 @@ var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){ }) }, "sub": func(s *dg.Session, i *dg.InteractionCreate) { - // red := 0 - // green := 0 + red := 0 + green := 0 + guesses := []string{} + ids := make(map[string]struct{}, len(descs["sub"].Options)) for _, opt := range descs["sub"].Options { val := i.ApplicationCommandData().GetOption(opt.Name) if val == nil { @@ -86,36 +128,42 @@ var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){ send_error(s, i.Interaction, "Invalid option type") return } - // guess := val.Value.(string) - - } - }, -} - -// Just in case... -var duplicateNames = []string{ - "bis", "ter", "quater", "quinquies", "sexies", "septies", "octies", "nonies", "decies", "undecies", "duodecies", "terdecies", "quaterdecies", "quindecies", "sexdecies", "septdecies", "octodecies", "novodecies", "vicies", -} - -func init() { - for _, r := range stg.Ranking { - for i, _ := range r[1:] { - opt := dg.ApplicationCommandOption{ - Type: dg.ApplicationCommandOptionUser, - Description: "Guess for student " + r[0], - DescriptionLocalizations: map[dg.Locale]string{ - dg.French: "Supposition pour l'élève " + r[0], - }, - Required: true, - } - if i == 0 { - opt.Name = r[0] + guess := val.Value.(string) + if _, ok := ids[guess]; ok { + send_error(s, i.Interaction, "Les doublons ne sont pas autorisés !") + return } else { - opt.Name = r[0] + "_" + duplicateNames[i-1] + ids[guess] = struct{}{} } - descs["sub"].Options = append(descs["sub"].Options, &opt) + grstr, _, _ := strings.Cut(opt.Name, "_") + guessRank, err := strconv.Atoi(grstr) + guessRank -= 1 + if err != nil || guessRank < 0 || stg.rankings[guessRank] == nil { + send_error(s, i.Interaction, "Invalid option name") + } + if num, ok := stg.students[guess]; ok { + if num == guessRank { + green += 1 + } else { + red += 1 + } + } + + guesses = append(guesses, strconv.Itoa(guessRank+1)+": <@"+guess+">") } - } + s.InteractionRespond(i.Interaction, &dg.InteractionResponse{ + Type: dg.InteractionResponseChannelMessageWithSource, + Data: &dg.InteractionResponseData{ + Embeds: []*dg.MessageEmbed{ + { + Title: "Guess", + Description: strings.Join(guesses, "\n") + "\n\n**" + strconv.Itoa(green) + " 🟩**\n**" + strconv.Itoa(red) + " 🟥**", + Color: stg.Color, + }, + }, + }, + }) + }, } func main() {