main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "encoding/binary"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/bwmarrin/discordgo"
  11. )
  12. func init() {
  13. flag.StringVar(&token, "t", "", "Account Token")
  14. flag.Parse()
  15. }
  16. var token string
  17. var buffer = make([][]byte, 0)
  18. func main() {
  19. if token == "" {
  20. fmt.Println("No token provided. Please run: airhorn -t <bot token>")
  21. return
  22. }
  23. // Load the sound file.
  24. err := loadSound()
  25. if err != nil {
  26. fmt.Println("Error loading sound: ", err)
  27. fmt.Println("Please copy $GOPATH/src/github.com/bwmarrin/examples/airhorn/airhorn.dca to this directory.")
  28. return
  29. }
  30. // Create a new Discord session using the provided token.
  31. dg, err := discordgo.New(token)
  32. if err != nil {
  33. fmt.Println("Error creating Discord session: ", err)
  34. return
  35. }
  36. // Register ready as a callback for the ready events.
  37. dg.AddHandler(ready)
  38. // Register messageCreate as a callback for the messageCreate events.
  39. dg.AddHandler(messageCreate)
  40. // Register guildCreate as a callback for the guildCreate events.
  41. dg.AddHandler(guildCreate)
  42. // Open the websocket and begin listening.
  43. err = dg.Open()
  44. if err != nil {
  45. fmt.Println("Error opening Discord session: ", err)
  46. }
  47. fmt.Println("Airhorn is now running. Press CTRL-C to exit.")
  48. // Simple way to keep program running until CTRL-C is pressed.
  49. <-make(chan struct{})
  50. return
  51. }
  52. func ready(s *discordgo.Session, event *discordgo.Ready) {
  53. // Set the playing status.
  54. s.UpdateStatus(0, "!airhorn")
  55. }
  56. // This function will be called (due to AddHandler above) every time a new
  57. // message is created on any channel that the autenticated bot has access to.
  58. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  59. if strings.HasPrefix(m.Content, "!airhorn") {
  60. // Find the channel that the message came from.
  61. c, err := s.State.Channel(m.ChannelID)
  62. if err != nil {
  63. // Could not find channel.
  64. return
  65. }
  66. // Find the guild for that channel.
  67. g, err := s.State.Guild(c.GuildID)
  68. if err != nil {
  69. // Could not find guild.
  70. return
  71. }
  72. // Look for the message sender in that guilds current voice states.
  73. for _, vs := range g.VoiceStates {
  74. if vs.UserID == m.Author.ID {
  75. err = playSound(s, g.ID, vs.ChannelID)
  76. if err != nil {
  77. fmt.Println("Error playing sound:", err)
  78. }
  79. return
  80. }
  81. }
  82. }
  83. }
  84. // This function will be called (due to AddHandler above) every time a new
  85. // guild is joined.
  86. func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
  87. if event.Guild.Unavailable != nil {
  88. return
  89. }
  90. for _, channel := range event.Guild.Channels {
  91. if channel.ID == event.Guild.ID {
  92. s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.")
  93. return
  94. }
  95. }
  96. }
  97. // loadSound attempts to load an encoded sound file from disk.
  98. func loadSound() error {
  99. file, err := os.Open("airhorn.dca")
  100. if err != nil {
  101. fmt.Println("Error opening dca file :", err)
  102. return err
  103. }
  104. var opuslen int16
  105. for {
  106. // Read opus frame length from dca file.
  107. err = binary.Read(file, binary.LittleEndian, &opuslen)
  108. // If this is the end of the file, just return.
  109. if err == io.EOF || err == io.ErrUnexpectedEOF {
  110. return nil
  111. }
  112. if err != nil {
  113. fmt.Println("Error reading from dca file :", err)
  114. return err
  115. }
  116. // Read encoded pcm from dca file.
  117. InBuf := make([]byte, opuslen)
  118. err = binary.Read(file, binary.LittleEndian, &InBuf)
  119. // Should not be any end of file errors
  120. if err != nil {
  121. fmt.Println("Error reading from dca file :", err)
  122. return err
  123. }
  124. // Append encoded pcm data to the buffer.
  125. buffer = append(buffer, InBuf)
  126. }
  127. }
  128. // playSound plays the current buffer to the provided channel.
  129. func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
  130. // Join the provided voice channel.
  131. vc, err := s.ChannelVoiceJoin(guildID, channelID, false, false)
  132. if err != nil {
  133. return err
  134. }
  135. // Sleep for a specified amount of time before playing the sound
  136. time.Sleep(250 * time.Millisecond)
  137. // Start speaking.
  138. vc.Speaking(true)
  139. // Send the buffer data.
  140. for _, buff := range buffer {
  141. vc.OpusSend <- buff
  142. }
  143. // Stop speaking
  144. vc.Speaking(false)
  145. // Sleep for a specificed amount of time before ending.
  146. time.Sleep(250 * time.Millisecond)
  147. // Disconnect from the provided voice channel.
  148. vc.Disconnect()
  149. return nil
  150. }