main.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // We need information about guilds (which includes their channels),
  45. // messages and voice states.
  46. dg.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates)
  47. // Open the websocket and begin listening.
  48. err = dg.Open()
  49. if err != nil {
  50. fmt.Println("Error opening Discord session: ", err)
  51. }
  52. // Wait here until CTRL-C or other term signal is received.
  53. fmt.Println("Airhorn is now running. Press CTRL-C to exit.")
  54. sc := make(chan os.Signal, 1)
  55. signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
  56. <-sc
  57. // Cleanly close down the Discord session.
  58. dg.Close()
  59. }
  60. // This function will be called (due to AddHandler above) when the bot receives
  61. // the "ready" event from Discord.
  62. func ready(s *discordgo.Session, event *discordgo.Ready) {
  63. // Set the playing status.
  64. s.UpdateStatus(0, "!airhorn")
  65. }
  66. // This function will be called (due to AddHandler above) every time a new
  67. // message is created on any channel that the autenticated bot has access to.
  68. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  69. // Ignore all messages created by the bot itself
  70. // This isn't required in this specific example but it's a good practice.
  71. if m.Author.ID == s.State.User.ID {
  72. return
  73. }
  74. // check if the message is "!airhorn"
  75. if strings.HasPrefix(m.Content, "!airhorn") {
  76. // Find the channel that the message came from.
  77. c, err := s.State.Channel(m.ChannelID)
  78. if err != nil {
  79. // Could not find channel.
  80. return
  81. }
  82. // Find the guild for that channel.
  83. g, err := s.State.Guild(c.GuildID)
  84. if err != nil {
  85. // Could not find guild.
  86. return
  87. }
  88. // Look for the message sender in that guild's current voice states.
  89. for _, vs := range g.VoiceStates {
  90. if vs.UserID == m.Author.ID {
  91. err = playSound(s, g.ID, vs.ChannelID)
  92. if err != nil {
  93. fmt.Println("Error playing sound:", err)
  94. }
  95. return
  96. }
  97. }
  98. }
  99. }
  100. // This function will be called (due to AddHandler above) every time a new
  101. // guild is joined.
  102. func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
  103. if event.Guild.Unavailable {
  104. return
  105. }
  106. for _, channel := range event.Guild.Channels {
  107. if channel.ID == event.Guild.ID {
  108. _, _ = s.ChannelMessageSend(channel.ID, "Airhorn is ready! Type !airhorn while in a voice channel to play a sound.")
  109. return
  110. }
  111. }
  112. }
  113. // loadSound attempts to load an encoded sound file from disk.
  114. func loadSound() error {
  115. file, err := os.Open("airhorn.dca")
  116. if err != nil {
  117. fmt.Println("Error opening dca file :", err)
  118. return err
  119. }
  120. var opuslen int16
  121. for {
  122. // Read opus frame length from dca file.
  123. err = binary.Read(file, binary.LittleEndian, &opuslen)
  124. // If this is the end of the file, just return.
  125. if err == io.EOF || err == io.ErrUnexpectedEOF {
  126. err := file.Close()
  127. if err != nil {
  128. return err
  129. }
  130. return nil
  131. }
  132. if err != nil {
  133. fmt.Println("Error reading from dca file :", err)
  134. return err
  135. }
  136. // Read encoded pcm from dca file.
  137. InBuf := make([]byte, opuslen)
  138. err = binary.Read(file, binary.LittleEndian, &InBuf)
  139. // Should not be any end of file errors
  140. if err != nil {
  141. fmt.Println("Error reading from dca file :", err)
  142. return err
  143. }
  144. // Append encoded pcm data to the buffer.
  145. buffer = append(buffer, InBuf)
  146. }
  147. }
  148. // playSound plays the current buffer to the provided channel.
  149. func playSound(s *discordgo.Session, guildID, channelID string) (err error) {
  150. // Join the provided voice channel.
  151. vc, err := s.ChannelVoiceJoin(guildID, channelID, false, true)
  152. if err != nil {
  153. return err
  154. }
  155. // Sleep for a specified amount of time before playing the sound
  156. time.Sleep(250 * time.Millisecond)
  157. // Start speaking.
  158. vc.Speaking(true)
  159. // Send the buffer data.
  160. for _, buff := range buffer {
  161. vc.OpusSend <- buff
  162. }
  163. // Stop speaking
  164. vc.Speaking(false)
  165. // Sleep for a specificed amount of time before ending.
  166. time.Sleep(250 * time.Millisecond)
  167. // Disconnect from the provided voice channel.
  168. vc.Disconnect()
  169. return nil
  170. }