diff --git a/commands/cases.go b/commands/cases.go new file mode 100644 index 0000000..acf36bb --- /dev/null +++ b/commands/cases.go @@ -0,0 +1,12 @@ +package commands + +import ( + "github.com/bwmarrin/discordgo" +) + +// CommandMap maps command strings to their corresponding handler functions +var commandMap = map[string]func(*discordgo.Session, *discordgo.MessageCreate){ + "!hello2": helloCommand, + "!test": testCommand, + // Add more commands and their corresponding handler functions here +} diff --git a/commands/handler.go b/commands/handler.go new file mode 100644 index 0000000..a16b52c --- /dev/null +++ b/commands/handler.go @@ -0,0 +1,31 @@ +package commands + +import ( + "strings" + + "github.com/bwmarrin/discordgo" +) + +// This function will be called whenever a new message is created +func CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) { + // Ignore messages created by the bot itself + if m.Author.ID == s.State.User.ID { + return + } + + // Check if the message starts with a command prefix + if strings.HasPrefix(m.Content, "!") { + // Extract the command and arguments + parts := strings.Fields(m.Content) + command := parts[0] + + // Find the corresponding handler function in the map + if handlerFunc, ok := commandMap[command]; ok { + // Call the handler function + handlerFunc(s, m) + } else { + // Respond to unknown commands + _, _ = s.ChannelMessageSend(m.ChannelID, "Unknown command. Type !help for a list of commands.") + } + } +} diff --git a/commands/hello_command.go b/commands/hello_command.go new file mode 100644 index 0000000..c25cb3d --- /dev/null +++ b/commands/hello_command.go @@ -0,0 +1,9 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +// HelloCommand handles the !hello command +func helloCommand(s *discordgo.Session, m *discordgo.MessageCreate) { + // Respond to the !hello command + _, _ = s.ChannelMessageSend(m.ChannelID, "Hello!") +} diff --git a/commands/test_command.go b/commands/test_command.go new file mode 100644 index 0000000..88898c0 --- /dev/null +++ b/commands/test_command.go @@ -0,0 +1,9 @@ +package commands + +import "github.com/bwmarrin/discordgo" + +// HelloCommand handles the !hello command +func testCommand(s *discordgo.Session, m *discordgo.MessageCreate) { + // Respond to the !hello command + _, _ = s.ChannelMessageSend(m.ChannelID, "Hello! test") +} diff --git a/go.mod b/go.mod index 52b6c41..862a816 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module goDiscordBot +module NukaNewsBot go 1.22.2 diff --git a/main.go b/main.go index bd9b612..8c28fdc 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "NukaNewsBot/commands" "context" "encoding/json" "fmt" @@ -84,8 +85,11 @@ func main() { // After creating the Discord session log.Println("Created Discord session") - // Register messageCreate as a callback for the messageCreate events - dg.AddHandler(messageCreate) + // Register commandHandler as a callback for message creation events + dg.AddHandler(commands.CommandHandler) + + // After creating the Discord session + log.Println("Listening for Commands") // Open a websocket connection to Discord and begin listening err = dg.Open() @@ -97,7 +101,7 @@ func main() { log.Println("Opened Discord connection") // Run the scraping and message sending function at start up - sendNotifications(dg, fetchUrl(url), channelMap, roleMap, 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) @@ -234,29 +238,31 @@ func sendNotifications(session *discordgo.Session, html string, channelMap, role } 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) - // } - //} } } } } // This function will be called whenever a new message is created -func messageCreate(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, "Hello!") - } -} +//func CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) { +// // Ignore messages created by the bot itself +// if m.Author.ID == s.State.User.ID { +// return +// } +// +// // Check if the message starts with a command prefix +// if strings.HasPrefix(m.Content, "!") { +// // Extract the command and arguments +// parts := strings.Fields(m.Content) +// command := parts[0] +// +// // Find the corresponding handler function in the map +// if handlerFunc, ok := commands.CommandMap[command]; ok { +// // Call the handler function +// handlerFunc(s, m) +// } else { +// // Respond to unknown commands +// _, _ = s.ChannelMessageSend(m.ChannelID, "Unknown command. Type !help for a list of commands.") +// } +// } +//}