From 9988ec8f6987e84d1d78885458230604a0f3f019 Mon Sep 17 00:00:00 2001 From: Astoria Floyd Date: Sun, 31 Oct 2021 15:29:10 -0500 Subject: [PATCH] Added queue to playMusic --- Emery.rb | 5 ++--- modules/modules.txt | 3 ++- modules/playMusic/main.rb | 47 +++++++++++++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/Emery.rb b/Emery.rb index 7b75b2b..a91036c 100755 --- a/Emery.rb +++ b/Emery.rb @@ -10,13 +10,12 @@ bang = '!' def loadCommands(bot, bang) File.foreach("modules/modules.txt", chop: true) { |line| mod = "./modules/" + line + "/main.rb" - require mod.delete("\n") + load mod.delete("\n") send("#{line.delete("\n")}", bot, bang) } end - +$queue = {} loadCommands(bot, bang) - #require './pingcommand.rb' #main(bot) diff --git a/modules/modules.txt b/modules/modules.txt index 626038b..2e8b83b 100644 --- a/modules/modules.txt +++ b/modules/modules.txt @@ -1,3 +1,4 @@ pingcommand pongcommand -playMusic \ No newline at end of file +playMusic +quit \ No newline at end of file diff --git a/modules/playMusic/main.rb b/modules/playMusic/main.rb index f45b3c9..939696c 100644 --- a/modules/playMusic/main.rb +++ b/modules/playMusic/main.rb @@ -4,20 +4,22 @@ def playMusic(bot, bang) author = event.author voice_channel = author.voice_channel.id server = event.server - bot.voice_connect(voice_channel) arguments = message.sub("#{bang}Play ", '').split.first #event.voice.play_file('./example.mp3') if arguments.include? "&list" event.respond "Do not play playlists. They are unsupported." + elsif ENV["#{voice_channel}"] == 'playing' + event.respond "Music currently playing. Adding to queue" + if $queue.key?(voice_channel) == false + $queue[voice_channel] = Array.new + $queue[voice_channel].push arguments + puts $queue[voice_channel] + else + $queue[voice_channel].push arguments + puts $queue[voice_channel] + end elsif arguments.include? "youtu" - event.respond "Playing song! This may take some time." - tempFile = Dir::Tmpname.create(['youtubedlruby-', '.mp3']) {} - cmd = "yt-dlp --extract-audio --audio-format mp3 --output " + tempFile + " " + arguments - system(cmd) - event.voice.play_file(tempFile) - File.delete(tempFile) if File.exist?(tempFile) - bot.voice_destroy(server) - event.respond "Done playing!" + actuallyPlayMusic(bot, event, arguments, voice_channel, server) else event.respond "Invalid youtube link!" end @@ -25,4 +27,31 @@ def playMusic(bot, bang) bot.message(with_text: "#{bang}Play") do |event| event.respond "Please provide youtube link!" end +end + +def actuallyPlayMusic(bot, event, arguments, voice_channel, server) + event.respond "Playing song! This may take some time." + bot.voice_connect(voice_channel) + ENV["#{voice_channel}"] = 'playing' + tempFile = Dir::Tmpname.create(['youtubedlruby-', '.mp3']) {} + cmd = "yt-dlp --extract-audio --audio-format mp3 --output " + tempFile + " " + arguments + if arguments.include? "youtu" + system(cmd) + end + event.voice.play_file(tempFile) + File.delete(tempFile) if File.exist?(tempFile) + if $queue.key?(voice_channel) == true + if $queue[voice_channel][0] != nil + event.respond "Queue exists. Playing next song." + actuallyPlayMusic(bot, event, $queue[voice_channel].shift, voice_channel, server) + else + bot.voice_destroy(server) + event.respond "Done playing!" + ENV["#{voice_channel}"] = 'open' + end + else + bot.voice_destroy(server) + event.respond "Done playing!" + ENV["#{voice_channel}"] = 'open' + end end \ No newline at end of file