From f8a725b7718d3814c3c15755e933dc82a15cfcfd Mon Sep 17 00:00:00 2001 From: Astoria Floyd Date: Fri, 19 Nov 2021 18:18:50 -0600 Subject: [PATCH] Major fixes to music system, youtube search working, a queue system, skip and stop, reload works, quit works, ping works. General refactors. --- modules/modules.txt | 5 ++- modules/pauseMusic/main.rb | 66 +++++++++++++++++++++++++++ modules/pingcommand/main.rb | 14 +++--- modules/playMusic/main.rb | 89 ++++++++++++++++++++----------------- modules/quit/main.rb | 20 +++++---- modules/reload/main.rb | 25 ++++------- modules/skipMusic/main.rb | 19 ++++++++ modules/stopMusic/main.rb | 24 ++++++++++ 8 files changed, 188 insertions(+), 74 deletions(-) create mode 100644 modules/pauseMusic/main.rb create mode 100644 modules/skipMusic/main.rb create mode 100644 modules/stopMusic/main.rb diff --git a/modules/modules.txt b/modules/modules.txt index e39930a..78f3b1e 100644 --- a/modules/modules.txt +++ b/modules/modules.txt @@ -1,5 +1,6 @@ pingcommand -pongcommand playMusic quit -reload \ No newline at end of file +reload +skipMusic +stopMusic \ No newline at end of file diff --git a/modules/pauseMusic/main.rb b/modules/pauseMusic/main.rb new file mode 100644 index 0000000..c375dee --- /dev/null +++ b/modules/pauseMusic/main.rb @@ -0,0 +1,66 @@ +def playMusic_initialize(bot, bang) + bot.message(start_with: "#{bang}Play ") do |event| + playMusic(bot, event, bang) + end + + bot.message(with_text: "#{bang}Play") do |event| + event.respond "Please provide youtube link!" + end +end + +def playMusic_defined() + puts "This should not run" +end + +def playMusic(bot, event, bang) + message = event.content + author = event.author + voice_channel = event.author.voice_channel.id + server = event.server + 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" + actuallyPlayMusic(bot, event, arguments, voice_channel, server) + else + event.respond "Invalid 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 diff --git a/modules/pingcommand/main.rb b/modules/pingcommand/main.rb index 083f2b6..e4461c7 100644 --- a/modules/pingcommand/main.rb +++ b/modules/pingcommand/main.rb @@ -1,9 +1,11 @@ -def pingcommand_initialize(bot, bang) - bot.message(with_text: "#{bang}Ping") do |event| - ping(bot, event) - end +def pingcommand_main(event) + event.respond "Ping!" end -def ping(bot, event) - event.respond 'Pong!' +def pingcommand_getName() + return "ping" +end + +def pingcommand_getCommand() + return "pingcommand_main" end \ No newline at end of file diff --git a/modules/playMusic/main.rb b/modules/playMusic/main.rb index 78cab91..c4bf220 100644 --- a/modules/playMusic/main.rb +++ b/modules/playMusic/main.rb @@ -1,65 +1,72 @@ -def playMusic_initialize(bot, bang) - bot.message(start_with: "#{bang}Play ") do |event| - playMusic(bot, event, bang) - end - - bot.message(with_text: "#{bang}Play") do |event| - event.respond "Please provide youtube link!" - end +def playMusic_getName() + return "play" end -def playMusic_defined() - puts "This should not run" +def playMusic_getCommand() + return "playMusic_main" end -def playMusic(bot, event, bang) +def playMusic_main(event) message = event.content - author = event.author - voice_channel = author.voice_channel.id - server = event.server - 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] + if message == '' + event.respond "Please add a search query" + else + voice_channel = event.author.voice_channel.id + arguments = message.sub("!Play ", '').chomp + #event.voice.play_file('./example.mp3') + if 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 else - $queue[voice_channel].push arguments - puts $queue[voice_channel] + actuallyPlayMusic(event, arguments, voice_channel) end - elsif arguments.include? "youtu" - actuallyPlayMusic(bot, event, arguments, voice_channel, server) - else - event.respond "Invalid 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) +def actuallyPlayMusic(event, arguments, voice_channel) + event.respond "Searching for song! This may take some time." 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) + if $voicebots.key?(voice_channel) == false + voiceBot = $bot.voice_connect(voice_channel) + $voicebots[voice_channel] = voiceBot + puts "Instance 1" + else + voiceBot = $voicebots[voice_channel] + puts "Instance 2" end - event.voice.play_file(tempFile) + tempFile = Dir::Tmpname.create(['youtubedlruby-', '.mp3']) {} + id = `yt-dlp --get-id 'ytsearch:#{arguments}'` + puts id + name = `yt-dlp --get-title #{id}` + puts name + cmd = "yt-dlp --extract-audio --audio-format mp3 --output " + tempFile + " https://www.youtube.com/watch?v=#{id}" + system(cmd) + puts "#{arguments}" + event.respond "Playing #{name.chomp}!" + voiceBot.play_file(tempFile) + #puts arguments + #puts 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) + actuallyPlayMusic(event, $queue[voice_channel].shift, voice_channel) else - bot.voice_destroy(server) + $voicebots.delete(voice_channel) + voiceBot.destroy() event.respond "Done playing!" ENV["#{voice_channel}"] = 'open' end else - bot.voice_destroy(server) + $voicebots.delete(voice_channel) + voiceBot.destroy() event.respond "Done playing!" ENV["#{voice_channel}"] = 'open' end diff --git a/modules/quit/main.rb b/modules/quit/main.rb index 34ee4a6..ddb6057 100644 --- a/modules/quit/main.rb +++ b/modules/quit/main.rb @@ -1,14 +1,16 @@ -def quit_initialize(bot, bang) - bot.message(with_text: "#{bang}Quit") do |event| - quit(bot, event) - end -end - -def quit(bot, event) +def quit_main(event) if event.user.id == 185533351866662913 - bot.send_message(event.channel.id, 'Bot is shutting down') - bot.stop + $bot.send_message(event.channel.id, 'Bot is shutting down') + $bot.stop else event.respond "You cannot quit this bot!" end +end + +def quit_getName() + return "quit" +end + +def quit_getCommand() + return "quit_main" end \ No newline at end of file diff --git a/modules/reload/main.rb b/modules/reload/main.rb index b76216d..277223e 100644 --- a/modules/reload/main.rb +++ b/modules/reload/main.rb @@ -1,19 +1,12 @@ -def reload_initialize(bot, bang) - bot.message(with_text: "#{bang}Reload") do |event| - reload(event) - end +def reload_getName() + return "reload" end -def reload(event) - event.respond 'Reloading!' - File.foreach("modules/modules.txt", chop: true) { |line| - mod = "./modules/" + line + "/main.rb" - code = "defined? #{line.delete("\n")}_initialize" - if eval(code) == nil - load mod.delete("\n") - send("#{line.delete("\n")}_initialize", bot, bang) - else - load mod.delete("\n") - end - } +def reload_getCommand() + return "reload_main" +end + +def reload_main(event) + emery_loadModules + event.respond "Reloading!" end \ No newline at end of file diff --git a/modules/skipMusic/main.rb b/modules/skipMusic/main.rb new file mode 100644 index 0000000..2e2a2a2 --- /dev/null +++ b/modules/skipMusic/main.rb @@ -0,0 +1,19 @@ +def skipMusic_getName() + return "skip" +end + +def skipMusic_getCommand() + return "skipMusic_main" +end + +def skipMusic_main(event) + voice_channel = event.author.voice_channel.id + if ENV["#{voice_channel}"] == 'playing' + event.respond "Skipping currently playing song" + voiceBot = $voicebots[voice_channel] + voiceBot.stop_playing() + ENV["#{voice_channel}"] = 'open' + else + event.respond "Bot not playing!" + end +end \ No newline at end of file diff --git a/modules/stopMusic/main.rb b/modules/stopMusic/main.rb new file mode 100644 index 0000000..8803e2c --- /dev/null +++ b/modules/stopMusic/main.rb @@ -0,0 +1,24 @@ +def stopMusic_getName() + return "stop" +end + +def stopMusic_getCommand() + return "stopMusic_main" +end + + +def stopMusic_main(event) + voice_channel = event.author.voice_channel.id + if ENV["#{voice_channel}"] == 'playing' + event.respond "Stopping music" + while $queue[voice_channel][0] != nil + puts "Stop 2" + $queue[voice_channel].pop + end + voiceBot = $voicebots[voice_channel] + voiceBot.destroy() + ENV["#{voice_channel}"] = 'open' + else + event.respond "Bot not playing!" + end +end \ No newline at end of file