irc_bot/main.go

73 lines
1.9 KiB
Go

package main
import (
"fmt"
"log"
"strings"
"crypto/tls"
"github.com/thoj/go-ircevent"
"irc_bot/utils"
)
func main() {
// Load configuration
config, err := utils.LoadConfig("config.json")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
// Initialize the IRC bot
irccon := irc.IRC(config.Nickname, config.Nickname)
irccon.VerboseCallbackHandler = config.Debug
irccon.Debug = config.Callback
irccon.UseTLS = config.SSL
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true}
err = irccon.Connect(fmt.Sprintf("%s:%d", config.IRCServer, config.IRCPort))
if err != nil {
log.Fatalf("Failed to connect to IRC server: %v", err)
}
irccon.AddCallback("001", func(e *irc.Event) {
for _, channel := range config.Channels {
irccon.Join(channel)
}
})
// Read the messages to get the content and call the apropriate command if has
irccon.AddCallback("PRIVMSG", func(e *irc.Event) {
if len(e.Arguments) < 2 {
return
}
message := e.Arguments[1]
parts := strings.Fields(message)
if len(parts) == 0 {
return
}
cmd := strings.TrimPrefix(parts[0], "!")
switch {
// For each new command, add a new case
case cmd == "geoip":
// Call GeoIP command that return the query data
response, err := utils.HandleGeoIPCommand(parts[1], config.GeoIP_City, config.GeoIP_ASN)
if err != nil {
irccon.Privmsg(e.Arguments[0], fmt.Sprintf("%v", err))
} else {
irccon.Privmsg(e.Arguments[0], response)
}
case cmd == "commands":
// List which are the available commands to use
irccon.Privmsg(e.Arguments[0], "Current available commands: !geoip")
default:
return
}
})
irccon.Loop()
}