From 61ef0c1051d65c90ca9badb37e4837a704a4f1ed Mon Sep 17 00:00:00 2001 From: Skip R Date: Tue, 25 Nov 2025 14:25:17 -0800 Subject: [PATCH] connector: limit the amount of private channels initially synced Otherwise, we'll hit the ratelimit pretty easily. --- pkg/connector/client.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index b6e24ba..521186a 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -17,11 +17,13 @@ package connector import ( + "cmp" "context" "errors" "fmt" "io" "net/http" + "slices" "time" "github.com/bwmarrin/discordgo" @@ -172,12 +174,18 @@ func (cl *DiscordClient) BeginSyncingIfUserLoginPresent(ctx context.Context) { log.Err(err).Msg("Couldn't save UserLogin after connecting") } - go cl.syncChannels(ctx) + go cl.syncPrivateChannels(ctx) } -func (d *DiscordClient) syncChannels(ctx context.Context) { - for _, dm := range d.Session.State.PrivateChannels { - d.UserLogin.Log.Debug().Str("channel_id", dm.ID).Msg("Syncing private channel") +func (d *DiscordClient) syncPrivateChannels(ctx context.Context) { + dms := slices.Clone(d.Session.State.PrivateChannels) + // Only sync the top n private channels with recent activity. + slices.SortFunc(dms, func(a, b *discordgo.Channel) int { + return cmp.Compare(b.LastMessageID, a.LastMessageID) + }) + // TODO(skip): This is startup_private_channel_create_limit. Support this in the config. + for _, dm := range dms[:10] { + zerolog.Ctx(ctx).Debug().Str("channel_id", dm.ID).Msg("Syncing private channel with recent activity") d.syncChannel(ctx, dm, true) } }