Guest User

Untitled

a guest
Jul 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "strings"
  9. "sync"
  10. "syscall"
  11. "time"
  12.  
  13. "github.com/bwmarrin/discordgo"
  14. )
  15.  
  16. const (
  17. MaxQueueSize int = 6
  18. )
  19.  
  20. var (
  21. queues = make(map[string][]*PlayRequest)
  22. dg *discordgo.Session
  23. err error
  24. m sync.Mutex
  25. )
  26.  
  27. type PlayRequest struct {
  28. ChannelID string
  29. GuildID string
  30. Sound uint
  31. }
  32.  
  33. func main() {
  34. token := os.Getenv("BOT_TOKEN")
  35.  
  36. dg, err = discordgo.New("Bot " + token)
  37. if err != nil {
  38. panic(err)
  39. }
  40.  
  41. dg.LogLevel = discordgo.LogDebug
  42.  
  43. dg.AddHandler(messageCreate)
  44.  
  45. err = dg.Open()
  46. if err != nil {
  47. panic(err)
  48. }
  49.  
  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.  
  55. dg.Close()
  56. }
  57.  
  58. func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  59.  
  60. if m.Author.ID == s.State.User.ID {
  61. return
  62. }
  63.  
  64. if strings.HasPrefix(m.Content, "!join") {
  65.  
  66. c, err := s.State.Channel(m.ChannelID)
  67. if err != nil {
  68. // Could not find channel.
  69. return
  70. }
  71.  
  72. g, err := s.State.Guild(c.GuildID)
  73. if err != nil {
  74. // Could not find guild.
  75. return
  76. }
  77.  
  78. for _, vs := range g.VoiceStates {
  79. if vs.UserID == m.Author.ID {
  80. requestPlaySound(g.ID, vs.ChannelID, 1)
  81. return
  82. }
  83. }
  84. }
  85. }
  86.  
  87. func requestPlaySound(guildID, channelID string, soundID uint) {
  88. item := &PlayRequest{
  89. ChannelID: channelID,
  90. GuildID: guildID,
  91. Sound: soundID,
  92. }
  93.  
  94. // If there is a queue setup there is alaso a player running, so just add it to the queue then
  95. m.Lock()
  96. if queue, ok := queues[guildID]; ok {
  97. if len(queue) < MaxQueueSize {
  98. queues[guildID] = append(queue, item)
  99. }
  100. } else {
  101. queues[guildID] = []*PlayRequest{item}
  102. go runPlayer(guildID)
  103. }
  104. m.Unlock()
  105. }
  106.  
  107. func runPlayer(guildID string) {
  108. var lastChannel string
  109. var vc *discordgo.VoiceConnection
  110. for {
  111. m.Lock()
  112. var item *PlayRequest
  113.  
  114. // Get the next item in the queue or quit life
  115. if queue, ok := queues[guildID]; ok && len(queue) > 0 {
  116. item = queue[0]
  117. queues[guildID] = queue[1:]
  118. } else {
  119. break
  120. }
  121.  
  122. m.Unlock()
  123. // Should probably to changechannel but eh..
  124. if lastChannel != item.ChannelID && vc != nil {
  125. vc.Disconnect()
  126. vc = nil
  127. }
  128.  
  129. var err error
  130. vc, err = playSound(vc, dg, item)
  131. if err != nil {
  132. log.Println(err)
  133. }
  134. lastChannel = item.ChannelID
  135. }
  136. if vc != nil {
  137. vc.Disconnect()
  138. }
  139.  
  140. // When we break out, playqueuemutex is locked
  141. delete(queues, guildID)
  142. m.Unlock()
  143. }
  144.  
  145. func playSound(vc *discordgo.VoiceConnection, session *discordgo.Session, item *PlayRequest) (*discordgo.VoiceConnection, error) {
  146. // Either use the passed voice connection, or create a new one
  147. if vc == nil || !vc.Ready {
  148. vc, err = session.ChannelVoiceJoin(item.GuildID, item.ChannelID, false, true)
  149. if err != nil {
  150. return nil, err
  151. }
  152. vc.Speaking(true)
  153. }
  154.  
  155. // Sending voice mock
  156. time.Sleep(time.Second)
  157.  
  158. return vc, nil
  159. }
Add Comment
Please, Sign In to add comment