This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
183 lines (147 loc) · 4.66 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
/*
Copyright (C) 2017 Noah Santschi-Cooney
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bytes"
"net/http"
"os"
"os/signal"
"regexp"
"runtime"
"strconv"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-chi/chi"
)
const (
happyEmoji string = "https://cdn.discordapp.com/emojis/332968429210435585.png"
thinkEmoji string = "https://cdn.discordapp.com/emojis/333694872802426880.png"
reviewChan string = "334092230845267988"
logChan string = "312352242504040448"
serverID string = "312292616089894924"
xmark string = "<:xmark:314349398824058880>"
zerowidth string = ""
)
var (
conf = new(config)
dg *discordgo.Session
lastReboot string
log = newLog()
emojiRegex = regexp.MustCompile("<(a)?:.*?:(.*?)>")
userIDRegex = regexp.MustCompile("<@!?([0-9]+)>")
channelRegex = regexp.MustCompile("<#([0-9]+)>")
status = map[discordgo.Status]string{"dnd": "busy", "online": "online", "idle": "idle", "offline": "offline"}
footer = new(discordgo.MessageEmbedFooter)
)
func init() {
footer.Text = "Created with ❤ by Strum355\nLast Bot reboot: " + time.Now().Format("Mon, 02-Jan-06 15:04:05 MST")
}
func dailyJobs() {
for {
postServerCount()
time.Sleep(time.Hour * 24)
}
}
func postServerCount() {
url := "https://bots.discord.pw/api/bots/301819949683572738/stats"
count := len(dg.State.Guilds)
jsonStr := []byte(`{"server_count":` + strconv.Itoa(count) + `}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
log.Error("error making bots.discord.pw request", err)
return
}
req.Header.Set("Authorization", conf.DiscordPWKey)
req.Header.Set("Content-Type", "application/json")
resp, err := new(http.Client).Do(req)
if err != nil {
log.Error("bots.discord.pw error", err)
return
}
defer resp.Body.Close()
log.Info("POSTed " + strconv.Itoa(count) + " to bots.discord.pw")
if resp.StatusCode != http.StatusNoContent {
log.Error("received " + strconv.Itoa(resp.StatusCode) + " from bots.discord.pw")
}
}
func setBotGame(s *discordgo.Session) {
if err := s.UpdateStatus(0, conf.Game); err != nil {
log.Error("Update status err:", err)
return
}
log.Info("set initial game to", conf.Game)
}
// Set all handlers for queued images, in case the bot crashes with images still in queue
func setQueuedImageHandlers() {
for imgNum := range imageQueue {
imgNumInt, err := strconv.Atoi(imgNum)
if err != nil {
log.Error("Error converting string to num for queue:", err)
continue
}
go fimageReview(dg, imgNumInt)
}
}
func main() {
runtime.GOMAXPROCS(conf.MaxProc)
log.Info("/*********BOT RESTARTING*********\\")
names := []string{"config", "users", "servers", "queue"}
for i, f := range []func() error{loadConfig, loadUsers, loadServers, loadQueue} {
if err := f(); err != nil {
switch i {
case 0:
os.Exit(404)
default:
}
continue
}
log.Trace("loaded", names[i])
}
defer cleanup()
log.Info("files loaded")
var err error
dg, err = discordgo.New("Bot " + conf.Token)
if err != nil {
log.Error("Error creating Discord session,", err)
return
}
log.Trace("session created")
dg.AddHandler(messageCreateEvent)
dg.AddHandler(presenceChangeEvent)
dg.AddHandler(guildKickedEvent)
dg.AddHandler(memberJoinEvent)
dg.AddHandler(readyEvent)
dg.AddHandler(guildJoinEvent)
if err := dg.Open(); err != nil {
log.Error("Error opening connection,", err)
return
}
defer dg.Close()
log.Trace("connection opened")
sMap.Count = len(sMap.serverMap)
go setQueuedImageHandlers()
if !conf.InDev {
go dailyJobs()
}
// Setup http server for selfbots
router := chi.NewRouter()
router.Get("/image/{id:[0-9]{18}}/recall/{img:[0-9a-z]{64}}", httpImageRecall)
router.Get("/inServer/{id:[0-9]{18}}", isInServer)
go func() { log.Error("error starting http server", http.ListenAndServe("0.0.0.0:8080", router)) }()
log.Info("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV, syscall.SIGHUP)
<-sc
}