Compare commits

...

3 Commits

Author SHA1 Message Date
50513cf3cd new message 2024-04-26 22:30:28 -04:00
22d05c4807 posting to selected channels, dates not working 2024-04-26 22:15:11 -04:00
96a33f61d7 scraping desired tags 2024-04-26 21:59:05 -04:00

88
main.go
View File

@ -60,6 +60,8 @@ func main() {
// Register messageCreate as a callback for the messageCreate events // Register messageCreate as a callback for the messageCreate events
dg.AddHandler(messageCreate) dg.AddHandler(messageCreate)
dg.AddHandler(message2Create)
// Open a websocket connection to Discord and begin listening // Open a websocket connection to Discord and begin listening
err = dg.Open() err = dg.Open()
if err != nil { if err != nil {
@ -114,35 +116,62 @@ func scrapeNews() string {
return html return html
} }
// Function to scrape the webpage and send a message if the date matches // Add a function to extract relevant tags from the HTML content
func sendNotifications(session *discordgo.Session, channelID string, html string) { func extractTags(html string) []string {
// Parse the HTML content var tags []string
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil { if err != nil {
fmt.Println("Error parsing HTML:", err) fmt.Println("Error parsing HTML:", err)
return return tags
}
// 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
} }
// Find and extract tags with class name "news-module-feed-item-details-tag"
doc.Find("span.news-module-feed-item-details-tag").Each(func(i int, s *goquery.Selection) {
tag := strings.TrimSpace(s.Text())
tags = append(tags, tag)
}) })
return tags
// 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")
// Define a map to associate each desired tag with its corresponding Discord channel ID
var tagChannelMap = map[string]string{
"Patch Notes": "556093419341086749",
"Atomic Shop": "590581442392621067",
"News": "558335339018846228",
// Add more tags and corresponding channel IDs as needed
}
func sendNotifications(session *discordgo.Session, channelID string, html string) {
// Extract tags from the HTML content
tags := extractTags(html)
// Define today's date string
today := time.Now().Format("January 2, 2006")
// Iterate over extracted tags
for _, tag := range tags {
// 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.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err)
continue
}
fmt.Println("Message sent to Discord channel:", tagChannelID)
} 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)
}
}
} }
} }
@ -154,7 +183,20 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
// If the message content is "!hello", respond with "Hello!" // If the message content is "!hello", respond with "Hello!"
if m.Content == "!hello" { if m.Content == "!hello1" {
_, _ = s.ChannelMessageSend(m.ChannelID, "Hello!") _, _ = s.ChannelMessageSend(m.ChannelID, "Hello!")
} }
} }
// This function will be called whenever a new message is created
func message2Create(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// If the message content is "!hello", respond with "Hello!"
if m.Content == "!hello" {
_, _ = s.ChannelMessageSend(m.ChannelID, "You are SUCH a NERD")
}
}