-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (175 loc) · 4.24 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
dg "github.com/bwmarrin/discordgo"
)
type config struct {
BotID string
}
type memory struct {
ChristianChannels []string
}
type swearWord struct {
word string
language string
}
var newMessageInstructions = map[string]func(*dg.Session, *dg.MessageCreate){
"$headsortails": headsTails,
"$christian": christian,
"$doot": doot,
"$spook": spookyScarySkeletons,
}
func spookyScarySkeletons(s *dg.Session, m *dg.MessageCreate) {
// Sends in the original "Spooky Scary Skeletons" video
s.ChannelMessageSend(m.ChannelID, "https://www.youtube.com/watch?v=XTgFtxHhCQ0")
}
func doot(s *dg.Session, m *dg.MessageCreate) {
f, err := os.Open("doot.jpg")
if err != nil {
fmt.Println("Error reading file")
return
}
defer f.Close()
ms := &dg.MessageSend{
Embed: &dg.MessageEmbed{
Image: &dg.MessageEmbedImage{
URL: "attachment://" + "doot.jpg",
},
},
Files: []*dg.File{
&dg.File{
Name: "doot.jpg",
Reader: f,
},
},
}
s.ChannelMessageSendComplex(m.ChannelID, ms)
}
func christian(s *dg.Session, m *dg.MessageCreate) {
for i, v := range mem.ChristianChannels {
if v == m.ChannelID {
mem.ChristianChannels = append(mem.ChristianChannels[:i], mem.ChristianChannels[i+1:]...)
s.ChannelMessageSend(m.ChannelID, "This is no longer a christian channel")
return
}
}
mem.ChristianChannels = append(mem.ChristianChannels, m.ChannelID)
s.ChannelMessageSend(m.ChannelID, "This is now a christian channel")
}
func headsTails(s *dg.Session, m *dg.MessageCreate) {
num := rand.Intn(2)
if num == 0 {
s.ChannelMessageSend(m.ChannelID, "Heads")
} else {
s.ChannelMessageSend(m.ChannelID, "Tails")
}
}
func help(s *dg.Session, m *dg.MessageCreate) {
out := "The following instructions are available:\n$help\n"
for k := range newMessageInstructions {
out += k + "\n"
}
s.ChannelMessageSend(m.ChannelID, out)
}
func swear(s *dg.Session, m *dg.MessageCreate) {
if _, ok := swears[m.Author.ID+m.ChannelID]; !ok {
swears[m.Author.ID+m.ChannelID] = 0
}
index := swears[m.Author.ID+m.ChannelID]
if index >= len(anger) {
index = len(anger) - 1
}
phrase := anger[index]
oops := false
if strings.Contains(phrase, "%s") {
phrase = fmt.Sprintf(phrase, m.Author.Username)
}
if strings.Contains(phrase, "<oops>") {
phrase = strings.Replace(phrase, "<oops>", "", -1)
oops = true
}
s.ChannelMessageSend(m.ChannelID, phrase)
if oops {
s.ChannelMessageSend(m.ChannelID, "oops")
}
swears[m.Author.ID+m.ChannelID]++
}
var cfg config
var mem memory
var swears map[string]int
var swearWords []map[string]string
var anger []string
func main() {
runtime.GOMAXPROCS(2)
readConfig(&cfg, "config.json")
readConfig(&mem, "memory.json")
readConfig(&swears, "swears.json")
readConfig(&swearWords, "DirtyWords.json")
readConfig(&anger, "anger.json")
if swears == nil {
swears = make(map[string]int)
}
session, err := dg.New(cfg.BotID)
if err != nil {
fmt.Println(err)
return
}
defer session.Close()
if err = session.Open(); err != nil {
fmt.Println(err)
return
}
session.AddHandler(newMessage)
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV, syscall.SIGHUP)
fmt.Println("Setup Complete")
<-sc
writeConfig(&mem, "memory.json")
writeConfig(&swears, "swears.json")
}
func readConfig(cfg interface{}, fileName string) {
raw, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Println(err)
}
json.Unmarshal(raw, cfg)
}
func writeConfig(cfg interface{}, fileName string) {
b, err := json.Marshal(cfg)
if err != nil {
fmt.Println(err)
}
ioutil.WriteFile(fileName, b, os.FileMode(0))
}
func newMessage(s *dg.Session, m *dg.MessageCreate) {
if !m.Author.Bot {
if len(m.Content) > 0 && m.Content[0] == '$' {
keyWord := strings.Split(m.Content, " ")[0]
if val, ok := newMessageInstructions[keyWord]; ok {
val(s, m)
}
if keyWord == "$help" {
help(s, m)
}
}
for _, swearWord := range swearWords {
if strings.Contains(strings.ToLower(m.Content), swearWord["word"]) {
for _, channelid := range mem.ChristianChannels {
if channelid == m.ChannelID {
swear(s, m)
break
}
}
break
}
}
}
}