-
Notifications
You must be signed in to change notification settings - Fork 31
/
command.go
96 lines (74 loc) · 2.32 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package stream_chat // nolint: golint
import (
"errors"
"net/http"
"net/url"
"path"
)
// Command represents a custom command.
type Command struct {
Name string `json:"name"`
Description string `json:"description"`
Args string `json:"args"`
Set string `json:"set"`
}
// commandResponse represents an API response containing one Command.
type commandResponse struct {
Command *Command
}
// commandsResponse represents an API response containing a list of Command.
type commandsResponse struct {
Commands []*Command
}
// CreateCommand registers a new custom command.
func (c *Client) CreateCommand(cmd *Command) (*Command, error) {
if cmd == nil {
return nil, errors.New("command is nil")
}
var resp commandResponse
err := c.makeRequest(http.MethodPost, "commands", nil, cmd, &resp)
if err != nil {
return nil, err
}
if resp.Command == nil {
return nil, errors.New("unexpected error: command response is nil")
}
return resp.Command, nil
}
// GetCommand retrieves a custom command referenced by cmdName.
func (c *Client) GetCommand(cmdName string) (*Command, error) {
if cmdName == "" {
return nil, errors.New("command name is empty")
}
p := path.Join("commands", url.PathEscape(cmdName))
cmd := Command{}
err := c.makeRequest(http.MethodGet, p, nil, nil, &cmd)
return &cmd, err
}
// DeleteCommand deletes a custom command referenced by cmdName.
func (c *Client) DeleteCommand(cmdName string) error {
if cmdName == "" {
return errors.New("command name is empty")
}
p := path.Join("commands", url.PathEscape(cmdName))
return c.makeRequest(http.MethodDelete, p, nil, nil, nil)
}
// ListCommands returns a list of custom commands.
func (c *Client) ListCommands() ([]*Command, error) {
var resp commandsResponse
err := c.makeRequest(http.MethodGet, "commands", nil, nil, &resp)
return resp.Commands, err
}
// UpdateCommand updates a custom command referenced by cmdName.
func (c *Client) UpdateCommand(cmdName string, options map[string]interface{}) (*Command, error) {
switch {
case cmdName == "":
return nil, errors.New("command name is empty")
case len(options) == 0:
return nil, errors.New("options are empty")
}
p := path.Join("commands", url.PathEscape(cmdName))
var resp commandResponse
err := c.makeRequest(http.MethodPut, p, nil, options, &resp)
return resp.Command, err
}