posting to selected channels, dates not working
This commit is contained in:
parent
96a33f61d7
commit
22d05c4807
71
main.go
71
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
|
// Define a map to associate each desired tag with its corresponding Discord channel ID
|
||||||
var tagChannelMap = map[string]string{
|
var tagChannelMap = map[string]string{
|
||||||
"Patch Notes": "patch-notes-channel-id",
|
"Patch Notes": "556093419341086749",
|
||||||
"Atomic Shop": "atomic-shop-channel-id",
|
"Atomic Shop": "590581442392621067",
|
||||||
"News": "news-channel-id",
|
"News": "558335339018846228",
|
||||||
// Add more tags and corresponding channel IDs as needed
|
// 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) {
|
func sendNotifications(session *discordgo.Session, channelID string, html string) {
|
||||||
// Extract tags from the HTML content
|
// Extract tags from the HTML content
|
||||||
tags := extractTags(html)
|
tags := extractTags(html)
|
||||||
|
|
||||||
// Define the desired tags to filter for
|
// Define today's date string
|
||||||
desiredTags := []string{"Patch Notes", "Atomic Shop", "News"}
|
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 _, tag := range tags {
|
||||||
for _, desiredTag := range desiredTags {
|
// Check if the tag is in the desired tags list
|
||||||
if tag == desiredTag {
|
if tagChannelID, ok := tagChannelMap[tag]; ok {
|
||||||
// If the desired tag is found, send a message to the Discord channel
|
// Check if today's date matches the tag
|
||||||
_, err := session.ChannelMessageSend(channelID, fmt.Sprintf("Found tag: %s", 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 {
|
if err != nil {
|
||||||
fmt.Println("Error sending message to Discord:", err)
|
fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("Message sent to Discord channel")
|
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 {
|
} else {
|
||||||
fmt.Println("Today's date not found on the Fallout news page")
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This function will be called whenever a new message is created
|
// This function will be called whenever a new message is created
|
||||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
// Ignore messages created by the bot itself
|
// Ignore messages created by the bot itself
|
||||||
|
Loading…
Reference in New Issue
Block a user