From 108102c41e6c7999acea1ff52b93eee12aa796a0 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Dec 2023 01:27:49 -0600 Subject: [PATCH] Defer idle connection closing, seed random location selection with user ID & moving interval --- main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ac969fd..3608589 100644 --- a/main.go +++ b/main.go @@ -76,10 +76,12 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I var choices []*discordgo.ApplicationCommandOptionChoice if data.Options[0].Focused { - locations := GetLocations() - log.Printf("%d location%s provided", len(locations), Plural(len(locations))) + // Seed value is based on the user ID + a 15 minute interval) + user_id, _ := strconv.Atoi(interaction.Member.User.ID) + seed_value := int64(user_id) + (time.Now().Unix() / 15 * 60) + locations := FilterLocations(GetLocations(), data.Options[0].StringValue(), 25, seed_value) - choices = make([]*discordgo.ApplicationCommandOptionChoice, min(len(locations), 25)) + choices = make([]*discordgo.ApplicationCommandOptionChoice, len(locations)) for i, location := range locations { choices[i] = &discordgo.ApplicationCommandOptionChoice{ Name: location.name, @@ -154,6 +156,8 @@ func main() { if err != nil { log.Fatalf("Cannot open the session: %v", err) } + defer session.Close() + defer client.CloseIdleConnections() session.AddHandler(func(internalSession *discordgo.Session, interaction *discordgo.InteractionCreate) { if handler, ok := commandHandlers[interaction.ApplicationCommandData().Name]; ok { @@ -171,8 +175,6 @@ func main() { registeredCommands[definitionIndex] = command } - defer session.Close() - tryReload("") stop := make(chan os.Signal, 1)