-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommands.go
100 lines (83 loc) · 2.23 KB
/
commands.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
97
98
99
100
package corde
import (
"github.com/Karitham/corde/internal/rest"
)
// CommandsOpt is an option for a Command
type CommandsOpt struct {
guildID Snowflake
}
// GuildOpt is an option for setting the guild of a Command
func GuildOpt(guildID Snowflake) func(*CommandsOpt) {
return func(opt *CommandsOpt) {
opt.guildID = guildID
}
}
// GetCommands returns a slice of Command from the Mux
func (m *Mux) GetCommands(options ...func(*CommandsOpt)) ([]Command, error) {
opt := &CommandsOpt{}
for _, option := range options {
option(opt)
}
r := rest.Req("applications", m.AppID)
if opt.guildID != 0 {
r.Append("guilds", opt.guildID)
}
r.Append("commands")
var commands []Command
_, err := rest.DoJSON(m.Client, r.Get(m.authorize, rest.JSON), &commands)
if err != nil {
return nil, err
}
return commands, nil
}
// 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)
}
r := rest.Req("applications", m.AppID).JSONBody(c)
if opt.guildID != 0 {
r.Append("guilds", opt.guildID)
}
r.Append("commands")
resp, err := m.Client.Do(r.Post(m.authorize, rest.JSON))
if err != nil {
return err
}
return rest.CodeBetween(resp, 200, 299)
}
// 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)
}
r := rest.Req("applications", m.AppID).JSONBody(c)
if opt.guildID != 0 {
r.Append("guilds", opt.guildID)
}
r.Append("commands")
resp, err := m.Client.Do(r.Put(m.authorize, rest.JSON))
if err != nil {
return err
}
return rest.CodeBetween(resp, 200, 299)
}
// DeleteCommand deletes a Command from discord
func (m *Mux) DeleteCommand(ID Snowflake, options ...func(*CommandsOpt)) error {
opt := &CommandsOpt{}
for _, option := range options {
option(opt)
}
r := rest.Req("applications", m.AppID)
if opt.guildID != 0 {
r.Append("guilds", opt.guildID)
}
r.Append("commands", ID)
resp, err := m.Client.Do(r.Delete(m.authorize, rest.JSON))
if err != nil {
return err
}
return rest.ExpectCode(resp, 204)
}