Milen/level/listener.go

54 lines
1.1 KiB
Go

package level
import (
"fmt"
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/persistent"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
var ShouldListen = true
func Event(dg *discordgo.Session, m *discordgo.MessageCreate) {
if !ShouldListen || m.GuildID == "" || persistent.IgnoredChannels[m.ChannelID] {
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))
}
}
}()
prevTime := db.GetLastActive(m.Author.ID)
nextTime, err := discordgo.SnowflakeTimestamp(m.ID)
if err != nil {
util.ReportError(dg, err)
return
}
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)
if _, ok := ShouldCheck[m.GuildID]; !ok {
ShouldCheck[m.GuildID] = make(map[string]bool)
}
ShouldCheck[m.GuildID][m.Author.ID] = true
}