add interaction handler to debug build info

This commit is contained in:
Khinshan Khan
2025-04-01 10:24:30 -04:00
parent 52e4fd18aa
commit cb5b812386
2 changed files with 58 additions and 2 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ MAKEFLAGS += --no-builtin-rules
# VARIABLES # VARIABLES
GIT_COMMIT = $(shell git rev-parse --verify HEAD) GIT_COMMIT = $(shell git rev-parse --verify HEAD)
BUILD_DATE = $(shell date +%Y.%m.%d.%H%M%S) BUILD_DATE = $(shell date +%Y.%m.%d.%H%M%S)
GOSERVICES = $(sort $(notdir $(realpath $(dir $(wildcard ./cmd/*/main.go))))) GOSERVICES = $(sort $(notdir $(realpath $(dir $(wildcard ./cmd/*/main.go)))))
+57 -1
View File
@@ -16,6 +16,14 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
var (
// CommitHash and BuildData get replaced during build with the commit hash and time of build.
CommitHash string
BuildDate string
// StartTime is reset every time the application is restarted
StartTime = time.Now()
)
// Embed JSON files at build time // Embed JSON files at build time
//go:embed praises.json //go:embed praises.json
@@ -63,13 +71,31 @@ func main() {
log.Fatalf("[x] Error creating Discord session: %v", err) log.Fatalf("[x] Error creating Discord session: %v", err)
} }
// Open WebSocket connection
dg.AddHandler(readyHandler) dg.AddHandler(readyHandler)
interactionHandlers := make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate))
for _, interaction := range interactions {
interactionHandlers[interaction.command.Name] = interaction.handler
}
dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := interactionHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
// Open WebSocket connection
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
log.Fatalf("[x] Error opening connection: %v", err) log.Fatalf("[x] Error opening connection: %v", err)
} }
defer dg.Close() defer dg.Close()
for _, entry := range interactions {
created, err := dg.ApplicationCommandCreate(dg.State.User.ID, "", entry.command)
if err != nil {
log.Fatalf("Cannot create '%s' command: %v", entry.command.Name, err)
}
interactionHandlers[created.Name] = entry.handler
}
// Start the message scheduler // Start the message scheduler
go startScheduler(dg, channelID) go startScheduler(dg, channelID)
@@ -161,6 +187,36 @@ func readyHandler(s *discordgo.Session, r *discordgo.Ready) {
log.Println("[✓] Swiftspiracy Bot is online and ready!") log.Println("[✓] Swiftspiracy Bot is online and ready!")
} }
var interactions = []struct {
command *discordgo.ApplicationCommand
handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
}{
{
command: &discordgo.ApplicationCommand{
Name: "buildinfo",
Description: "Get the bot's build info and uptime",
},
handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
uptime := time.Since(StartTime).Round(time.Second)
response := fmt.Sprintf(
":tools: Build Info:\n- Commit Hash: `%s`\n- Build Date: `%s`\n- Uptime: `%s`",
CommitHash,
BuildDate,
uptime,
)
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
})
if err != nil {
log.Printf("[x] Failed to respond to interaction: %v", err)
}
},
},
}
// startScheduler handles sending messages at intervals // startScheduler handles sending messages at intervals
func startScheduler(s *discordgo.Session, channelID string) { func startScheduler(s *discordgo.Session, channelID string) {
randomDuration := 0 * time.Second randomDuration := 0 * time.Second