124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
|
|
dg "github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
type Settings struct {
|
|
Token string `json:"token"`
|
|
Server string `json:"server"`
|
|
Ranking [][]string `json:"ranking"`
|
|
}
|
|
|
|
var stg Settings
|
|
|
|
func init() {
|
|
file, err := os.ReadFile("settings.json")
|
|
if err != nil {
|
|
log.Fatalf("Error while reading settings.json : %s", err)
|
|
}
|
|
if err := json.Unmarshal(file, &stg); err != nil {
|
|
log.Fatalf("Error while reading settings.json : %s", err.Error())
|
|
}
|
|
}
|
|
|
|
type Command struct {
|
|
desc *dg.ApplicationCommand
|
|
handler func(*dg.Session, *dg.InteractionCreate)
|
|
}
|
|
|
|
var cmds = map[string]Command{
|
|
"ping": {
|
|
desc: &dg.ApplicationCommand{
|
|
Name: "ping",
|
|
Description: "Replies with pong",
|
|
DescriptionLocalizations: &map[dg.Locale]string{
|
|
dg.French: "Répond pong",
|
|
},
|
|
},
|
|
handler: func(s *dg.Session, i *dg.InteractionCreate) {
|
|
s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
|
Data: &dg.InteractionResponseData{
|
|
Content: ":ping_pong: Pong !",
|
|
},
|
|
})
|
|
},
|
|
},
|
|
"sub": {
|
|
desc: &dg.ApplicationCommand{
|
|
Name: "sub",
|
|
Description: "Submit a guess",
|
|
DescriptionLocalizations: &map[dg.Locale]string{
|
|
dg.French: "Envoie un essais pour deviner",
|
|
},
|
|
Options: []*dg.ApplicationCommandOption{},
|
|
},
|
|
handler: func(s *dg.Session, i *dg.InteractionCreate) {
|
|
|
|
},
|
|
},
|
|
}
|
|
|
|
// 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 {
|
|
opt.Name = r[0] + "_" + duplicateNames[i-1]
|
|
}
|
|
cmds["sub"].desc.Options = append(cmds["sub"].desc.Options, &opt)
|
|
}
|
|
}
|
|
}
|
|
|
|
func interactionHandler(s *dg.Session, i *dg.InteractionCreate) {
|
|
if cmd, ok := cmds[i.ApplicationCommandData().Name]; ok {
|
|
cmd.handler(s, i)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
s, _ := dg.New("Bot " + stg.Token)
|
|
s.AddHandler(func(s *dg.Session, r *dg.Ready) {
|
|
log.Printf("Ready!")
|
|
})
|
|
err := s.Open()
|
|
if err != nil {
|
|
log.Panicf("Failed to create session : %s", err)
|
|
}
|
|
s.AddHandler(interactionHandler)
|
|
|
|
// Create commands
|
|
for _, cmd := range cmds {
|
|
if _, err := s.ApplicationCommandCreate(s.State.User.ID, stg.Server, cmd.desc); err != nil {
|
|
log.Panicf("Failed to create command : %s", err)
|
|
}
|
|
}
|
|
|
|
defer s.Close()
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, os.Interrupt)
|
|
log.Println("Press Ctrl+C to exit")
|
|
<-stop
|
|
}
|