From 933c1c6892f704b0412f5a098b74d29b5f1e5a68 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Dec 2023 02:07:49 -0600 Subject: [PATCH] Embed commit id into footer at compile time, use switch for autocomplete focus flow --- commands.go | 17 +++++++++++------ helpers.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/commands.go b/commands.go index b4fea9d..c0daeca 100644 --- a/commands.go +++ b/commands.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "strconv" "time" @@ -49,7 +48,7 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I Embeds: []*discordgo.MessageEmbed{ { Footer: &discordgo.MessageEmbedFooter{ - Text: fmt.Sprintf("Fetched at %s", time.Now().Format("Monday, January 2, 2006 at 3:04:05PM")), + Text: GetFooterText(), }, Description: "", Fields: []*discordgo.MessageEmbedField{}, @@ -60,15 +59,20 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I }) case discordgo.InteractionApplicationCommandAutocomplete: data := interaction.ApplicationCommandData() - var choices []*discordgo.ApplicationCommandOptionChoice - if data.Options[0].Focused { + LocationOption := data.Options[0] + MakeOption := data.Options[1] + ModelOption := data.Options[2] + + switch { + case LocationOption.Focused: // 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) + // Convert the locations to choices choices = make([]*discordgo.ApplicationCommandOptionChoice, len(locations)) for i, location := range locations { choices[i] = &discordgo.ApplicationCommandOptionChoice{ @@ -76,8 +80,9 @@ func RegisterCommandHandler(session *discordgo.Session, interaction *discordgo.I Value: strconv.Itoa(int(location.id)), } } - } else { - choices = []*discordgo.ApplicationCommandOptionChoice{} + case MakeOption.Focused: + + case ModelOption.Focused: } err := session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{ diff --git a/helpers.go b/helpers.go index 0ab96e3..acdb231 100644 --- a/helpers.go +++ b/helpers.go @@ -7,7 +7,9 @@ import ( "math/rand" "net/http" "reflect" + "runtime/debug" "strings" + "time" ) const baseURL = "https://www.register2park.com" @@ -138,3 +140,22 @@ func FilterLocations(all_locations []Location, query string, limit int, seed_val return filtered_locations } + +// The commit ID from the Git repository. Only valid at build time, but is compiled into the binary. +var CommitId = func() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + + return strings.Repeat("gubed", 8) // 40 characters +}() + +func GetFooterText() string { + return fmt.Sprintf("Fetched at %s @%s", + time.Now().Format("Monday, January 2, 2006 at 3:04:05PM"), + strings.ToLower(CommitId[:7])) +}