forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
311 lines (255 loc) · 8.83 KB
/
bot.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
package bot
//go:generate sqlboiler --no-hooks psql
import (
"runtime"
"sync"
"time"
"github.com/jonas747/discordgo"
"github.com/jonas747/dshardmanager"
"github.com/jonas747/dshardorchestrator/node"
"github.com/jonas747/dstate"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/bot/deletequeue"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/common/pubsub"
)
var (
// When the bot was started
Started = time.Now()
Enabled bool // wether the bot is set to run at some point in this process
Running bool // wether the bot is currently running
State *dstate.State
ShardManager *dshardmanager.Manager
NodeConn *node.Conn
UsingOrchestrator bool
MessageDeleteQueue = deletequeue.NewQueue()
FlagNodeID string
)
var (
confConnEventChannel = config.RegisterOption("yagpdb.connevt.channel", "Gateway connection logging channel", 0)
confConnStatus = config.RegisterOption("yagpdb.connstatus.channel", "Gateway connection status channel", 0)
confShardOrchestratorAddress = config.RegisterOption("yagpdb.orchestrator.address", "Sharding orchestrator address to connect to, if set it will be put into orchstration mode", "")
)
var (
// the variables below specify shard clustering information, for splitting shards across multiple processes and machines
// this is very work in process and does not work at all atm
//
// plugins that needs to perform shard specific tasks, not directly related to incoming gateway events should check here
// to make sure the action they're doing is relevant to the current cluster (example: scheduled events should only run events for guilds on this cluster)
// the total amount of shards this bot is set to use across all processes
totalShardCount int
// The shards running on this process, protected by the processShardsLock muted
processShards []int
processShardsLock sync.RWMutex
)
func setup() {
common.InitSchema(DBSchema, "core_bot")
discordgo.IdentifyRatelimiter = &identifyRatelimiter{}
// Things may rely on state being available at this point for initialization
State = dstate.NewState()
State.MaxChannelMessages = 1000
State.MaxMessageAge = time.Hour
// State.Debug = true
State.ThrowAwayDMMessages = true
State.TrackPrivateChannels = false
State.CacheExpirey = time.Minute * 10
go State.RunGCWorker()
eventsystem.AddHandlerFirst(HandleReady, eventsystem.EventReady)
eventsystem.AddHandlerSecond(StateHandler, eventsystem.EventAll)
eventsystem.AddHandlerAsyncLast(EventLogger.handleEvent, eventsystem.EventAll)
eventsystem.AddHandlerAsyncLast(HandleGuildCreate, eventsystem.EventGuildCreate)
eventsystem.AddHandlerAsyncLast(HandleGuildDelete, eventsystem.EventGuildDelete)
eventsystem.AddHandlerAsyncLast(HandleGuildUpdate, eventsystem.EventGuildUpdate)
eventsystem.AddHandlerAsyncLast(HandleGuildRoleCreate, eventsystem.EventGuildRoleCreate)
eventsystem.AddHandlerAsyncLast(HandleGuildRoleUpdate, eventsystem.EventGuildRoleUpdate)
eventsystem.AddHandlerAsyncLast(HandleGuildRoleRemove, eventsystem.EventGuildRoleDelete)
eventsystem.AddHandlerAsyncLast(HandleChannelCreate, eventsystem.EventChannelCreate)
eventsystem.AddHandlerAsyncLast(HandleChannelUpdate, eventsystem.EventChannelUpdate)
eventsystem.AddHandlerAsyncLast(HandleChannelDelete, eventsystem.EventChannelDelete)
eventsystem.AddHandlerAsyncLast(HandleGuildMemberUpdate, eventsystem.EventGuildMemberUpdate)
eventsystem.AddHandlerAsyncLast(HandleGuildMemberAdd, eventsystem.EventGuildMemberAdd)
eventsystem.AddHandlerAsyncLast(HandleGuildMemberRemove, eventsystem.EventGuildMemberRemove)
eventsystem.AddHandlerAsyncLast(HandleGuildMembersChunk, eventsystem.EventGuildMembersChunk)
eventsystem.AddHandlerAsyncLast(HandleReactionAdd, eventsystem.EventMessageReactionAdd)
eventsystem.AddHandlerAsyncLast(HandleMessageCreate, eventsystem.EventMessageCreate)
eventsystem.AddHandlerAsyncLast(HandleResumed, eventsystem.EventResumed)
}
func Run() {
setup()
logger.Println("Running bot")
connEvtChannel := confConnEventChannel.GetInt()
connStatusChannel := confConnStatus.GetInt()
// Set up shard manager
ShardManager = dshardmanager.New(common.GetBotToken())
ShardManager.LogChannel = int64(connEvtChannel)
ShardManager.StatusMessageChannel = int64(connStatusChannel)
ShardManager.Name = "YAGPDB"
ShardManager.GuildCountsFunc = GuildCountsFunc
ShardManager.SessionFunc = func(token string) (session *discordgo.Session, err error) {
session, err = discordgo.New(token)
if err != nil {
return
}
session.StateEnabled = false
session.LogLevel = discordgo.LogInformational
session.SyncEvents = true
// Certain discordgo internals expect this to be present
// but in case of shard migration it's not, so manually assign it here
session.State.Ready = discordgo.Ready{
User: &discordgo.SelfUser{
User: common.BotUser,
},
}
return
}
// Only handler
ShardManager.AddHandler(eventsystem.HandleEvent)
orcheStratorAddress := confShardOrchestratorAddress.GetString()
if orcheStratorAddress != "" {
UsingOrchestrator = true
logger.Infof("Set to use orchestrator at address: %s", orcheStratorAddress)
} else {
logger.Info("Running standalone without any orchestrator")
SetupStandalone()
}
go MemberFetcher.Run()
go mergedMessageSender()
Running = true
if UsingOrchestrator {
// TODO
NodeConn = node.NewNodeConn(&NodeImpl{}, orcheStratorAddress, common.VERSION, FlagNodeID, nil)
NodeConn.Run()
} else {
go ShardManager.Start()
InitPlugins()
}
// if masterAddr != "" {
// stateLock.Lock()
// state = StateWaitingHelloMaster
// stateLock.Unlock()
// logger.Println("Connecting to master at ", masterAddr, ", wont start until connected and told to start")
// var err error
// SlaveClient, err = slave.ConnectToMaster(&SlaveImpl{}, masterAddr)
// if err != nil {
// logger.WithError(err).Error("Failed connecting to master")
// os.Exit(1)
// }
// } else {
// stateLock.Lock()
// state = StateRunningNoMaster
// stateLock.Unlock()
// InitPlugins()
// logger.Println("Running normally without a master")
// go ShardManager.Start()
// go MonitorLoading()
// }
// for _, p := range common.Plugins {
// starter, ok := p.(BotStarterHandler)
// if ok {
// starter.StartBot()
// logger.Debug("Ran StartBot for ", p.Name())
// }
// }
}
func SetupStandalone() {
shardCount, err := ShardManager.GetRecommendedCount()
if err != nil {
panic("Failed getting shard count: " + err.Error())
}
totalShardCount = shardCount
EventLogger.init(shardCount)
go EventLogger.run()
processShards = make([]int, totalShardCount)
for i := 0; i < totalShardCount; i++ {
processShards[i] = i
}
err = common.RedisPool.Do(retryableredis.FlatCmd(nil, "SET", "yagpdb_total_shards", shardCount))
if err != nil {
logger.WithError(err).Error("failed setting shard count")
}
}
func InitPlugins() {
pubsub.AddHandler("bot_status_changed", func(evt *pubsub.Event) {
updateAllShardStatuses()
}, nil)
// Initialize all plugins
for _, plugin := range common.Plugins {
if initBot, ok := plugin.(BotInitHandler); ok {
initBot.BotInit()
}
}
// Initialize all plugins late
for _, plugin := range common.Plugins {
if initBot, ok := plugin.(LateBotInitHandler); ok {
initBot.LateBotInit()
}
}
if common.Statsd != nil {
go goroutineLogger()
}
go loopCheckAdmins()
watchMemusage()
}
var stopOnce sync.Once
func StopAllPlugins(wg *sync.WaitGroup) {
stopOnce.Do(func() {
for _, v := range common.Plugins {
stopper, ok := v.(BotStopperHandler)
if !ok {
continue
}
wg.Add(1)
logger.Debug("Calling bot stopper for: ", v.PluginInfo().Name)
go stopper.StopBot(wg)
}
close(stopRunCheckAdmins)
})
}
func Stop(wg *sync.WaitGroup) {
StopAllPlugins(wg)
ShardManager.StopAll()
wg.Done()
}
func GuildCountsFunc() []int {
numShards := ShardManager.GetNumShards()
result := make([]int, numShards)
State.RLock()
for _, v := range State.Guilds {
shard := (v.ID >> 22) % int64(numShards)
result[shard]++
}
State.RUnlock()
return result
}
// Standard implementation of the GatewayIdentifyRatelimiter
type identifyRatelimiter struct {
ch chan bool
once sync.Once
}
func (rl *identifyRatelimiter) RatelimitIdentify() {
const key = "yagpdb.gateway.identify.limit"
for {
var resp string
err := common.RedisPool.Do(retryableredis.Cmd(&resp, "SET", key, "1", "EX", "5", "NX"))
if err != nil {
logger.WithError(err).Error("failed ratelimiting gateway")
time.Sleep(time.Second)
continue
}
if resp == "OK" {
return // success
}
// otherwise a identify was sent by someone else last 5 seconds
time.Sleep(time.Second)
}
}
func goroutineLogger() {
t := time.NewTicker(time.Second * 10)
for {
<-t.C
num := runtime.NumGoroutine()
common.Statsd.Gauge("yagpdb.numgoroutine", float64(num), nil, 1)
}
}