-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
208 lines (173 loc) · 3.91 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
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"errors"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
var (
s *discordgo.Session
token = os.Getenv("TOKEN_YANG")
banYour = []string{
"ur",
"your",
"you're",
"u're",
"u'r",
"yo",
"thou",
}
banMom = []string{
"mom",
"moom",
"mooom",
"moooom",
"mooooom",
"mother",
"moother",
"mooother",
"moooother",
"mooooother",
"mommy",
"moommy",
"mooommy",
"moooommy",
"mooooommy",
"mama",
"mamma",
"madre",
"momma",
"mawmaw",
"maw maw",
}
bads []bad
)
// A person who is being monitored as because they have been
// determined to potentially say "ur mom" in two messages in a row
type bad struct {
guildID string // Guild the message came from
userID string
}
func init() {
// Print boot "splash"
fmt.Println("YangBot 1.0.0")
log.Println("[Info] Minimum permissions are 1099780129792")
}
func main() {
// Declare error here so it can be set without :=
var err error
// Create bot client session
log.Println("Logging in")
s, err = discordgo.New("Bot " + token)
if err != nil {
log.Fatalf("Could not create session: %v", err)
}
// Pass on message event to functions
s.AddHandler(single)
s.AddHandler(multiYour)
s.AddHandler(multiMom)
// We only care about message + guild member intents
s.Identify.Intents = discordgo.MakeIntent(discordgo.IntentMessageContent | discordgo.IntentGuildMessages | discordgo.IntentGuildMembers)
// Open websocket connection to Discord and listen
err = s.Open()
if err != nil {
log.Fatalf("Error opening websocket connection: %v", err)
}
// Close Discord session cleanly
defer s.Close()
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-stop
log.Println("Ciao")
}
// Single message check
func single(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bot messages
if m.Author.ID == s.State.User.ID {
return
}
// Iterate throughout every permutation of "your" and "mom" specified previously
for _, your := range banYour {
for _, mom := range banMom {
// If message contains any specific permutation, timeout user
if strings.Contains(strings.ToLower(m.Content), fmt.Sprintf("%s %s", your, mom)) {
err := timeout(m.GuildID, m.Author.ID)
if err != nil {
return
}
}
}
}
}
// Multi-message checking
// PART ONE - YOUR
func multiYour(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bot messages
if m.Author.ID == s.State.User.ID {
return
}
// Check if already in list and ignore if yes
if isBad(m.GuildID, m.Author.ID) {
return
}
// If message matches first part, add to watchlist o.o
for _, your := range banYour {
if strings.ToLower(m.Content) == your {
fresh := bad{m.GuildID, m.Author.ID}
bads = append(bads, fresh)
}
}
}
func multiMom(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bot messages
if m.Author.ID == s.State.User.ID {
return
}
if isBad(m.GuildID, m.Author.ID) {
for _, mom := range banMom {
if strings.ToLower(m.Content) == mom {
timeout(m.GuildID, m.Author.ID)
}
}
err := removeBad(m.GuildID, m.Author.ID)
if err != nil {
return
}
}
}
// Check if given member is in slice
func isBad(g string, u string) (res bool) {
for _, bad := range bads {
if bad.guildID == g && bad.userID == u {
return true
}
}
return false
}
// Remove a bad user from slice (no longer tracked)
func removeBad(g string, u string) (err error) {
for i, bad := range bads {
if bad.guildID == g && bad.userID == u {
bads[i] = bads[len(bads) - 1]
bads = bads[:len(bads) - 1]
} else {
return errors.New("bad user not found in list")
}
}
return nil
}
// Serve punishment
func timeout(g string, u string) (err error) {
// Get time a minute in future
until := time.Now().Add(time.Minute)
err = s.GuildMemberTimeout(g, u, &until)
if err != nil {
return err
}
return nil
}