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

Commit

Permalink
remove translation stuff & Command/Autocomplete/Component/Modal Context
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Aug 11, 2022
1 parent 5982c21 commit d9b8866
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 261 deletions.
23 changes: 12 additions & 11 deletions _example/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

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

Expand Down Expand Up @@ -29,22 +30,22 @@ func TestCommand(b *Bot) handler.Command {
},
Check: simpleCommandCheck(b),
CommandHandlers: map[string]handler.CommandHandler{
"test1": func(ctx *handler.CommandContext) error {
b.Logger.Info(ctx.Printer.Sprintf("commands.test1"))
"test1": func(event *events.ApplicationCommandInteractionCreate) error {
b.Logger.Info("Test Command 1")

return ctx.CreateMessage(discord.MessageCreate{
Content: ctx.Printer.Sprintf("commands.test1"),
return event.CreateMessage(discord.MessageCreate{
Content: "Test Command 1",
Components: []discord.ContainerComponent{
discord.ActionRowComponent{
discord.NewPrimaryButton("test1", "handler:test"),
},
},
})
},
"test/test2": func(ctx *handler.CommandContext) error {
b.Logger.Info(ctx.Printer.Sprintf("commands.test2"))
"test/test2": func(event *events.ApplicationCommandInteractionCreate) error {
b.Logger.Info("Test Command 2")

return ctx.CreateModal(discord.ModalCreate{
return event.CreateModal(discord.ModalCreate{
CustomID: "handler:test",
Title: "Test Modal",
Components: []discord.ContainerComponent{
Expand All @@ -58,9 +59,9 @@ func TestCommand(b *Bot) handler.Command {
}
}

func simpleCommandCheck(b *Bot) func(ctx *handler.CommandContext) bool {
return func(ctx *handler.CommandContext) bool {
b.Logger.Info(ctx.Printer.Sprintf("checks.command"))
return ctx.User().ID == userID
func simpleCommandCheck(b *Bot) handler.Check[*events.ApplicationCommandInteractionCreate] {
return func(event *events.ApplicationCommandInteractionCreate) bool {
b.Logger.Info("Command Check")
return event.User().ID == userID
}
}
17 changes: 9 additions & 8 deletions _example/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ 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{
Name: "test",
Check: simpleComponentCheck(b),
Handler: func(ctx *handler.ComponentContext) error {
b.Logger.Info(ctx.Printer.Sprintf("components.test"))
return ctx.CreateMessage(discord.MessageCreate{
Content: ctx.Printer.Sprintf("checks.component"),
Handler: func(event *events.ComponentInteractionCreate) error {
b.Logger.Info("Test Component")
return event.CreateMessage(discord.MessageCreate{
Content: "Test Component",
})
},
}
}

func simpleComponentCheck(b *Bot) func(ctx *handler.ComponentContext) bool {
return func(ctx *handler.ComponentContext) bool {
b.Logger.Info(ctx.Printer.Sprintf("checks.component"))
return ctx.User().ID == userID
func simpleComponentCheck(b *Bot) handler.Check[*events.ComponentInteractionCreate] {
return func(event *events.ComponentInteractionCreate) bool {
b.Logger.Info("Test Component Check")
return event.User().ID == userID
}
}
21 changes: 0 additions & 21 deletions _example/languages/de.json

This file was deleted.

21 changes: 0 additions & 21 deletions _example/languages/en.json

This file was deleted.

10 changes: 0 additions & 10 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"embed"
"os"
"os/signal"
"syscall"
Expand All @@ -12,17 +11,13 @@ import (
"github.com/disgoorg/handler"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
"golang.org/x/text/language"
)

var (
token = os.Getenv("TOKEN")
userID = snowflake.GetEnv("USER_ID")
)

//go:embed languages/*.json
var languages embed.FS

type Bot struct {
Logger log.Logger
Client bot.Client
Expand All @@ -41,11 +36,6 @@ func main() {
h.AddComponents(TestComponent(testBot))
h.AddModals(TestModal(testBot))

h.InitI18n(language.English)
if err := h.I18n.LoadFromEmbedFS(languages, "languages"); err != nil {
logger.Fatal("Failed to load languages: ", err)
}

var err error
if testBot.Client, err = disgo.New(token,
bot.WithLogger(logger),
Expand Down
17 changes: 9 additions & 8 deletions _example/modals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ 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{
Name: "test",
Check: simpleModalCheck(b),
Handler: func(ctx *handler.ModalContext) error {
b.Logger.Info(ctx.Printer.Sprintf("modals.test"))
return ctx.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("test modal: %s", ctx.Data.Text("test-input")),
Handler: func(event *events.ModalSubmitInteractionCreate) error {
b.Logger.Info("Test Modal")
return event.CreateMessage(discord.MessageCreate{
Content: fmt.Sprintf("test modal: %s", event.Data.Text("test-input")),
})
},
}
}

func simpleModalCheck(b *Bot) func(ctx *handler.ModalContext) bool {
return func(ctx *handler.ModalContext) bool {
b.Logger.Info(ctx.Printer.Sprintf("checks.modal"))
return ctx.User().ID == userID
func simpleModalCheck(b *Bot) handler.Check[*events.ModalSubmitInteractionCreate] {
return func(event *events.ModalSubmitInteractionCreate) bool {
b.Logger.Info("Modal Check")
return event.User().ID == userID
}
}
38 changes: 14 additions & 24 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,31 @@ import (
)

type (
CommandHandler func(ctx *CommandContext) error
AutocompleteHandler func(ctx *AutocompleteContext) error
CommandHandler func(event *events.ApplicationCommandInteractionCreate) error
AutocompleteHandler func(event *events.AutocompleteInteractionCreate) error
)

type Command struct {
Create discord.ApplicationCommandCreate
Check Check[*CommandContext]
AutocompleteCheck Check[*AutocompleteContext]
Check Check[*events.ApplicationCommandInteractionCreate]
AutocompleteCheck Check[*events.AutocompleteInteractionCreate]
CommandHandlers map[string]CommandHandler
AutocompleteHandlers map[string]AutocompleteHandler
}

func (h *Handler) handleCommand(e *events.ApplicationCommandInteractionCreate) {
name := e.Data.CommandName()
func (h *Handler) handleCommand(event *events.ApplicationCommandInteractionCreate) {
name := event.Data.CommandName()
cmd, ok := h.Commands[name]
if !ok || cmd.CommandHandlers == nil {
h.Logger.Errorf("No command or handler found for \"%s\"", name)
}

ctx := &CommandContext{
ApplicationCommandInteractionCreate: e,
Printer: h.I18n.NewPrinter(e.Locale()),
}

if cmd.Check != nil && !cmd.Check(ctx) {
if cmd.Check != nil && !cmd.Check(event) {
return
}

var path string
if d, ok := e.Data.(discord.SlashCommandInteractionData); ok {
if d, ok := event.Data.(discord.SlashCommandInteractionData); ok {
path = buildCommandPath(d.SubCommandName, d.SubCommandGroupName)
}

Expand All @@ -45,36 +40,31 @@ func (h *Handler) handleCommand(e *events.ApplicationCommandInteractionCreate) {
return
}

if err := handler(ctx); err != nil {
if err := handler(event); err != nil {
h.Logger.Errorf("Failed to handle command \"%s\" with path \"%s\": %s", name, path, err)
}
}

func (h *Handler) handleAutocomplete(e *events.AutocompleteInteractionCreate) {
name := e.Data.CommandName
func (h *Handler) handleAutocomplete(event *events.AutocompleteInteractionCreate) {
name := event.Data.CommandName
cmd, ok := h.Commands[name]
if !ok || cmd.AutocompleteHandlers == nil {
h.Logger.Errorf("No command or handler found for \"%s\"", name)
}

ctx := &AutocompleteContext{
AutocompleteInteractionCreate: e,
Printer: h.I18n.NewPrinter(e.Locale()),
}

if cmd.AutocompleteCheck != nil && !cmd.AutocompleteCheck(ctx) {
if cmd.AutocompleteCheck != nil && !cmd.AutocompleteCheck(event) {
return
}

path := buildCommandPath(e.Data.SubCommandName, e.Data.SubCommandGroupName)
path := buildCommandPath(event.Data.SubCommandName, event.Data.SubCommandGroupName)

handler, ok := cmd.AutocompleteHandlers[path]
if !ok {
h.Logger.Warnf("No autocomplete handler for command \"%s\" with path \"%s\" found", name, path)
return
}

if err := handler(ctx); err != nil {
if err := handler(event); err != nil {
h.Logger.Errorf("Failed to handle autocomplete for command \"%s\" with path \"%s\": %s", name, path, err)
}
}
Expand Down
21 changes: 7 additions & 14 deletions component.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,31 @@ import (
"github.com/disgoorg/disgo/events"
)

type ComponentHandler func(ctx *ComponentContext) error
type ComponentHandler func(event *events.ComponentInteractionCreate) error

type Component struct {
Name string
Check Check[*ComponentContext]
Check Check[*events.ComponentInteractionCreate]
Handler ComponentHandler
}

func (h *Handler) handleComponent(e *events.ComponentInteractionCreate) {
customID := e.Data.CustomID()
func (h *Handler) handleComponent(event *events.ComponentInteractionCreate) {
customID := event.Data.CustomID()
if !strings.HasPrefix(customID, "handler:") {
return
}

args := strings.Split(customID, ":")
componentName := args[1]
componentName := strings.Split(customID, ":")[1]
component, ok := h.Components[componentName]
if !ok || component.Handler == nil {
h.Logger.Errorf("No component handler for \"%s\" found", componentName)
}

ctx := &ComponentContext{
ComponentInteractionCreate: e,
Printer: h.I18n.NewPrinter(e.Locale()),
Args: args[2:],
}

if component.Check != nil && !component.Check(ctx) {
if component.Check != nil && !component.Check(event) {
return
}

if err := component.Handler(ctx); err != nil {
if err := component.Handler(event); err != nil {
h.Logger.Errorf("Failed to handle component interaction for \"%s\" : %s", componentName, err)
}
}
28 changes: 0 additions & 28 deletions context.go

This file was deleted.

3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/disgoorg/handler
go 1.18

require (
github.com/disgoorg/disgo v0.13.15
github.com/disgoorg/disgo v0.13.16
github.com/disgoorg/log v1.2.0
github.com/disgoorg/snowflake/v2 v2.0.0
golang.org/x/text v0.3.7
)

require (
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/disgoorg/disgo v0.13.15 h1:BLWwE0A1sT/EY3Ri7Qze2J1RaRirOLHgc87bo3OyhCQ=
github.com/disgoorg/disgo v0.13.15/go.mod h1:Cyip4bCYHD3rHgDhBPT9cLo81e9AMbDe8ocM50UNRM4=
github.com/disgoorg/disgo v0.13.16 h1:vObm6rdJdA6plmruqGUmhmWcEfF0EdFelo1x7u+vjCA=
github.com/disgoorg/disgo v0.13.16/go.mod h1:Cyip4bCYHD3rHgDhBPT9cLo81e9AMbDe8ocM50UNRM4=
github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo=
github.com/disgoorg/log v1.2.0/go.mod h1:3x1KDG6DI1CE2pDwi3qlwT3wlXpeHW/5rVay+1qDqOo=
github.com/disgoorg/snowflake/v2 v2.0.0 h1:+xvyyDddXmXLHmiG8SZiQ3sdZdZPbUR22fSHoqwkrOA=
Expand All @@ -13,6 +13,4 @@ github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3z
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 h1:Xt4/LzbTwfocTk9ZLEu4onjeFucl88iW+v4j4PWbQuE=
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Loading

0 comments on commit d9b8866

Please sign in to comment.