From 0b65cd40c9880f4ff9aafdfe32022f6e84febc58 Mon Sep 17 00:00:00 2001 From: zekro Date: Wed, 31 Aug 2022 09:03:51 +0000 Subject: [PATCH] make SubCommandContext available via interface --- context.go | 36 ++++++++++----- examples/components/main.go | 1 - examples/help/commands/test.go | 4 +- .../{components => modals}/commands/modal.go | 4 +- examples/modals/main.go | 45 +++++++++++++++++++ examples/subcommands/commands/subs.go | 4 +- ken.go | 2 +- middlewares/cmdhelp/cmdhelp.go | 2 +- middlewares/cmdhelp/command.go | 2 +- 9 files changed, 78 insertions(+), 22 deletions(-) rename examples/{components => modals}/commands/modal.go (95%) create mode 100644 examples/modals/main.go diff --git a/context.go b/context.go index 495637a..12b83d2 100644 --- a/context.go +++ b/context.go @@ -331,28 +331,40 @@ func (c *Ctx) MessageCommand() (cmd MessageCommand, ok bool) { // to handle sub command calls. type SubCommandHandler struct { Name string - Run func(ctx *SubCommandCtx) error + Run func(ctx SubCommandContext) error } -// SubCommandCtx wraps the current command Ctx and -// with the called sub command name and scopes the -// command options to the options of the called -// sub command. +// SubCommandContext wraps the current command +// Context and with the called sub command name +// and scopes the command options to the +// options of the called sub command. // // The SubCommandCtx must not be stored or used // after command execution. -type SubCommandCtx struct { +type SubCommandContext interface { + Context + + // GetSubCommandName returns the sub command + // name which has been invoked. + GetSubCommandName() string +} + +type subCommandCtx struct { *Ctx - SubCommandName string + subCommandName string } -var _ Context = (*SubCommandCtx)(nil) +var _ SubCommandContext = (*subCommandCtx)(nil) // Options returns the options array of the called // sub command. -func (c *SubCommandCtx) Options() CommandOptions { - return c.Ctx.Options().GetByName(c.SubCommandName).Options +func (c *subCommandCtx) Options() CommandOptions { + return c.Ctx.Options().GetByName(c.subCommandName).Options +} + +func (c *subCommandCtx) GetSubCommandName() string { + return c.subCommandName } // HandleSubCommands takes a list of sub command handles. @@ -373,9 +385,9 @@ func (c *Ctx) HandleSubCommands(handler ...SubCommandHandler) (err error) { continue } - ctx := c.ken.subCtxPool.Get().(*SubCommandCtx) + ctx := c.ken.subCtxPool.Get().(*subCommandCtx) ctx.Ctx = c - ctx.SubCommandName = h.Name + ctx.subCommandName = h.Name err = h.Run(ctx) c.ken.subCtxPool.Put(ctx) break diff --git a/examples/components/main.go b/examples/components/main.go index 0262ad6..d335979 100644 --- a/examples/components/main.go +++ b/examples/components/main.go @@ -33,7 +33,6 @@ func main() { must(k.RegisterCommands( new(commands.TestCommand), - new(commands.ModalCommand), )) defer k.Unregister() diff --git a/examples/help/commands/test.go b/examples/help/commands/test.go index c4165d8..4a8d7b9 100644 --- a/examples/help/commands/test.go +++ b/examples/help/commands/test.go @@ -49,7 +49,7 @@ func (c *TestCommand) IsDmCapable() bool { return true } -func (C *TestCommand) Help(ctx *ken.SubCommandCtx) (emb *discordgo.MessageEmbed, err error) { +func (C *TestCommand) Help(ctx ken.SubCommandContext) (emb *discordgo.MessageEmbed, err error) { emb = &discordgo.MessageEmbed{ Color: 0x00ff00, Description: "This is a help message that describes how to use this command!", @@ -65,7 +65,7 @@ func (c *TestCommand) Run(ctx ken.Context) (err error) { return } -func (c *TestCommand) pog(ctx *ken.SubCommandCtx) (err error) { +func (c *TestCommand) pog(ctx ken.SubCommandContext) (err error) { return ctx.Respond(&discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ diff --git a/examples/components/commands/modal.go b/examples/modals/commands/modal.go similarity index 95% rename from examples/components/commands/modal.go rename to examples/modals/commands/modal.go index 0057ad5..a33f489 100644 --- a/examples/components/commands/modal.go +++ b/examples/modals/commands/modal.go @@ -10,8 +10,8 @@ import ( type ModalCommand struct{} var ( - _ ken.SlashCommand = (*TestCommand)(nil) - _ ken.DmCapable = (*TestCommand)(nil) + _ ken.SlashCommand = (*ModalCommand)(nil) + _ ken.DmCapable = (*ModalCommand)(nil) ) func (c *ModalCommand) Name() string { diff --git a/examples/modals/main.go b/examples/modals/main.go new file mode 100644 index 0000000..96d1d69 --- /dev/null +++ b/examples/modals/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "os" + "os/signal" + "syscall" + + "github.com/bwmarrin/discordgo" + "github.com/zekrotja/ken" + "github.com/zekrotja/ken/examples/modals/commands" + "github.com/zekrotja/ken/store" +) + +func must(err error) { + if err != nil { + panic(err) + } +} + +func main() { + token := os.Getenv("TOKEN") + + session, err := discordgo.New("Bot " + token) + if err != nil { + panic(err) + } + defer session.Close() + + k, err := ken.New(session, ken.Options{ + CommandStore: store.NewDefault(), + }) + must(err) + + must(k.RegisterCommands( + new(commands.ModalCommand), + )) + + defer k.Unregister() + + must(session.Open()) + + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc +} diff --git a/examples/subcommands/commands/subs.go b/examples/subcommands/commands/subs.go index 454d4df..15f7dbc 100644 --- a/examples/subcommands/commands/subs.go +++ b/examples/subcommands/commands/subs.go @@ -74,7 +74,7 @@ func (c *SubsCommand) Run(ctx ken.Context) (err error) { return } -func (c *SubsCommand) one(ctx *ken.SubCommandCtx) (err error) { +func (c *SubsCommand) one(ctx ken.SubCommandContext) (err error) { if err = ctx.Defer(); err != nil { return } @@ -85,7 +85,7 @@ func (c *SubsCommand) one(ctx *ken.SubCommandCtx) (err error) { return } -func (c *SubsCommand) two(ctx *ken.SubCommandCtx) (err error) { +func (c *SubsCommand) two(ctx ken.SubCommandContext) (err error) { var arg int if argV, ok := ctx.Options().GetByNameOptional("arg"); ok { arg = int(argV.IntValue()) diff --git a/ken.go b/ken.go index 6b6d90f..1c025ba 100644 --- a/ken.go +++ b/ken.go @@ -102,7 +102,7 @@ func New(s *discordgo.Session, options ...Options) (k *Ken, err error) { }, subCtxPool: sync.Pool{ New: func() interface{} { - return &SubCommandCtx{} + return &subCommandCtx{} }, }, mwBefore: make([]MiddlewareBefore, 0), diff --git a/middlewares/cmdhelp/cmdhelp.go b/middlewares/cmdhelp/cmdhelp.go index 3473c3e..8fcff04 100644 --- a/middlewares/cmdhelp/cmdhelp.go +++ b/middlewares/cmdhelp/cmdhelp.go @@ -52,7 +52,7 @@ func (m *Middleware) Before(ctx *ken.Ctx) (next bool, err error) { func (m *Middleware) subCmdHandler(cmd HelpProvider, executed chan<- bool) ken.SubCommandHandler { return ken.SubCommandHandler{ Name: m.subCommandName, - Run: func(ctx *ken.SubCommandCtx) (err error) { + Run: func(ctx ken.SubCommandContext) (err error) { executed <- true emb, err := cmd.Help(ctx) if err != nil { diff --git a/middlewares/cmdhelp/command.go b/middlewares/cmdhelp/command.go index 0263099..329119a 100644 --- a/middlewares/cmdhelp/command.go +++ b/middlewares/cmdhelp/command.go @@ -8,5 +8,5 @@ import ( // HelpProvider defines a command which provides // help content. type HelpProvider interface { - Help(ctx *ken.SubCommandCtx) (emb *discordgo.MessageEmbed, err error) + Help(ctx ken.SubCommandContext) (emb *discordgo.MessageEmbed, err error) }