feat(level): update roles on new level
This commit is contained in:
parent
beb0ea0ca4
commit
9e6a5ce1d3
8 changed files with 78 additions and 73 deletions
19
xp/events.go
19
xp/events.go
|
@ -21,12 +21,24 @@ const (
|
|||
)
|
||||
|
||||
func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.Bot {
|
||||
return
|
||||
}
|
||||
c := GetCopaing(m.Author.ID, m.GuildID)
|
||||
// add xp
|
||||
trimmed := utils.TrimMessage(strings.ToLower(m.Content))
|
||||
c.AddXP(s, XPMessage(uint(len(trimmed)), calcDiversity(trimmed)), func(_ uint, _ uint) {
|
||||
m.Member.User = m.Author
|
||||
m.Member.GuildID = m.GuildID
|
||||
c.AddXP(s, m.Member, XPMessage(uint(len(trimmed)), calcDiversity(trimmed)), func(_ uint, _ uint) {
|
||||
if err := s.MessageReactionAdd(m.ChannelID, m.Message.ID, "⬆"); err != nil {
|
||||
utils.SendAlert("xp/events.go - reaction add new level", "cannot add the reaction: "+err.Error())
|
||||
utils.SendAlert(
|
||||
"xp/events.go - add reaction for new level",
|
||||
err.Error(),
|
||||
"channel id",
|
||||
m.ChannelID,
|
||||
"message id",
|
||||
m.Message.ID,
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -119,7 +131,8 @@ func onDisconnect(s *discordgo.Session, e *discordgo.VoiceStateUpdate, client *r
|
|||
timeInVocal = MaxTimeInVocal
|
||||
}
|
||||
c := GetCopaing(u.DiscordID, u.GuildID)
|
||||
c.AddXP(s, XPVocal(uint(timeInVocal)), func(_ uint, _ uint) {
|
||||
e.Member.GuildID = e.GuildID
|
||||
c.AddXP(s, e.Member, XPVocal(uint(timeInVocal)), func(_ uint, _ uint) {
|
||||
//TODO: handle new level in vocal
|
||||
})
|
||||
}
|
||||
|
|
|
@ -28,6 +28,6 @@ func XPForLevel(level uint) uint {
|
|||
|
||||
func Lose(time uint, xp uint) uint {
|
||||
return uint(math.Floor(
|
||||
math.Pow(float64(time), 2) * math.Pow(10, -2+math.Log(float64(time/85))) * math.Floor(float64(time/500)),
|
||||
math.Pow(float64(time), 2) * math.Pow(10, -2+math.Log(float64(time/85))) * math.Floor(float64(xp/500)),
|
||||
))
|
||||
}
|
||||
|
|
39
xp/level.go
39
xp/level.go
|
@ -1,9 +1,44 @@
|
|||
package xp
|
||||
|
||||
import (
|
||||
"github.com/anhgelus/gokord/utils"
|
||||
"github.com/anhgelus/les-copaings-bot/config"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func onNewLevel(s *discordgo.Session, level uint) {
|
||||
// check roles
|
||||
func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
|
||||
cfg := config.GetGuildConfig(m.GuildID)
|
||||
xpForLevel := XPForLevel(level)
|
||||
for _, role := range cfg.XpRoles {
|
||||
if role.XP <= xpForLevel && !slices.Contains(m.Roles, role.RoleID) {
|
||||
utils.SendDebug(
|
||||
"Add role",
|
||||
"role_id",
|
||||
role.RoleID,
|
||||
"user_id",
|
||||
m.User.ID,
|
||||
"guild_id",
|
||||
m.GuildID,
|
||||
)
|
||||
err := s.GuildMemberRoleAdd(m.GuildID, m.User.ID, role.RoleID)
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/level.go - Adding role", err.Error(), "role_id", role.RoleID)
|
||||
}
|
||||
} else if role.XP > xpForLevel && slices.Contains(m.Roles, role.RoleID) {
|
||||
utils.SendDebug(
|
||||
"Remove role",
|
||||
"role_id",
|
||||
role.RoleID,
|
||||
"user_id",
|
||||
m.User.ID,
|
||||
"guild_id",
|
||||
m.GuildID,
|
||||
)
|
||||
err := s.GuildMemberRoleRemove(m.GuildID, m.User.ID, role.RoleID)
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/level.go - Removing role", err.Error(), "role_id", role.RoleID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,14 +31,14 @@ func (c *Copaing) Save() {
|
|||
gokord.DB.Save(c)
|
||||
}
|
||||
|
||||
func (c *Copaing) AddXP(s *discordgo.Session, xp uint, fn func(uint, uint)) {
|
||||
func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn func(uint, uint)) {
|
||||
pastLevel := Level(c.XP)
|
||||
c.XP += xp
|
||||
c.Save()
|
||||
newLevel := Level(c.XP)
|
||||
if newLevel > pastLevel {
|
||||
fn(c.XP, newLevel)
|
||||
onNewLevel(s, newLevel)
|
||||
onNewLevel(s, m, newLevel)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue