-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
176 lines (142 loc) · 3.35 KB
/
bot.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bufio"
"container/ring"
"fmt"
"log"
"math/rand"
"net"
"net/textproto"
"os"
"regexp"
"strings"
"time"
)
// BotConfig provides Bot configuration parameters
type BotConfig struct {
token string
username string
channel string
}
// Bot is a Twitch.tv chatbot
type Bot struct {
Connection net.Conn
Config BotConfig
Events []*Event
EventRotation *ring.Ring
}
var (
messageRegex = regexp.MustCompile(`(?::(?P<username>\w+)!\w+@(?:\w+\.?)+\s)?(?P<command>\w+)\s(?:#(?P<channel>\w+)\s?)?(?::(?P<isCommand>\!)?(?P<message>.+\s*)+)?`)
)
// NewBot returns a Bot pointer
func NewBot(config BotConfig) *Bot {
c, err := net.Dial("tcp", "irc.chat.twitch.tv:6667")
if err != nil {
log.Fatal(err)
}
return &Bot{
Connection: c,
Config: config,
}
}
// Authenticate authenticates against Twitch.tv's IRC server and joins a channel
func (b Bot) Authenticate() {
b.SendMessage(&IRCMessage{
Command: pass,
Message: b.Config.token,
})
b.SendMessage(&IRCMessage{
Username: b.Config.username,
Command: nick,
})
b.SendMessage(&IRCMessage{
Command: join,
Channel: b.Config.channel,
})
}
// SendMessage sends an IRCMessage to Twitch.tv's IRC
func (b Bot) SendMessage(message *IRCMessage) {
fmt.Printf("%s\n", message)
fmt.Fprintf(b.Connection, "%s\r\n", message)
}
// Respond responds to user commands
func (b Bot) Respond(message *IRCMessage, timeNow time.Time) IRCMessage {
if message.Message == "wb" {
nextEvents := []string{}
// Sets b.EventRotation to the upcoming event
for i := 0; i < b.EventRotation.Len(); i++ {
event := b.EventRotation.Value.(*Event)
eventMinutes, err := event.GetMinutes()
if err != nil {
log.Fatal(err)
}
minutesNow := timeNow.Hour()*60 + timeNow.Minute()
if eventMinutes > minutesNow {
break
}
b.EventRotation = b.EventRotation.Next()
}
// Get the message for the 3 upcoming events
for i := 0; i < 3; i++ {
event := b.EventRotation.Value.(*Event)
eventMessage := event.GetMessage()
nextEvents = append(nextEvents, eventMessage)
b.EventRotation = b.EventRotation.Next()
}
// Easter Egg
if rand.Intn(999) == 999 {
return IRCMessage{
Command: priv,
Channel: b.Config.channel,
Message: "Não interessa pra você, palhaço",
}
}
nextEventsMessage := "Próximos World Bosses: " + strings.Join(nextEvents, " -- ")
return IRCMessage{
Command: priv,
Channel: b.Config.channel,
Message: nextEventsMessage,
}
}
return IRCMessage{}
}
func (b Bot) parseMessage(line string) *IRCMessage {
matches := messageRegex.FindStringSubmatch(line)
if matches == nil {
return &IRCMessage{}
}
message := &IRCMessage{
Username: matches[1],
Command: strToIRCCommand(matches[2]),
Channel: matches[3],
Message: matches[5],
}
if matches[4] == "!" {
message.IsCommand = true
}
return message
}
// Listen listens for messages from Twitch.tv's IRC
func (b Bot) Listen() {
reader := bufio.NewReader(b.Connection)
tp := textproto.NewReader(reader)
for {
line, err := tp.ReadLine()
if err != nil {
log.Fatal(err)
}
message := b.parseMessage(line)
if message.Command == ping {
b.SendMessage(&IRCMessage{
Command: pong,
})
}
if message.IsCommand {
response := b.Respond(message, time.Now())
b.SendMessage(&response)
}
if os.Getenv("DEBUG") != "" {
fmt.Println(line)
}
}
}