tagging role, embedding message content
This commit is contained in:
parent
42d5fc9dbe
commit
075bdb2782
@ -4,5 +4,6 @@
|
|||||||
"Patch Notes": "556093419341086749",
|
"Patch Notes": "556093419341086749",
|
||||||
"Atomic Shop": "590581442392621067",
|
"Atomic Shop": "590581442392621067",
|
||||||
"News": "558335339018846228"
|
"News": "558335339018846228"
|
||||||
}
|
},
|
||||||
|
"url": "https://fallout.bethesda.net/en/news"
|
||||||
}
|
}
|
||||||
|
129
main.go
129
main.go
@ -17,10 +17,15 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
DiscordBotToken string `json:"discord_bot_token"`
|
DiscordBotToken string `json:"discord_bot_token"`
|
||||||
ChannelMap map[string]string `json:"channel_map"` // Add the channel map to the config
|
ChannelMap map[string]string `json:"channel_map"` // Add the channel map to the config
|
||||||
|
URL string `json:"url"` // Add URL field to the config
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
//Initialize Time
|
||||||
|
date := "April 26, 2024"
|
||||||
|
//date := time.Now().Format("January 2, 2006")
|
||||||
|
|
||||||
// At the beginning of the main function
|
// At the beginning of the main function
|
||||||
log.Println("Starting the bot...")
|
log.Println("Starting the bot...")
|
||||||
|
|
||||||
@ -53,6 +58,12 @@ func main() {
|
|||||||
// Access the channel map from the configuration
|
// Access the channel map from the configuration
|
||||||
tagChannelMap := config.ChannelMap
|
tagChannelMap := config.ChannelMap
|
||||||
|
|
||||||
|
url := config.URL
|
||||||
|
if url == "" {
|
||||||
|
fmt.Println("URL is not set in config file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new Discord session using the provided bot token
|
// Create a new Discord session using the provided bot token
|
||||||
dg, err := discordgo.New("Bot " + token)
|
dg, err := discordgo.New("Bot " + token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,8 +86,8 @@ func main() {
|
|||||||
// After opening the Discord connection
|
// After opening the Discord connection
|
||||||
log.Println("Opened Discord connection")
|
log.Println("Opened Discord connection")
|
||||||
|
|
||||||
// Run the scraping and message sending function once at the specified time
|
// Run the scraping and message sending function at start up
|
||||||
sendNotifications(dg, scrapeNews(), tagChannelMap)
|
sendNotifications(dg, fetchUrl(url), tagChannelMap, date)
|
||||||
|
|
||||||
// Schedule the scraping and message sending function to run once a day
|
// Schedule the scraping and message sending function to run once a day
|
||||||
ticker := time.NewTicker(24 * time.Hour)
|
ticker := time.NewTicker(24 * time.Hour)
|
||||||
@ -87,7 +98,7 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
sendNotifications(dg, scrapeNews(), tagChannelMap)
|
sendNotifications(dg, fetchUrl(url), tagChannelMap, date)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -97,7 +108,7 @@ func main() {
|
|||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func scrapeNews() string {
|
func fetchUrl(url string) string {
|
||||||
// Create a new context
|
// Create a new context
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, cancel := chromedp.NewContext(
|
ctx, cancel := chromedp.NewContext(
|
||||||
@ -109,7 +120,7 @@ func scrapeNews() string {
|
|||||||
// Navigate to the Fallout news page
|
// Navigate to the Fallout news page
|
||||||
var html string
|
var html string
|
||||||
err := chromedp.Run(ctx, chromedp.Tasks{
|
err := chromedp.Run(ctx, chromedp.Tasks{
|
||||||
chromedp.Navigate("https://fallout.bethesda.net/en/news"),
|
chromedp.Navigate(url),
|
||||||
chromedp.OuterHTML("html", &html),
|
chromedp.OuterHTML("html", &html),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -121,46 +132,92 @@ func scrapeNews() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add a function to extract relevant tags from the HTML content
|
// Add a function to extract relevant tags from the HTML content
|
||||||
func extractTags(html string) []string {
|
func extractNewsArticles(html string) []map[string]string {
|
||||||
var tags []string
|
var articles []map[string]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 tags
|
return articles
|
||||||
}
|
}
|
||||||
// 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) {
|
// Find and extract each article
|
||||||
tag := strings.TrimSpace(s.Text())
|
doc.Find("article.news-module-feed-item").Each(func(i int, s *goquery.Selection) {
|
||||||
tags = append(tags, tag)
|
article := make(map[string]string)
|
||||||
|
|
||||||
|
// Extract link
|
||||||
|
link, exists := s.Find("a.news-module-feed-item-image").Attr("href")
|
||||||
|
if exists {
|
||||||
|
article["link"] = "https://fallout.bethesda.net" + link
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract title
|
||||||
|
title := strings.TrimSpace(s.Find("h3.news-module-feed-item-title").Text())
|
||||||
|
article["title"] = title
|
||||||
|
|
||||||
|
// Extract tag
|
||||||
|
tag := strings.TrimSpace(s.Find("span.news-module-feed-item-details-tag").Text())
|
||||||
|
article["tag"] = tag
|
||||||
|
|
||||||
|
// Extract date
|
||||||
|
date := strings.TrimSpace(s.Find("span.news-module-feed-item-details-date").Text())
|
||||||
|
article["date"] = date
|
||||||
|
|
||||||
|
// Extract game
|
||||||
|
game := strings.TrimSpace(s.Find("span.news-module-feed-item-details-game").Text())
|
||||||
|
article["game"] = game
|
||||||
|
|
||||||
|
// Extract blurb
|
||||||
|
blurb := strings.TrimSpace(s.Find("p.news-module-feed-item-blurb").Text())
|
||||||
|
article["blurb"] = blurb
|
||||||
|
|
||||||
|
// Extract image URL
|
||||||
|
imageURL, exists := s.Find("img.news-module-feed-item-image-tag").Attr("src")
|
||||||
|
if exists {
|
||||||
|
article["imageURL"] = "https:" + imageURL
|
||||||
|
}
|
||||||
|
|
||||||
|
articles = append(articles, article)
|
||||||
})
|
})
|
||||||
return tags
|
|
||||||
|
return articles
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendNotifications(session *discordgo.Session, html string, tagChannelMap map[string]string) {
|
func sendNotifications(session *discordgo.Session, html string, tagChannelMap map[string]string, specifiedDate string) {
|
||||||
// Extract tags from the HTML content
|
// Extract articles from the HTML content
|
||||||
tags := extractTags(html)
|
articles := extractNewsArticles(html)
|
||||||
|
|
||||||
// Define today's date string
|
// Iterate over extracted articles
|
||||||
today := time.Now().Format("January 2, 2006")
|
for _, article := range articles {
|
||||||
|
tag := article["tag"]
|
||||||
|
date := article["date"]
|
||||||
|
link := article["link"]
|
||||||
|
blurb := article["blurb"]
|
||||||
|
title := article["title"]
|
||||||
|
imageURL := article["imageURL"]
|
||||||
|
|
||||||
// Iterate over extracted tags
|
|
||||||
for _, tag := range tags {
|
|
||||||
// Check if the tag is in the desired tags list
|
// Check if the tag is in the desired tags list
|
||||||
if tagChannelID, ok := tagChannelMap[tag]; ok {
|
if tagChannelID, ok := tagChannelMap[tag]; ok {
|
||||||
// Check if today's date matches the tag
|
// Check if the article's date matches the specified date
|
||||||
if tag == today {
|
if date == specifiedDate {
|
||||||
|
// Create a rich embed
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: title,
|
||||||
|
Description: blurb,
|
||||||
|
URL: link,
|
||||||
|
Image: &discordgo.MessageEmbedImage{
|
||||||
|
URL: imageURL,
|
||||||
|
},
|
||||||
|
Color: 0x00ff00, // Green color for the embed
|
||||||
|
}
|
||||||
|
|
||||||
// Send a message to the corresponding Discord channel
|
// Send a message to the corresponding Discord channel
|
||||||
message := fmt.Sprintf("Today's date matches the tag '%s' on the Fallout news page!", tag)
|
message := "New <@&568064042086694913> have been released!"
|
||||||
_, err := session.ChannelMessageSend(tagChannelID, message)
|
_, err := session.ChannelMessageSendComplex(tagChannelID, &discordgo.MessageSend{
|
||||||
if err != nil {
|
Content: message, // Replace 568064042086694913 with the ID of the role to mention
|
||||||
fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err)
|
Embed: embed,
|
||||||
continue
|
AllowedMentions: &discordgo.MessageAllowedMentions{Roles: []string{"568064042086694913"}}, // Replace ROLE_ID with the ID of the role to mention
|
||||||
}
|
})
|
||||||
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 {
|
if err != nil {
|
||||||
fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err)
|
fmt.Printf("Error sending message to Discord channel %s: %s\n", tagChannelID, err)
|
||||||
continue
|
continue
|
||||||
@ -169,12 +226,6 @@ func sendNotifications(session *discordgo.Session, html string, tagChannelMap ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Inside the sendNotifications function, after extracting tags
|
|
||||||
//log.Println("Extracted tags:", tags)
|
|
||||||
|
|
||||||
// Inside the sendNotifications function, after checking tag-channel mappings
|
|
||||||
//log.Println("Tag-channel mappings:", tagChannelMap)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function will be called whenever a new message is created
|
// This function will be called whenever a new message is created
|
||||||
|
Loading…
Reference in New Issue
Block a user