-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (148 loc) · 3.99 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)
var (
Token string
DenylistPath string
lock sync.Mutex
denylist []*regexp.Regexp
)
func main() {
flag.StringVar(&Token, "token", "", "Discord bot token")
flag.StringVar(&DenylistPath, "denylist", "", "Filepath to denylist of regular expressions, separated by new line delimiters")
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
log.WithError(err).Fatal("Error creating Discord session")
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
// Monitor denylist changes
go monitorDenylistFile(ctx, DenylistPath)
// Open a websocket connection to Discord and begin listening.
if err = dg.Open(); err != nil {
log.WithError(err).Fatal("Error opening websocket")
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
<-sc
if err := dg.Close(); err != nil {
log.Fatal(err)
}
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
for _, re := range denylist {
if re.MatchString(m.Content) {
// Allow longer messages
if len(m.Content) > 60 {
continue
}
// Only filter "gm" if outside the gm channel.
if strings.Contains(re.String(), "gm") && m.ChannelID == "859518192282763325" {
continue
}
addrRegex := regexp.MustCompile("^0x[a-fA-F0-9]{40}$")
isFaucetChannel := m.ChannelID == "765654444825641001" || m.ChannelID == "961349868612386856"
if addrRegex.MatchString(m.Content) {
if !isFaucetChannel {
continue
}
}
if err := deleteMessage(s, m, re); err != nil {
log.WithError(err).Error("Could not delete message")
}
}
}
}
func deleteMessage(s *discordgo.Session, m *discordgo.MessageCreate, re *regexp.Regexp) error {
if err := s.ChannelMessageDelete(m.ChannelID, m.ID); err != nil {
return err
}
ts, err := discordgo.SnowflakeTimestamp(m.Author.ID)
if err != nil {
return err
}
age := time.Since(ts)
log.WithFields(log.Fields{
"username": m.Author.Username,
"id": m.Author.ID,
"content": m.Content,
"accountAge": age,
"regexp": re.String(),
}).Info("Message deleted")
return nil
}
func monitorDenylistFile(ctx context.Context, fp string) {
updateDenyList(fp)
log.WithField("filepath", fp).Info("Monitoring denylist for file changes")
w, err := fsnotify.NewWatcher()
if err != nil {
log.WithError(err).Error("Failed to create fsnotify watcher")
return
}
if err := w.Add(fp); err != nil {
log.WithError(err).Error("Failed to create fsnotify watcher")
return
}
for {
select {
case <-w.Events:
lock.Lock()
updateDenyList(fp)
lock.Unlock()
case <-ctx.Done():
return
}
}
}
func updateDenyList(fp string) {
newDenyList := make([]*regexp.Regexp, 0)
content, err := ioutil.ReadFile(fp)
if err != nil {
log.WithError(err).Error("Failed to read denylist")
return
}
s := string(content)
for _, row := range strings.Split(s, "\n") {
if row == "" {
continue
}
re, err := regexp.Compile("(?i)" + row) // Prefix (?i) to make case insenstive.
if err != nil {
log.WithError(err).Errorf("Failed to parse regex: %s", row)
continue
}
newDenyList = append(newDenyList, re)
}
if len(newDenyList) > 0 {
denylist = newDenyList
log.WithField("count", len(newDenyList)).Info("Updated deny list")
}
}