updated room discord message
This commit is contained in:
parent
d601ccc789
commit
87887832c8
2 changed files with 38 additions and 29 deletions
|
|
@ -134,7 +134,21 @@ func saveAccount(userID, username, password string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// Helper functions for per-user timetable files
|
||||||
|
|
@ -194,6 +208,8 @@ func checkAllUsersTimetables(s *discordgo.Session) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var DiscordSession *discordgo.Session
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
token := os.Getenv("DISCORD_BOT_TOKEN")
|
token := os.Getenv("DISCORD_BOT_TOKEN")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
|
|
@ -206,6 +222,7 @@ func Start() {
|
||||||
fmt.Println("error creating Discord session,", err)
|
fmt.Println("error creating Discord session,", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
DiscordSession = dg // Save session for use elsewhere
|
||||||
|
|
||||||
dg.AddHandler(messageCreate)
|
dg.AddHandler(messageCreate)
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
|
|
@ -231,6 +248,13 @@ func Start() {
|
||||||
dg.Close()
|
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) {
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
// Ignore bot's own messages
|
// Ignore bot's own messages
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
|
|
||||||
35
main.go
35
main.go
|
|
@ -103,6 +103,8 @@ func scheduleTimetableUpdate() {
|
||||||
sendUpdateDiscordWebhook()
|
sendUpdateDiscordWebhook()
|
||||||
prevData = data
|
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) {
|
func sendDiscordWebhook(subject string, room string, nextTime string, Status string) {
|
||||||
log.Println("Sending Discord webhook notification...")
|
log.Println("Sending Discord webhook notification...")
|
||||||
// Create a rich embed message
|
// Create a rich embed message
|
||||||
message := fmt.Sprintf(
|
var message string
|
||||||
|
if Status != "" {
|
||||||
|
message = fmt.Sprintf(
|
||||||
"Subject: %s\nRoom: %s\nStart-Time: %s\nStatus: %s",
|
"Subject: %s\nRoom: %s\nStart-Time: %s\nStatus: %s",
|
||||||
subject, room, nextTime, Status,
|
subject, room, nextTime, Status,
|
||||||
)
|
)
|
||||||
/*embed := Embed{
|
} else {
|
||||||
Title: "Next Lesson ",
|
message = fmt.Sprintf(
|
||||||
Description: "The next lesson is starting soon: ",
|
"Subject: %s\nRoom: %s\nStart-Time: %s",
|
||||||
Color: 3066993, // Green color
|
subject, room, nextTime,
|
||||||
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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
payload := DiscordWebhookPayload{
|
payload := DiscordWebhookPayload{
|
||||||
Content: message,
|
Content: message,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue