-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
64 lines (55 loc) · 1 KB
/
log.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
package logger
import (
"time"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/whyrusleeping/hellabot"
)
type Message struct {
Nickname string
Timestamp time.Time
Content string
}
type Logger struct {
channelname string
db *gorm.DB
}
var l Logger
func init() {
l = Logger{}
}
func Configure(channelname, location string) (err error) {
l.channelname = channelname
db, err := gorm.Open("sqlite3", location)
if err != nil {
return err
}
l.db = &db
l.db.AutoMigrate(&Message{})
return err
}
func GetTrigger() (trigger *hbot.Trigger) {
trigger = &hbot.Trigger{
func(mes *hbot.Message) bool {
if mes.Command == "PRIVMSG" {
if mes.To == l.channelname {
return true
}
}
return false
},
func(irc *hbot.IrcCon, mes *hbot.Message) bool {
l.log(mes)
return true
},
}
return trigger
}
func (l *Logger) log(msg *hbot.Message) {
m := Message{
Nickname: msg.From,
Timestamp: msg.TimeStamp,
Content: msg.Content,
}
l.db.Save(&m)
}