Skip to content

Commit

Permalink
make SubCommandContext available via interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zekro committed Aug 31, 2022
1 parent 7e1b4eb commit 0b65cd4
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 22 deletions.
36 changes: 24 additions & 12 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion examples/components/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func main() {

must(k.RegisterCommands(
new(commands.TestCommand),
new(commands.ModalCommand),
))

defer k.Unregister()
Expand Down
4 changes: 2 additions & 2 deletions examples/help/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 45 additions & 0 deletions examples/modals/main.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions examples/subcommands/commands/subs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion ken.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion middlewares/cmdhelp/cmdhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion middlewares/cmdhelp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 0b65cd4

Please sign in to comment.