better autocomplete

This commit is contained in:
2026-01-22 22:24:24 -05:00
parent 950662e0b9
commit f3698f0fc6
4 changed files with 33 additions and 14 deletions

22
main.go
View File

@@ -9,7 +9,6 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"time"
"git.sunturtle.xyz/zephyr/horse/horse"
@@ -110,6 +109,13 @@ func main() {
stop()
}
slog.Info("ready")
go func() {
// Preload autocomplete in a separate goroutine.
slog.Info("begin computing skill autocomplete")
set := skillGlobalAuto()
buckets, longest := set.Metrics()
slog.Info("done computing skill autocomplete", slog.Int("buckets", buckets), slog.Int("longest", longest))
}()
<-ctx.Done()
stop()
@@ -162,15 +168,11 @@ func skillHandler(data discord.SlashCommandInteractionData, e *handler.CommandEv
}
func skillAutocomplete(e *handler.AutocompleteEvent) error {
q := strings.ToLower(e.Data.String("query"))
r := make([]discord.AutocompleteChoice, 0, 25)
for k, _ := range global.SkillNameToID {
if strings.HasPrefix(strings.ToLower(k), q) {
r = append(r, discord.AutocompleteChoiceString{Name: k, Value: k})
if len(r) == cap(r) {
break
}
}
q := e.Data.String("query")
opts := skillGlobalAuto().Find(nil, q)
r := make([]discord.AutocompleteChoice, min(len(opts), 25))
for i, k := range opts[:min(len(opts), len(r))] {
r[i] = discord.AutocompleteChoiceString{Name: k, Value: k}
}
return e.AutocompleteResult(r)
}