/
main.go
179 lines (149 loc) Β· 4.04 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
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
package main
import (
_ "embed"
"fmt"
"os"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/moniquelive/moniquelive-bot/twitch/commands"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)
//go:embed .oauth
var oauth string
var cmd commands.Commands
const (
username = "moniquelive_bot"
queueName = "ms.twitch"
createTtsTopicName = "create_tts"
spotifyTopicName = "spotify_music_updated"
musicSkipPollName = "twitch-bot:twitch:poll:skip_music"
musicKeepPollName = "twitch-bot:twitch:poll:keep_music"
)
var (
amqpURL = os.Getenv("RABBITMQ_URL")
redisURL = os.Getenv("REDIS_URL")
log = logrus.WithField("package", "main")
)
func init() {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.StampMilli,
})
logrus.SetLevel(logrus.TraceLevel) // sets log level
cmd.Reload()
}
func check(err error) {
if err != nil {
log.Fatalln("failed:", err)
}
}
func main() {
defer log.Debugln("AMQP consumer shutdown.")
go NewWatcher()
var err error
conn, err := amqp.Dial(amqpURL)
check(err)
defer conn.Close()
go func() { log.Debugf("closing: %s", <-conn.NotifyClose(make(chan *amqp.Error))) }()
log.Debugln("got Connection, getting Channel")
channel, err := conn.Channel()
check(err)
defer channel.Close()
log.Debugf("declaring Queue %q", queueName)
queue, err := channel.QueueDeclare(
queueName, // name of the queue
true, // durable
false, // delete when unused
false, // exclusive
false, // noWait
nil, // arguments
)
check(err)
log.Debugf("binding Queue %q to amq.topic", queueName)
err = channel.QueueBind(queueName, spotifyTopicName, "amq.topic", false, nil)
check(err)
log.Debugln("Setting QoS")
err = channel.Qos(1, 0, true)
check(err)
log.Debugf("declared Queue (%q %d messages, %d consumers)", queue.Name, queue.Messages, queue.Consumers)
tag := uuid.NewString()
log.Debugf("starting Consume (tag:%q)", tag)
deliveries, err := channel.Consume(
queue.Name, // name
tag, // consumerTag,
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
check(err)
client, err := NewTwitch(username, oauth, &cmd, channel)
if err != nil {
log.Panicln("NewTwitch(): ", err)
}
go handle(deliveries, client)
err = client.Connect()
if err != nil {
log.Panicln("client.Connect(): ", err)
}
//// wait for interrupt signal
//stopChan := make(chan os.Signal, 1)
//signal.Notify(stopChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
//for {
// select {
// case <-stopChan:
// // will close the deliveries channel
// err = channel.Cancel(tag, true)
// check(err)
//
// // wait for go handle(...)
// case <-deliveriesDoneChan:
// return
// }
//}
}
func handle(deliveries <-chan amqp.Delivery, client *Twitch) {
defer log.Println("AMQP Handler: Exiting from deliveries handler")
log.Debugln("Listening...")
for delivery := range deliveries {
if delivery.Body == nil {
return
}
_ = delivery.Ack(false)
if len(delivery.Body) == 0 {
log.Debugln("empty message. ignoring...")
continue
}
log.Infoln("DELIVERY:", string(delivery.Body))
var songInfo songInfo
err := parseSongInfo(delivery.Body, &songInfo)
if err != nil {
log.Errorln("handle > json.Unmarshal:", err)
continue
}
client.Say("/color Chocolate")
client.Say(fmt.Sprintf("/me %v - %v - %v (%v)",
songInfo.Artist, songInfo.Title,
strings.ReplaceAll(songInfo.SongUrl, "https://open.spotify.com/track/", "https://song.link/s/"),
commands.FormatDuration(time.Duration(songInfo.Length)*time.Second)))
createPoll(songInfo.Length)
}
}
func createPoll(ttl int64) {
r := redis.NewClient(&redis.Options{Addr: redisURL})
if r.Ping().Err() != nil {
log.Println("Twitch.createPoll > Sem redis...")
return
}
defer r.Close()
for _, setName := range []string{musicSkipPollName, musicKeepPollName} {
r.Del(setName)
r.SAdd(setName, ".")
r.Expire(setName, time.Duration(ttl)*time.Second)
}
}