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" ) type ConcertJob struct { service *service.ConcertService cron *cron.Cron discord *discordgo.Session } // NewConcertJob // Initializes ApiController. // // Args: // *services.ConcertService: Concert service // *Fiber.RouterGroup: Fiber Router Group // Returns: // *ConcertJob: Controller for "api" interactions func NewConcertJob(as *service.ConcertService, cron *cron.Cron, dsc *discordgo.Session) *ConcertJob { ac := &ConcertJob{ service: as, cron: cron, discord: dsc, } cron.AddFunc(os.Getenv("CONCERT_CRON"), ac.checkAndUpdate) return ac } // checkAndUpdate returns Concert // // @Summary Return Concert // @Description Return Concert // @Tags Concert // @Success 200 {array} string // @Router /v1/Concert [get] func (ac *ConcertJob) checkAndUpdate() { fmt.Println("Started CheckAndUpdate") ctx := context.Background() newConcerts := ac.service.CheckAndUpdateConcerts(ctx) fmt.Println("Finished CheckAndUpdate") if len(newConcerts) > 0 { 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) } } } } }