From 01119918184798e8ab6ceb5172ade265ad2700b0 Mon Sep 17 00:00:00 2001 From: Marrub Date: Fri, 11 Aug 2017 21:25:32 -0400 Subject: [PATCH] Move modules to a subfolder --- source/mod_base.rb | 72 ------------------- source/modules/mod_audio.rb | 107 ++++++++++++++++++++++++++++ source/{ => modules}/mod_discord.rb | 0 source/{ => modules}/mod_doomrla.rb | 0 source/{ => modules}/mod_fun.rb | 0 source/{ => modules}/mod_util.rb | 0 source/server.rb | 73 ++++++++++++++++++- 7 files changed, 178 insertions(+), 74 deletions(-) delete mode 100644 source/mod_base.rb create mode 100644 source/modules/mod_audio.rb rename source/{ => modules}/mod_discord.rb (100%) rename source/{ => modules}/mod_doomrla.rb (100%) rename source/{ => modules}/mod_fun.rb (100%) rename source/{ => modules}/mod_util.rb (100%) diff --git a/source/mod_base.rb b/source/mod_base.rb deleted file mode 100644 index 7aff276..0000000 --- a/source/mod_base.rb +++ /dev/null @@ -1,72 +0,0 @@ -module Vrobot4::Server - class Mod_Base < Vrobot4::Module::Module - def initialize info - super - register :c_help, "help", "Prints documentation for commands." - register :c_die, "die", "Kills all bot instances.", roles: "o" - register :c_modr, "modr", "Reloads a module.", roles: "o" - register :c_modl, "modl", "Loads a module.", roles: "o" - register :c_modu, "modu", "Unloads a module.", roles: "o" - register :c_dbg, "dbg", "Sets the debug level.", roles: "o" - end - - def c_help m, argv - check_args argv, "", "S" - if argv.length == 0 - cmds = [] - m.serv.each_mod {|mod| cmds << mod.all_cmds(m).keys.join(", ")} - cmds.delete "" - m.reply "Commands:", cmds.join(", ") - else - name = argv[0] - m.serv.each_mod do |mod| - if (cmd = mod.get_cmd m, name) - return m.reply name + ":", cmd.help_str - end - end - m.reply "Command not found:", name - end - end - - def c_die m, argv - m.serv.voice_quit m if m.serv.flags.include? "A" - m.reply \ - ["STATUS: DYING", - "ded", - "proceeding to die", - "bye", - "dedededed", - "Thanks, bye!", - "GOTTAGOBYE", - "the orks insisted upon dying"].sample - exit - end - - def c_modr m, argv - check_args argv, "S", "S" - m.serv.drop_mod argv[0] - load argv[1], true if argv.length > 1 - m.serv.load_mod argv[0] - end - - def c_modl m, argv - check_args argv, "S" - m.serv.load_mod argv[0] - end - - def c_modu m, argv - check_args argv, "S" - m.serv.drop_mod argv[0] - end - - def c_dbg m, argv - check_args argv, "N" - Vrobot4.debug = argv[0].to_i - end - - def on_command m, cnam, argv - Vrobot4.log :DEBUGV, "command", cnam.to_s, argv.to_s - super - end - end -end diff --git a/source/modules/mod_audio.rb b/source/modules/mod_audio.rb new file mode 100644 index 0000000..a49ac28 --- /dev/null +++ b/source/modules/mod_audio.rb @@ -0,0 +1,107 @@ +require 'uri' +require 'open-uri' +require 'tempfile' +require 'youtube-dl.rb' +require 'streamio-ffmpeg' + +# @!visibility private +class QueueItem + def initialize uri + if uri.scheme == "file" + @fname = uri.path + elsif uri.host and uri.host.include? "youtube.com" + @fname = get_yt_vid_from uri + else + @fname = uri.to_s + end + + if (mov = FFMPEG::Movie.new(@fname)) + @time = mov.duration + @size = mov.size + else + throw ArgumentError, "File is invalid" + end + end + + def get_file + open @fname + end + + def cleanup + Vrobot4.log :DEBUGV, "cleaning up audio cruft" + File.delete @tmpf if @tmpf + end + + private + def get_yt_vid_from uri + yt_tmp = get_tmp_name "vrobot4_yt_temp_", ".m4a" + mv_tmp = get_tmp_name "vrobot4_mv_temp_", ".mp3" + opt = { + output: yt_tmp, + extract_audio: true, + audio_format: "m4a" + } + YoutubeDL.get uri.to_s, opt + FFMPEG::Movie.new(yt_tmp).transcode(mv_tmp) + File.delete yt_tmp + @tmpf = mv_tmp + end + + def get_tmp_name name, ext + fname = Dir::Tmpname.tmpdir + "/" + fname += Dir::Tmpname.make_tmpname name, ext + end +end + +class Mod_Audio < Vrobot4::Module::Module + def self.type + "Audio" + end + + Vrobot4::Module.add_module_type self, servflags: "A" + + def initialize info + super + register :c_summon, "summon", "Brings the bot to your voice channel." + register :c_vanquish, "vanquish", "Kicks the bot from voice.", roles: "o" + register :c_play, "play", "Overwrites the queue.", roles: "o" + register :c_queue, "queue", "Adds an item to the queue." + @queue = [] + end + + def c_summon m, argv + m.serv.voice_join m + end + + def c_vanquish m, argv + m.serv.voice_quit m + end + + def c_play m, argv + @queue.clear + push_queue m, argv + start_playback m, qi + end + + def c_queue m, argv + push_queue m, argv + start_playback m, qi unless m.serv.is_playing? m + end + + private + def push_queue m, argv + m.reply "Queueing '" + uri.to_s + "'..." + + uri = URI.parse argv.join(" ") + qi = @queue.push QueueItem.new(uri) + + m.reply "'" + uri.to_s + "' loaded (%i sec, %i bytes)" % + [qi.time, qi.size] + end + + def start_playback m, qi + m.serv.play m, qi.get_file + end +end + +## EOF diff --git a/source/mod_discord.rb b/source/modules/mod_discord.rb similarity index 100% rename from source/mod_discord.rb rename to source/modules/mod_discord.rb diff --git a/source/mod_doomrla.rb b/source/modules/mod_doomrla.rb similarity index 100% rename from source/mod_doomrla.rb rename to source/modules/mod_doomrla.rb diff --git a/source/mod_fun.rb b/source/modules/mod_fun.rb similarity index 100% rename from source/mod_fun.rb rename to source/modules/mod_fun.rb diff --git a/source/mod_util.rb b/source/modules/mod_util.rb similarity index 100% rename from source/mod_util.rb rename to source/modules/mod_util.rb diff --git a/source/server.rb b/source/server.rb index 10205d4..e76451a 100644 --- a/source/server.rb +++ b/source/server.rb @@ -1,6 +1,75 @@ -require './mod_base.rb' - module Vrobot4::Server + class Mod_Base < Vrobot4::Module::Module + def initialize info + super + register :c_help, "help", "Prints documentation for commands." + register :c_die, "die", "Kills all bot instances.", roles: "o" + register :c_modr, "modr", "Reloads a module.", roles: "o" + register :c_modl, "modl", "Loads a module.", roles: "o" + register :c_modu, "modu", "Unloads a module.", roles: "o" + register :c_dbg, "dbg", "Sets the debug level.", roles: "o" + end + + def c_help m, argv + check_args argv, "", "S" + if argv.length == 0 + cmds = [] + m.serv.each_mod {|mod| cmds << mod.all_cmds(m).keys.join(", ")} + cmds.delete "" + m.reply "Commands:", cmds.join(", ") + else + name = argv[0] + m.serv.each_mod do |mod| + if (cmd = mod.get_cmd m, name) + return m.reply name + ":", cmd.help_str + end + end + m.reply "Command not found:", name + end + end + + def c_die m, argv + m.serv.voice_quit m if m.serv.flags.include? "A" + m.reply \ + ["STATUS: DYING", + "ded", + "proceeding to die", + "bye", + "dedededed", + "Thanks, bye!", + "GOTTAGOBYE", + "the orks insisted upon dying"].sample + exit + end + + def c_modr m, argv + check_args argv, "S", "S" + m.serv.drop_mod argv[0] + load argv[1], true if argv.length > 1 + m.serv.load_mod argv[0] + end + + def c_modl m, argv + check_args argv, "S" + m.serv.load_mod argv[0] + end + + def c_modu m, argv + check_args argv, "S" + m.serv.drop_mod argv[0] + end + + def c_dbg m, argv + check_args argv, "N" + Vrobot4.debug = argv[0].to_i + end + + def on_command m, cnam, argv + Vrobot4.log :DEBUGV, "command", cnam.to_s, argv.to_s + super + end + end + class User attr_reader :name, :roles end