sub command finished

This commit is contained in:
Arkitu 2026-01-18 23:38:30 +01:00
parent 112d3ce4dd
commit ebd4bf8002
2 changed files with 83 additions and 34 deletions

View File

@ -7,5 +7,6 @@
["3", "USER_ID"], ["3", "USER_ID"],
["4", "USER_ID", "USER_ID"], ["4", "USER_ID", "USER_ID"],
["5", "USER_ID"] ["5", "USER_ID"]
] ],
"color": 16711935
} }

108
main.go
View File

@ -5,6 +5,8 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strconv"
"strings"
dg "github.com/bwmarrin/discordgo" dg "github.com/bwmarrin/discordgo"
) )
@ -12,7 +14,10 @@ import (
type Settings struct { type Settings struct {
Token string `json:"token"` Token string `json:"token"`
Server string `json:"server"` Server string `json:"server"`
Ranking [][]string `json:"ranking"` Leaderboard [][]string `json:"leaderboard"`
Color int `json:"color"`
students map[string]int
rankings []map[string]struct{}
} }
var stg Settings var stg Settings
@ -25,6 +30,14 @@ func init() {
if err := json.Unmarshal(file, &stg); err != nil { if err := json.Unmarshal(file, &stg); err != nil {
log.Fatalf("Error while reading settings.json : %s", err.Error()) 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{ 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) { func send_error(s *dg.Session, i *dg.Interaction, reason string) {
s.InteractionRespond(i, &dg.InteractionResponse{ s.InteractionRespond(i, &dg.InteractionResponse{
Type: dg.InteractionResponseChannelMessageWithSource, Type: dg.InteractionResponseChannelMessageWithSource,
Data: &dg.InteractionResponseData{ Data: &dg.InteractionResponseData{
Embeds: []*dg.MessageEmbed{ Embeds: []*dg.MessageEmbed{
{ {
Title: "⚠️ Something went wrong", Title: "⚠️ Error",
Description: reason, Description: reason,
Color: 0xFF0000, Color: 0xFF0000,
}, },
@ -74,8 +114,10 @@ var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){
}) })
}, },
"sub": func(s *dg.Session, i *dg.InteractionCreate) { "sub": func(s *dg.Session, i *dg.InteractionCreate) {
// red := 0 red := 0
// green := 0 green := 0
guesses := []string{}
ids := make(map[string]struct{}, len(descs["sub"].Options))
for _, opt := range descs["sub"].Options { for _, opt := range descs["sub"].Options {
val := i.ApplicationCommandData().GetOption(opt.Name) val := i.ApplicationCommandData().GetOption(opt.Name)
if val == nil { if val == nil {
@ -86,36 +128,42 @@ var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){
send_error(s, i.Interaction, "Invalid option type") send_error(s, i.Interaction, "Invalid option type")
return return
} }
// guess := val.Value.(string) guess := val.Value.(string)
if _, ok := ids[guess]; ok {
} send_error(s, i.Interaction, "Les doublons ne sont pas autorisés !")
}, return
}
// 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]
} else { } 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() { func main() {