Skip to content

Commit

Permalink
Draft idea
Browse files Browse the repository at this point in the history
Signed-off-by: jolheiser <[email protected]>
  • Loading branch information
jolheiser committed Jan 4, 2022
1 parent 5688b03 commit 6d69693
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 101 deletions.
2 changes: 1 addition & 1 deletion 0_example/moderate-myself/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var command = corde.NewSlashCommand(
"cmd",
"edit and view existing slash commands",
corde.NewSubcommand("list", "list existing slash commands", false),
corde.NewSubcommand("list", "list existing slash commands"),
)

func main() {
Expand Down
11 changes: 5 additions & 6 deletions 0_example/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import (
)

var commands = []corde.CreateCommander{

corde.NewSlashCommand(
"todo",
"view edit and remove todos",
corde.NewSubcommand(
"list",
"list todos",
false,
corde.NewStringOption("name", "todo name", true),
corde.NewStringOption("value", "todo value", true),
corde.NewCommandOption[string]("name", "todo name", true),
corde.NewCommandOption[string]("value", "todo value", true),
),
corde.NewSubcommand("add", "add a todo", false),
corde.NewSubcommand("add", "add a todo"),
corde.NewSubcommand(
"rm",
"remove a todo",
false,
corde.NewStringOption("name", "todo name", true),
corde.NewCommandOption[string]("name", "todo name", true),
),
),
}
Expand Down
132 changes: 38 additions & 94 deletions register-commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package corde

import "encoding/json"
import (
"encoding/json"
"fmt"
)

// CreateCommander is a command that can be registered
type CreateCommander interface {
Expand Down Expand Up @@ -34,14 +37,6 @@ type CreateCommand struct {
Options []CreateOptioner `json:"options,omitempty"`
}

// CreateCommand is a slash command that can be registered to discord
type SlashCommand struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type CommandType `json:"type,omitempty"`
Options []CreateOptioner `json:"options,omitempty"`
}

// NewSlashCommand returns a new slash command
func NewSlashCommand(name string, description string, options ...CreateOptioner) CreateCommand {
return CreateCommand{
Expand All @@ -53,121 +48,74 @@ func NewSlashCommand(name string, description string, options ...CreateOptioner)
}

func (c CreateCommand) createCommand() CreateCommand {
return CreateCommand{
Name: c.Name,
Description: c.Description,
Options: c.Options,
Type: c.Type,
}
}

// StringOption is an option that is a string
type StringOption struct {
Name string
Description string
Required bool
Choices []Choice[string]
}

// NewStringOption returns a new string option
func NewStringOption(name string, description string, required bool, choices ...Choice[string]) *StringOption {
return &StringOption{
Name: name,
Description: description,
Required: required,
Choices: choices,
}
}

func (o *StringOption) createOption() CreateOption {
return CreateOption{
Name: o.Name,
Description: o.Description,
Required: o.Required,
Type: OPTION_STRING,
}
return c
}

func (o *StringOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.createOption())
// CommandOptionConstraint is the constraint for CommandOption types
type CommandOptionConstraint interface {
string | int | bool | float64 // This could be enhanced for user/mention/etc types?
}

// IntOption represents an option that is an integer
type IntOption struct {
// CommandOption is an option for a CreateCommand
type CommandOption[T CommandOptionConstraint] struct {
Name string
Description string
Required bool
Choices []Choice[int]
Choices []Choice[T]
}

// NewIntOption returns a new integer option
func NewIntOption(name string, description string, required bool, choices ...Choice[int]) *IntOption {
return &IntOption{
// NewCommandOption is a new CommandOption of type T
func NewCommandOption[T CommandOptionConstraint](name string, description string, required bool, choices ...Choice[T]) *CommandOption[T] {
return &CommandOption[T]{
Name: name,
Description: description,
Required: required,
Choices: choices,
}
}

func (o *IntOption) createOption() CreateOption {
return CreateOption{
Name: o.Name,
Description: o.Description,
Required: o.Required,
Type: OPTION_INTEGER,
func (c *CommandOption[T]) createOption() CreateOption {
var typ OptionType
var t T
switch fmt.Sprintf("%T", t) {

This comment has been minimized.

Copy link
@jolheiser

jolheiser Jan 4, 2022

Author Collaborator

This hack is pain.

case "string":
typ = OPTION_STRING
case "int":
typ = OPTION_INTEGER
case "bool":
typ = OPTION_BOOLEAN
case "float64":
typ = OPTION_NUMBER
}
}

func (o *IntOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.createOption())
}

// BoolOption is an option that is a boolean
type BoolOption struct {
Name string
Description string
Required bool
Choices []Choice[bool]
}

// NewBoolOption returns a new boolean option
func NewBoolOption(name string, description string, required bool, choices ...Choice[bool]) *BoolOption {
return &BoolOption{
Name: name,
Description: description,
Required: required,
Choices: choices,
var choices []Choice[any]
for _, ch := range c.Choices {
choices = append(choices, Choice[any]{Name: ch.Name, Value: ch.Value})

This comment has been minimized.

Copy link
@jolheiser

jolheiser Jan 4, 2022

Author Collaborator

This hack is also pain.

}
}

func (o *BoolOption) createOption() CreateOption {
return CreateOption{
Name: o.Name,
Description: o.Description,
Required: o.Required,
Type: OPTION_BOOLEAN,
Name: c.Name,
Description: c.Description,
Required: c.Required,
Type: typ,
Choices: choices,
}
}

func (o *BoolOption) MarshalJSON() ([]byte, error) {
return json.Marshal(o.createOption())
func (c *CommandOption[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(c.createOption())
}

// SubcommandOption is an option that is a subcommand
type SubcommandOption struct {
Name string
Description string
Required bool
Options []CreateOptioner
}

// NewSubcommand returns a new subcommand
func NewSubcommand(name string, description string, required bool, options ...CreateOptioner) *SubcommandOption {
func NewSubcommand(name string, description string, options ...CreateOptioner) *SubcommandOption {
return &SubcommandOption{
Name: name,
Description: description,
Required: required,
Options: options,
}
}
Expand All @@ -177,7 +125,6 @@ func (o *SubcommandOption) createOption() CreateOption {
Options: o.Options,
Name: o.Name,
Description: o.Description,
Required: o.Required,
Type: OPTION_SUB_COMMAND,
}
}
Expand All @@ -190,16 +137,14 @@ func (o *SubcommandOption) MarshalJSON() ([]byte, error) {
type SubcommandGroupOption struct {
Name string
Description string
Required bool
Options []CreateOptioner
}

// NewSubcommandGroup returns a new subcommand group
func NewSubcommandGroup(name string, description string, required bool, options ...CreateOptioner) *SubcommandGroupOption {
func NewSubcommandGroup(name string, description string, options ...CreateOptioner) *SubcommandGroupOption {
return &SubcommandGroupOption{
Name: name,
Description: description,
Required: required,
Options: options,
}
}
Expand All @@ -209,7 +154,6 @@ func (o *SubcommandGroupOption) createOption() CreateOption {
Options: o.Options,
Name: o.Name,
Description: o.Description,
Required: o.Required,
Type: OPTION_SUB_COMMAND_GROUP,
}
}
Expand Down

0 comments on commit 6d69693

Please sign in to comment.