ping + sub commands
This commit is contained in:
commit
acca79ef18
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/settings.json
|
||||
11
_settings.json
Normal file
11
_settings.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"token": "BOT_TOKEN",
|
||||
"server": "SERVER_ID",
|
||||
"ranking": [
|
||||
["1", "USER_ID"],
|
||||
["2", "USER_ID"],
|
||||
["3", "USER_ID"],
|
||||
["4", "USER_ID", "USER_ID"],
|
||||
["5", "USER_ID"]
|
||||
]
|
||||
}
|
||||
10
go.mod
Normal file
10
go.mod
Normal file
@ -0,0 +1,10 @@
|
||||
module arkitu/mastermind-bot
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.29.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||
)
|
||||
12
go.sum
Normal file
12
go.sum
Normal file
@ -0,0 +1,12 @@
|
||||
github.com/bwmarrin/discordgo v0.29.0 h1:FmWeXFaKUwrcL3Cx65c20bTRW+vOb6k8AnaP+EgjDno=
|
||||
github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
123
main.go
Normal file
123
main.go
Normal file
@ -0,0 +1,123 @@
|
||||
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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user