Remove automatic argument splitting and add a * arg specifier
parent
f987c780f7
commit
e84f48c4a5
|
@ -10,8 +10,9 @@ module Vrobot4
|
||||||
type = botinfo["type"]
|
type = botinfo["type"]
|
||||||
serv = Server::get_server_type(type).new(botinfo)
|
serv = Server::get_server_type(type).new(botinfo)
|
||||||
|
|
||||||
botinfo["modules"].each \
|
if botinfo.key? "modules" then for mod in botinfo["modules"] do
|
||||||
{|mod| serv.load_mod mod} if botinfo.key? "modules"
|
serv.load_mod mod
|
||||||
|
end end
|
||||||
|
|
||||||
serv
|
serv
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
module Vrobot4::Module
|
module Vrobot4::Module
|
||||||
# A command function. Holds extra info for i.e. permissions.
|
# A command function. Holds extra info for i.e. permissions.
|
||||||
class Command
|
class Command
|
||||||
attr_reader :help_str # Help string for this command.
|
attr_reader :help_str # @return [String] help string for this command
|
||||||
|
|
||||||
# @param fn [Method] method to be called on run
|
# @param fn [Method] method to be called on run
|
||||||
# @param help [String] help string for this command
|
# @param help [String] help string for this command
|
||||||
|
@ -27,6 +27,8 @@ module Vrobot4::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
# Calls the method attached to this command.
|
# Calls the method attached to this command.
|
||||||
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
|
# @param argv [String] argument string
|
||||||
def run m, argv
|
def run m, argv
|
||||||
@function.call(m, argv)
|
@function.call(m, argv)
|
||||||
end
|
end
|
||||||
|
@ -51,10 +53,10 @@ module Vrobot4::Module
|
||||||
# Callback for command receiving.
|
# Callback for command receiving.
|
||||||
# @param m [Vrobot4::Server::Message] message that triggered this call
|
# @param m [Vrobot4::Server::Message] message that triggered this call
|
||||||
# @param cnam [String] name of command to call
|
# @param cnam [String] name of command to call
|
||||||
# @param argv [Array] array of string arguments
|
# @param argv [String] array of string arguments
|
||||||
# @return [Boolean] if this should block further command callbacks
|
# @return [Boolean] if this should block further command callbacks
|
||||||
def on_command m, cnam, argv
|
def on_command m, cnam, argv
|
||||||
if (cmd = all_cmds(m)[cnam])
|
if (cmd = cmds(m)[cnam])
|
||||||
begin
|
begin
|
||||||
cmd.run(m, argv)
|
cmd.run(m, argv)
|
||||||
rescue
|
rescue
|
||||||
|
@ -69,7 +71,7 @@ module Vrobot4::Module
|
||||||
# Gets all commands usable in a specified context.
|
# Gets all commands usable in a specified context.
|
||||||
# @param m [Vrobot4::Server::Message] message for context
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
# @return [Hash] a hash of all commands by name
|
# @return [Hash] a hash of all commands by name
|
||||||
def all_cmds m
|
def cmds m
|
||||||
@commands.select {|name, cmd| cmd.usable_in? m}
|
@commands.select {|name, cmd| cmd.usable_in? m}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,24 +98,37 @@ module Vrobot4::Module
|
||||||
# Specifiers include:
|
# Specifiers include:
|
||||||
# [N] A number.
|
# [N] A number.
|
||||||
# [S] An arbitrary string.
|
# [S] An arbitrary string.
|
||||||
|
# [*] Any amount of arbitrary arguments. Must be the last optional arg.
|
||||||
#
|
#
|
||||||
# @param argv [Array] array of arguments
|
# @param argv [String, Array] array of arguments or a string
|
||||||
# @param req [String] specifier string of required arguments
|
# @param req [String] specifier string of required arguments
|
||||||
# @param opt [String] specifier string of optional arguments
|
# @param opt [String] specifier string of optional arguments
|
||||||
|
# @return argv
|
||||||
def check_args argv, req, opt = ""
|
def check_args argv, req, opt = ""
|
||||||
if argv.length < req.length
|
if argv.is_a? Array then
|
||||||
raise ArgumentError, "Not enough arguments"
|
if argv.length < req.length
|
||||||
elsif argv.length > req.length + opt.length
|
raise ArgumentError, "Not enough arguments"
|
||||||
raise ArgumentError, "Too many arguments"
|
elsif opt[-1] != '*' and argv.length > req.length + opt.length
|
||||||
else
|
raise ArgumentError, "Too many arguments"
|
||||||
(0...req.length).each do |i|
|
end
|
||||||
|
|
||||||
|
for i in (0 ... req.length)
|
||||||
check_arg argv[i], i, req[i]
|
check_arg argv[i], i, req[i]
|
||||||
end
|
end
|
||||||
|
|
||||||
(0...argv.length - req.length).each do |i|
|
for i in (0 ... argv.length - req.length)
|
||||||
check_arg argv[i + req.length], i + req.length, opt[i]
|
break if opt[i] == '*'
|
||||||
|
argn = i + req.length
|
||||||
|
check_arg argv[argn], argn, opt[i]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if opt.length
|
||||||
|
check_arg argv, 0, opt[0] if argv.length
|
||||||
|
else
|
||||||
|
check_arg argv, 0, req[0]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
argv
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -123,6 +138,9 @@ module Vrobot4::Module
|
||||||
unless Vrobot4.is_num? arg.strip
|
unless Vrobot4.is_num? arg.strip
|
||||||
raise ArgumentError, "Expected a number for arg " + i.to_s
|
raise ArgumentError, "Expected a number for arg " + i.to_s
|
||||||
end
|
end
|
||||||
|
when 'S' # Don't need to check anything here.
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Invalid argument specifier " + req.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -92,7 +92,7 @@ class Mod_Audio < Vrobot4::Module::Module
|
||||||
def push_queue m, argv
|
def push_queue m, argv
|
||||||
m.reply "Queueing '" + uri.to_s + "'..."
|
m.reply "Queueing '" + uri.to_s + "'..."
|
||||||
|
|
||||||
uri = URI.parse argv.join(" ")
|
uri = URI.parse argv
|
||||||
qi = @queue.push QueueItem.new(uri)
|
qi = @queue.push QueueItem.new(uri)
|
||||||
|
|
||||||
m.reply "'" + uri.to_s + "' loaded (%i sec, %i bytes)" %
|
m.reply "'" + uri.to_s + "' loaded (%i sec, %i bytes)" %
|
||||||
|
@ -100,7 +100,7 @@ class Mod_Audio < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_playback m, qi
|
def start_playback m, qi
|
||||||
m.serv.play m, qi.get_file
|
m.serv.voice_play m, qi.get_file
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class Mod_Discord < Vrobot4::Module::Module
|
||||||
|
|
||||||
def c_purge m, argv
|
def c_purge m, argv
|
||||||
check_args argv, "N"
|
check_args argv, "N"
|
||||||
n = argv[0].to_i
|
n = argv.to_i
|
||||||
n = 2 if n < 2
|
n = 2 if n < 2
|
||||||
n = 100 if n > 100
|
n = 100 if n > 100
|
||||||
m.chan.real.prune n
|
m.chan.real.prune n
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
|
||||||
#
|
#--
|
||||||
# ===== WARNING =====
|
# ===== WARNING =====
|
||||||
#
|
#
|
||||||
# !!! INCOMING SPAGHETTI !!!
|
# !!! INCOMING SPAGHETTI !!!
|
||||||
|
@ -31,8 +31,7 @@ class Mod_DoomRLA < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def c_weaponinfo m, argv
|
def c_weaponinfo m, argv
|
||||||
name = argv.join(" ")
|
w = @winfo.select {|wep| wep if wep["Name"].casecmp? argv}[0]
|
||||||
w = @winfo.select {|wep| wep if wep["Name"].casecmp? name}[0]
|
|
||||||
raise ArgumentError, "Weapon not found" if w == nil
|
raise ArgumentError, "Weapon not found" if w == nil
|
||||||
t = "%s %s damage\n" % [w["Damage"], w["DamageType"]]
|
t = "%s %s damage\n" % [w["Damage"], w["DamageType"]]
|
||||||
t << "%s shots\n" % [w["Shots"]] if w["Shots"].to_i > 1
|
t << "%s shots\n" % [w["Shots"]] if w["Shots"].to_i > 1
|
||||||
|
@ -53,10 +52,9 @@ class Mod_DoomRLA < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def c_monsterinfo m, argv
|
def c_monsterinfo m, argv
|
||||||
name = argv.join(" ")
|
|
||||||
mi = @minfo.select do |mon|
|
mi = @minfo.select do |mon|
|
||||||
mon if mon["Name"].casecmp? name or
|
mon if mon["Name"].casecmp? argv or
|
||||||
mon["Tags"].split(", ").include? name
|
mon["Tags"].split(", ").include? argv
|
||||||
end
|
end
|
||||||
raise ArgumentError, "No monsters found" if mi == nil
|
raise ArgumentError, "No monsters found" if mi == nil
|
||||||
mi.first(3).each do |w|
|
mi.first(3).each do |w|
|
||||||
|
@ -78,7 +76,7 @@ class Mod_DoomRLA < Vrobot4::Module::Module
|
||||||
embed.title = "[%s] %s" % [w["Difficulty"], w["Name"]]
|
embed.title = "[%s] %s" % [w["Difficulty"], w["Name"]]
|
||||||
embed.title << " %s" % [w["Weapon"]] if w["Weapon"]
|
embed.title << " %s" % [w["Weapon"]] if w["Weapon"]
|
||||||
embed.image = Discordrb::Webhooks::EmbedImage.new \
|
embed.image = Discordrb::Webhooks::EmbedImage.new \
|
||||||
url: w["Sprite"] if w.key? "Sprite"
|
url: w["Sprite"] if w["Sprite"]
|
||||||
embed.description = t
|
embed.description = t
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -29,15 +29,12 @@ class Mod_Fun < Vrobot4::Module::Module
|
||||||
def c_nyan (m, argv) m.reply "nyan " * (rand(10) + 1) + "~" end
|
def c_nyan (m, argv) m.reply "nyan " * (rand(10) + 1) + "~" end
|
||||||
|
|
||||||
def c_box m, argv
|
def c_box m, argv
|
||||||
str = argv.join(" ")
|
text = argv + "\n"
|
||||||
text = str + "\n"
|
raise ArgumentError, "String too long" if argv.length > 30
|
||||||
raise ArgumentError, "String too long" if str.length > 30
|
if argv.length > 2 then for i in (1..argv.length - 2)
|
||||||
if str.length > 2
|
text << argv[i] + " " * (argv.length - 2) + argv.reverse[i] + "\n"
|
||||||
for i in (1..str.length - 2)
|
end end
|
||||||
text << str[i] + " " * (str.length - 2) + str.reverse[i] + "\n"
|
text << argv.reverse
|
||||||
end
|
|
||||||
end
|
|
||||||
text << str.reverse
|
|
||||||
m.reply_b text
|
m.reply_b text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -48,8 +45,8 @@ class Mod_Fun < Vrobot4::Module::Module
|
||||||
def c_marble m, argv
|
def c_marble m, argv
|
||||||
check_args argv, "", "N"
|
check_args argv, "", "N"
|
||||||
|
|
||||||
if argv.length == 0 then n = 20
|
if argv.empty? then n = 20
|
||||||
else n = argv[0].to_i end
|
else n = argv.to_i end
|
||||||
|
|
||||||
if m.serv.flags.include? "L" and n > 200 then n = 200
|
if m.serv.flags.include? "L" and n > 200 then n = 200
|
||||||
elsif n > 1000 then n = 1000 end
|
elsif n > 1000 then n = 1000 end
|
||||||
|
@ -64,8 +61,7 @@ class Mod_Fun < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def c_quote m, argv
|
def c_quote m, argv
|
||||||
argv = argv.join(" ")
|
if argv.empty?
|
||||||
if argv.length == 0
|
|
||||||
q, num = nil, nil
|
q, num = nil, nil
|
||||||
max = JSON.parse(open(QDB + "/interface.cgi").read)["numQuotes"]
|
max = JSON.parse(open(QDB + "/interface.cgi").read)["numQuotes"]
|
||||||
loop do
|
loop do
|
||||||
|
|
|
@ -14,7 +14,7 @@ class Mod_Util < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def c_rand m, argv
|
def c_rand m, argv
|
||||||
check_args argv, "N", "N"
|
argv = check_args argv.split, "N", "N"
|
||||||
min, max = 0, argv[0].to_f
|
min, max = 0, argv[0].to_f
|
||||||
min, max = max, argv[1].to_f if argv.length >= 2
|
min, max = max, argv[1].to_f if argv.length >= 2
|
||||||
max, min = min, max if max < min
|
max, min = min, max if max < min
|
||||||
|
@ -23,7 +23,7 @@ class Mod_Util < Vrobot4::Module::Module
|
||||||
end
|
end
|
||||||
|
|
||||||
def c_decide m, argv
|
def c_decide m, argv
|
||||||
argv = argv.join(" ").split(",").each {|arg| arg.chomp!}
|
argv = check_args argv.split(/\s*,\s*/), "S", "*"
|
||||||
m.reply argv.sample
|
m.reply argv.sample
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,20 +13,18 @@ module Vrobot4::Server
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def c_help m, argv
|
def c_help m, argv
|
||||||
check_args argv, "", "S"
|
if argv.empty?
|
||||||
if argv.length == 0
|
|
||||||
cmds = []
|
cmds = []
|
||||||
m.serv.each_mod {|mod| cmds << mod.all_cmds(m).keys.join(", ")}
|
m.serv.each_mod {|mod| cmds << mod.cmds(m).keys.join(", ")}
|
||||||
cmds.delete ""
|
cmds.delete ""
|
||||||
m.reply "Commands:", cmds.join(", ")
|
m.reply "Commands:", cmds.join(", ")
|
||||||
else
|
else
|
||||||
name = argv[0]
|
|
||||||
m.serv.each_mod do |mod|
|
m.serv.each_mod do |mod|
|
||||||
if (cmd = mod.get_cmd m, name)
|
if (cmd = mod.cmds(m)[argv])
|
||||||
return m.reply name + ":", cmd.help_str
|
return m.reply argv + ":", cmd.help_str
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
m.reply "Command not found:", name
|
m.reply "Command not found:", argv
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,7 +45,7 @@ module Vrobot4::Server
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def c_modr m, argv
|
def c_modr m, argv
|
||||||
check_args argv, "S", "S"
|
argv = check_args argv.split, "S", "S"
|
||||||
m.serv.drop_mod argv[0]
|
m.serv.drop_mod argv[0]
|
||||||
load argv[1], true if argv.length > 1
|
load argv[1], true if argv.length > 1
|
||||||
m.serv.load_mod argv[0]
|
m.serv.load_mod argv[0]
|
||||||
|
@ -55,25 +53,23 @@ module Vrobot4::Server
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def c_modl m, argv
|
def c_modl m, argv
|
||||||
check_args argv, "S"
|
m.serv.load_mod argv
|
||||||
m.serv.load_mod argv[0]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def c_modu m, argv
|
def c_modu m, argv
|
||||||
check_args argv, "S"
|
m.serv.drop_mod argv
|
||||||
m.serv.drop_mod argv[0]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def c_dbg m, argv
|
def c_dbg m, argv
|
||||||
check_args argv, "N"
|
check_args argv, "N"
|
||||||
Vrobot4.debug = argv[0].to_i
|
Vrobot4.debug = argv.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
# @!visibility private
|
# @!visibility private
|
||||||
def on_command m, cnam, argv
|
def on_command m, cnam, argv
|
||||||
Vrobot4.log :DEBUGV, "command", cnam.to_s, argv.to_s
|
Vrobot4.log :DEBUGV, "command", cnam.to_s, argv
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -81,21 +77,21 @@ module Vrobot4::Server
|
||||||
|
|
||||||
# Generic user information. May be extended.
|
# Generic user information. May be extended.
|
||||||
class User
|
class User
|
||||||
attr_reader :name # Plaintext name of the user.
|
attr_reader :name # @return [String] plaintext name of the user
|
||||||
attr_reader :roles # List of user's roles.
|
attr_reader :roles # @return [String] list of user's roles
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generic channel information. May be extended.
|
# Generic channel information. May be extended.
|
||||||
class Channel
|
class Channel
|
||||||
attr_reader :name # Plaintext name of the channel.
|
attr_reader :name # @return [String] plaintext name of the channel
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generic event information. May not be extended.
|
# Generic event information. May not be extended.
|
||||||
class Message
|
class Message
|
||||||
attr_reader :msg # Plaintext of message (if any.)
|
attr_reader :msg # @return [String] plaintext of message
|
||||||
attr_reader :user # User that triggered this message (if any.)
|
attr_reader :user # @return [User] user that triggered this message
|
||||||
attr_reader :chan # Channel this message was sent to (if any.)
|
attr_reader :chan # @return [Channel] channel this message was sent to
|
||||||
attr_reader :serv # Server this message was sent to.
|
attr_reader :serv # @return [Server] server this message was sent to
|
||||||
|
|
||||||
# @param info [Hash] A hash containing message info. Keys may be omitted.
|
# @param info [Hash] A hash containing message info. Keys may be omitted.
|
||||||
# [:msg] Plaintext of message.
|
# [:msg] Plaintext of message.
|
||||||
|
@ -126,7 +122,7 @@ module Vrobot4::Server
|
||||||
|
|
||||||
# Generic server interface.
|
# Generic server interface.
|
||||||
class Server
|
class Server
|
||||||
attr_reader :mprm # Module permissions for this server.
|
attr_reader :mprm # @return [Hash] module permissions for this server
|
||||||
|
|
||||||
# @param info [Hash] arbitrary extra information for this server
|
# @param info [Hash] arbitrary extra information for this server
|
||||||
def initialize info
|
def initialize info
|
||||||
|
@ -195,21 +191,26 @@ module Vrobot4::Server
|
||||||
# Basis for an audio-enabled server interface.
|
# Basis for an audio-enabled server interface.
|
||||||
class AudioServer < Server
|
class AudioServer < Server
|
||||||
# Joins a voice channel, using a message for context.
|
# Joins a voice channel, using a message for context.
|
||||||
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
def voice_join m
|
def voice_join m
|
||||||
raise NotImplementedError, "AudioServer#voice_join not implemented"
|
raise NotImplementedError, "AudioServer#voice_join not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Quits a voice channel, using a message for context.
|
# Quits a voice channel, using a message for context.
|
||||||
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
def voice_quit m
|
def voice_quit m
|
||||||
raise NotImplementedError, "AudioServer#voice_quit not implemented"
|
raise NotImplementedError, "AudioServer#voice_quit not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Plays an arbitrary audio file in a given context.
|
# Plays an arbitrary audio file in a given context.
|
||||||
def play m, io
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
raise NotImplementedError, "AudioServer#play not implemented"
|
# @param io [IO] audio stream to play
|
||||||
|
def voice_play m, io
|
||||||
|
raise NotImplementedError, "AudioServer#voice_play not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Check if the bot is playing audio in a given context.
|
# Check if the bot is playing audio in a given context.
|
||||||
|
# @param m [Vrobot4::Server::Message] message for context
|
||||||
def is_playing? m
|
def is_playing? m
|
||||||
raise NotImplementedError, "AudioServer#is_playing? not implemented"
|
raise NotImplementedError, "AudioServer#is_playing? not implemented"
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,9 +32,8 @@ class Sv_Discord < Vrobot4::Server::AudioServer
|
||||||
reply_b: -> (text) {evt.respond "```\n" + text + "```"}
|
reply_b: -> (text) {evt.respond "```\n" + text + "```"}
|
||||||
|
|
||||||
if m.msg.start_with? '.'
|
if m.msg.start_with? '.'
|
||||||
argv = m.msg.split
|
arg = m.msg.split(' ', 2)
|
||||||
cnam = argv.shift[1..-1]
|
on_command m, arg[0][1..-1], (arg[1] or "")
|
||||||
on_command m, cnam, argv
|
|
||||||
else
|
else
|
||||||
on_message m
|
on_message m
|
||||||
end
|
end
|
||||||
|
@ -53,8 +52,8 @@ class Sv_Discord < Vrobot4::Server::AudioServer
|
||||||
@bot.voice_destroy m.chan.real.server.id
|
@bot.voice_destroy m.chan.real.server.id
|
||||||
end
|
end
|
||||||
|
|
||||||
# (see Vrobot4::Server::AudioServer#play)
|
# (see Vrobot4::Server::AudioServer#voice_play)
|
||||||
def play m, io
|
def voice_play m, io
|
||||||
v = @bot.voice(m.chan.real)
|
v = @bot.voice(m.chan.real)
|
||||||
raise ArgumentError, "Invalid i/o stream" unless io
|
raise ArgumentError, "Invalid i/o stream" unless io
|
||||||
raise RuntimeError, "I'm not in a voice channel" unless v
|
raise RuntimeError, "I'm not in a voice channel" unless v
|
||||||
|
|
Loading…
Reference in New Issue