-
Notifications
You must be signed in to change notification settings - Fork 927
/
plugin.go
99 lines (80 loc) · 2.43 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
package bot
import (
"context"
"github.com/jonas747/retryableredis"
"github.com/jonas747/yagpdb/bot/models"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/queries/qm"
"sync"
"time"
"github.com/jonas747/discordgo"
"github.com/jonas747/dshardorchestrator/v2"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/common"
)
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.GuildState, toThisSlave bool)
}
func guildRemoved(guildID int64) {
if common.Statsd != nil {
common.Statsd.Incr("yagpdb.left_guilds", nil, 1)
}
common.RedisPool.Do(retryableredis.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")
}
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,
}
}