add config

This commit is contained in:
Fran Jurmanović
2024-09-30 23:08:06 +02:00
parent 86804e04d2
commit f5a7749ad9
9 changed files with 79 additions and 27 deletions

View File

@@ -3,8 +3,11 @@ package job
import (
"context"
"fmt"
"log"
"os"
"rockhu-bot/local/model"
"rockhu-bot/local/service"
"time"
"github.com/bwmarrin/discordgo"
"github.com/robfig/cron/v3"
@@ -30,7 +33,6 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
cron: cron,
discord: dsc,
}
cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate)
return ac
}
@@ -43,13 +45,44 @@ func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.S
// @Success 200 {array} string
// @Router /v1/Concert [get]
func (ac *ConcertJob) checkAndUpdate() {
fmt.Print("Started CheckAndUpdate")
fmt.Println("Started CheckAndUpdate")
ctx := context.Background()
newConcerts := ac.service.CheckAndUpdateConcerts(ctx)
fmt.Print("Finished CheckAndUpdate")
fmt.Println("Finished CheckAndUpdate")
if len(newConcerts) > 0 {
ac.
for _, concert := range newConcerts {
sendMessageToAllChannels(ac.discord, concert)
}
}
}
func sendMessageToAllChannels(s *discordgo.Session, concert model.ConcertModel) {
// Get all guilds (servers) the bot is part of
guilds, err := s.UserGuilds(100, "", "", false)
if err != nil {
log.Printf("Error getting guilds: %v", err)
return
}
// Iterate over all guilds
for _, guild := range guilds {
// Get all channels in the guild
channels, err := s.GuildChannels(guild.ID)
if err != nil {
log.Printf("Error getting channels for guild %s: %v", guild.Name, err)
continue
}
// Iterate over all channels and send a message to text channels
for _, channel := range channels {
if channel.Type == discordgo.ChannelTypeGuildText {
_, err := s.ChannelMessageSendEmbed(channel.ID, &discordgo.MessageEmbed{Title: "New concert", Description: concert.Name, Timestamp: (time.Time)(concert.StartDate).Format("2006-01-02")})
if err != nil {
log.Printf("Error sending message to channel %s: %v", channel.Name, err)
}
}
}
}
}