feat(copaing): restore data if copaing rejoins after max 48 hours
This commit is contained in:
parent
48c9f5ae80
commit
d0253f36b4
2 changed files with 68 additions and 7 deletions
14
main.go
14
main.go
|
@ -18,9 +18,11 @@ var (
|
||||||
updatesData []byte
|
updatesData []byte
|
||||||
Version = gokord.Version{
|
Version = gokord.Version{
|
||||||
Major: 2,
|
Major: 2,
|
||||||
Minor: 3,
|
Minor: 4,
|
||||||
Patch: 5,
|
Patch: 0,
|
||||||
} // git version: 0.3.5 (it's the v2 of the bot)
|
} // git version: 0.4.0 (it's the v2 of the bot)
|
||||||
|
|
||||||
|
stopPeriodicReducer chan<- interface{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -170,6 +172,10 @@ func main() {
|
||||||
}
|
}
|
||||||
bot.Start()
|
bot.Start()
|
||||||
|
|
||||||
|
if stopPeriodicReducer != nil {
|
||||||
|
stopPeriodicReducer <- true
|
||||||
|
}
|
||||||
|
|
||||||
xp.CloseRedisClient()
|
xp.CloseRedisClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +191,7 @@ func afterInit(dg *discordgo.Session) {
|
||||||
// reduce time for debug
|
// reduce time for debug
|
||||||
d = time.Minute
|
d = time.Minute
|
||||||
}
|
}
|
||||||
utils.NewTimer(d, func(stop chan<- interface{}) {
|
stopPeriodicReducer = utils.NewTimer(d, func(stop chan<- interface{}) {
|
||||||
xp.PeriodicReducer(dg)
|
xp.PeriodicReducer(dg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
59
xp/member.go
59
xp/member.go
|
@ -21,7 +21,16 @@ type Copaing struct {
|
||||||
GuildID string `gorm:"not null"`
|
GuildID string `gorm:"not null"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var redisClient *redis.Client
|
type leftCopaing struct {
|
||||||
|
ID uint
|
||||||
|
StopDelete chan<- interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
redisClient *redis.Client
|
||||||
|
|
||||||
|
leftCopaingsMap = map[string]*leftCopaing{}
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LastEvent = "last_event"
|
LastEvent = "last_event"
|
||||||
|
@ -45,8 +54,51 @@ func GetCopaing(discordID string, guildID string) *Copaing {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Copaing) Load() error {
|
func (c *Copaing) Load() error {
|
||||||
|
// check if user left in the past 48 hours
|
||||||
|
k := c.GuildID + ":" + c.DiscordID
|
||||||
|
l, ok := leftCopaingsMap[k]
|
||||||
|
if !ok || l == nil {
|
||||||
|
// if not, common first or create
|
||||||
return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
|
return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
|
||||||
}
|
}
|
||||||
|
// else, getting last data
|
||||||
|
tmp := Copaing{
|
||||||
|
Model: gorm.Model{
|
||||||
|
ID: c.ID,
|
||||||
|
},
|
||||||
|
DiscordID: c.DiscordID,
|
||||||
|
GuildID: c.GuildID,
|
||||||
|
}
|
||||||
|
if err := gokord.DB.Unscoped().Find(&tmp).Error; err != nil {
|
||||||
|
// if error, avoid getting old data and use new one
|
||||||
|
utils.SendAlert(
|
||||||
|
"xp/member.go - Getting copaing in soft delete", err.Error(),
|
||||||
|
"discord_id", c.DiscordID,
|
||||||
|
"guild_id", c.DiscordID,
|
||||||
|
"last_id", l.ID,
|
||||||
|
)
|
||||||
|
return gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c).Error
|
||||||
|
}
|
||||||
|
// resetting internal data
|
||||||
|
tmp.Model = gorm.Model{}
|
||||||
|
l.StopDelete <- true
|
||||||
|
leftCopaingsMap[k] = nil
|
||||||
|
// creating new data
|
||||||
|
err := gokord.DB.Create(&tmp).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// delete old data
|
||||||
|
if err = gokord.DB.Unscoped().Delete(&tmp).Error; err != nil {
|
||||||
|
utils.SendAlert(
|
||||||
|
"xp/member.go - Deleting copaing in soft delete", err.Error(),
|
||||||
|
"discord_id", c.DiscordID,
|
||||||
|
"guild_id", c.DiscordID,
|
||||||
|
"last_id", l.ID,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Copaing) Save() error {
|
func (c *Copaing) Save() error {
|
||||||
return gokord.DB.Save(c).Error
|
return gokord.DB.Save(c).Error
|
||||||
|
@ -208,7 +260,8 @@ func (c *Copaing) AfterDelete(db *gorm.DB) error {
|
||||||
id := c.ID
|
id := c.ID
|
||||||
dID := c.DiscordID
|
dID := c.DiscordID
|
||||||
gID := c.GuildID
|
gID := c.GuildID
|
||||||
utils.NewTimer(48*time.Hour, func(stop chan<- interface{}) {
|
k := c.GuildID + ":" + c.DiscordID
|
||||||
|
ch := utils.NewTimer(48*time.Hour, func(stop chan<- interface{}) {
|
||||||
if err := db.Unscoped().Where("id = ?", id).Delete(c).Error; err != nil {
|
if err := db.Unscoped().Where("id = ?", id).Delete(c).Error; err != nil {
|
||||||
utils.SendAlert(
|
utils.SendAlert(
|
||||||
"xp/member.go - Removing copaing from database", err.Error(),
|
"xp/member.go - Removing copaing from database", err.Error(),
|
||||||
|
@ -217,7 +270,9 @@ func (c *Copaing) AfterDelete(db *gorm.DB) error {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
stop <- true
|
stop <- true
|
||||||
|
leftCopaingsMap[k] = nil
|
||||||
})
|
})
|
||||||
|
leftCopaingsMap[k] = &leftCopaing{id, ch}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue