more robust commands sub package
This commit is contained in:
parent
971af52f6e
commit
2e789b1f26
12
commands/cases.go
Normal file
12
commands/cases.go
Normal file
@ -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
|
||||||
|
}
|
31
commands/handler.go
Normal file
31
commands/handler.go
Normal file
@ -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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
commands/hello_command.go
Normal file
9
commands/hello_command.go
Normal file
@ -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!")
|
||||||
|
}
|
9
commands/test_command.go
Normal file
9
commands/test_command.go
Normal file
@ -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")
|
||||||
|
}
|
52
main.go
52
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"NukaNewsBot/commands"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -84,8 +85,11 @@ func main() {
|
|||||||
// After creating the Discord session
|
// After creating the Discord session
|
||||||
log.Println("Created Discord session")
|
log.Println("Created Discord session")
|
||||||
|
|
||||||
// Register messageCreate as a callback for the messageCreate events
|
// Register commandHandler as a callback for message creation events
|
||||||
dg.AddHandler(messageCreate)
|
dg.AddHandler(commands.CommandHandler)
|
||||||
|
|
||||||
|
// After creating the Discord session
|
||||||
|
log.Println("Listening for Commands")
|
||||||
|
|
||||||
// Open a websocket connection to Discord and begin listening
|
// Open a websocket connection to Discord and begin listening
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
@ -97,7 +101,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), channelMap, roleMap, 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)
|
||||||
@ -234,29 +238,31 @@ func sendNotifications(session *discordgo.Session, html string, channelMap, role
|
|||||||
}
|
}
|
||||||
fmt.Println("Message sent to Discord channel:", channelID)
|
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
|
// This function will be called whenever a new message is created
|
||||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
//func CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
// Ignore messages created by the bot itself
|
// // Ignore messages created by the bot itself
|
||||||
if m.Author.ID == s.State.User.ID {
|
// if m.Author.ID == s.State.User.ID {
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// If the message content is "!hello", respond with "Hello!"
|
// // Check if the message starts with a command prefix
|
||||||
if m.Content == "!hello" {
|
// if strings.HasPrefix(m.Content, "!") {
|
||||||
_, _ = s.ChannelMessageSend(m.ChannelID, "Hello!")
|
// // 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.")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
Loading…
Reference in New Issue
Block a user