forked from fabioxgn/go-bot
/
main.go
95 lines (79 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"github.com/fabioxgn/go-bot/commands"
"github.com/thoj/go-ircevent"
"github.com/yvasiyarov/gorelic"
"log"
"os"
)
const (
configFile = "config.json"
)
var (
irccon *irc.Connection
config = &Config{}
)
func isPrivateMsg(channel string) bool {
return channel == config.Nick
}
func onPRIVMSG(e *irc.Event) {
channel := e.Arguments[0]
if isPrivateMsg(channel) {
channel = e.Nick //e.Nick is who sent the pvt message
}
cmd := commands.Parse(e.Message, config.CmdPrefix, channel, e.Nick)
if cmd.IsCommand {
commands.HandleCmd(cmd, irccon)
} else {
// It's not a command
// TODO: Test for passive commands (parse url, etc) ?
}
}
func connect() {
irccon = irc.IRC(config.User, config.Nick)
irccon.UseTLS = config.UseTLS
err := irccon.Connect(config.Server)
if err != nil {
log.Fatal(err)
}
}
func onWelcome(e *irc.Event) {
for _, channel := range config.Channels {
irccon.Join(channel)
}
}
func onEndOfNames(e *irc.Event) {
log.Println("onEndOfNames: %v", e.Arguments)
irccon.Privmsg(e.Arguments[1], "Hi there.\n")
}
func configureEvents() {
irccon.AddCallback("001", onWelcome)
irccon.AddCallback("366", onEndOfNames)
irccon.AddCallback("PRIVMSG", onPRIVMSG)
}
func readConfig() {
file, err := os.Open(configFile)
if err != nil {
panic(err)
}
config.Read(file)
fmt.Printf("%v\n", config)
}
func startMetrics() {
newRelicAPIKey := os.Getenv("NEW_RELIC_KEY")
if newRelicAPIKey == "" {
return
}
agent := gorelic.NewAgent()
agent.Verbose = false
agent.NewrelicLicense = newRelicAPIKey
agent.Run()
}
func main() {
startMetrics()
readConfig()
connect()
configureEvents()
irccon.Loop()
}