diff --git a/Makefile b/Makefile index 30129aa..da96068 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ MAKEFLAGS += --no-builtin-rules # VARIABLES 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))))) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index a378c07..4bc5b26 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -16,6 +16,14 @@ import ( "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 //go:embed praises.json @@ -63,13 +71,31 @@ func main() { log.Fatalf("[x] Error creating Discord session: %v", err) } - // Open WebSocket connection 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 { log.Fatalf("[x] Error opening connection: %v", err) } 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 go startScheduler(dg, channelID) @@ -161,6 +187,36 @@ func readyHandler(s *discordgo.Session, r *discordgo.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 func startScheduler(s *discordgo.Session, channelID string) { randomDuration := 0 * time.Second