-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
188 lines (147 loc) · 3.71 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package discord
// TODO: change docs to slice for ordering
import (
"encoding/csv"
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"strings"
)
type CommandFunc func(s *discordgo.Session, m *discordgo.Message, args []string) error
type Command struct {
Name string
Usage string
Permission *CommandPermission
Prefix string
Func CommandFunc
}
func NewCommand(name, usage string, f CommandFunc) *Command {
return &Command{
Name: name,
Usage: usage,
Func: f,
}
}
func (c *Command) SetPrefix(prefix string) *Command {
c.Prefix = prefix
return c
}
func (c *Command) SetFunc(f CommandFunc) *Command {
c.Func = f
return c
}
func (c *Command) SetPerms(perms int64) *Command {
if c.Permission == nil {
c.Permission = new(CommandPermission)
}
c.Permission.Perms = perms
return c
}
func (c *Command) SetPermRoleID(id string) *Command {
if c.Permission == nil {
c.Permission = new(CommandPermission)
}
c.Permission.Role.ID = id
return c
}
func (c *Command) SetPermRoleName(name string) *Command {
if c.Permission == nil {
c.Permission = new(CommandPermission)
}
c.Permission.Role.Name = name
return c
}
type CommandPermission struct {
Perms int64
Role Role
}
func (c CommandPermission) Authorized(s *discordgo.Session, member *discordgo.Member) (bool, error) {
for _, roleID := range member.Roles {
if roleID == c.Role.ID {
return true, nil
}
role, err := s.State.Role(member.GuildID, roleID)
if err != nil {
return false, err
}
if role.Name == c.Role.Name {
return true, nil
}
if role.Permissions&c.Perms != 0 {
return true, nil
}
}
return false, nil
}
type CommandListener struct {
ErrorHandler func(*discordgo.Session, *discordgo.Message, error)
commands []*Command
}
func NewCommandListner(s *discordgo.Session, prefix string) *CommandListener {
cl := &CommandListener{
ErrorHandler: defaultCommandErrorHandler,
}
cl.Add(NewCommand("commands", "list commands", cl.Commands))
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, prefix) {
r := csv.NewReader(strings.NewReader(m.Content))
r.Comma = ' '
data, err := r.Read()
if err != nil {
log.Println(err)
return
}
commandName := strings.TrimPrefix(data[0], prefix)
var args []string
if len(data) > 1 {
args = data[1:]
}
cmd := cl.Find(commandName)
if cmd == nil {
return
}
if cmd.Permission != nil {
// set member guild id because it is not set by this event.
m.Member.GuildID = m.GuildID
ok, err := cmd.Permission.Authorized(s, m.Member)
if err != nil {
cl.ErrorHandler(s, m.Message, err)
return
}
if !ok {
cl.ErrorHandler(s, m.Message, errors.New("You do not have sufficient permissions to use this command."))
return
}
}
if err := cmd.Func(s, m.Message, args); err != nil {
if _, ok := err.(*relayError); ok {
if err := Notify(s, m.ChannelID, Error, "", err.Error()); err != nil {
cl.ErrorHandler(s, m.Message, err)
}
return
}
infoLog.Printf("command error: sender[%s] | name[%s] | args[%s] | error[%s]\n", m.Author.Username, commandName, args, err)
}
}
})
return cl
}
func (cl *CommandListener) Add(cmd *Command) {
cl.commands = append(cl.commands, cmd)
}
func (cl *CommandListener) Commands(s *discordgo.Session, m *discordgo.Message, args []string) error {
var sb strings.Builder
for _, cmd := range cl.commands {
sb.WriteString(fmt.Sprintf("```::%s -> %s\n```", cmd.Name, cmd.Usage))
}
return Notify(s, m.ChannelID, Info, "Commands", sb.String())
}
func (cl *CommandListener) Find(name string) *Command {
for _, c := range cl.commands {
if c.Name == name {
return c
}
}
return nil
}