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
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue