Skip to content

Instantly share code, notes, and snippets.

@yamamushi
Created July 9, 2017 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamamushi/c9f63afc1cee131abbe4f600cc737fc5 to your computer and use it in GitHub Desktop.
Save yamamushi/c9f63afc1cee131abbe4f600cc737fc5 to your computer and use it in GitHub Desktop.
discordgo ogg example
/*
I use the following to convert my audio source into ogg/opus :
avconv -i source.mp4 -f wav - | opusenc --bitrate 256 - output.opus
On OSX avconv is in libav, and opusenc is in opus-tools
$ brew install libav opus-tools
On Ubuntu:
$ sudo apt-get install libav-tools opus-tools -f
*/
import (
"github.com/jonas747/ogg"
"github.com/bwmarrin/discordgo"
)
func makeBuffer(path string) (output [][]byte){
reader, err := os.Open(path)
if err != nil {
fmt.Println(err.Error())
return
}
// Setup our ogg and packet decoders
oggdecoder := ogg.NewDecoder(reader)
packetdecoder := ogg.NewPacketDecoder(oggdecoder)
// Run through the packet decoder appending the bytes to our output [][]byte
for {
packet, _, err := packetdecoder.Decode()
if err != nil {
fmt.Println(err.Error())
return output
}
output = append(output, packet)
}
}
// This is just copied from the airhorn example
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
// check if the message is "!airhorn"
if strings.HasPrefix(m.Content, "!airhorn") {
// 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 {
data := makeBuffer("test.opus")
err = playSound(s, g.ID, vs.ChannelID, data)
if err != nil {
fmt.Println("Error playing sound:", err.Error())
return
}
return
}
}
}
}
func playSound(s *discordgo.Session, guildID, channelID string, buffer [][]byte) (err error) {
// Join the provided voice channel.
vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
if err != nil {
return err
}
// Sleep for a specified amount of time before playing the sound
time.Sleep(250 * time.Millisecond)
// Start speaking.
vc.Speaking(true)
// Send the buffer data.
// Send the buffer data.
fmt.Println("Making Buffer")
fmt.Println("Speaking")
for _, buff := range buffer {
vc.OpusSend <- buff
}
fmt.Println("Done")
// Stop speaking
vc.Speaking(false)
// Sleep for a specificed amount of time before ending.
time.Sleep(250 * time.Millisecond)
// Disconnect from the provided voice channel.
vc.Disconnect()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment