channel map moved to config
This commit is contained in:
parent
50513cf3cd
commit
42d5fc9dbe
@ -1,4 +1,8 @@
|
|||||||
{
|
{
|
||||||
"discord_bot_token": "MTIxNzA3NDM3ODE1MjM0NTcwMA.G5Evvi.AQ571YcyJK-Zt1RflylKU1K1Cnwuf80LOD43qw",
|
"discord_bot_token": "MTIxNzA3NDM3ODE1MjM0NTcwMA.G5Evvi.AQ571YcyJK-Zt1RflylKU1K1Cnwuf80LOD43qw",
|
||||||
"discord_channel_id": "590581442392621067"
|
"channel_map": {
|
||||||
|
"Patch Notes": "556093419341086749",
|
||||||
|
"Atomic Shop": "590581442392621067",
|
||||||
|
"News": "558335339018846228"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
61
main.go
61
main.go
@ -16,11 +16,14 @@ import (
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DiscordBotToken string `json:"discord_bot_token"`
|
DiscordBotToken string `json:"discord_bot_token"`
|
||||||
DiscordChannelID string `json:"discord_channel_id"` // Add more configuration fields here
|
ChannelMap map[string]string `json:"channel_map"` // Add the channel map to the config
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// At the beginning of the main function
|
||||||
|
log.Println("Starting the bot...")
|
||||||
|
|
||||||
// Open and read the configuration file
|
// Open and read the configuration file
|
||||||
configFile, err := os.Open("config.json")
|
configFile, err := os.Open("config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -37,18 +40,18 @@ func main() {
|
|||||||
return
|
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
|
token := config.DiscordBotToken
|
||||||
if token == "" {
|
if token == "" {
|
||||||
fmt.Println("Discord bot token is not set in config file")
|
fmt.Println("Discord bot token is not set in config file")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// Access the channel map from the configuration
|
||||||
channelID := config.DiscordChannelID
|
tagChannelMap := config.ChannelMap
|
||||||
if channelID == "" {
|
|
||||||
fmt.Println("Discord channel ID is not set in config file")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
@ -57,20 +60,23 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// After creating the Discord session
|
||||||
|
log.Println("Created Discord session")
|
||||||
|
|
||||||
// Register messageCreate as a callback for the messageCreate events
|
// Register messageCreate as a callback for the messageCreate events
|
||||||
dg.AddHandler(messageCreate)
|
dg.AddHandler(messageCreate)
|
||||||
|
|
||||||
dg.AddHandler(message2Create)
|
|
||||||
|
|
||||||
// Open a websocket connection to Discord and begin listening
|
// Open a websocket connection to Discord and begin listening
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error opening Discord connection:", err)
|
fmt.Println("Error opening Discord connection:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// After opening the 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 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
|
// Schedule the scraping and message sending function to run once a day
|
||||||
ticker := time.NewTicker(24 * time.Hour)
|
ticker := time.NewTicker(24 * time.Hour)
|
||||||
@ -81,13 +87,11 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
sendNotifications(dg, channelID, scrapeNews())
|
sendNotifications(dg, scrapeNews(), tagChannelMap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//test()
|
|
||||||
|
|
||||||
// Wait here until CTRL-C or other term signal is received
|
// Wait here until CTRL-C or other term signal is received
|
||||||
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||||
<-make(chan struct{})
|
<-make(chan struct{})
|
||||||
@ -132,15 +136,7 @@ func extractTags(html string) []string {
|
|||||||
return tags
|
return tags
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a map to associate each desired tag with its corresponding Discord channel ID
|
func sendNotifications(session *discordgo.Session, html string, tagChannelMap map[string]string) {
|
||||||
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) {
|
|
||||||
// Extract tags from the HTML content
|
// Extract tags from the HTML content
|
||||||
tags := extractTags(html)
|
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
|
// 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 the message content is "!hello", respond with "Hello!"
|
||||||
if m.Content == "!hello1" {
|
if m.Content == "!hello" {
|
||||||
_, _ = s.ChannelMessageSend(m.ChannelID, "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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user