From 42d5fc9dbe1c594973d884b1dcf25d2aaac41ccd Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Sat, 27 Apr 2024 12:34:35 -0400 Subject: [PATCH] channel map moved to config --- config.json | 6 ++++- main.go | 63 ++++++++++++++++++++++------------------------------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/config.json b/config.json index 399ef5d..01efad8 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,8 @@ { "discord_bot_token": "MTIxNzA3NDM3ODE1MjM0NTcwMA.G5Evvi.AQ571YcyJK-Zt1RflylKU1K1Cnwuf80LOD43qw", - "discord_channel_id": "590581442392621067" + "channel_map": { + "Patch Notes": "556093419341086749", + "Atomic Shop": "590581442392621067", + "News": "558335339018846228" + } } diff --git a/main.go b/main.go index f3ede10..e17b5dd 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,15 @@ import ( ) type Config struct { - DiscordBotToken string `json:"discord_bot_token"` - DiscordChannelID string `json:"discord_channel_id"` // Add more configuration fields here + DiscordBotToken string `json:"discord_bot_token"` + ChannelMap map[string]string `json:"channel_map"` // Add the channel map to the config } func main() { + // At the beginning of the main function + log.Println("Starting the bot...") + // Open and read the configuration file configFile, err := os.Open("config.json") if err != nil { @@ -37,18 +40,18 @@ func main() { return } - // Access the Discord bot token from the configuration + // After loading the configuration + log.Println("Loaded configuration from config.json") + + // Access the Discord bot token and channel ID from the configuration token := config.DiscordBotToken if token == "" { fmt.Println("Discord bot token is not set in config file") return } - // - channelID := config.DiscordChannelID - if channelID == "" { - fmt.Println("Discord channel ID is not set in config file") - } + // Access the channel map from the configuration + tagChannelMap := config.ChannelMap // Create a new Discord session using the provided bot token dg, err := discordgo.New("Bot " + token) @@ -57,20 +60,23 @@ func main() { return } + // After creating the Discord session + log.Println("Created Discord session") + // Register messageCreate as a callback for the messageCreate events dg.AddHandler(messageCreate) - dg.AddHandler(message2Create) - // Open a websocket connection to Discord and begin listening err = dg.Open() if err != nil { fmt.Println("Error opening Discord connection:", err) return } + // After opening the Discord connection + log.Println("Opened Discord connection") // Run the scraping and message sending function once at the specified time - sendNotifications(dg, channelID, scrapeNews()) + sendNotifications(dg, scrapeNews(), tagChannelMap) // Schedule the scraping and message sending function to run once a day ticker := time.NewTicker(24 * time.Hour) @@ -81,13 +87,11 @@ func main() { for { select { case <-ticker.C: - sendNotifications(dg, channelID, scrapeNews()) + sendNotifications(dg, scrapeNews(), tagChannelMap) } } }() - //test() - // Wait here until CTRL-C or other term signal is received fmt.Println("Bot is now running. Press CTRL-C to exit.") <-make(chan struct{}) @@ -132,15 +136,7 @@ func extractTags(html string) []string { return tags } -// 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) { +func sendNotifications(session *discordgo.Session, html string, tagChannelMap map[string]string) { // Extract tags from the HTML content tags := extractTags(html) @@ -173,6 +169,12 @@ func sendNotifications(session *discordgo.Session, channelID string, html string } } } + // 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 @@ -183,20 +185,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { } // If the message content is "!hello", respond with "Hello!" - if m.Content == "!hello1" { + if m.Content == "!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") - } -}