From 22d05c480777c8844ce276e1b8afe6653bd8777c Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Fri, 26 Apr 2024 22:15:11 -0400 Subject: [PATCH] posting to selected channels, dates not working --- main.go | 69 ++++++++++++++++++++++----------------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/main.go b/main.go index d5f52e1..52f5512 100644 --- a/main.go +++ b/main.go @@ -132,66 +132,49 @@ func extractTags(html string) []string { // Define a map to associate each desired tag with its corresponding Discord channel ID var tagChannelMap = map[string]string{ - "Patch Notes": "patch-notes-channel-id", - "Atomic Shop": "atomic-shop-channel-id", - "News": "news-channel-id", + "Patch Notes": "556093419341086749", + "Atomic Shop": "590581442392621067", + "News": "558335339018846228", // Add more tags and corresponding channel IDs as needed } -// Function to scrape the webpage and send a message if the date matches func sendNotifications(session *discordgo.Session, channelID string, html string) { // Extract tags from the HTML content tags := extractTags(html) - // Define the desired tags to filter for - desiredTags := []string{"Patch Notes", "Atomic Shop", "News"} + // Define today's date string + today := time.Now().Format("January 2, 2006") - // Check if any of the desired tags are present in the extracted tags + // Iterate over extracted tags for _, tag := range tags { - for _, desiredTag := range desiredTags { - if tag == desiredTag { - // If the desired tag is found, send a message to the Discord channel - _, err := session.ChannelMessageSend(channelID, fmt.Sprintf("Found tag: %s", tag)) + // Check if the tag is in the desired tags list + if tagChannelID, ok := tagChannelMap[tag]; ok { + // Check if today's date matches the tag + if tag == today { + // Send a message to the corresponding Discord channel + message := fmt.Sprintf("Today's date matches the tag '%s' on the Fallout news page!", tag) + _, err := session.ChannelMessageSend(tagChannelID, message) if err != nil { - fmt.Println("Error sending message to Discord:", err) - return + fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err) + continue } - fmt.Println("Message sent to Discord channel") + fmt.Println("Message sent to Discord channel:", tagChannelID) + } else { + // Send a different message indicating the tag was found + message := fmt.Sprintf("Tag '%s' found on the Fallout news page, but it's not today's date.", tag) + _, err := session.ChannelMessageSend(tagChannelID, message) + if err != nil { + fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err) + continue + } + fmt.Println("Message sent to Discord channel:", tagChannelID) } } } - - // Parse the HTML content - doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) - if err != nil { - fmt.Println("Error parsing HTML:", err) - return - } - - // Find and extract the date from the webpage - var foundDate bool - doc.Find("span.news-module-feed-item-details-date").Each(func(i int, s *goquery.Selection) { - dateString := strings.TrimSpace(s.Text()) - fmt.Println("Found date:", dateString) - if dateString == time.Now().Format("January 2, 2006") { - foundDate = true - } - }) - - // If today's date was found on the webpage, send a message to the Discord channel - if foundDate { - _, err := session.ChannelMessageSend(channelID, "Today's date was found on the Fallout news page!") - if err != nil { - fmt.Println("Error sending message to Discord:", err) - return - } - fmt.Println("Message sent to Discord channel") - } else { - fmt.Println("Today's date not found on the Fallout news page") - } } + // This function will be called whenever a new message is created func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Ignore messages created by the bot itself