From 89b23632f5ceeebd82132210c1407dc9514a547b Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Sat, 7 Mar 2026 13:20:56 +0100 Subject: feat(gokord): replace snowflake by uint --- config/channel.go | 9 +++++++-- config/guild.go | 23 ++++++++++++----------- config/xp_role.go | 17 +++++++++++++---- 3 files changed, 32 insertions(+), 17 deletions(-) (limited to 'config') diff --git a/config/channel.go b/config/channel.go index 4650e15..5cc88aa 100644 --- a/config/channel.go +++ b/config/channel.go @@ -2,6 +2,7 @@ package config import ( "context" + "strconv" "strings" "github.com/nyttikord/gokord/bot" @@ -21,9 +22,13 @@ const ( func HandleModifyFallbackChannel(ctx context.Context, dg bot.Session, i *interaction.MessageComponent) bool { cfg := GetGuildConfig(ctx, i.GuildID) - var channelID string + var channelID uint64 if len(i.Data.Values) > 0 { - channelID = i.Data.Values[0] + var err error + channelID, err = strconv.ParseUint(i.Data.Values[0], 10, 64) + if err != nil { + panic(err) + } } cfg.FallbackChannel = channelID err := cfg.Save(ctx) diff --git a/config/guild.go b/config/guild.go index 6c46a1a..bcebd65 100644 --- a/config/guild.go +++ b/config/guild.go @@ -2,6 +2,7 @@ package config import ( "context" + "fmt" "strings" "git.anhgelus.world/anhgelus/les-copaings-bot/common" @@ -11,19 +12,19 @@ import ( type Guild struct { ID uint `gorm:"primarykey"` - GuildID string `gorm:"not null;unique"` + GuildID uint64 `gorm:"not null;unique"` XpRoles []XpRole `gorm:"foreignKey:GuildConfigID"` DisabledChannels string - FallbackChannel string + FallbackChannel uint64 DaysXPRemains uint `gorm:"default:90"` // 30 * 3 = 90 (three months) RrMessages []RoleReactMessage } type RoleReactMessage struct { ID uint `gorm:"primarykey"` - MessageID string `gorm:"not null;unique"` - ChannelID string - GuildID string + MessageID uint64 `gorm:"not null;unique"` + ChannelID uint64 + GuildID uint64 Note string Roles []*RoleReact GuildConfigID uint @@ -32,12 +33,12 @@ type RoleReactMessage struct { type RoleReact struct { ID uint `gorm:"primarykey"` Reaction string - RoleID string + RoleID uint64 RoleReactMessageID uint CounterID uint `gorm:"-"` } -func GetGuildConfig(ctx context.Context, guildID string) *Guild { +func GetGuildConfig(ctx context.Context, guildID uint64) *Guild { cfg := Guild{GuildID: guildID} if err := cfg.Load(ctx); err != nil { panic(err) @@ -53,10 +54,10 @@ func (cfg *Guild) Save(ctx context.Context) error { return common.GetDB(ctx).Save(cfg).Error } -func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID string) bool { +func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID uint64) bool { ok := true - for channelID != "" && ok { - ok = !strings.Contains(cfg.DisabledChannels, channelID) + 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) @@ -74,7 +75,7 @@ func (cfg *Guild) IsDisabled(ctx context.Context, dg bot.Session, channelID stri return !ok } -func (cfg *Guild) FindXpRole(roleID string) (int, *XpRole) { +func (cfg *Guild) FindXpRole(roleID uint64) (int, *XpRole) { for i, r := range cfg.XpRoles { if r.RoleID == roleID { return i, &r diff --git a/config/xp_role.go b/config/xp_role.go index e00d3f7..6592ac9 100644 --- a/config/xp_role.go +++ b/config/xp_role.go @@ -19,7 +19,7 @@ import ( type XpRole struct { ID uint `gorm:"primarykey"` XP uint - RoleID string + RoleID uint64 GuildConfigID uint } @@ -189,7 +189,11 @@ func HandleXpRoleEdit(ctx context.Context, dg bot.Session, i *interaction.Intera func HandleXpRoleEditRole(ctx context.Context, dg bot.Session, i *interaction.MessageComponent, params *XpRoleId) { id := params.ID - role := i.Data.Values[0] + role, err := strconv.ParseUint(i.Data.Values[0], 10, 64) + if err != nil { + // panic because must ensure that the role is valid before + panic(err) + } cfg := GetGuildConfig(ctx, i.GuildID) _, xpRole := cfg.FindXpRoleID(id) if xpRole == nil { @@ -206,7 +210,7 @@ func HandleXpRoleEditRole(ctx context.Context, dg bot.Session, i *interaction.Me return } xpRole.RoleID = role - err := common.GetDB(ctx).Save(xpRole).Error + err = common.GetDB(ctx).Save(xpRole).Error if err != nil { bot.Logger(ctx).Error("saving config", "error", err, "guild", i.GuildID, "id", id, "type", "add") } @@ -340,7 +344,12 @@ func HandleXpRoleAdd(ctx context.Context, dg bot.Session, i *interaction.ModalSu } xp := exp.LevelXP(uint(in)) - roleId := i.Data.Components[1].(*component.Label).Component.(*component.SelectMenu).Values[0] + rawRoleId := i.Data.Components[1].(*component.Label).Component.(*component.SelectMenu).Values[0] + roleId, err := strconv.ParseUint(rawRoleId, 10, 64) + if err != nil { + // panic because select menu must ensure that the value is valid + panic(err) + } cfg := GetGuildConfig(ctx, i.GuildID) cfg.XpRoles = append(cfg.XpRoles, XpRole{ -- cgit v1.2.3