feat(config): base of guild config
This commit is contained in:
parent
48ca185105
commit
beb0ea0ca4
7 changed files with 238 additions and 13 deletions
42
config/guild.go
Normal file
42
config/guild.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue