Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
fix example & rename some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 23, 2022
1 parent 405b1b5 commit 7ec2fb2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
.env
25 changes: 21 additions & 4 deletions _example/comands.go → _example/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ func TestCommand(b *Bot) handler.Command {
},
CommandHandlers: map[string]handler.CommandHandler{
"test1": func(e *events.ApplicationCommandInteractionCreate) error {
b.Logger.Info("test1")
return nil
b.Logger.Info("Test command 1")

return e.CreateMessage(discord.MessageCreate{
Content: "test1",
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
discord.NewPrimaryButton("test1", "handler:test"),
},
},
})
},
"test/test2": func(e *events.ApplicationCommandInteractionCreate) error {
b.Logger.Info("test2")
return nil
b.Logger.Info("Test command 2")

return e.CreateModal(discord.ModalCreate{
CustomID: "handler:test",
Title: "Test Modal",
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
discord.NewShortTextInput("test-input", "test input"),
},
},
})
},
},
}
Expand Down
7 changes: 5 additions & 2 deletions _example/components.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package main

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/handler"
)

func TestComponent(b *Bot) handler.Component {
return handler.Component{
Action: "test",
Name: "test",
Handler: func(args []string, e *events.ComponentInteractionCreate) error {
b.Logger.Info("test component")
return nil
return e.CreateMessage(discord.MessageCreate{
Content: "test button",
})
},
}
}
1 change: 1 addition & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {

var err error
if testBot.Client, err = disgo.New(token,
bot.WithLogger(logger),
bot.WithDefaultGateway(),
bot.WithEventListeners(h),
); err != nil {
Expand Down
9 changes: 7 additions & 2 deletions _example/modals.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package main

import (
"fmt"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/handler"
)

func TestModal(b *Bot) handler.Modal {
return handler.Modal{
Action: "test",
Name: "test",
Handler: func(args []string, e *events.ModalSubmitInteractionCreate) error {
b.Logger.Info("test modal")
return nil
return e.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("test modal: %s", e.Data.Text("test-input")),
})
},
}
}
2 changes: 1 addition & 1 deletion component.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ComponentHandler func(args []string, e *events.ComponentInteractionCreate) error

type Component struct {
Action string
Name string
Handler ComponentHandler
}

Expand Down
13 changes: 9 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ var _ bot.EventListener = (*Handler)(nil)

func New(logger log.Logger) *Handler {
return &Handler{
Logger: logger,
Logger: logger,
Commands: map[string]Command{},
Components: map[string]Component{},
Modals: map[string]Modal{},
}
}

Expand All @@ -32,20 +35,22 @@ func (h *Handler) AddCommands(commands ...Command) {

func (h *Handler) AddComponents(components ...Component) {
for _, component := range components {
h.Components[component.Action] = component
h.Components[component.Name] = component
}
}

func (h *Handler) AddModals(modals ...Modal) {
for _, modal := range modals {
h.Modals[modal.Action] = modal
h.Modals[modal.Name] = modal
}
}

func (h *Handler) SyncCommands(client bot.Client, guildIDs ...snowflake.ID) {
commands := make([]discord.ApplicationCommandCreate, len(h.Commands))
var i int
for _, command := range h.Commands {
commands = append(commands, command.Create)
commands[i] = command.Create
i++
}

if len(guildIDs) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ModalHandler func(args []string, e *events.ModalSubmitInteractionCreate) error

type Modal struct {
Action string
Name string
Handler ModalHandler
}

Expand Down

0 comments on commit 7ec2fb2

Please sign in to comment.