-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
82 lines (75 loc) · 1.62 KB
/
handler.go
File metadata and controls
82 lines (75 loc) · 1.62 KB
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
package main
import (
"bytes"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/mattn/go-shellwords"
)
func handler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
args, err := shellwords.Parse(m.Content)
if logOnError(err) {
return
}
command, ok := dish.Commands[args[0]]
if !ok {
return
}
if !checkIdentity(command, m.Author, discordgo.Roles{}) {
return
}
if command.Args {
args = append(command.Command, args[1:]...)
} else {
args = command.Command
}
result, err := command.Run(args)
if logOnError(err) {
return
}
lines := bytes.Split(result, []byte{'\n'})
buf := &bytes.Buffer{}
msg, err := s.ChannelMessage(m.ChannelID, m.ID)
if logOnError(err) {
return
}
for i, line := range lines {
if (buf.Len()+len(line)+13 > 2000) || (i == len(lines)-1) {
text := fmt.Sprintf("```ansi\n%s```", buf.String())
msg, err = s.ChannelMessageSendReply(m.ChannelID, text, msg.Reference())
if err != nil {
fmt.Println(err)
return
}
buf.Truncate(0)
}
buf.Write(line)
buf.WriteByte('\n')
}
}
func checkIdentity(command Command, user *discordgo.User, roles discordgo.Roles) bool {
ids := make([]string, len(roles)+1, len(roles)+1)
for i, role := range roles {
ids[i] = role.ID
}
ids[len(ids)-1] = user.ID
fmt.Println(command.WhiteList)
fmt.Println(command.BlackList)
for _, check := range command.WhiteList {
for _, id := range ids {
if check == id {
return true
}
}
}
for _, check := range command.BlackList {
for _, id := range ids {
if check == id {
return false
}
}
}
return len(command.WhiteList) == 0
}