Milen/level/listener.go

54 lines
1.1 KiB
Go

package level
import (
"fmt"
"github.com/bwmarrin/discordgo"
2020-05-30 12:48:22 +07:00
"gitea.teamortix.com/chanbakjsd/Milen/db"
2020-06-06 09:25:21 +07:00
"gitea.teamortix.com/chanbakjsd/Milen/persistent"
2020-05-30 12:48:22 +07:00
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
var ShouldListen = true
func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
2020-06-06 09:25:21 +07:00
if !ShouldListen || m.GuildID == "" || persistent.IgnoredChannels[m.ChannelID] {
2020-05-30 12:48:22 +07:00
return
}
defer func() {
if r := recover(); r != nil {
// Oh crap. We recovered from a panic.
if val, ok := r.(error); ok {
util.ReportError(dg, val)
} else {
util.ReportError(dg, fmt.Errorf("%T: %v", r, r))
}
}
}()
2020-05-30 12:48:22 +07:00
prevTime := db.GetLastActive(m.Author.ID)
nextTime, err := discordgo.SnowflakeTimestamp(m.ID)
if err != nil {
util.ReportError(dg, err)
return
}
2020-05-30 12:48:22 +07:00
xp := int64(nextTime.Sub(prevTime).Seconds())
if xp > int64(len(m.Content)-3)/3 {
xp = int64(len(m.Content)-3) / 3
}
if xp > 10 {
xp = 10
}
if xp < 0 {
xp = 0
}
db.IncrementXP(m.Author.ID, xp, nextTime)
2020-05-30 14:50:26 +07:00
if _, ok := ShouldCheck[m.GuildID]; !ok {
ShouldCheck[m.GuildID] = make(map[string]bool)
}
ShouldCheck[m.GuildID][m.Author.ID] = true
}