main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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", "", "Bot 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 bot token.
  31. dg, err := discordgo.New("Bot " + 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 {
  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. file.Close()
  111. if err != nil {
  112. return err
  113. }
  114. return nil
  115. }
  116. if err != nil {
  117. fmt.Println("Error reading from dca file :", err)
  118. return err
  119. }
  120. // Read encoded pcm from dca file.
  121. InBuf := make([]byte, opuslen)
  122. err = binary.Read(file, binary.LittleEndian, &InBuf)
  123. // Should not be any end of file errors
  124. if err != nil {
  125. fmt.Println("Error reading from dca file :", err)
  126. return err
  127. }
  128. // Append encoded pcm data to the buffer.
  129. buffer = append(buffer, InBuf)
  130. }
  131. }
  132. // playSound plays the current buffer to the provided channel.
  133. func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
  134. // Join the provided voice channel.
  135. vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
  136. if err != nil {
  137. return err
  138. }
  139. // Sleep for a specified amount of time before playing the sound
  140. time.Sleep(250 * time.Millisecond)
  141. // Start speaking.
  142. _ = vc.Speaking(true)
  143. // Send the buffer data.
  144. for _, buff := range buffer {
  145. vc.OpusSend <- buff
  146. }
  147. // Stop speaking
  148. _ = vc.Speaking(false)
  149. // Sleep for a specificed amount of time before ending.
  150. time.Sleep(250 * time.Millisecond)
  151. // Disconnect from the provided voice channel.
  152. _ = vc.Disconnect()
  153. return nil
  154. }