forked from BryanSLam/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_writer.go
174 lines (147 loc) · 4.07 KB
/
discord_writer.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
package discordbot
import (
"bytes"
"encoding/binary"
"io"
"log"
// "time"
dg "github.com/bwmarrin/discordgo"
"github.com/layeh/gopus"
)
const (
channels int = 2 // 1 for mono, 2 for stereo
frameRate int = 48000 // audio sampling rate
frameSize int = 960 // uint16 size of each audio frame
maxBytes int = (frameSize * 2) * 2 // max size of opus data
)
// DiscordWriter implements io.Writer
type DiscordWriter struct {
session *dg.Session
messageCreate *dg.MessageCreate
channelID string
guildID string
isVoiceChannel bool
}
// DiscordWriterOption configures additional properties on DiscordWriter
type DiscordWriterOption func(dw *DiscordWriter) error
// WithIsVoiceChannel set flag for voice channel
func WithIsVoiceChannel(isVoiceChannel bool) DiscordWriterOption {
return func(dw *DiscordWriter) error {
dw.isVoiceChannel = isVoiceChannel
return nil
}
}
// WithGuildID sets the guild ID
func WithGuildID(guildID string) DiscordWriterOption {
return func(dw *DiscordWriter) error {
dw.guildID = guildID
return nil
}
}
// NewDiscordWriter returns struct that implements io.Writer
func NewDiscordWriter(s *dg.Session, m *dg.MessageCreate, ch string, options ...DiscordWriterOption) *DiscordWriter {
dw := &DiscordWriter{
session: s,
messageCreate: m,
channelID: ch,
}
for _, option := range options {
if err := option(dw); err != nil {
return nil
}
}
return dw
}
// Write sends the bytes to the Discord channel which can be text or voice channel
func (w *DiscordWriter) Write(b []byte) (int, error) {
// 1. try voice channel
// 2. try text channel
// 3. default to respond to text channel
if w.isVoiceChannel {
mute := false
deaf := false
voiceChannel, err := w.session.ChannelVoiceJoin(w.guildID, w.channelID, mute, deaf)
if err != nil {
log.Println("voice channel error")
log.Println(err)
return 0, err
}
// Send "speaking" packet over the voice websocket
err = voiceChannel.Speaking(true)
if err != nil {
return 0, err
}
// Send not "speaking" packet over the websocket when we finish
defer func() {
voiceChannel.Speaking(false)
}()
send := make(chan []int16, 2)
defer close(send)
close := make(chan bool)
go func() {
sendPCM(voiceChannel, send)
close <- true
}()
buf := bytes.NewReader(b)
for {
// read data from ffmpeg stdout
audiobuf := make([]int16, frameSize*channels)
err = binary.Read(buf, binary.LittleEndian, &audiobuf)
if err == io.EOF || err == io.ErrUnexpectedEOF {
return 0, err
}
if err != nil {
return 0, err
}
// Send received PCM to the sendPCM channel
select {
case send <- audiobuf:
case <-close:
return 0, err
}
}
} else if w.channelID != "" {
// if writer has channel then send bytes to it
w.session.ChannelMessageSend(w.channelID, string(b))
} else {
// else send bytes to respond to the channel where message came from
w.session.ChannelMessageSend(w.messageCreate.ChannelID, string(b))
}
return len(b), nil
}
// ChannelMessageSend send string to channel
func (w *DiscordWriter) ChannelMessageSend(content string) (*dg.Message, error) {
return w.session.ChannelMessageSend(w.channelID, content)
}
// Pin takes slice of bytes and creates a PinMessage to send to the channel
func (w *DiscordWriter) Pin(messageID string) error {
return w.session.ChannelMessagePin(w.channelID, messageID)
}
// SendPCM will receive on the provided channel encode
// received PCM data into Opus then send that to Discordgo
func sendPCM(v *dg.VoiceConnection, pcm <-chan []int16) {
if pcm == nil {
return
}
opusEncoder, err := gopus.NewEncoder(frameRate, channels, gopus.Audio)
if err != nil {
return
}
for {
// read pcm from chan, exit if channel is closed.
recv, ok := <-pcm
if !ok {
return
}
// try encoding pcm frame with Opus
opus, err := opusEncoder.Encode(recv, frameSize, maxBytes)
if err != nil {
return
}
if v.Ready == false || v.OpusSend == nil {
return
}
// send encoded opus data to the sendOpus channel
v.OpusSend <- opus
}
}