posting to selected channels, dates not working

This commit is contained in:
Chris kerr 2024-04-26 22:15:11 -04:00
parent 96a33f61d7
commit 22d05c4807

69
main.go
View File

@ -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