sub get guess
This commit is contained in:
parent
0078ab4378
commit
112d3ce4dd
111
main.go
111
main.go
@ -27,41 +27,68 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command struct {
|
var descs = map[string]*dg.ApplicationCommand{
|
||||||
desc *dg.ApplicationCommand
|
"ping": &dg.ApplicationCommand{
|
||||||
handler func(*dg.Session, *dg.InteractionCreate)
|
Name: "ping",
|
||||||
}
|
Description: "Replies with pong",
|
||||||
|
DescriptionLocalizations: &map[dg.Locale]string{
|
||||||
var cmds = map[string]Command{
|
dg.French: "Répond pong",
|
||||||
"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": {
|
"sub": &dg.ApplicationCommand{
|
||||||
desc: &dg.ApplicationCommand{
|
Name: "sub",
|
||||||
Name: "sub",
|
Description: "Submit a guess",
|
||||||
Description: "Submit a guess",
|
DescriptionLocalizations: &map[dg.Locale]string{
|
||||||
DescriptionLocalizations: &map[dg.Locale]string{
|
dg.French: "Envoie un essais pour deviner",
|
||||||
dg.French: "Envoie un essais pour deviner",
|
|
||||||
},
|
|
||||||
Options: []*dg.ApplicationCommandOption{},
|
|
||||||
},
|
},
|
||||||
handler: func(s *dg.Session, i *dg.InteractionCreate) {
|
// /!\ Make sure to update handler if other options are added
|
||||||
|
// Guess options are added in init function
|
||||||
|
// It's assumed in handler that there aren't other options
|
||||||
|
Options: []*dg.ApplicationCommandOption{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
Description: reason,
|
||||||
|
Color: 0xFF0000,
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handlers are separated because we need to be able to access descs
|
||||||
|
var handlers = map[string]func(*dg.Session, *dg.InteractionCreate){
|
||||||
|
"ping": func(s *dg.Session, i *dg.InteractionCreate) {
|
||||||
|
s.InteractionRespond(i.Interaction, &dg.InteractionResponse{
|
||||||
|
Type: dg.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &dg.InteractionResponseData{
|
||||||
|
Content: ":ping_pong: Pong !",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
"sub": func(s *dg.Session, i *dg.InteractionCreate) {
|
||||||
|
// red := 0
|
||||||
|
// green := 0
|
||||||
|
for _, opt := range descs["sub"].Options {
|
||||||
|
val := i.ApplicationCommandData().GetOption(opt.Name)
|
||||||
|
if val == nil {
|
||||||
|
send_error(s, i.Interaction, "Missing option")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if val.Type != dg.ApplicationCommandOptionUser {
|
||||||
|
send_error(s, i.Interaction, "Invalid option type")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// guess := val.Value.(string)
|
||||||
|
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,17 +113,11 @@ func init() {
|
|||||||
} else {
|
} else {
|
||||||
opt.Name = r[0] + "_" + duplicateNames[i-1]
|
opt.Name = r[0] + "_" + duplicateNames[i-1]
|
||||||
}
|
}
|
||||||
cmds["sub"].desc.Options = append(cmds["sub"].desc.Options, &opt)
|
descs["sub"].Options = append(descs["sub"].Options, &opt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func interactionHandler(s *dg.Session, i *dg.InteractionCreate) {
|
|
||||||
if cmd, ok := cmds[i.ApplicationCommandData().Name]; ok {
|
|
||||||
cmd.handler(s, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s, _ := dg.New("Bot " + stg.Token)
|
s, _ := dg.New("Bot " + stg.Token)
|
||||||
s.AddHandler(func(s *dg.Session, r *dg.Ready) {
|
s.AddHandler(func(s *dg.Session, r *dg.Ready) {
|
||||||
@ -106,11 +127,21 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("Failed to create session : %s", err)
|
log.Panicf("Failed to create session : %s", err)
|
||||||
}
|
}
|
||||||
s.AddHandler(interactionHandler)
|
s.AddHandler(func(s *dg.Session, i *dg.InteractionCreate) {
|
||||||
|
if i.Type != dg.InteractionApplicationCommand {
|
||||||
|
log.Printf("Warning : Received unknown interaction type (%s)", i.Type.String())
|
||||||
|
}
|
||||||
|
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
|
||||||
|
h(s, i)
|
||||||
|
log.Printf("Reveived command \"%s\"", i.ApplicationCommandData().Name)
|
||||||
|
} else {
|
||||||
|
log.Printf("Warning : Received unknown command \"%s\" (id:%s)", i.ApplicationCommandData().Name, i.ApplicationCommandData().ID)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Create commands
|
// Create commands
|
||||||
for _, cmd := range cmds {
|
for _, d := range descs {
|
||||||
if _, err := s.ApplicationCommandCreate(s.State.User.ID, stg.Server, cmd.desc); err != nil {
|
if _, err := s.ApplicationCommandCreate(s.State.User.ID, stg.Server, d); err != nil {
|
||||||
log.Panicf("Failed to create command : %s", err)
|
log.Panicf("Failed to create command : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user