main.go 4.8 KB

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