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 --- commands/config.go | 8 +++++--- commands/deploy.go | 2 +- commands/stats.go | 2 +- commands/top.go | 2 +- config/channel.go | 9 +++++++-- config/guild.go | 23 ++++++++++++----------- config/xp_role.go | 17 +++++++++++++---- events.go | 10 +++++----- rolereact/events.go | 2 +- rolereact/manager.go | 8 ++++---- rolereact/rolereact.go | 12 +++++++++--- rolereact/views.go | 8 ++++---- user/member.go | 8 ++++---- user/state.go | 13 +++++++------ user/xp.go | 2 +- 15 files changed, 75 insertions(+), 51 deletions(-) diff --git a/commands/config.go b/commands/config.go index 37aa953..f2dcf60 100644 --- a/commands/config.go +++ b/commands/config.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "slices" + "strconv" "strings" "git.anhgelus.world/anhgelus/les-copaings-bot/config" @@ -20,7 +21,7 @@ const ( OpenConfig = "config" ) -func ConfigResponse(ctx context.Context, guildID string) *interaction.Response { +func ConfigResponse(ctx context.Context, guildID uint64) *interaction.Response { cfg := config.GetGuildConfig(ctx, guildID) roles := "" l := len(cfg.XpRoles) - 1 @@ -41,14 +42,15 @@ func ConfigResponse(ctx context.Context, guildID string) *interaction.Response { var disChansDefault []component.SelectMenuDefaultValue for _, c := range disChans { if c != "" { + v, _ := strconv.ParseUint(c, 10, 64) disChansDefault = append(disChansDefault, component.SelectMenuDefaultValue{ - ID: c, + ID: v, Type: types.SelectMenuDefaultValueChannel, }) } } var defaultChan []component.SelectMenuDefaultValue - if len(cfg.FallbackChannel) > 0 { + if cfg.FallbackChannel != 0 { defaultChan = append(defaultChan, component.SelectMenuDefaultValue{ ID: cfg.FallbackChannel, Type: types.SelectMenuDefaultValueChannel, diff --git a/commands/deploy.go b/commands/deploy.go index 7b039f2..0890cf8 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -68,7 +68,7 @@ func init() { } func Deploy(ctx context.Context, dg bot.Session) error { - guildID := "" + var guildID uint64 if common.IsDebug(ctx) { guildID = dg.GuildState().ListGuilds()[0] bot.Logger(ctx).Debug("using guild as debug", "guild", guildID) diff --git a/commands/stats.go b/commands/stats.go index 2cc0b3e..15f258b 100644 --- a/commands/stats.go +++ b/commands/stats.go @@ -118,7 +118,7 @@ func statsAll(ctx context.Context, dg bot.Session, i *interaction.ApplicationCom }) } -func statsMember(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand, days int, discordID string) (io.WriterTo, error) { +func statsMember(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand, days int, discordID uint64) (io.WriterTo, error) { _, err := guild.GetMember(i.GuildID, discordID).Do(ctx) if err != nil { return nil, err diff --git a/commands/top.go b/commands/top.go index aa0c288..82183a1 100644 --- a/commands/top.go +++ b/commands/top.go @@ -59,7 +59,7 @@ func Top(ctx context.Context, dg bot.Session, i *interaction.ApplicationCommand) func genTopsMessage(tops []user.CopaingCached) string { msg := "" for i, c := range tops { - msg += fmt.Sprintf("%d. **<@%s>** - niveau %d", i+1, c.DiscordID, exp.Level(c.XP)) + msg += fmt.Sprintf("%d. **<@%d>** - niveau %d", i+1, c.DiscordID, exp.Level(c.XP)) if i != len(tops)-1 { msg += "\n" } 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{ diff --git a/events.go b/events.go index a9a14a7..855e498 100644 --- a/events.go +++ b/events.go @@ -54,12 +54,12 @@ func OnVoiceUpdate(ctx context.Context, dg bot.Session, e *event.VoiceStateUpdat } cfg := config.GetGuildConfig(ctx, e.GuildID) dis := cfg.IsDisabled(ctx, dg, e.BeforeUpdate.ChannelID) - if (e.BeforeUpdate == nil || dis) && e.ChannelID != "" { + if (e.BeforeUpdate == nil || dis) && e.ChannelID != 0 { if dis { return } onConnection(ctx, dg, e) - } else if e.BeforeUpdate != nil && (e.ChannelID == "" || dis) { + } else if e.BeforeUpdate != nil && (e.ChannelID == 0 || dis) { if dis { return } @@ -67,8 +67,8 @@ func OnVoiceUpdate(ctx context.Context, dg bot.Session, e *event.VoiceStateUpdat } } -func genMapKey(guildID string, userID string) string { - return fmt.Sprintf("%s:%s", guildID, userID) +func genMapKey(guildID, userID uint64) string { + return fmt.Sprintf("%d:%d", guildID, userID) } func onConnection(ctx context.Context, _ bot.Session, e *event.VoiceStateUpdate) { @@ -100,7 +100,7 @@ func onDisconnect(ctx context.Context, dg bot.Session, e *event.VoiceStateUpdate e.Member.GuildID = e.GuildID cc.AddXP(ctx, dg, e.Member, exp.VocalXP(uint(timeInVocal)), func(_ uint, newLevel uint) { cfg := config.GetGuildConfig(ctx, e.GuildID) - if len(cfg.FallbackChannel) == 0 { + if cfg.FallbackChannel == 0 { return } _, err := channel.SendMessage(cfg.FallbackChannel, fmt.Sprintf( diff --git a/rolereact/events.go b/rolereact/events.go index 537b6dd..ac5ad60 100644 --- a/rolereact/events.go +++ b/rolereact/events.go @@ -11,7 +11,7 @@ import ( ) type RoleReact struct { - RoleID string + RoleID uint64 } func HandleReactionAdd(ctx context.Context, dg bot.Session, e *event.MessageReactionAdd) { diff --git a/rolereact/manager.go b/rolereact/manager.go index 84081e5..91e1bcd 100644 --- a/rolereact/manager.go +++ b/rolereact/manager.go @@ -22,7 +22,7 @@ func MessageContent(message *config.RoleReactMessage) string { content = fmt.Sprintf("%s\n%s", content, message.Note) } for _, role := range message.Roles { - if role.Reaction != "" && role.RoleID != "" { + if role.Reaction != "" && role.RoleID != 0 { content += fmt.Sprintf("\n> -# %s <@&%s>", FormatEmoji(role.Reaction), role.RoleID) } } @@ -45,7 +45,7 @@ func ApplyMessageChange(ctx context.Context, dg bot.Session, i *interaction.Inte return "Impossible de mettre à jour le message." } for _, role := range message.Roles { - if role.Reaction != "" && role.RoleID != "" && err == nil { + if role.Reaction != "" && role.RoleID != 0 && err == nil { err = channel.AddReaction(message.ChannelID, message.MessageID, role.Reaction).Do(ctx) } } @@ -91,7 +91,7 @@ func ApplyMessageChange(ctx context.Context, dg bot.Session, i *interaction.Inte return "Message de réaction mis à jour avec succès !" } -func WaitForEmoji(ctx context.Context, dg bot.Session, userID string, messageID string) (string, bool) { +func WaitForEmoji(ctx context.Context, dg bot.Session, userID, messageID uint64) (string, bool) { ctx, cancel := context.WithTimeout(ctx, 1*time.Minute) defer cancel() @@ -122,7 +122,7 @@ func GetMessageFromEditID(ctx context.Context, i *interaction.Interaction, editI return m, true } -func GetGuildConfigPreloaded(ctx context.Context, guildID string) *config.Guild { +func GetGuildConfigPreloaded(ctx context.Context, guildID uint64) *config.Guild { cfg := config.Guild{GuildID: guildID} // err := oldGokord.DB.Where("guild_id = ?", cfg.GuildID).Preload("XpRoles").Preload("RrMessages.Roles").FirstOrCreate(cfg).Error err := common.GetDB(ctx).Where("guild_id = ?", cfg.GuildID).Preload("RrMessages.Roles").FirstOrCreate(&cfg).Error diff --git a/rolereact/rolereact.go b/rolereact/rolereact.go index dce4e81..44aad2a 100644 --- a/rolereact/rolereact.go +++ b/rolereact/rolereact.go @@ -3,6 +3,7 @@ package rolereact import ( "context" "slices" + "strconv" "git.anhgelus.world/anhgelus/les-copaings-bot/config" "git.anhgelus.world/anhgelus/les-copaings-bot/dynamicid" @@ -54,9 +55,9 @@ func HandleCommand(ctx context.Context, dg bot.Session, i *interaction.Applicati } o := i.OptionMap() c := o["salon"] - var channelID string + var channelID uint64 if c != nil { - channelID = c.Value.(string) + channelID = c.ChannelValue(ctx, dg.ChannelState()).ID } else { channelID = i.ChannelID } @@ -362,7 +363,12 @@ func HandleSetRole(ctx context.Context, dg bot.Session, i *interaction.MessageCo }, } } else { - role.RoleID = i.Data.Values[0] + var err error + role.RoleID, err = strconv.ParseUint(i.Data.Values[0], 10, 64) + if err != nil { + // panic because must ensure before that the value is valid + panic(err) + } responseData = MessageModifyRoleData(ctx, i.Interaction, params, "") } err := interaction.Respond(i.Interaction, &interaction.Response{ diff --git a/rolereact/views.go b/rolereact/views.go index 47aa4f6..72a1ea0 100644 --- a/rolereact/views.go +++ b/rolereact/views.go @@ -47,8 +47,8 @@ func MessageModifyComponents(ctx context.Context, i *interaction.Interaction, pa reaction = ":no_entry_sign:" } var roleMention string - if role.RoleID != "" { - roleMention = fmt.Sprintf("<@&%s>", role.RoleID) + if role.RoleID != 0 { + roleMention = fmt.Sprintf("<@&%d>", role.RoleID) } else { roleMention = "*Pas de rôle sélectionné*" } @@ -147,13 +147,13 @@ func MessageModifyRoleComponents(ctx context.Context, i *interaction.Interaction } reactionButton.CustomID = dynamicid.FormatCustomID(SetRoleReaction, *params) defaultRoleValues := make([]component.SelectMenuDefaultValue, 0) - if role.RoleID != "" { + if role.RoleID != 0 { defaultRoleValues = append(defaultRoleValues, component.SelectMenuDefaultValue{ Type: types.SelectMenuDefaultValueRole, ID: role.RoleID, }) } - disableBack = disableBack || (role.RoleID == "") + disableBack = disableBack || (role.RoleID == 0) one := 1 components := []component.Message{ &component.TextDisplay{Content: "## Modifier un message de réaction"}, diff --git a/user/member.go b/user/member.go index e3a1d30..db39911 100644 --- a/user/member.go +++ b/user/member.go @@ -9,16 +9,16 @@ import ( type Copaing struct { ID uint `gorm:"primarykey"` - DiscordID string `gorm:"not null"` + DiscordID uint64 `gorm:"not null"` CopaingXPs []CopaingXP `gorm:"constraint:OnDelete:SET NULL;"` - GuildID string `gorm:"not null"` + GuildID uint64 `gorm:"not null"` } type CopaingXP struct { ID uint `gorm:"primarykey"` XP uint `gorm:"default:0"` CopaingID uint - GuildID string `gorm:"not null;"` + GuildID uint64 `gorm:"not null;"` CreatedAt time.Time } @@ -27,7 +27,7 @@ type CopaingAccess interface { GetXP() uint } -func GetCopaing(ctx context.Context, discordID string, guildID string) *CopaingCached { +func GetCopaing(ctx context.Context, discordID, guildID uint64) *CopaingCached { state := GetState(ctx) cc, err := state.Copaing(guildID, discordID) if err != nil { diff --git a/user/state.go b/user/state.go index d1f84b5..9f1c90e 100644 --- a/user/state.go +++ b/user/state.go @@ -3,6 +3,7 @@ package user import ( "context" "errors" + "fmt" "math" "sync" "time" @@ -22,8 +23,8 @@ type XPCached struct { type CopaingCached struct { ID uint - DiscordID string - GuildID string + DiscordID uint64 + GuildID uint64 XP uint XPs []XPCached XPToAdd uint @@ -130,8 +131,8 @@ func KeyCopaingCached(c *Copaing) string { return KeyCopaingCachedRaw(c.GuildID, c.DiscordID) } -func KeyCopaingCachedRaw(guildID, copaingID string) string { - return guildID + ":" + copaingID +func KeyCopaingCachedRaw(guildID, copaingID uint64) string { + return fmt.Sprintf("%d:%d", guildID, copaingID) } type State struct { @@ -176,7 +177,7 @@ func deepCopy(src CopaingCached) CopaingCached { return res } -func (s *State) Copaing(guildID, copaingID string) (*CopaingCached, error) { +func (s *State) Copaing(guildID, copaingID uint64) (*CopaingCached, error) { s.mu.RLock() defer s.mu.RUnlock() @@ -187,7 +188,7 @@ func (s *State) Copaing(guildID, copaingID string) (*CopaingCached, error) { return &c, nil } -func (s *State) Copaings(guild string) []CopaingCached { +func (s *State) Copaings(guild uint64) []CopaingCached { s.mu.RLock() defer s.mu.RUnlock() diff --git a/user/xp.go b/user/xp.go index d2b7ca6..dad7865 100644 --- a/user/xp.go +++ b/user/xp.go @@ -54,7 +54,7 @@ func (cc *CopaingCached) GetXPForDays(d int) uint { } // GetBestXP returns n Copaings with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain) -func GetBestXP(ctx context.Context, guildId string, n uint, d int) []CopaingCached { +func GetBestXP(ctx context.Context, guildId uint64, n uint, d int) []CopaingCached { ccs := GetState(ctx).Copaings(guildId) if d > 0 { for i, cc := range ccs { -- cgit v1.2.3