mirror of
https://github.com/the-programmers-hangout/swiftspiracy-bot.git
synced 2026-09-04 01:06:01 +02:00
add interaction handler to debug build info
This commit is contained in:
@@ -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)))))
|
||||
|
||||
|
||||
+57
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user