From 7f95c31498756636172a9fbf4ee9f4e8140642b0 Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Sat, 27 Apr 2024 17:05:14 -0400 Subject: [PATCH] alpha release --- .gitignore | 1 + config.json | 9 -------- example.config.json | 14 ++++++++++++ main.go | 52 +++++++++++++++++++++++++++++++-------------- readme.md | 21 +++++++++++++++--- 5 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 .gitignore delete mode 100644 config.json create mode 100644 example.config.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cffcb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index 4d15a71..0000000 --- a/config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "discord_bot_token": "MTIxNzA3NDM3ODE1MjM0NTcwMA.G5Evvi.AQ571YcyJK-Zt1RflylKU1K1Cnwuf80LOD43qw", - "channel_map": { - "Patch Notes": "556093419341086749", - "Atomic Shop": "590581442392621067", - "News": "558335339018846228" - }, - "url": "https://fallout.bethesda.net/en/news" -} diff --git a/example.config.json b/example.config.json new file mode 100644 index 0000000..d467262 --- /dev/null +++ b/example.config.json @@ -0,0 +1,14 @@ +{ + "discord_bot_token": "YOUR_BOT_TOKEN", + "role_map": { + "Role 1": "ROLE_ID_NUMBER", + "Role 2": "ROLE_ID_NUMBER", + "Role 3": "ROLE_ID_NUMBER" + }, + "channel_map": { + "Channel 1": "ROLE_ID_NUMBER", + "Channel 2": "ROLE_ID_NUMBER", + "Channel 3": "ROLE_ID_NUMBER" + }, + "url": "https://fallout.bethesda.net/en/news" +} diff --git a/main.go b/main.go index f1b8af7..bd9b612 100644 --- a/main.go +++ b/main.go @@ -16,15 +16,16 @@ import ( type Config struct { DiscordBotToken string `json:"discord_bot_token"` - ChannelMap map[string]string `json:"channel_map"` // Add the channel map to the config - URL string `json:"url"` // Add URL field to the config + ChannelMap map[string]string `json:"channel_map"` + RoleMap map[string]string `json:"role_map"` + URL string `json:"url"` } func main() { //Initialize Time - date := "April 26, 2024" - //date := time.Now().Format("January 2, 2006") + //date := "April 16, 2024" + date := time.Now().Format("January 2, 2006") // At the beginning of the main function log.Println("Starting the bot...") @@ -55,8 +56,17 @@ func main() { return } - // Access the channel map from the configuration - tagChannelMap := config.ChannelMap + channelMap := config.ChannelMap + if len(channelMap) == 0 { + fmt.Println("Channel map is not set in config file") + return + } + + roleMap := config.RoleMap + if len(roleMap) == 0 { + fmt.Println("Role map is not set in config file") + return + } url := config.URL if url == "" { @@ -87,7 +97,7 @@ func main() { log.Println("Opened Discord connection") // Run the scraping and message sending function at start up - sendNotifications(dg, fetchUrl(url), tagChannelMap, date) + sendNotifications(dg, fetchUrl(url), channelMap, roleMap, date) // Schedule the scraping and message sending function to run once a day ticker := time.NewTicker(24 * time.Hour) @@ -98,7 +108,7 @@ func main() { for { select { case <-ticker.C: - sendNotifications(dg, fetchUrl(url), tagChannelMap, date) + sendNotifications(dg, fetchUrl(url), channelMap, roleMap, date) } } }() @@ -183,7 +193,7 @@ func extractNewsArticles(html string) []map[string]string { return articles } -func sendNotifications(session *discordgo.Session, html string, tagChannelMap map[string]string, specifiedDate string) { +func sendNotifications(session *discordgo.Session, html string, channelMap, roleMap map[string]string, specifiedDate string) { // Extract articles from the HTML content articles := extractNewsArticles(html) @@ -197,7 +207,7 @@ func sendNotifications(session *discordgo.Session, html string, tagChannelMap ma imageURL := article["imageURL"] // Check if the tag is in the desired tags list - if tagChannelID, ok := tagChannelMap[tag]; ok { + if channelID, ok := channelMap[tag]; ok { // Check if the article's date matches the specified date if date == specifiedDate { // Create a rich embed @@ -212,17 +222,27 @@ func sendNotifications(session *discordgo.Session, html string, tagChannelMap ma } // Send a message to the corresponding Discord channel - message := "New <@&568064042086694913> have been released!" - _, err := session.ChannelMessageSendComplex(tagChannelID, &discordgo.MessageSend{ - Content: message, // Replace 568064042086694913 with the ID of the role to mention + message := fmt.Sprintf("New <@&%s> have been released!", roleMap[tag]) + _, err := session.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{ + Content: message, Embed: embed, - AllowedMentions: &discordgo.MessageAllowedMentions{Roles: []string{"568064042086694913"}}, // Replace ROLE_ID with the ID of the role to mention + AllowedMentions: &discordgo.MessageAllowedMentions{}, // Allow mentions }) 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", channelID, err) continue } - fmt.Println("Message sent to Discord channel:", tagChannelID) + fmt.Println("Message sent to Discord channel:", channelID) + + // Tag the corresponding role + //if roleID, ok := roleMap[tag]; ok { + // _, err := session.ChannelMessageSend(channelID, fmt.Sprintf("<@&%s>", roleID)) + // if err != nil { + // fmt.Printf("Error tagging role %s in Discord channel %s: %s\n", roleID, channelID, err) + // } else { + // fmt.Printf("Role %s tagged in Discord channel %s\n", roleID, channelID) + // } + //} } } } diff --git a/readme.md b/readme.md index cca2372..0ef4b13 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,19 @@ -go mod download -go run main.go +# Fallout News Discord Bot -right now it scrapes bethesda news and spit back dates in console and prints if today is today +## PreReqs + +- Go +- Discord Developer Bot Credentials +- Discord Channels IDs +- Discord Role IDs + +## Get Started + +Go Get dependancies +`go mod download` + +`cp example.config.json config.json` +fill out credentials in config with your information + +Run the Bot: +`go run main.go`