aboutsummaryrefslogtreecommitdiff
path: root/config/guild.go
blob: ddeb3000e154ea46f6d189652416f5eb3257512c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
}