-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (181 loc) · 5.72 KB
/
Copy pathmain.go
File metadata and controls
209 lines (181 loc) · 5.72 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
"time"
markov "github.com/IAmPattycakes/Go-Markov"
"github.com/gempir/go-twitch-irc/v2"
)
type ChatRat struct {
graph markov.Graph
client *twitch.Client
ratSettings settings
emoteTimers [][]time.Time
emoteTimeout time.Duration
emoteLastTime []time.Time
emoteSpamCooldown time.Duration
chatDelay chatDelay
logger RatLogger
}
type chatDelay struct {
mu sync.RWMutex
ticker *time.Ticker
duration time.Duration
paused bool
}
func main() {
var rat ChatRat
settingsFile := flag.String("settings", "settings.json", "The name of the settings json file")
flag.Parse()
rat.ratSettings = *NewSettings(*settingsFile)
rat.graph = *markov.NewGraph(rat.ratSettings.ChatContextDepth)
rat.logger = *NewLogger(rat.ratSettings.logType, rat.ratSettings.LogName, rat.ratSettings.logLevel)
go rat.logger.HandleLogs()
defer rat.logger.Close()
//Timer settings
rat.chatDelay.mu.RLock()
rat.chatDelay.duration = rat.ratSettings.chatDelay
rat.chatDelay.ticker = time.NewTicker(rat.chatDelay.duration)
rat.chatDelay.paused = false
rat.log(Debug, fmt.Sprintf("Setting new chat delay to %s", rat.ratSettings.chatDelay.String()))
rat.chatDelay.mu.RUnlock()
//Emote spam settings
rat.emoteTimeout = rat.ratSettings.emoteSpamTimeout
rat.emoteSpamCooldown = rat.ratSettings.emoteSpamCooldown
rat.emoteTimers = make([][]time.Time, len(rat.ratSettings.EmotesToSpam))
rat.emoteLastTime = make([]time.Time, len(rat.ratSettings.EmotesToSpam))
rat.log(Debug, fmt.Sprintf("Setting emoteTimeout to %s and emoteSpamCooldown to %s", rat.ratSettings.emoteSpamTimeout.String(), rat.ratSettings.emoteSpamCooldown.String()))
client := twitch.NewClient(rat.ratSettings.BotName, rat.ratSettings.Oauth)
rat.client = client
rat.client.OnPrivateMessage(func(message twitch.PrivateMessage) {
rat.log(Debug, fmt.Sprintf("Passing message to messageParser, raw: %s", message.Raw))
rat.messageParser(message)
})
//Loading the chat history to give the model something to go off of at the start.
rat.log(Debug, fmt.Sprintf("Starting loading chat log at %s", time.Now().String()))
rat.loadChatLog()
rat.log(Debug, fmt.Sprintf("Finished loading chat log at %s", time.Now().String()))
client.Join(rat.ratSettings.StreamName)
defer client.Disconnect()
defer client.Depart(rat.ratSettings.StreamName)
rat.speak("Hi chat I'm back! =^.^=")
rat.logger.Log(Info, "Chatrat starting in stream "+rat.ratSettings.StreamName+" running as "+rat.ratSettings.BotName)
go rat.speechHandler()
err := client.Connect()
if err != nil {
panic(err)
}
}
// speak checks to see if the message given is able to be said in chat, says it, and returns true if it can. Returns false if it can't.
func (rat *ChatRat) speak(message string) bool {
if len(message) > 512 {
rat.log(Debug, fmt.Sprintf("Failed to speak message: %s, too long", message))
return false
}
rat.client.Say(rat.ratSettings.StreamName, message)
return true
}
func (rat *ChatRat) log(sev LogSeverity, m string) {
rat.logger.Log(sev, m)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (rat *ChatRat) isUserTrusted(username string) bool {
return contains(rat.ratSettings.TrustedUsers, username)
}
func (rat *ChatRat) isUserIgnored(username string) bool {
return contains(rat.ratSettings.IgnoredUsers, username)
}
func (rat *ChatRat) speechHandler() {
for range rat.chatDelay.ticker.C {
if rat.chatDelay.paused {
continue
}
spoken := false
for !spoken {
rat.log(Debug, "Trying to generate speech for routine speech handler")
words := rat.graph.GenerateMarkovString()
spoken = rat.speak(words)
if spoken {
rat.log(Info, "Saying \""+words+"\" from the routine speech handler")
}
}
}
}
func (rat *ChatRat) writeText(text string) {
f, err := os.OpenFile(rat.ratSettings.ChatLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
rat.log(Critical, "Couldn't open chat log to write, "+err.Error())
}
if _, err := f.Write([]byte(text + "\n")); err != nil {
rat.log(Critical, "Couldn't write to chat log, "+err.Error())
}
if err := f.Close(); err != nil {
rat.log(Critical, "Couldn't close chat log, "+err.Error())
}
}
func (rat *ChatRat) loadChatLog() {
file, err := os.Open(rat.ratSettings.ChatLog)
quitnow := false
if err != nil {
log.Print(err)
quitnow = true
}
defer file.Close()
if quitnow {
return
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
rat.LoadPhrase(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func (rat *ChatRat) reloadGraph(depth int) {
//Pause chatting so it doesn't try to talk with a bad/incomplete graph
rat.chatDelay.mu.RLock()
rat.chatDelay.ticker.Stop()
rat.chatDelay.paused = true
//Set the context depth in the settings,
rat.ratSettings.ChatContextDepth = int(depth)
rat.graph = *markov.NewGraph(depth)
rat.loadChatLog()
//Unpause the chatting and release the lock
rat.chatDelay.ticker.Reset(rat.chatDelay.duration)
rat.chatDelay.paused = false
rat.chatDelay.mu.RUnlock()
}
// LoadPhrase attemptes to load the phrase into the markov chain. If the phrase has a blacklisted word/phrase in it
// then the whole phrase gets ignored and the function returns false. If it passes, then returns true.
func (rat *ChatRat) LoadPhrase(s string) (bool, string) {
for _, v := range rat.ratSettings.blacklist {
if strings.Contains(strings.ToLower(s), strings.ToLower(v)) {
return false, v
}
}
for _, v := range rat.ratSettings.blacklist {
match, err := regexp.Match(v, []byte(s))
if err != nil {
fmt.Println("Error in regex matching: ", err)
}
if match {
return false, v
}
}
rat.graph.LoadPhrase(s)
return true, ""
}