Skip to content

Commit

Permalink
automatically join voice channel in play command
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanleo committed Oct 16, 2019
1 parent 4b59276 commit 47f4ef0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 6 additions & 1 deletion commands/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
)

func join(s *discordgo.Session, m *discordgo.MessageCreate) {
voiceChannelInit(s, m)
guildSession := safeGetGuildSession(m.GuildID)
go guildSession.Loop()
}

func voiceChannelInit(s *discordgo.Session, m *discordgo.MessageCreate) {
guildSession := safeGetGuildSession(m.GuildID)
voiceState, err := util.FindUserVoiceState(s, m.GuildID, m.Author.ID)
if err != nil {
Expand Down Expand Up @@ -44,5 +50,4 @@ func join(s *discordgo.Session, m *discordgo.MessageCreate) {
updateCmd.Run()
}
guildSession.PlayURL(url, 0.5)
go guildSession.Loop()
}
9 changes: 6 additions & 3 deletions commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var tempSearchResultsCache = make(map[string][]*youtube.SearchResult)

func play(s *discordgo.Session, m *discordgo.MessageCreate) {
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
var isNotInVoiceChannel = guildSession.VoiceConnection == nil
if isNotInVoiceChannel {
voiceChannelInit(s, m)
}
var b strings.Builder
// Terminate if user has a pending search, forgets and uses /play again.
Expand Down Expand Up @@ -144,6 +144,9 @@ func play(s *discordgo.Session, m *discordgo.MessageCreate) {
}
delete(tempSearchResultsCache, mm.Author.ID)
awaitFuncRemove()
if isNotInVoiceChannel {
go guildSession.Loop()
}
})
}

Expand Down

0 comments on commit 47f4ef0

Please sign in to comment.