optimize discord command registration for faster startup and shutdown

new-sql-view
William Perron 11 months ago
parent c1d5e472f2
commit 3d670c420d
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -37,6 +37,9 @@ var (
type Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) type Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
func main() { func main() {
log.Info().Msg("startup.")
start := time.Now()
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
defer cancel() defer cancel()
@ -47,8 +50,7 @@ func main() {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
zerolog.DurationFieldUnit = time.Millisecond
log.Info().Msg("startup.")
go func() { go func() {
if err := serve(":8080"); err != nil { if err := serve(":8080"); err != nil {
@ -632,19 +634,12 @@ func main() {
} }
defer discord.Close() defer discord.Close()
total := len(commands) log.Debug().Int("count", len(commands)).Msg("registering commands via bulk overwrite")
registeredCommands := make([]*discordgo.ApplicationCommand, total) created, err := discord.ApplicationCommandBulkOverwrite(appId, guildId, commands)
log.Debug().Int("total", total).Msg("registering commands with Discord") if err != nil {
for i, c := range commands { log.Fatal().Err(err).Msg("failed to register commands with discord")
log.Debug().Msg(fmt.Sprintf("registering command %d of %d", i+1, total))
command, err := discord.ApplicationCommandCreate(appId, guildId, c)
if err != nil {
log.Fatal().Err(err).Msg("failed to register command")
}
registeredCommands[i] = command
} }
log.Info().Int("count", len(created)).Dur("startup_latency_ms", time.Since(start)).Msg("registered commands, ready to operate")
log.Info().Int("count", len(registeredCommands)).Msg("registered commands")
go notifier.NotifyFunc(ctx, func() { go notifier.NotifyFunc(ctx, func() {
log.Info().Msg("sending weekly reminder") log.Info().Msg("sending weekly reminder")
@ -674,16 +669,6 @@ func main() {
<-ctx.Done() <-ctx.Done()
log.Info().Msg("context cancelled, exiting") log.Info().Msg("context cancelled, exiting")
log.Debug().Int("total", total).Msg("deregistering commands with Discord")
for i, c := range registeredCommands {
log.Debug().Msg(fmt.Sprintf("registering command %d of %d", i+1, total))
err = discord.ApplicationCommandDelete(appId, guildId, c.ID)
if err != nil {
log.Error().Err(err).Msg("failed to deregister commands")
}
}
log.Info().Msg("deregistered commands, exiting")
os.Exit(0) os.Exit(0)
} }

Loading…
Cancel
Save