-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedia.go
109 lines (90 loc) · 2.28 KB
/
media.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
package main
import (
"os"
"fmt"
"encoding/binary"
"io"
"github.com/bwmarrin/discordgo"
"time"
)
func PlayAirhorn(s *discordgo.Session, m *discordgo.MessageCreate) {
// Find the channel that the message came from.
c, err := s.State.Channel(m.ChannelID)
if err != nil {
// Could not find channel
return
}
// Find the guild for that channel.
g, err := s.State.Guild(c.GuildID)
if err != nil {
// Could not find guild
return
}
// Look for the message sender in that guild's current voice states.
for _, vs := range g.VoiceStates {
if vs.UserID == m.Author.ID {
err = playSound(s, g.ID, vs.ChannelID, buffer)
if err != nil {
fmt.Println("Error playing sound: ", err)
}
return
}
}
}
func loadSound(buffer *[][]byte) (error) {
file, err := os.Open("assets/airhorn.dca")
if err != nil {
fmt.Println("Error opening dca file :", err)
return err
}
var opuslen int16
for {
// Read opus frame length from dca file.
err = binary.Read(file, binary.LittleEndian, &opuslen)
// If this is the end of the file, just return.
if err == io.EOF || err == io.ErrUnexpectedEOF {
err := file.Close()
if err != nil {
return err
}
return nil
}
if err != nil {
fmt.Println("Error reading from dca file: ", err)
return err
}
// Read encoded pcm from dca file.
InBuf := make([]byte, opuslen)
err = binary.Read(file, binary.LittleEndian, &InBuf)
// Should not be any EOF errors
if err != nil {
fmt.Println("Error reading from dca file: ", err)
return err
}
// Append encoded pcm data to the buffer.
*buffer = append(*buffer, InBuf)
}
}
// plays the current buffer to the provided channel.
func playSound(s *discordgo.Session, guildID, channelID string, buffer [][]byte) (err error) {
// Join the provided channel
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
return err
}
// Sleep for a specified ammount of time before playing the sound
time.Sleep(250 * time.Millisecond)
// Start speaking
vc.Speaking(true)
// Send the buffer data
for _, buff := range buffer {
vc.OpusSend <- buff
}
// Stop speaking
vc.Speaking(false)
// Sleep for a specified amount of time before ending.
time.Sleep(250 * time.Millisecond)
// Disconnect from the provided channel.
vc.Disconnect()
return nil
}