forked from GDGVIT/mombot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_audio.go
176 lines (151 loc) · 3.75 KB
/
record_audio.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
package main
import (
"context"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
"log"
"os"
"os/signal"
"strings"
"syscall"
)
var (
ctx = context.Background()
dgVoice *discordgo.Session
connection *discordgo.VoiceConnection
fileLocation uint32
)
func main() {
LoadEnv()
Token := GetEnvWithKey("Token")
var err error
dgVoice, err = discordgo.New("Bot " + Token)
if err != nil {
log.Println("error creating Discord session,", err)
return
}
defer dgVoice.Close()
dgVoice.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildVoiceStates)
err = dgVoice.Open()
if err != nil {
log.Println("error opening connection:", err)
return
}
handleMessages()
}
func GetEnvWithKey(key string) string {
return os.Getenv(key)
}
func LoadEnv() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
os.Exit(1)
}
}
func createPitonRTPPacket(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 handleVoice(c chan *discordgo.Packet, channel string) {
files := make(map[uint32]media.Writer)
for p := range c {
file, ok := files[p.SSRC]
if !ok {
fileLocation= p.SSRC
var err error
file, err = oggwriter.New(fmt.Sprintf("%d.ogg", p.SSRC), 48000, 2)
if err != nil {
log.Printf("failed to create file %d.ogg, giving up on recording: %v\n", p.SSRC, err)
return
}
files[p.SSRC] = file
}
rtp := createPitonRTPPacket(p)
err := file.WriteRTP(rtp)
if err != nil {
log.Printf("failed to write to file %d.ogg, giving up on recording: %v\n", p.SSRC, err)
}
}
for _, f := range files {
f.Close()
log.Println(fileLocation)
log.Println("Closed file")
}
}
func handleConfig(status bool, channelName string) {
if status {
GuildID := GetEnvWithKey("GuildID")
ChannelID := GetEnvWithKey("ChannelID")
channels, _ := dgVoice.GuildChannels(GuildID)
for _, c := range channels {
if c.Name == channelName {
ChannelID = c.ID
}
}
v, err := dgVoice.ChannelVoiceJoin(GuildID, ChannelID, true, false)
if err != nil {
log.Println("failed to join voice channel:", err)
return
}
connection=v
log.Println("JOINING CHANNEL")
handleVoice(v.OpusRecv,channelName)
} else {
log.Println("LEAVING CHANNEL")
close(connection.OpusRecv)
connection.Close()
connection.Disconnect()
AddtoS3(fmt.Sprint(fileLocation)+".ogg")
}
}
func handleMessages() {
Token := GetEnvWithKey("Token")
dg, err := discordgo.New("Bot " + Token)
if err != nil {
log.Println("error creating Discord session,", err)
return
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages)
err = dg.Open()
if err != nil {
log.Println("error opening connection,", err)
return
}
log.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
stri := strings.Split(m.Content, " ")
if len(stri) >= 1 {
if stri[1] == "join" {
stri = strings.Split(m.Content, "join ")
s.ChannelMessageSend(m.ChannelID, "Got it! Joining Channel: "+stri[1])
handleConfig(true, stri[1])
} else if stri[1] == "leave" {
stri = strings.Split(m.Content, "leave ")
s.ChannelMessageSend(m.ChannelID, "Got it! Leaving Channel: "+stri[1])
handleConfig(false, stri[1])
}
}
}