-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
324 lines (277 loc) · 8.33 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"sync"
"time"
"unicode"
"github.com/Syltaris/DiscordCompanionBot/lib"
"github.com/bwmarrin/discordgo"
"github.com/cdipaolo/sentiment"
"github.com/joho/godotenv"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
)
const helpText = "You wan sum help? \nType in `@CompanionBot join me` and I will respond to what you say\nType in `@CompanionBot repeat after me` and I will echo your words :)"
var sentimentModel sentiment.Models
var s *discordgo.Session
func createPionRTPPacket(p *discordgo.Packet) *rtp.Packet {
return &rtp.Packet{
Header: rtp.Header{
Version: 2,
// Taken from Discord voice docs
PayloadType: 0x78,
SequenceNumber: p.Sequence,
Timestamp: p.Timestamp,
SSRC: p.SSRC,
},
Payload: p.Opus,
}
}
func UnpackPacketsToFile(rtpChannel chan rtp.Packet, ssrc uint32, messages chan string) {
var file media.Writer
var firstWrittenTimestamp, lastWrittenTimestamp int64 = 0, 0
var filename string
for {
now := time.Now().Unix()
select {
case rtpPacket, _ := <- rtpChannel:
if firstWrittenTimestamp != 0 && now - firstWrittenTimestamp > 15 { // ! init && speaking too long, so skip to write to file and avoid 20s limit
break
}
if file == nil { // indicates a new file to write for interval for
var err error
filename = fmt.Sprintf("%d-%d.ogg", rtpPacket.SSRC, now)
file, err = oggwriter.New(fmt.Sprintf("userAudio/%s", filename), 48000, 2)
if err != nil {
fmt.Printf("failed to create file %d.ogg, giving up on recording: %v\n", rtpPacket.SSRC, err)
return
}
// this is also when the file is first written
firstWrittenTimestamp = now
fmt.Println("write open:", rtpPacket.SSRC, rtpPacket.Timestamp, now)
}
lastWrittenTimestamp = now
err := file.WriteRTP(&rtpPacket)
if err != nil {
fmt.Printf("failed to write to file %d.ogg, giving up on recording: %v\n", rtpPacket.SSRC, err)
}
default: // check timeouts
if now - lastWrittenTimestamp > 1 { // delay
if file == nil { // skip if no file, cuz convo not started
break
}
fmt.Println("write close:", ssrc, lastWrittenTimestamp, now)
file.Close()
file = nil
messages <- filename
firstWrittenTimestamp = 0
}
}
}
}
func HandleVoiceReceive(v *discordgo.VoiceConnection, messages chan string, wg *sync.WaitGroup, speaking *bool) {
defer wg.Done()
c := v.OpusRecv
rtpChannels := sync.Map{} // make(map[uint32]chan rtp.Packet)
lastActivityTs := time.Now().Unix()
// receives p packets if incoming
// should trigger a listen for each start of packet?
for {
select {
case p := <- c:
if *speaking == true { // warning: speaking should be read only
break // don't bother listening if bot is talking
}
lastActivityTs = time.Now().Unix()
// process into rtp packets and send to channel
// if no channel, create 1
rtpChannel, ok := rtpChannels.Load(p.SSRC)
if !ok {
rtpChannel = make(chan rtp.Packet)
rtpChannels.Store(p.SSRC, rtpChannel)
go UnpackPacketsToFile(rtpChannel.(chan rtp.Packet), p.SSRC, messages)
}
rtpPacket := createPionRTPPacket(p)
rtpChannel.(chan rtp.Packet) <- *rtpPacket
default:
if time.Now().Unix() - lastActivityTs > 60 { // no activity for 1 min
fmt.Println("closing from inactivity...")
fetchAndCacheAndPlayMP3(v, "bye bye")
close(messages)
return
}
}
}
}
var positiveReplies = []string {
"omegalul",
"that is great",
"bao chicka wow wow",
"berry good job",
"lol",
"l m eh oh",
"you are the best",
"so beautiful",
"good good",
"awesome pawsome",
}
var negativeReplies = []string{
"oh noes",
"sorry",
"so sad",
"better luck next time",
"oopsies",
"resident sleeper zz",
"there there",
"pe pehands",
}
// horrible, should break this up to simpler logic
// cache elsewhere
func fetchAndCacheAndPlayMP3(v *discordgo.VoiceConnection, text string) {
stop := make(chan bool)
filename := "cache/" + text + ".mp3"
lib.GetMP3ForText(text)
lib.PlayAudioFile(v, filename, stop)
}
func HandleBotReply(v *discordgo.VoiceConnection, messages chan string, wg *sync.WaitGroup, echoMode bool, speaking *bool) {
defer wg.Done()
for filename := range messages {
// get input files and then use witai to get utterance
*speaking = true
ogg_filename := fmt.Sprintf("userAudio/" + filename)
mp3_filename, err := lib.OggToMp3(ogg_filename)
if err != nil {
fmt.Println("mp3 conv err:", err)
}
outputText := lib.WitAiCustomPostGetText(mp3_filename)
// suppose we got the output here, feed it to sentiment engine and see result
analysis := sentimentModel.SentimentAnalysis(outputText, sentiment.English)
fmt.Println("score:", analysis.Score, outputText)
if outputText == "" { // unknown prediction
//fetchAndCacheAndPlayMP3(v, "sorry ai do not understand")
// just skip
} else {
if echoMode {
filename := "cache/" + outputText + ".mp3" // warning: this is broken from fetchandplaymp3
fetchAndCacheAndPlayMP3(v, outputText)
e := os.Remove(filename)
if e!= nil {
fmt.Println("error removing userAudio:",err)
}
} else {
if analysis.Score == 1{
// play congrats sound
posText := positiveReplies[rand.Intn( len(positiveReplies))]
fetchAndCacheAndPlayMP3(v, posText)
} else {
// play oh noes sound
negText := negativeReplies[rand.Intn( len(negativeReplies))]
fetchAndCacheAndPlayMP3(v, negText)
}
}
}
// cleanup userAudio files once done
e := os.Remove(ogg_filename)
if e!= nil {
fmt.Println("error removing userAudio:",err)
}
e = os.Remove(mp3_filename)
if e!= nil {
fmt.Println("error removing userAudio:",err)
}
*speaking = false
}
fmt.Println("messages closed")
}
func eventHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if !strings.HasPrefix(m.Content, "<") {
return
}
halves := strings.Split(m.Content, ">")
uid := strings.TrimFunc(halves[0], func(r rune) bool {
return !unicode.IsNumber(r)
})
command := strings.ToLower(strings.ReplaceAll(halves[1], " ", ""))
fmt.Println(m.Author.ID, s.State.User.ID, m.Content, m.ChannelID)
if s.State.User.ID == uid && command == "joinme" || command == "repeatafterme" || command == "help" {
if command == "help" {
s.ChannelMessageSend(m.ChannelID, helpText)
return
}
guildId := m.GuildID
channels, err := s.GuildChannels(guildId)//which voice channel the user is on? or just any 1st voice channel of guild
var channelId string
for _, channel := range channels {
if channel.Type == discordgo.ChannelTypeGuildVoice {
channelId = channel.ID
break
}
// check if bot alr in channel, if so don't open another
_, exist := s.VoiceConnections[guildId]
if exist {
fmt.Println("error: alr have active voice connection")
return
}
}
if err != nil {
fmt.Println(err)
}
// hacky
echoMode := command == "repeatafterme"
go voiceEchoEventLoop(guildId, channelId, echoMode)
}
}
func voiceEchoEventLoop(guildId string, channelId string, echoMode bool) {
v, err := s.ChannelVoiceJoin(guildId, channelId, false, false);
if err != nil {
fmt.Println("can't join guild/channel:", err)
return
}
defer v.Close()
messages := make(chan string) // of p.SSRC-timestamp
speaking := false
wg := sync.WaitGroup{}
wg.Add(2)
go HandleVoiceReceive(v, messages, &wg, &speaking)
go HandleBotReply(v, messages, &wg, echoMode, &speaking)
wg.Wait()
fmt.Println("closing voice conn")
v.Disconnect()
}
func main() {
err := godotenv.Load(".env")
s, err = discordgo.New("Bot "+ os.Getenv("DISCORD_BOT_TOKEN"));
if err != nil {
fmt.Println("can't init Discord sesh:", err)
return
}
defer s.Close()
// configure listener's tracked intents?
s.Identify.Intents = discordgo.IntentsGuildVoiceStates | discordgo.IntentsGuildMessages
s.AddHandler(eventHandler)
err = s.Open()
if err != nil {
fmt.Println("can't open conn:", err)
return
}
err = s.UpdateGameStatus(0, "@CompanionBot help")
if err != nil {
fmt.Println("err setting presence", err)
}
// init sentiment model
sentimentModel, err = sentiment.Restore()
if err != nil {
panic(err)
}
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, os.Kill)
<-stop
log.Println("Gracefully shutdowning")
}