Skip to content

Commit

Permalink
feat: create command helpers (#6)
Browse files Browse the repository at this point in the history
* feat: create command helpers

also move _example to 0_example since gopls
doesn't like the _example directory name.

* doc comment + impl createOption on base type

* generate create command helpers
  • Loading branch information
PL Pery authored Jan 5, 2022
1 parent 82efc61 commit b03180c
Show file tree
Hide file tree
Showing 20 changed files with 727 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.vscode/
.env
.env*
17 changes: 4 additions & 13 deletions _example/bongo/main.go → 0_example/bongo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import (
"github.com/Karitham/corde"
)

var command = corde.Command{
Name: "bongo",
Description: "send a big bongo",
Type: corde.COMMAND_CHAT_INPUT,
}
var command = corde.NewSlashCommand("bongo", "send a big bongo")

func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
Expand All @@ -36,23 +32,18 @@ func main() {
log.Fatalln("error registering command: ", err)
}

log.Println("serving on :8070")
if err := m.ListenAndServe(":8070"); err != nil {
log.Fatalln(err)
}
}

func bongoHandler(w corde.ResponseWriter, i *corde.Interaction) {
func bongoHandler(w corde.ResponseWriter, _ *corde.Interaction) {
resp, err := http.Get("https://cdn.discordapp.com/emojis/745709799890747434.gif?size=128")
if err != nil {
w.Respond(corde.NewResp().Content("couldn't retrieve bongo").Ephemeral().B())
return
}
defer resp.Body.Close()
w.Respond(corde.NewResp().
Attachments(corde.Attachment{
Body: resp.Body,
ID: corde.Snowflake(0),
Filename: "bongo.gif",
}).B(),
)
w.Respond(corde.NewResp().Attachment(resp.Body, "bongo.gif").B())
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ import (
"github.com/Karitham/corde"
)

var command = corde.Command{
Name: "cmd",
Description: "edit and view existing slash commands",
Type: corde.COMMAND_CHAT_INPUT,
Options: []corde.Option{
{
Name: "list",
Type: corde.OPTION_SUB_COMMAND,
Description: "list existing slash commands",
},
},
}
var command = corde.NewSlashCommand(
"cmd",
"edit and view existing slash commands",
corde.NewSubcommand("list", "list existing slash commands"),
)

func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
Expand Down Expand Up @@ -53,6 +46,7 @@ func main() {
log.Fatalln("error registering command: ", err)
}

log.Println("serving on :8070")
if err := m.ListenAndServe(":8070"); err != nil {
log.Fatalln(err)
}
Expand All @@ -75,7 +69,7 @@ var delBtn = corde.Component{
}

func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *corde.Interaction) {
return func(w corde.ResponseWriter, i *corde.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
w.Respond(corde.NewResp().
ActionRow(nextBtn).
Ephemeral().
Expand All @@ -86,10 +80,14 @@ func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(corde.ResponseWriter, *
}

func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.Interaction) {
return func(w corde.ResponseWriter, i *corde.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
mu.Lock()
defer mu.Unlock()
commands, _ := m.GetCommands(g)
commands, err := m.GetCommands(g)
if err != nil {
w.Update(corde.NewResp().Contentf("Error getting commands: %s", err.Error()).Ephemeral().B())
return
}
if len(commands) == 0 {
w.Update(corde.NewResp().Content("No commands found.").Ephemeral().B())
return
Expand All @@ -107,10 +105,14 @@ func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedI
}

func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(corde.ResponseWriter, *corde.Interaction) {
return func(w corde.ResponseWriter, i *corde.Interaction) {
return func(w corde.ResponseWriter, _ *corde.Interaction) {
mu.Lock()
defer mu.Unlock()
commands, _ := m.GetCommands(g)
commands, err := m.GetCommands(g)
if err != nil {
w.Update(corde.NewResp().Contentf("Error getting commands: %s", err.Error()).Ephemeral().B())
return
}
c := commands[*selectedID]

m.DeleteCommand(c.ID, g)
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions 0_example/todo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"log"
"os"
"sync"

"github.com/Karitham/corde"
)

var commands = corde.NewSlashCommand("todo", "view edit and remove todos",
corde.NewSubcommand("list", "list todos"),
corde.NewSubcommand("add", "add a todo",
corde.NewStringOption("name", "todo name", true),
corde.NewStringOption("value", "todo value", true),
corde.NewUserOption("user", "assign it to a user", false),
),
corde.NewSubcommand("rm", "remove a todo",
corde.NewStringOption("name", "todo name", true),
),
)

func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
if token == "" {
log.Fatalln("DISCORD_BOT_TOKEN not set")
}
appID := corde.SnowflakeFromString(os.Getenv("DISCORD_APP_ID"))
if appID == 0 {
log.Fatalln("DISCORD_APP_ID not set")
}
pk := os.Getenv("DISCORD_PUBLIC_KEY")
if pk == "" {
log.Fatalln("DISCORD_PUBLIC_KEY not set")
}

t := todo{
mu: sync.Mutex{},
list: make(map[string]todoItem),
}

m := corde.NewMux(pk, appID, token)
m.Route("todo", func(m *corde.Mux) {
m.Command("add", t.addHandler)
m.Command("rm", t.removeHandler)
m.Command("list", t.listHandler)
})

g := corde.GuildOpt(corde.SnowflakeFromString(os.Getenv("DISCORD_GUILD_ID")))
if err := m.RegisterCommand(commands, g); err != nil {
log.Fatalln(err)
}

log.Println("serving on :8070")
if err := m.ListenAndServe(":8070"); err != nil {
log.Fatalln(err)
}
}
28 changes: 20 additions & 8 deletions _example/todo/todo.go → 0_example/todo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,40 @@ import (
"sync"

"github.com/Karitham/corde"
"github.com/Karitham/corde/format"
)

type todo struct {
mu sync.Mutex
list map[string]string
list map[string]todoItem
}

type todoItem struct {
user corde.Snowflake
value string
}

func (t *todo) addHandler(w corde.ResponseWriter, i *corde.Interaction) {
value := i.Data.Options.String("value")
name := i.Data.Options.String("name")

user := i.Data.Options.Snowflake("user")
if user == 0 {
user = i.User.ID
}

t.mu.Lock()
defer t.mu.Unlock()
t.list[name] = value

t.list[name] = todoItem{
user: user,
value: value,
}

w.Respond(corde.NewResp().Contentf("Sucessfully added %s", name).Ephemeral().B())
}

func (t *todo) listHandler(w corde.ResponseWriter, i *corde.Interaction) {
func (t *todo) listHandler(w corde.ResponseWriter, _ *corde.Interaction) {
t.mu.Lock()
defer t.mu.Unlock()

Expand All @@ -38,14 +53,11 @@ func (t *todo) listHandler(w corde.ResponseWriter, i *corde.Interaction) {
Title("Todo list").
// build todo list description
Description(func() string {
s := &strings.Builder{}
s.WriteString("```todo\n")
i := 0
s, i := &strings.Builder{}, 1
for k, v := range t.list {
s.WriteString(fmt.Sprintf("%d. %s: %s - %s\n", i, k, v.value, format.User(v.user.String())))
i++
s.WriteString(fmt.Sprintf("%d. %s: %s\n", i, k, v))
}
s.WriteString("```")
return s.String()
}()).
Color(0x69b00b).
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ Be aware that breaking changes will happen as of now, at least until v1.
go get github.com/Karitham/corde
```

### Usage

The easiest way to run the examples, or even to run your own is to use [ngrok](https://ngrok.com/) no login or domain required, and the product itself is great.

## Examples

The most basic one is [**bongo**](_example/bongo/main.go)
The most basic one is [**bongo**](0_example/bongo/main.go)

Then comes [**todo**](_example/todo/) that shows off subcommands routing and options
Then comes [**todo**](0_example/todo/) that shows off subcommands routing and options

And then we have [**moderate-myself**](_example/moderate-myself/main.go) which is able to show and delete currently available commands.
And then we have [**moderate-myself**](0_example/moderate-myself/main.go) which is able to show and delete currently available commands.
It demonstrates the usage of components such as buttons.

Corde is also actively used to rewrite and develop another discord bot of mine called [**WaifuBot**](https://github.com/Karitham/WaifuBot/) (for now see the corde branch)
Expand Down
92 changes: 0 additions & 92 deletions _example/todo/main.go

This file was deleted.

10 changes: 5 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (m *Mux) GetCommands(options ...func(*CommandsOpt)) ([]Command, error) {
return commands, nil
}

// RegisterCommand registers a new Command to the Mux
func (m *Mux) RegisterCommand(c Command, options ...func(*CommandsOpt)) error {
// RegisterCommand registers a new Command on discord
func (m *Mux) RegisterCommand(c CreateCommander, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
option(opt)
Expand All @@ -112,8 +112,8 @@ func (m *Mux) RegisterCommand(c Command, options ...func(*CommandsOpt)) error {
return rest.CodeBetween(resp, 200, 299)
}

// BulkRegisterCommand registers a slice of Command to the Mux
func (m *Mux) BulkRegisterCommand(c []Command, options ...func(*CommandsOpt)) error {
// BulkRegisterCommand registers a slice of Command on discord
func (m *Mux) BulkRegisterCommand(c []CreateCommander, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
option(opt)
Expand All @@ -132,7 +132,7 @@ func (m *Mux) BulkRegisterCommand(c []Command, options ...func(*CommandsOpt)) er
return rest.CodeBetween(resp, 200, 299)
}

// DeleteCommand deletes a Command from the Mux
// DeleteCommand deletes a Command from discord
func (m *Mux) DeleteCommand(ID Snowflake, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
Expand Down
Loading

0 comments on commit b03180c

Please sign in to comment.