-
Notifications
You must be signed in to change notification settings - Fork 920
/
bot.go
221 lines (179 loc) · 5.7 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
package bot
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/dshardmanager"
"github.com/jonas747/dutil/dstate"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
log "github.com/sirupsen/logrus"
"os"
"strconv"
"sync"
"time"
)
var (
// When the bot was started
Started = time.Now()
Running bool
State *dstate.State
ShardManager *dshardmanager.Manager
StateHandlerPtr *eventsystem.Handler
)
func Setup() {
// Things may rely on state being available at this point for initialization
State = dstate.NewState()
eventsystem.AddHandler(HandleReady, eventsystem.EventReady)
StateHandlerPtr = eventsystem.AddHandler(StateHandler, eventsystem.EventAll)
eventsystem.ConcurrentAfter = StateHandlerPtr
// eventsystem.AddHandler(HandlePresenceUpdate, eventsystem.EventPresenceUpdate)
eventsystem.AddHandler(ConcurrentEventHandler(EventLogger.handleEvent), eventsystem.EventAll)
eventsystem.AddHandler(RedisWrapper(HandleGuildCreate), eventsystem.EventGuildCreate)
eventsystem.AddHandler(RedisWrapper(HandleGuildDelete), eventsystem.EventGuildDelete)
eventsystem.AddHandler(RedisWrapper(HandleGuildUpdate), eventsystem.EventGuildUpdate)
eventsystem.AddHandler(RedisWrapper(HandleGuildRoleCreate), eventsystem.EventGuildRoleCreate)
eventsystem.AddHandler(RedisWrapper(HandleGuildRoleUpdate), eventsystem.EventGuildRoleUpdate)
eventsystem.AddHandler(RedisWrapper(HandleGuildRoleRemove), eventsystem.EventGuildRoleDelete)
eventsystem.AddHandler(RedisWrapper(HandleChannelCreate), eventsystem.EventChannelCreate)
eventsystem.AddHandler(RedisWrapper(HandleChannelUpdate), eventsystem.EventChannelUpdate)
eventsystem.AddHandler(RedisWrapper(HandleChannelDelete), eventsystem.EventChannelDelete)
eventsystem.AddHandler(RedisWrapper(HandleGuildMemberUpdate), eventsystem.EventGuildMemberUpdate)
log.Info("Initializing bot plugins")
for _, plugin := range common.Plugins {
if botPlugin, ok := plugin.(Plugin); ok {
botPlugin.InitBot()
log.Info("Initialized bot plugin ", plugin.Name())
}
}
log.Printf("Registered %d event handlers", eventsystem.NumHandlers(eventsystem.EventAll))
}
func Run() {
log.Println("Running bot")
connEvtChannel, _ := strconv.ParseInt(os.Getenv("YAGPDB_CONNEVT_CHANNEL"), 10, 64)
connStatusChannel, _ := strconv.ParseInt(os.Getenv("YAGPDB_CONNSTATUS_CHANNEL"), 10, 64)
// Set up shard manager
ShardManager = dshardmanager.New(common.Conf.BotToken)
ShardManager.LogChannel = connEvtChannel
ShardManager.StatusMessageChannel = 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
return
}
// Only handler
ShardManager.AddHandler(eventsystem.HandleEvent)
shardCount, err := ShardManager.GetRecommendedCount()
if err != nil {
panic("Failed getting shard count: " + err.Error())
}
EventLogger.init(shardCount)
go EventLogger.run()
for i := 0; i < shardCount; i++ {
waitingReadies = append(waitingReadies, i)
}
State.MaxChannelMessages = 1000
State.MaxMessageAge = time.Hour
// State.Debug = true
Running = true
go ShardManager.Start()
go MemberFetcher.Run()
go mergedMessageSender()
go MonitorLoading()
for _, p := range common.Plugins {
starter, ok := p.(BotStarterHandler)
if ok {
starter.StartBot()
log.Debug("Ran StartBot for ", p.Name())
}
}
}
func MonitorLoading() {
t := time.NewTicker(time.Second)
defer t.Stop()
for {
<-t.C
waitingGuildsMU.Lock()
numWaitingGuilds := len(waitingGuilds)
numWaitingShards := len(waitingReadies)
waitingGuildsMU.Unlock()
log.Infof("Starting up... GC's Remaining: %d, Shards remaining: %d", numWaitingGuilds, numWaitingShards)
if numWaitingGuilds == 0 && numWaitingShards == 0 {
return
}
}
}
func Stop(wg *sync.WaitGroup) {
for _, v := range common.Plugins {
stopper, ok := v.(BotStopperHandler)
if !ok {
continue
}
wg.Add(1)
log.Debug("Sending stop event to stopper: ", v.Name())
go stopper.StopBot(wg)
}
ShardManager.StopAll()
wg.Done()
}
// checks all connected guilds and emites guildremoved on those no longer connected
func checkConnectedGuilds() {
log.Info("Checking joined guilds")
client, err := common.RedisPool.Get()
if err != nil {
log.WithError(err).Error("Failed retrieving connection from redis pool")
return
}
defer common.RedisPool.Put(client)
currentlyConnected, err := client.Cmd("SMEMBERS", "connected_guilds").List()
if err != nil {
log.WithError(err).Error("Failed retrieving currently connected guilds")
return
}
guilds := make([]*discordgo.UserGuild, 0)
var after int64
for {
g, err := common.BotSession.UserGuilds(100, 0, after)
if err != nil {
log.WithError(err).Error("Userguilds failed")
return
}
guilds = append(guilds, g...)
if len(g) < 100 {
break
}
after = g[len(g)-1].ID
}
OUTER:
for _, gID := range currentlyConnected {
parsedGID, _ := strconv.ParseInt(gID, 10, 64)
for _, g := range guilds {
if g.ID == parsedGID {
continue OUTER
}
}
err := client.Cmd("SREM", "connected_guilds", gID).Err
if err != nil {
log.WithError(err).Error("Failed removing guild from connected guilds")
} else {
go EmitGuildRemoved(parsedGID)
log.WithField("guild", gID).Info("Removed from guild when offline")
}
}
}
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
}