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 {
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)
// }
//}
}
}
}

View File

@ -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`