-
Notifications
You must be signed in to change notification settings - Fork 927
/
plugin.go
109 lines (87 loc) · 2.82 KB
/
plugin.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
package bot
import (
"context"
"sync"
"time"
"github.com/botlabs-gg/yagpdb/v2/bot/models"
"github.com/botlabs-gg/yagpdb/v2/common/featureflags"
"github.com/mediocregopher/radix/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/queries/qm"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dshardorchestrator"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
)
const (
// How long after removing a guild the config for it gets cleared
GuildRemoveConfigExpire = 60 * 60 * 24 // <- 1 day
)
// Used for deleting configuration about servers
type RemoveGuildHandler interface {
RemoveGuild(guildID int64) error
}
// Used for intializing stuff for new servers
type NewGuildHandler interface {
NewGuild(guild *discordgo.Guild) error
}
// Fired when the bot it starting up, not for the webserver
type BotInitHandler interface {
BotInit()
}
// Fired when the bot it starting up, after BotInit
type LateBotInitHandler interface {
LateBotInit()
}
// BotStopperHandler runs when the bot is shuttdown down
// you need to call wg.Done when you have completed your plugin shutdown (stopped background workers)
type BotStopperHandler interface {
StopBot(wg *sync.WaitGroup)
}
type ShardMigrationHandler interface {
GuildMigrated(guild *dstate.GuildSet, toThisSlave bool)
}
var metricsLeftGuilds = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_left_guilds",
Help: "Guilds yagpdb left",
})
func guildRemoved(guildID int64) {
metricsLeftGuilds.Inc()
commonEventsTotal.With(prometheus.Labels{"type": "Guild Delete"}).Inc()
common.RedisPool.Do(radix.Cmd(nil, "SREM", "connected_guilds", discordgo.StrID(guildID)))
_, err := models.JoinedGuilds(qm.Where("id = ?", guildID)).UpdateAll(context.Background(), common.PQ, models.M{
"left_at": null.TimeFrom(time.Now()),
})
if err != nil {
logger.WithError(err).WithField("guild", guildID).Error("failed marking guild as left")
}
featureflags.EvictCacheForGuild(guildID)
for _, v := range common.Plugins {
if remover, ok := v.(RemoveGuildHandler); ok {
err := remover.RemoveGuild(guildID)
if err != nil {
logger.WithError(err).Error("Error Running RemoveGuild on ", v.PluginInfo().Name)
}
}
}
}
type ShardMigrationSender interface {
ShardMigrationSend(shard int) int
}
type ShardMigrationReceiver interface {
ShardMigrationReceive(evt dshardorchestrator.EventType, data interface{})
}
// bot plugin
var BotPlugin = new(botPlugin)
var logger = common.GetPluginLogger(BotPlugin)
type botPlugin struct {
}
func (p *botPlugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Bot Core",
SysName: "bot_core",
Category: common.PluginCategoryCore,
}
}