Skip to content

Commit

Permalink
Check if the user is in a voice channel before accepting some commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Nov 7, 2020
1 parent 74dc009 commit 555f694
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
72 changes: 62 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {

link := strings.TrimPrefix(m.Content, splittedMessage[0]+" ")

vs := findUserVoiceState(s, m)

// Check if user is not in a voice channel
if vs == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
break
}

if isValidURL(link) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, false)
downloadAndPlay(s, m.GuildID, vs.ChannelID, link, m.Author.Username, m.ChannelID, false)
} else {
if strings.HasPrefix(link, "spotify:playlist:") {
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, false)
spotifyPlaylist(s, m.GuildID, vs.ChannelID, m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, false)
} else {
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, false)
searchDownloadAndPlay(s, m.GuildID, vs.ChannelID, link, m.Author.Username, m.ChannelID, false)
}
}
break
Expand All @@ -211,26 +219,48 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {

link := strings.TrimPrefix(m.Content, splittedMessage[0]+" ")

vs := findUserVoiceState(s, m)

// Check if user is not in a voice channel
if vs == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
break
}

if isValidURL(link) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, true)
downloadAndPlay(s, m.GuildID, vs.ChannelID, link, m.Author.Username, m.ChannelID, true)
} else {
if strings.HasPrefix(link, "spotify:playlist:") {
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, true)
spotifyPlaylist(s, m.GuildID, vs.ChannelID, m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, true)
} else {
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, true)
searchDownloadAndPlay(s, m.GuildID, vs.ChannelID, link, m.Author.Username, m.ChannelID, true)
}
}
break

// Skips a song
case prefix + "skip", prefix + "s":
go deleteMessage(s, m)

// Check if user is not in a voice channel
if findUserVoiceState(s, m) == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
break
}

skip[m.GuildID] = true
break

// Clear the queue of the guild
case prefix + "clear", prefix + "c":
go deleteMessage(s, m)

// Check if user is not in a voice channel
if findUserVoiceState(s, m) == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
break
}

clear[m.GuildID] = true
skip[m.GuildID] = true
break
Expand Down Expand Up @@ -308,13 +338,27 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
case prefix + "summon":
go deleteMessage(s, m)

// Check if user is not in a voice channel
if findUserVoiceState(s, m) == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
return
}

// Check if the queue is empty
if len(queue[m.GuildID]) == 0 {
var err error

vs := findUserVoiceState(s, m)

// Check if user is not in a voice channel
if vs == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
return
}

server[m.GuildID].Lock()

vc[m.GuildID], err = s.ChannelVoiceJoin(m.GuildID, findUserVoiceState(s, m), false, false)
vc[m.GuildID], err = s.ChannelVoiceJoin(m.GuildID, vs.ChannelID, false, false)
if err != nil {
lit.Error("%s", err)
}
Expand Down Expand Up @@ -460,13 +504,21 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if custom[m.GuildID][lower] != "" {
go deleteMessage(s, m)

vs := findUserVoiceState(s, m)

// Check if user is not in a voice channel
if vs == nil {
sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Error", "You're not in a voice channel in this guild!").SetColor(0x7289DA).MessageEmbed, m.ChannelID)
return
}

if isValidURL(custom[m.GuildID][lower]) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false)
downloadAndPlay(s, m.GuildID, vs.ChannelID, custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false)
} else {
if strings.HasPrefix(custom[m.GuildID][lower], "spotify:playlist:") {
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, custom[m.GuildID][lower], m.ChannelID, false)
spotifyPlaylist(s, m.GuildID, vs.ChannelID, m.Author.Username, custom[m.GuildID][lower], m.ChannelID, false)
} else {
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false)
searchDownloadAndPlay(s, m.GuildID, vs.ChannelID, custom[m.GuildID][lower], m.Author.Username, m.ChannelID, false)
}
}
break
Expand Down
11 changes: 8 additions & 3 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@ func deleteMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
}

// Finds user current voice channel
func findUserVoiceState(session *discordgo.Session, m *discordgo.MessageCreate) string {
func findUserVoiceState(session *discordgo.Session, m *discordgo.MessageCreate) *discordgo.VoiceState {

for _, guild := range session.State.Guilds {
if guild.ID != m.GuildID {
continue
}

for _, vs := range guild.VoiceStates {
if vs.UserID == m.Author.ID {
return vs.ChannelID
return vs
}
}
}

return ""
return nil
}

// Checks if a string is a valid URL
Expand Down Expand Up @@ -215,6 +219,7 @@ func formatLongMessage(text []string) []string {
return append(output, buffer)
}

// Deletes an array of discordgo.Message
func deleteMessages(s *discordgo.Session, messages []discordgo.Message) {
for _, m := range messages {
_ = s.ChannelMessageDelete(m.ChannelID, m.ID)
Expand Down

0 comments on commit 555f694

Please sign in to comment.