diff --git a/Botrun/BotStart.go b/Botrun/BotStart.go index de1590a..d3e0571 100644 --- a/Botrun/BotStart.go +++ b/Botrun/BotStart.go @@ -134,7 +134,21 @@ func saveAccount(userID, username, password string) error { if err != nil { return err } - return os.WriteFile(accountsFile, newData, 0644) + if err := os.WriteFile(accountsFile, newData, 0644); err != nil { + return err + } + + // Ensure timetable files exist for this user + timetableFile := getTimetableFile(userID) + timetableFilledFile := getTimetableFilledFile(userID) + if _, err := os.Stat(timetableFile); os.IsNotExist(err) { + os.WriteFile(timetableFile, []byte("[]"), 0644) + } + if _, err := os.Stat(timetableFilledFile); os.IsNotExist(err) { + os.WriteFile(timetableFilledFile, []byte("[]"), 0644) + } + + return nil } // Helper functions for per-user timetable files @@ -194,6 +208,8 @@ func checkAllUsersTimetables(s *discordgo.Session) { } } +var DiscordSession *discordgo.Session + func Start() { token := os.Getenv("DISCORD_BOT_TOKEN") if token == "" { @@ -206,6 +222,7 @@ func Start() { fmt.Println("error creating Discord session,", err) return } + DiscordSession = dg // Save session for use elsewhere dg.AddHandler(messageCreate) err = dg.Open() @@ -231,6 +248,13 @@ func Start() { dg.Close() } +// Expose this for main.go to trigger notifications +func NotifyAllUsers() { + if DiscordSession != nil { + checkAllUsersTimetables(DiscordSession) + } +} + func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Ignore bot's own messages if m.Author.ID == s.State.User.ID { diff --git a/main.go b/main.go index 16dd9ea..23c721d 100644 --- a/main.go +++ b/main.go @@ -103,6 +103,8 @@ func scheduleTimetableUpdate() { sendUpdateDiscordWebhook() prevData = data } + // Trigger bot notifications for all users + BotStart.NotifyAllUsers() } }() @@ -330,35 +332,18 @@ type Field struct { func sendDiscordWebhook(subject string, room string, nextTime string, Status string) { log.Println("Sending Discord webhook notification...") // Create a rich embed message - message := fmt.Sprintf( - "Subject: %s\nRoom: %s\nStart-Time: %s\nStatus: %s", - subject, room, nextTime, Status, - ) - /*embed := Embed{ - Title: "Next Lesson ", - Description: "The next lesson is starting soon: ", - Color: 3066993, // Green color - Timestamp: time.Now().Format(time.RFC3339), - Fields: []Field{ - { - Name: "New Lesson", - // Value: fmt.Sprintf("`%s`", ip), - Value: fmt.Sprintf("Room: %s", room), - Inline: true, - }, - { - Name: "Start-Time", - Value: fmt.Sprintf("Start time: %s", nextTime), - Inline: true, - }, - { - Name: "Status", - Value: fmt.Sprintf("Status: %s", Status), - Inline: true, - }, - }, + var message string + if Status != "" { + message = fmt.Sprintf( + "Subject: %s\nRoom: %s\nStart-Time: %s\nStatus: %s", + subject, room, nextTime, Status, + ) + } else { + message = fmt.Sprintf( + "Subject: %s\nRoom: %s\nStart-Time: %s", + subject, room, nextTime, + ) } - */ payload := DiscordWebhookPayload{ Content: message, }