-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
268 lines (252 loc) · 8.26 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//Various commands
package main
import (
"fmt"
"math/rand"
"regexp"
"strings"
"github.com/MYKatz/gojam"
"github.com/bwmarrin/discordgo"
)
func markdownWrapper(lang string, message string) string {
//hack for working with backticks in go strings
backticks := "`" + "`" + "`"
return backticks + lang + "\n" + message + backticks
}
func isAdmin(discord *discordgo.Session, userID string, channelID string) bool {
//return false if err, err on the side of caution.
c, err := discord.Channel(channelID)
if err != nil {
return false
}
g, err := discord.Guild(c.GuildID)
if err != nil {
return false
}
//Although owners may not *technically* be admins, they do have similar priviledges
//Not technically correct, but I think this should stay
if g.OwnerID == userID {
return true
}
member, err := discord.GuildMember(g.ID, userID)
if err != nil {
return false
}
roles := member.Roles
for i := 0; i < len(roles); i++ {
role, _ := discord.State.Role(g.ID, roles[i])
if (role.Permissions & discordgo.PermissionAdministrator) == discordgo.PermissionAdministrator {
return true
}
}
return false
}
func serverID(discord *discordgo.Session, message *discordgo.MessageCreate) string {
c, _ := discord.Channel(message.ChannelID)
return c.GuildID
}
func generateMessage(guildid string) (string, error) {
markov, err := keystore.Get(guildid + ":markov")
if err != nil || markov == "" {
return "", fmt.Errorf("Markov Error")
} else {
m := gojam.NewMarkov(1, " ")
m.FromJSON([]byte(markov))
return m.GenerateExample(), nil
}
}
func setup(discord *discordgo.Session, command []string) (*gojam.Markov, []string, error) {
//get message history of each channel
messagesPerChannel := 100 //max # of messages per channel. arbitrary, to be turned into a env variable later
channels := command[2:]
processedAMsg := false
mark := gojam.NewMarkov(1, " ")
cleanedChannels := make([]string, len(channels)-1)
for i := 0; i < len(channels)-1; i++ { //the -1 is cause we append an empty string earlier
channelID := cleanChannelId(channels[i])
cleanedChannels = append(cleanedChannels, channelID)
fmt.Println(channelID)
messages, err := discord.ChannelMessages(channelID, messagesPerChannel, "", "", "")
if err != nil {
continue
}
for j := 0; j < len(messages); j++ {
content := messages[j].Content
if len(content) < 1 {
continue
}
first := string(content[0])
author := messages[j].Author.ID
isProbablyBotCommand, _ := regexp.MatchString("[!$%^&*,.?:{}|<>`]", first)
if messages[j].Author.ID != botID && !isProbablyBotCommand {
processedAMsg = true
fmt.Printf("%s: %s, \n", author, content)
mark.TrainOnExample(content)
}
}
}
if !processedAMsg {
return mark, cleanedChannels, fmt.Errorf("No messages found")
}
return mark, cleanedChannels, nil
}
func cleanChannelId(channelID string) string {
//converts <#xyz> id to 'xyz'
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
fmt.Printf("Error: %s", err)
}
return reg.ReplaceAllString(channelID, "")
}
func modeHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
mode, err := keystore.Get(serverID(discord, message) + ":mode")
gid := serverID(discord, message)
msg, err := generateMessage(gid)
fmt.Println(mode)
if err == nil {
if mode == "normal" {
r := rand.Intn(20)
if r == 0 {
discord.ChannelMessageSend(message.ChannelID, msg)
}
}
if mode == "chatty" {
r := rand.Intn(5)
fmt.Println(r)
if r == 0 {
discord.ChannelMessageSend(message.ChannelID, msg)
}
}
}
}
func idInChannels(id string, channels []string) bool {
for _, b := range channels {
if b == id {
return true
}
}
return false
}
func messageHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
sID := serverID(discord, message)
chans, err := keystore.Get(sID + ":channels")
if err != nil || len(chans) == 0 {
fmt.Println("error w/ accessing channels from keyval store")
return
}
messageChannel := message.ChannelID
channels := strings.Split(chans, ",")
processMessage := idInChannels(messageChannel, channels)
if processMessage {
markov, err := keystore.Get(sID + ":markov")
if err != nil {
return
}
m := gojam.NewMarkov(1, " ")
m.FromJSON([]byte(markov))
m.TrainOnExample(message.Content)
keystore.Set(sID+":markov", string(m.ToJSON()))
}
}
func commandChooser(discord *discordgo.Session, message *discordgo.MessageCreate) {
command := strings.Fields(strings.ToLower(message.Content)) //note that channel names (hashtags) get converted to ID numbers, so this doesn't affect them
command = append(command, "")
fmt.Printf("Received command: %v \n", command)
if strings.ToLower(command[0]) != (commandPrefix + strings.ToLower(botName)) {
return
}
switch strings.ToLower(command[1]) {
case "setup":
admin := isAdmin(discord, message.Author.ID, message.ChannelID)
if admin {
discord.ChannelMessageSend(message.ChannelID, "Sure thing, gimme a sec")
var err error
m, chans, err := setup(discord, command)
if err != nil {
discord.ChannelMessageSend(message.ChannelID, "Error: no messages found")
} else {
sID := serverID(discord, message)
channels := strings.Join(chans, ",")
keystore.Set(sID+":markov", string(m.ToJSON()))
keystore.Set(sID+":channels", channels)
_, err := keystore.Get(sID + ":mode")
if err != nil {
keystore.Set(sID+":mode", "normal")
}
discord.ChannelMessageSend(message.ChannelID, ":bird: All set up :bird:")
}
} else {
discord.ChannelMessageSend(message.ChannelID, "You must have an administrator role to use this command.")
}
case "setmode":
admin := isAdmin(discord, message.Author.ID, message.ChannelID)
if admin {
switch strings.ToLower(command[2]) {
case "silent":
keystore.Set(serverID(discord, message)+":mode", "silent")
discord.ChannelMessageSend(message.ChannelID, "Mode set")
case "normal":
keystore.Set(serverID(discord, message)+":mode", "normal")
discord.ChannelMessageSend(message.ChannelID, "Mode set")
case "chatty":
keystore.Set(serverID(discord, message)+":mode", "chatty")
discord.ChannelMessageSend(message.ChannelID, "Mode set")
default:
discord.ChannelMessageSend(message.ChannelID, "Invalid option")
}
} else {
discord.ChannelMessageSend(message.ChannelID, "You must have an administrator role to use this command.")
}
case "usepreset":
admin := isAdmin(discord, message.Author.ID, message.ChannelID)
if admin {
m, err := usePreset(command[2])
if err != nil {
discord.ChannelMessageSend(message.ChannelID, "Invalid option")
} else {
keystore.Set(serverID(discord, message)+":markov", string(m.ToJSON()))
keystore.Set(serverID(discord, message)+":mode", "normal")
discord.ChannelMessageSend(message.ChannelID, ":bird: All set up :bird:")
}
} else {
discord.ChannelMessageSend(message.ChannelID, "You must have an administrator role to use this command.")
}
case "dance":
discord.ChannelMessageSend(message.ChannelID, ":dancer: Dancing! :dancer:") //w emojis
case "say":
gid := serverID(discord, message)
msg, err := generateMessage(gid)
if err != nil {
discord.ChannelMessageSend(message.ChannelID, "I haven't been set up yet!")
} else {
discord.ChannelMessageSend(message.ChannelID, msg)
}
case "meme":
gid := serverID(discord, message)
msg, err := generateMessage(gid)
if err != nil {
discord.ChannelMessageSend(message.ChannelID, "I haven't been set up yet!")
} else {
fmt.Println(msg)
embed := &discordgo.MessageEmbed{
Title: msg,
Image: &discordgo.MessageEmbedImage{
URL: getMeme(string(msg)),
},
}
discord.ChannelMessageSendEmbed(message.ChannelID, embed)
}
default:
msg := `= Welcome To Polly! =
[ Commands ]
= Admin Commands =
- setup #channel1 #channel2 #channel3... :: takes a space-separated list of channels to learn from.
- setmode silent/normal/chatty :: silent prevents the bot from speaking unless specifically invoked. normal/chatty allow the bot to speak randomly in the chat, but degree varies bassed on mode.
- usepreset kanye/beemovie/discord :: alternative to setup, use one of the presets to train Polly
= User Commands =
- say :: get Polly to say something!
- meme :: get Polly to make a meme`
discord.ChannelMessageSend(message.ChannelID, markdownWrapper("asciidoc", msg))
}
return
}