-
Notifications
You must be signed in to change notification settings - Fork 0
/
discom.go
132 lines (107 loc) · 3.12 KB
/
discom.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
package discom
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
//CommandHandler A callback function which is triggered when a command is ran
type CommandHandler func(*discordgo.Session, *discordgo.MessageCreate)
//Command Represents a Command to the discord bot.
type Command struct {
//Re the regex pattern when a message matches it will call the Handler func
Re *regexp.Regexp
//CaseInsensitive will to lower the incomming message before checking if it matches
CaseInsensitive bool
//Handler The handler function which is called on a message matching the regex
Handler CommandHandler
Description string
Example string
}
//CommandSet Use this to regsiter commands and get the handler to pass to discordgo.
// This should be created with CreateCommandSet.
type CommandSet struct {
PrefixRe *regexp.Regexp
commands []Command
}
var (
helpRe *regexp.Regexp
)
func init() {
helpRe = regexp.MustCompile("help")
}
func (c *Command) complete() bool {
return c.Re != nil && c.Handler != nil && c.Re.String() != ""
}
//CreateCommandSet Creates a command set
func CreateCommandSet(prefixRe *regexp.Regexp) *CommandSet {
return &CommandSet{prefixRe, make([]Command, 0)}
}
func cleanPattern(pattern string) string {
return strings.ReplaceAll(pattern, "\\", "")
}
//AddCommand Use this to add a command to a command set
func (cs *CommandSet) AddCommand(com Command) error {
if !com.complete() {
return errors.New("Must set all fields in command struct")
}
cs.commands = append(cs.commands, com)
return nil
}
//Handler Regsiter this with discordgo.AddHandler will be called every time a new message is sent on a guild.
func (cs *CommandSet) Handler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
msg := m.Content
if !cs.PrefixRe.Match([]byte(msg)) {
return
}
for _, com := range cs.commands {
tmpMsg := msg
if com.CaseInsensitive {
tmpMsg = strings.ToLower(msg)
}
if com.Re.Match([]byte(tmpMsg)) {
com.Handler(s, m)
return
}
}
var res string
if helpRe.Match([]byte(msg)) {
res = cs.getHelpMessage()
} else {
res = fmt.Sprintf("unknown command try \"%s help\"", cs.PrefixRe.String())
}
s.ChannelMessageSend(
m.ChannelID,
cs.replyMessage(m, res),
)
}
func (cs *CommandSet) getHelpMessage() string {
var result strings.Builder
fmt.Fprintf(&result, "here are all the commands I know\n")
for _, com := range cs.commands {
var desc string
if com.Description != "" {
desc = com.Description
} else {
desc = "missing description"
}
var example string
if com.Example != "" {
example = com.Example
} else {
example = cleanPattern(com.Re.String())
}
fmt.Fprintf(
&result, "\"%s %s\" Case Insensitive? %t, %s\n\n",
cleanPattern(cs.PrefixRe.String()), example, com.CaseInsensitive, desc,
)
}
return result.String()
}
func (cs *CommandSet) replyMessage(m *discordgo.MessageCreate, response string) string {
return fmt.Sprintf("<@%s> %s", m.Author.ID, response)
}