Skip to content

Commit

Permalink
New shuffle command
Browse files Browse the repository at this point in the history
Plays songs from a playlist in a random order
  • Loading branch information
TheTipo01 committed Sep 11, 2020
1 parent 7ed7b73 commit 5440168
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
16 changes: 11 additions & 5 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

//Download and plays a song from a youtube link
func downloadAndPlay(s *discordgo.Session, guildID, channelID, link, user, txtChannel string) {
func downloadAndPlay(s *discordgo.Session, guildID, channelID, link, user, txtChannel string, random bool) {
go sendAndDeleteEmbed(s, NewEmbed().SetTitle(s.State.User.Username).AddField("Enqueued", link).SetColor(0x7289DA).MessageEmbed, txtChannel)

//Check if the song is the db, to speedup things
Expand All @@ -32,6 +32,12 @@ func downloadAndPlay(s *discordgo.Session, guildID, channelID, link, user, txtCh
strOut := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")

var ytdl YoutubeDL

//If we want to play the song in a random order, we just shuffle the slice
if random {
strOut = shuffle(strOut)
}

//We parse every track as individual json, because youtube-dl
for _, singleJson := range strOut {
_ = json.Unmarshal([]byte(singleJson), &ytdl)
Expand Down Expand Up @@ -75,20 +81,20 @@ func downloadAndPlay(s *discordgo.Session, guildID, channelID, link, user, txtCh
}

//Searches a song from the query on youtube
func searchDownloadAndPlay(s *discordgo.Session, guildID, channelID, query, user, txtChannel string) {
func searchDownloadAndPlay(s *discordgo.Session, guildID, channelID, query, user, txtChannel string, random bool) {
//Gets video id
out, _ := exec.Command("youtube-dl", "--get-id", "ytsearch:\""+query+"\"").Output()
ids := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")

//Calls download and play for every id we get
for _, id := range ids {
downloadAndPlay(s, guildID, channelID, "https://www.youtube.com/watch?v="+id, user, txtChannel)
downloadAndPlay(s, guildID, channelID, "https://www.youtube.com/watch?v="+id, user, txtChannel, random)
}

}

//Enqueues song from a spotify playlist, searching them on youtube
func spotifyPlaylist(s *discordgo.Session, guildID, channelID, user, playlistId, txtChannel string) {
func spotifyPlaylist(s *discordgo.Session, guildID, channelID, user, playlistId, txtChannel string, random bool) {

//We get the playlist from it's link
playlist, err := client.GetPlaylist(spotify.ID(strings.TrimPrefix(playlistId, "spotify:playlist:")))
Expand All @@ -99,7 +105,7 @@ func spotifyPlaylist(s *discordgo.Session, guildID, channelID, user, playlistId,

//We parse every single song, searching it on youtube
for _, track := range playlist.Tracks.Tracks {
go searchDownloadAndPlay(s, guildID, channelID, track.Track.Name+" - "+track.Track.Artists[0].Name, user, txtChannel)
go searchDownloadAndPlay(s, guildID, channelID, track.Track.Name+" - "+track.Track.Artists[0].Name, user, txtChannel, random)
}

}
Expand Down
35 changes: 27 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/zmb3/spotify"
"golang.org/x/oauth2/clientcredentials"
"log"
"math/rand"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -51,6 +52,7 @@ var (
)

func init() {
rand.Seed(time.Now().UnixNano())

viper.SetConfigName("config")
viper.SetConfigType("yml")
Expand Down Expand Up @@ -172,12 +174,29 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
link = strings.TrimPrefix(link, prefix+"p ")

if isValidUrl(link) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID)
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), 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)
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), 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)
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, false)
}
}
break

//Randomly plays a song (or a playlist)
case prefix + "shuffle":
go deleteMessage(s, m)

link := strings.TrimPrefix(m.Content, prefix+"shuffle ")

if isValidUrl(link) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), 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)
} else {
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), link, m.Author.Username, m.ChannelID, true)
}
}
break
Expand Down Expand Up @@ -267,7 +286,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
case prefix + "help", prefix + "h":
go deleteMessage(s, m)

message := "Supported commands:\n```" + prefix + "play <link> - Plays a song from youtube or spotify playlist\n" + prefix + "queue - Returns all the songs in the server queue\n" + prefix + "summon - Make the bot join your voice channel\n" + prefix + "disconnect - Disconnect the bot from the voice channel\n" + prefix + "restart - Restarts the bot\n" + prefix + "pause - Pauses current song\n" + prefix + "resume - Resumes current song\n" + prefix + "custom <command> <song/playlist> - Creates a shortcut for a song/playlist\n" + prefix + "rmcustom <command> - Removes a custom command\n" + prefix + "lyrics <song> - Tries to search for lyrics of the specified song, or if not specified searches for the title of the currently playing song```"
message := "Supported commands:\n```" + prefix + "play <link> - Plays a song from youtube or spotify playlist\n" + prefix + "queue - Returns all the songs in the server queue\n" + prefix + "summon - Make the bot join your voice channel\n" + prefix + "disconnect - Disconnect the bot from the voice channel\n" + prefix + "restart - Restarts the bot\n" + prefix + "pause - Pauses current song\n" + prefix + "resume - Resumes current song\n" + prefix + "custom <command> <song/playlist> - Creates a shortcut for a song/playlist\n" + prefix + "rmcustom <command> - Removes a custom command\n" + prefix + "lyrics <song> - Tries to search for lyrics of the specified song, or if not specified searches for the title of the currently playing song\n"+prefix+"shuffle <playlist> - Randomly plays song from that playlist```"
//If we have custom commands, we add them to the help message
if len(custom[m.GuildID]) > 0 {
message += "\nCustom commands:\n```"
Expand Down Expand Up @@ -347,7 +366,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
song := strings.TrimPrefix(strings.TrimPrefix(m.Content, prefix+"lyrics"), " ")

if song == "" {
song=queue[m.GuildID][0].title
song = queue[m.GuildID][0].title
}

if len(queue[m.GuildID]) > 0 {
Expand Down Expand Up @@ -386,12 +405,12 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
go deleteMessage(s, m)

if isValidUrl(command.song) {
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID)
downloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID, false)
} else {
if strings.HasPrefix(command.song, "spotify:playlist:") {
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID)
spotifyPlaylist(s, m.GuildID, findUserVoiceState(s, m), m.Author.Username, strings.TrimPrefix(m.Content, prefix+"spotify "), m.ChannelID, false)
} else {
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID)
searchDownloadAndPlay(s, m.GuildID, findUserVoiceState(s, m), command.song, m.Author.Username, m.ChannelID, false)
}
}
break
Expand Down
11 changes: 11 additions & 0 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/bwmarrin/discordgo"
"log"
"math/rand"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -223,3 +224,13 @@ func deleteMessages(s *discordgo.Session, messages []discordgo.Message) {
_ = s.ChannelMessageDelete(m.ChannelID, m.ID)
}
}

//Shuffles a slice of strings
func shuffle(a []string) []string {
final := make([]string, len(a))

for i, v := range rand.Perm(len(a)) {
final[v] = a[i]
}
return final
}

0 comments on commit 5440168

Please sign in to comment.