feat(config): base of guild config

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-15 14:42:08 +02:00
parent 48ca185105
commit beb0ea0ca4
No known key found for this signature in database
GPG key ID: CF4550297832A29F
7 changed files with 238 additions and 13 deletions

42
config/guild.go Normal file
View file

@ -0,0 +1,42 @@
package config
import (
"github.com/anhgelus/gokord"
"gorm.io/gorm"
)
type GuildConfig struct {
gorm.Model
GuildID string `gorm:"not null"`
XpRoles []XpRole
}
type XpRole struct {
gorm.Model
XP uint
RoleID string
GuildConfigID uint
}
func GetGuildConfig(guildID string) *GuildConfig {
cfg := GuildConfig{GuildID: guildID}
return cfg.Load()
}
func (cfg *GuildConfig) Load() *GuildConfig {
gokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg)
return cfg
}
func (cfg *GuildConfig) Save() {
gokord.DB.Save(cfg)
}
func (cfg *GuildConfig) FindXpRole(xp uint, roleID string) (int, *XpRole) {
for i, r := range cfg.XpRoles {
if r.XP == xp && r.RoleID == roleID {
return i, &r
}
}
return 0, nil
}