alpha release

This commit is contained in:
Chris kerr 2024-04-27 17:05:14 -04:00
parent 075bdb2782
commit 7f95c31498
5 changed files with 69 additions and 28 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.json

View File

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

14
example.config.json Normal file
View File

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

52
main.go
View File

@ -16,15 +16,16 @@ 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"`
URL string `json:"url"` // Add URL field to the config RoleMap map[string]string `json:"role_map"`
URL string `json:"url"`
} }
func main() { func main() {
//Initialize Time //Initialize Time
date := "April 26, 2024" //date := "April 16, 2024"
//date := time.Now().Format("January 2, 2006") 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...")
@ -55,8 +56,17 @@ func main() {
return return
} }
// Access the channel map from the configuration channelMap := config.ChannelMap
tagChannelMap := 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 url := config.URL
if url == "" { if url == "" {
@ -87,7 +97,7 @@ func main() {
log.Println("Opened Discord connection") log.Println("Opened Discord connection")
// Run the scraping and message sending function at start up // 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 // Schedule the scraping and message sending function to run once a day
ticker := time.NewTicker(24 * time.Hour) ticker := time.NewTicker(24 * time.Hour)
@ -98,7 +108,7 @@ func main() {
for { for {
select { select {
case <-ticker.C: 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 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 // Extract articles from the HTML content
articles := extractNewsArticles(html) articles := extractNewsArticles(html)
@ -197,7 +207,7 @@ func sendNotifications(session *discordgo.Session, html string, tagChannelMap ma
imageURL := article["imageURL"] imageURL := article["imageURL"]
// 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 channelID, ok := channelMap[tag]; ok {
// Check if the article's date matches the specified date // Check if the article's date matches the specified date
if date == specifiedDate { if date == specifiedDate {
// Create a rich embed // 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 // Send a message to the corresponding Discord channel
message := "New <@&568064042086694913> have been released!" message := fmt.Sprintf("New <@&%s> have been released!", roleMap[tag])
_, err := session.ChannelMessageSendComplex(tagChannelID, &discordgo.MessageSend{ _, err := session.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
Content: message, // Replace 568064042086694913 with the ID of the role to mention Content: message,
Embed: embed, 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 { 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 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)
// }
//}
} }
} }
} }

View File

@ -1,4 +1,19 @@
go mod download # Fallout News Discord Bot
go run main.go
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`