This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
274 lines (247 loc) · 6.93 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
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
269
270
271
272
273
274
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// Variables used for command line parameters
var (
Token string
APIUrl string
RPCUrl string
EncryptedPriv string
Password string
DBPath string
DenylistPath string
Debug bool
conn *grpc.ClientConn
beaconClient eth.BeaconChainClient
nodeClient eth.NodeClient
log = logrus.WithField("prefix", "prysmBot")
)
func init() {
flag.StringVar(&Token, "token", "", "Bot Token")
flag.StringVar(&APIUrl, "api-url", "", "API Url for gRPC")
flag.StringVar(&RPCUrl, "rpc-url", "", "RPC Url for Goerli network")
flag.StringVar(&EncryptedPriv, "private-key", "", "Private key for Goerli wallet")
flag.StringVar(&Password, "password", "", "Password for encrypted private key")
flag.StringVar(&DenylistPath, "denylist", "", "Filepath to denylist of regular expressions")
flag.BoolVar(&Debug, "debug", false, "Enable debug logging")
flag.Parse()
if Debug {
logrus.SetLevel(logrus.DebugLevel)
log.Debug("Debug logging enabled.")
}
}
func main() {
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
conn, err = grpc.Dial(APIUrl, grpc.WithInsecure())
if err != nil {
log.Error("Failed to dial: %v", err)
return
}
beaconClient = eth.NewBeaconChainClient(conn)
nodeClient = eth.NewNodeClient(conn)
defer conn.Close()
if err := initWallet(); err != nil {
log.Error(err)
return
}
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreate)
dg.AddHandler(messageReaction)
// Monitor denylist changes
go monitorDenylistFile(DenylistPath)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
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, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
// 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
}
if deniedMessage(s, m) {
return
}
if !whitelistedChannel(m.ChannelID) {
return
}
// Ignore all messages that don't start with "!".
if !strings.HasPrefix(m.Content, "!") {
return
}
if err := s.ChannelTyping(m.ChannelID); err != nil {
log.WithError(err).Error("Cannot send typing notification to channel")
}
fullCommand := m.Content[1:]
// If the message is "ping" reply with "Pong!"
if fullCommand == "ping" {
_, err := s.ChannelMessageSend(m.ChannelID, "Pong!")
if err != nil {
log.WithError(err).Errorf("Error sending embed %s", fullCommand)
}
return
}
if fullCommand == "help" && helpOkayChannel(m.ChannelID) {
embed := fullHelpEmbed()
_, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
log.WithError(err).Errorf("Error sending embed %s", fullCommand)
}
return
}
if isRandomCommand(fullCommand) {
result := getRandomResult(fullCommand)
_, err := s.ChannelMessageSend(m.ChannelID, result)
if err != nil {
log.WithError(err).Errorf("Error handling command %s", fullCommand)
return
}
}
splitCommand := strings.Split(fullCommand, ".")
if fullCommand == splitCommand[0] {
splitCommand = strings.Split(fullCommand, " ")
if splitCommand[0] == "send" && goerliOkayChannel(m.ChannelID) {
if err := validateUser(m); err != nil {
log.WithError(err).Error("Failed to validate user")
s.ChannelMessageSend(m.ChannelID, err.Error())
return
}
resp, err := SendGoeth(splitCommand[1:])
if err != nil {
log.WithError(err).Error("Could not send goerli eth")
return
}
_, err = s.ChannelMessageSend(m.ChannelID, resp)
if err != nil {
log.WithError(err).Errorf("Error handling command %s", fullCommand)
}
}
return
}
if len(splitCommand) > 1 && strings.TrimSpace(splitCommand[1]) == "" {
return
}
commandGroup := splitCommand[0]
endOfCommand := strings.Index(splitCommand[1], " ")
var parameters []string
if endOfCommand == -1 {
endOfCommand = len(splitCommand[1])
} else {
parameters = strings.Split(splitCommand[1][endOfCommand:], ",")
for i, param := range parameters {
parameters[i] = strings.TrimSpace(param)
}
}
command := splitCommand[1][:endOfCommand]
var cmdFound bool
var cmdGroupFound bool
var reqGroup *botCommandGroup
for _, flagGroup := range allFlagGroups {
if flagGroup.name == commandGroup || flagGroup.shorthand == commandGroup {
cmdGroupFound = true
reqGroup = flagGroup
for _, cmd := range reqGroup.commands {
if command == cmd.command || command == cmd.shorthand || command == "help" {
cmdFound = true
}
}
}
}
if !cmdGroupFound || !cmdFound {
return
}
if command == "help" && helpOkayChannel(m.ChannelID) {
embed := specificHelpEmbed(reqGroup)
_, err := s.ChannelMessageSendEmbed(m.ChannelID, embed)
if err != nil {
log.WithError(err).Errorf("Error sending embed %s", fullCommand)
}
return
}
var result string
switch commandGroup {
case currentCommandGroup.name, currentCommandGroup.shorthand:
result = getHeadCommandResult(command)
case stateCommandGroup.name, stateCommandGroup.shorthand:
result = getStateCommandResult(command, parameters)
case valCommandGroup.name, valCommandGroup.shorthand:
result = getValidatorCommandResult(command, parameters)
case blockCommandGroup.name, blockCommandGroup.shorthand:
result = getBlockCommandResult(command, parameters)
default:
result = "Command not found, sorry!"
}
if result == "" {
return
}
_, err := s.ChannelMessageSend(m.ChannelID, result)
if err != nil {
log.WithError(err).Errorf("Error handling command %s", fullCommand)
return
}
}
func helpOkayChannel(channelID string) bool {
switch channelID {
case prysmInternal:
return true
case personalTesting:
return true
case prysmRandom:
return true
default:
return false
}
}
func goerliOkayChannel(channelID string) bool {
switch channelID {
case personalTesting:
return true
case prysmGoerli:
return true
default:
return false
}
}
func whitelistedChannel(channelID string) bool {
switch channelID {
case prysmGeneral:
return true
case prysmGoerli:
return true
default:
return helpOkayChannel(channelID)
}
}
func messageReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
// Ignore reactions by the bot.
if m.UserID == s.State.User.ID {
return
}
handleDenyListMessageReaction(s, m)
}