aboutsummaryrefslogtreecommitdiff
path: root/config/guild.go
blob: dc8251fb050efa08cca1f104501055266f73c083 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package config

import (
	"context"
	"fmt"
	"strings"

	"github.com/nyttikord/gokord/bot"
	"github.com/nyttikord/gokord/channel"
)

type Guild struct {
	ID               uint     `gorm:"primarykey"`
	GuildID          uint64   `gorm:"not null;unique"`
	XpRoles          []XpRole `gorm:"foreignKey:GuildConfigID"`
	DisabledChannels string
	FallbackChannel  uint64
	DaysXPRemains    uint `gorm:"default:90"` // 30 * 3 = 90 (three months)
	RrMessages       []RoleReactMessage
}

type RoleReactMessage struct {
	ID            uint   `gorm:"primarykey"`
	MessageID     uint64 `gorm:"not null;unique"`
	ChannelID     uint64
	GuildID       uint64
	Note          string
	Roles         []*RoleReact
	GuildConfigID uint
}

type RoleReact struct {
	ID                 uint `gorm:"primarykey"`
	Reaction           string
	RoleID             uint64
	RoleReactMessageID uint
	CounterID          uint `gorm:"-"`
}

func GetGuildConfig(ctx context.Context, guildID uint64) *Guild {
	cfg := Guild{GuildID: guildID}
	if err := cfg.Load(ctx); err != nil {
		panic(err)
	}
	return &cfg
}

func (cfg *Guild) Load(ctx context.Context) error {
	return nil //common.GetDB(ctx).Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").FirstOrCreate(cfg).Error
}

func (cfg *Guild) Save(ctx context.Context) error {
	return nil //common.GetDB(ctx).Save(cfg).Error
}

func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID uint64) bool {
	ok := true
	for channelID != 0 && ok {
		ok = !strings.Contains(cfg.DisabledChannels, fmt.Sprintf("%d", channelID))
		c, err := dg.ChannelState().GetChannel(channelID)
		if err != nil {
			bot.Logger(ctx).Error("unable to find channel %s in state", "error", err, "channel", c)
			c, err = channel.Get(channelID).Do(ctx)
			if err != nil {
				bot.Logger(ctx).Error("unable to fetch channel", "error", err, "channel", c)
				return false
			}
		}
		if err != nil {
			return false
		}
		channelID = c.ParentID
	}
	return !ok
}

func (cfg *Guild) FindXpRole(roleID uint64) (int, *XpRole) {
	for i, r := range cfg.XpRoles {
		if r.RoleID == roleID {
			return i, &r
		}
	}
	return 0, nil
}

func (cfg *Guild) FindXpRoleID(ID uint) (int, *XpRole) {
	for i, r := range cfg.XpRoles {
		if r.ID == ID {
			return i, &r
		}
	}
	return -1, nil
}