-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
390 lines (314 loc) · 9.43 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"database/sql"
"fmt"
"math/rand"
"net/http"
"net/http/pprof"
"strconv"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
"github.com/gidoBOSSftw5731/log"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jinzhu/configor"
"github.com/gidoBOSSftw5731/ProjectMoocow/tools"
)
var config = struct {
APPName string `default:"PinnerBoi"`
Author string `default:"gidoBOSSftw5731#6422"`
Prefix string `default:"📌 "`
Token string `required:"true"`
DB struct {
User string `default:"pinnerboi"`
Password string `required:"true" env:"DBPassword"`
Port string `default:"3306"`
IP string `default:"127.0.0.1"`
}
}{}
const (
pinReaction string = "📌"
precision int = 10
)
// var allChannelIDs = map[string]string{}
var allChannelIDs []channelInfo
type channelInfo struct {
ChannelID string
GuildID string
}
var helpMenu discordgo.MessageEmbed
var (
botID string
//discordToken = flag.String("token", "", "Discord bot secret")
//commandPrefix = "📌 "
//author = ""
db *sql.DB
)
func main() {
configor.Load(&config, "config.yml")
// Create a new router
router := mux.NewRouter()
// Register pprof handlers
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handle("/debug/pprof/block", pprof.Handler("block"))
go http.ListenAndServe("192.110.255.55:56799", router)
helpMenu = discordgo.MessageEmbed{
Title: "PinnerBoi Help",
Author: &discordgo.MessageEmbedAuthor{},
Color: rand.Intn(16777215),
Description: fmt.Sprintf("Prefix is %v \n All commands are case sensitive \n React to any message with %v to have it be pinned \n Bot made by %v",
config.Prefix, pinReaction, config.Author),
Fields: []*discordgo.MessageEmbedField{
{
Name: "help",
Value: "Returns this message",
},
{
Name: "site",
Value: "Returns this channel's site",
},
{
Name: "chkpin",
Value: "Checks last 100 messages for new pins. The bot checks the last 25 messages every 5 seconds by default.",
},
{
Name: "invite",
Value: "Returns the invite URL",
},
{
Name: "Where is the code?",
Value: "This bot's code is accessible at https://github.com/gidoBOSSftw5731/ProjectMoocow \n My personal code can be found at https://imagen.click/git",
},
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: "https://imagen.click/i/9e1233.png",
},
Timestamp: time.Now().Format(time.RFC3339), // Discord wants ISO8601; RFC3339 is an extension of ISO8601 and should be completely compatible.
}
//println(config.Token)
log.SetCallDepth(4)
discord, err := discordgo.New("Bot " + config.Token)
errCheck("error creating discord session", err)
user, err := discord.User("@me")
errCheck("error retrieving account", err)
botID = user.ID
discord.AddHandler(commandHandler)
discord.AddHandler(func(discord *discordgo.Session, ready *discordgo.Ready) {
servers := discord.State.Guilds
err = discord.UpdateGameStatus(2, fmt.Sprintf("Pin all the things! | %vhelp | Pinning in %v servers!",
config.Prefix, len(servers)))
if err != nil {
log.Errorln("Error attempting to set my status")
}
log.Debugf("PinnerBoi has started on %d servers", len(servers))
})
db = startSQL()
err = discord.Open()
errCheck("Error opening connection to Discord", err)
defer discord.Close()
discord.AddHandler(func(discord *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
if reaction.UserID == botID {
return
}
if reaction.Emoji.Name != pinReaction {
return
}
// get message by ID
msg, err := discord.ChannelMessage(reaction.ChannelID, reaction.MessageID)
if err != nil {
log.Errorln(err)
return
}
// get channel object
channel, err := discord.Channel(reaction.ChannelID)
if err != nil {
log.Errorln(err)
return
}
addMsgToDB(msg, channel.GuildID)
log.Debugln("Added message to DB via Gateway")
})
go collectAllChannelIDs(discord)
<-make(chan struct{})
}
func errCheck(msg string, err error) {
if err != nil {
log.Fatalf("%s: %+v", msg, err)
}
}
func collectAllChannelIDs(s *discordgo.Session) {
for _, guild := range s.State.Guilds {
channels, err := s.GuildChannels(guild.ID)
if err != nil {
log.Errorln(err)
continue
}
for _, c := range channels {
// Check if channel is a guild text channel and not a voice or DM channel
if c.Type != discordgo.ChannelTypeGuildText {
continue
}
//allChannelIDs = append(allChannelIDs, c.ID)
//allChannelIDs[c.ID] = c.GuildID
allChannelIDs = append(allChannelIDs, channelInfo{ChannelID: c.ID, GuildID: c.GuildID})
}
}
//log.Tracef("All channel IDs: \n%v", allChannelIDs)
// don't use autochecker anymore, replace with intents and gw
//autoChecker(s)
}
// autoChecker checks for recent messages frequently.
// This is DEPRECATED due to the Gateway handler.
func autoChecker(s *discordgo.Session) {
iterations := make(map[channelInfo]int)
for _, channel := range allChannelIDs {
id, err := strconv.Atoi(channel.ChannelID)
if err != nil {
log.Errorln(err)
continue
}
iterations[channel] = id % precision
}
//log.Traceln(iterations)
for {
now := time.Now().Unix() % int64(precision)
var current []channelInfo
for channel := range iterations {
if int64(iterations[channel]) == now {
current = append(current, channel)
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
for _, channel := range current {
wg.Add(1)
check(s, &channel, &wg)
}
}()
//wg.Done()
//wg.Wait()
time.Sleep(1 * time.Second)
}
}
func check(s *discordgo.Session, channel *channelInfo, wg *sync.WaitGroup) {
last25, err := s.ChannelMessages(channel.ChannelID, 25, "", "", "")
if err != nil {
if strings.Contains(err.Error(), "HTTP 403 Forbidden, {\"message\": \"Missing Access\", \"code\": 50001}") {
return
}
log.Errorln(err)
return
}
checkAndPin(last25, channel.GuildID)
//wg.Done()
}
func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
if message.Content == "" || len(message.Content) < len(config.Prefix) {
return
}
if message.Content[:len(config.Prefix)] != config.Prefix ||
len(strings.Split(message.Content, config.Prefix)) < 2 {
return
}
log.Debugln("prefix found")
command := strings.Split(message.Content, config.Prefix)[1]
commandContents := strings.Split(message.Content, " ") // 0 = !, 1 = command, 2 = first arg, etc
if len(command) < 2 {
log.Errorln("No command sent")
return
}
switch strings.Split(command, " ")[1] {
case "chkpin":
last100, err := discord.ChannelMessages(message.ChannelID, 100, message.ID, "", "")
if err != nil {
log.Errorln(err)
return
}
err = checkAndPin(last100, message.GuildID)
if err != nil {
log.Errorln(err)
return
}
case "updatepin":
reactedMsg, err := discord.ChannelMessage(message.ChannelID, commandContents[2])
if err != nil {
return
}
last100, err := discord.ChannelMessages(reactedMsg.ChannelID, 100, "", "", reactedMsg.ID)
if err != nil {
log.Errorln(err)
return
}
err = checkAndPin(last100, message.GuildID)
if err != nil {
log.Errorln(err)
return
}
case "site", "website":
discord.ChannelMessageSend(message.ChannelID, fmt.Sprintf("https://imagen.click/pinbot/%v/%v", message.GuildID, message.ChannelID))
case "invite":
discord.ChannelMessageSend(message.ChannelID,
"https://discordapp.com/oauth2/authorize?client_id=631313783545004032&permissions=305216&redirect_uri=https%3A%2F%2Fdiscordapp.com%2Foauth2%2Fauthorize%3F%26client_id%3D181965297249550336%26scope%3Dbot&scope=bot")
case "help", "howto", "aahhhh", "sendhelp", "aid", "ayudame", "yonose", "info":
resp, err := discord.ChannelMessageSendEmbed(message.ChannelID, &helpMenu)
if err != nil {
log.Debugln(resp, err)
discord.ChannelMessageSend(message.ChannelID, "Internal Error!")
return
}
}
}
func checkAndPin(last100 []*discordgo.Message, serverID string) error {
for messageIndex := 0; messageIndex < len(last100); messageIndex++ {
msg := *last100[messageIndex]
reaction := msg.Reactions
isValid := tools.CheckIfValid(reaction, pinReaction)
if !isValid {
continue
}
// at this point the message is known good
err := addMsgToDB(&msg, serverID)
if err != nil {
log.Errorln(err)
return err
}
}
return nil
}
// addMsgToDB adds a message to the SQL Database and checks for error,
// does NOT verify if the reaction is actually there.
func addMsgToDB(msg *discordgo.Message, serverID string) error {
var tmpptr string //throwaway var
err := db.QueryRow("SELECT * FROM pinnedmessages WHERE channelid=? AND messageid=?",
msg.ChannelID, msg.ID).Scan(&tmpptr, &tmpptr, &tmpptr)
switch {
case err == sql.ErrNoRows:
log.Debug("New pin, adding..")
_, err := db.Exec("INSERT INTO pinnedmessages VALUES(?, ?, ?)",
serverID, msg.ChannelID, msg.ID)
if err != nil {
log.Error(err)
return err
}
log.Debug("Added pin info to table")
case err != nil:
log.Error(err)
return err
default:
// Pin already exists, do nothing
}
return nil
}
func startSQL() *sql.DB {
db := tools.StartSQL(config.DB.User, config.DB.Password, config.DB.IP, config.DB.Port)
return db
}