Skip to content

Commit

Permalink
Merge branch 'add-multiple-server-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanleo committed Oct 14, 2019
2 parents 6c13b5f + 4b14626 commit 9313b87
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 113 deletions.
53 changes: 44 additions & 9 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,51 @@ import (
youtube "google.golang.org/api/youtube/v3"
)

// CommandsMap a map of all the command handlers
var CommandsMap = make(map[string]func(*discordgo.Session, *discordgo.MessageCreate))
var Queue []models.QueueItem // current item = index 0
var VoiceConnection *discordgo.VoiceConnection
var youtubeService *youtube.Service
var previousAutoPlaylistListing *youtube.PlaylistItem
var MusicPlayer = &Player{
Close: make(chan struct{}),
Control: make(chan ControlMessage),

// MusicPlayer represents a music player
type MusicPlayer struct {
StartTime time.Time
IsPlaying bool
Close chan struct{}
Control chan ControlMessage
}

// GuildSession represents a guild voice session
type GuildSession struct {
GuildID string
Mutex sync.Mutex
Queue []models.QueueItem // current item = index 0
VoiceConnection *discordgo.VoiceConnection
previousAutoPlaylistListing *youtube.PlaylistItem
MusicPlayer MusicPlayer
}

func newGuildSession(guildID string) GuildSession {
return GuildSession{
GuildID: guildID,
Mutex: sync.Mutex{},
MusicPlayer: MusicPlayer{
Close: make(chan struct{}),
Control: make(chan ControlMessage),
},
}
}
var GameUpdateFunc func(game string)
var Mutex = sync.Mutex{}

// GuildSessionMap a map of all the guild sessions
var GuildSessionMap = make(map[string]*GuildSession)

func safeGetGuildSession(guildID string) *GuildSession {
if session, ok := GuildSessionMap[guildID]; ok {
return session
}
session := newGuildSession(guildID)
GuildSessionMap[guildID] = &session
return &session
}

var youtubeService *youtube.Service

func init() {
godotenv.Load()
Expand All @@ -48,6 +82,7 @@ func init() {
CommandsMap["pause"] = pause
CommandsMap["resume"] = resume
CommandsMap["help"] = help
CommandsMap["leave"] = leave
}

func deleteMessageDelayed(sess *discordgo.Session, msg *discordgo.Message) {
Expand Down
3 changes: 2 additions & 1 deletion commands/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

func help(s *discordgo.Session, m *discordgo.MessageCreate) {
guildSession := safeGetGuildSession(m.GuildID)
var b strings.Builder
for k := range CommandsMap {
b.WriteString(fmt.Sprintf("%s%s\n", os.Getenv("BOT_COMMAND_PREFIX"), k))
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s commands list:\n%s", m.Author.Mention(), b.String()))
SafeCheckPlay()
SafeCheckPlay(guildSession)
}
9 changes: 6 additions & 3 deletions commands/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"log"
"os"
"os/exec"

Expand All @@ -11,6 +12,7 @@ import (
)

func join(s *discordgo.Session, m *discordgo.MessageCreate) {
guildSession := safeGetGuildSession(m.GuildID)
voiceState, err := util.FindUserVoiceState(s, m.Author.ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s you are not in a voice channel", m.Author.Mention()))
Expand All @@ -26,15 +28,16 @@ func join(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s error occurred: %s", m.Author.Mention(), err))
return
}
VoiceConnection = voiceChannel
guildSession.VoiceConnection = voiceChannel
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s joined '%s'", m.Author.Mention(), channel.Name))
log.Printf(fmt.Sprintf("%s joined '%s' guild '%s'\n", m.Author.Mention(), channel.Name, m.GuildID))
url, _ := googletts.GetTTSURL("Ready", "en")
if os.Getenv("BOT_UPDATE_YTDL") == "true" {
updateCmd := exec.Command("/usr/bin/curl", "-L", "https://yt-dl.org/downloads/latest/youtube-dl", "-o", "/usr/local/bin/youtube-dl")
updateCmd.Stdout = os.Stdout
updateCmd.Stderr = os.Stderr
updateCmd.Run()
}
MusicPlayer.Play(url, "0.5")
SafeCheckPlay()
guildSession.Play(url, "0.5")
SafeCheckPlay(guildSession)
}
37 changes: 37 additions & 0 deletions commands/leave.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package commands

import (
"fmt"

"github.com/bottleneckco/discord-radio/util"
"github.com/bwmarrin/discordgo"
)

func leave(s *discordgo.Session, m *discordgo.MessageCreate) {
if guildSession, ok := GuildSessionMap[m.GuildID]; ok {
voiceState, err := util.FindUserVoiceState(s, m.Author.ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s you are not in a voice channel", m.Author.Mention()))
return
}
channel, err := s.Channel(voiceState.ChannelID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s error occurred: %s", m.Author.Mention(), err))
return
}
// Actual disconnect code
var tempVoiceConn = guildSession.VoiceConnection
guildSession.VoiceConnection = nil

guildSession.Mutex.Lock()
guildSession.Queue = guildSession.Queue[0:0]
guildSession.Mutex.Unlock()
guildSession.MusicPlayer.Close <- struct{}{}
tempVoiceConn.Disconnect()
delete(GuildSessionMap, m.GuildID)

s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s left '%s'", m.Author.Mention(), channel.Name))
} else {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s not in voice channel", m.Author.Mention()))
}
}
5 changes: 3 additions & 2 deletions commands/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

func pause(s *discordgo.Session, m *discordgo.MessageCreate) {
if !MusicPlayer.IsPlaying {
guildSession := safeGetGuildSession(m.GuildID)
if !guildSession.MusicPlayer.IsPlaying {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s nothing to pause", m.Author.Mention()))
return
}
MusicPlayer.Control <- Pause
guildSession.MusicPlayer.Control <- Pause
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s paused", m.Author.Mention()))
}
19 changes: 10 additions & 9 deletions commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
var tempSearchResultsCache = make(map[string][]*youtube.SearchResult)

func play(s *discordgo.Session, m *discordgo.MessageCreate) {
if VoiceConnection == nil {
guildSession := safeGetGuildSession(m.GuildID)
if guildSession.VoiceConnection == nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s I am not in any voice channel", m.Author.Mention()))
return
}
Expand Down Expand Up @@ -62,18 +63,18 @@ func play(s *discordgo.Session, m *discordgo.MessageCreate) {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Error occurred: %s", err))
return
}
Mutex.Lock()
guildSession.Mutex.Lock()
for _, youtubeListing := range youtubeListings.Items {
Queue = append(Queue, models.QueueItem{
guildSession.Queue = append(guildSession.Queue, models.QueueItem{
Title: youtubeListing.Snippet.Title,
ChannelTitle: youtubeListing.Snippet.ChannelTitle,
Author: m.Author.Username,
VideoID: youtubeListing.Id,
Thumbnail: youtubeListing.Snippet.Thumbnails.Default.Url,
})
}
Mutex.Unlock()
SafeCheckPlay()
guildSession.Mutex.Unlock()
SafeCheckPlay(guildSession)
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s enqueued %d videos`\n", m.Author.Mention(), len(videoIDs)))
} else {
maxResults, _ := strconv.ParseInt(os.Getenv("BOT_NUM_RESULTS"), 10, 64)
Expand Down Expand Up @@ -112,15 +113,15 @@ func play(s *discordgo.Session, m *discordgo.MessageCreate) {
return
} else if err == nil {
chosenItem := tempSearchResultsCache[mm.Author.ID][choice-1]
Mutex.Lock()
Queue = append(Queue, models.QueueItem{
guildSession.Mutex.Lock()
guildSession.Queue = append(guildSession.Queue, models.QueueItem{
Title: chosenItem.Snippet.Title,
ChannelTitle: chosenItem.Snippet.ChannelTitle,
Author: mm.Author.Username,
VideoID: chosenItem.Id.VideoId,
Thumbnail: chosenItem.Snippet.Thumbnails.Default.Url,
})
Mutex.Unlock()
guildSession.Mutex.Unlock()
ss.ChannelMessageSendEmbed(mm.ChannelID, &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: "Added to queue",
Expand All @@ -143,7 +144,7 @@ func play(s *discordgo.Session, m *discordgo.MessageCreate) {
}
delete(tempSearchResultsCache, mm.Author.ID)
awaitFuncRemove()
SafeCheckPlay()
SafeCheckPlay(guildSession)
})
}

Expand Down
Loading

0 comments on commit 9313b87

Please sign in to comment.