Fix syntax (again) and move use checks to Module
parent
5d929e258d
commit
d7509c80d6
|
@ -16,14 +16,7 @@ module Vrobot4::Module
|
|||
# Checks if this command is usable in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def usable_in? m
|
||||
type = @function.owner
|
||||
role = m.user.roles
|
||||
mprm = m.serv.mprm
|
||||
retm, retc, retr = true, false, false
|
||||
retm = mprm[:glob][type] if mprm[:glob].key? type
|
||||
retc = mprm[:chan][type][m.chan] if mprm[:chan].key? type
|
||||
retr = mprm[:role][type].scan /[#{role}]/ if mprm[:role].key? type
|
||||
role.scan(@roles).any? and (retm or retc or retr)
|
||||
m.user.roles.scan(@roles).any? and @function.owner.usable_in? m
|
||||
end
|
||||
|
||||
# Calls the method attached to this command.
|
||||
|
@ -36,6 +29,18 @@ module Vrobot4::Module
|
|||
|
||||
# A bot module. Holds commands and callbacks.
|
||||
class Module
|
||||
# Checks if this module is usable in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def self.usable_in? m
|
||||
role = m.user.roles
|
||||
mprm = m.serv.mprm
|
||||
retm, retc, retr = true, false, false
|
||||
retm = mprm[:glob][self] if mprm[:glob].key? self
|
||||
retc = mprm[:chan][self][m.chan] if mprm[:chan].key? self
|
||||
retr = mprm[:role][self].scan /[#{role}]/ if mprm[:role].key? self
|
||||
retm or retc or retr
|
||||
end
|
||||
|
||||
# @param info [Hash] arbitrary extra information for this module
|
||||
def initialize info
|
||||
@commands = {}
|
||||
|
|
|
@ -54,10 +54,7 @@ class QueueItem
|
|||
end
|
||||
|
||||
class Mod_Audio < Vrobot4::Module::Module
|
||||
def self.type
|
||||
"Audio"
|
||||
end
|
||||
|
||||
def self.type() "Audio" end
|
||||
Vrobot4::Module.add_module_type self, servflags: "A"
|
||||
|
||||
def initialize info
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
class Mod_Discord < Vrobot4::Module::Module
|
||||
def self.type
|
||||
"Discord"
|
||||
end
|
||||
|
||||
def self.type() "Discord" end
|
||||
Vrobot4::Module.add_module_type self, server: "Discord"
|
||||
|
||||
def initialize info
|
||||
|
|
|
@ -11,10 +11,7 @@ require 'json'
|
|||
# - BE PRAYING
|
||||
#
|
||||
class Mod_DoomRLA < Vrobot4::Module::Module
|
||||
def self.type
|
||||
"DoomRLA"
|
||||
end
|
||||
|
||||
def self.type() "DoomRLA" end
|
||||
Vrobot4::Module.add_module_type self, server: "Discord"
|
||||
|
||||
AType = ["Melee", "Ranged", "Special"].freeze
|
||||
|
@ -52,7 +49,7 @@ class Mod_DoomRLA < Vrobot4::Module::Module
|
|||
end
|
||||
|
||||
def c_monsterinfo m, argv
|
||||
mi = @minfo.select do |mon|
|
||||
mi = @@minfo.select do |mon|
|
||||
mon if mon["Name"].casecmp? argv or
|
||||
mon["Tags"].split(", ").include? argv
|
||||
end
|
||||
|
|
|
@ -2,10 +2,7 @@ require 'open-uri'
|
|||
require 'json'
|
||||
|
||||
class Mod_Fun < Vrobot4::Module::Module
|
||||
def self.type
|
||||
"Fun"
|
||||
end
|
||||
|
||||
def self.type() "Fun" end
|
||||
Vrobot4::Module.add_module_type self
|
||||
|
||||
QDB = "http://greyserv.net/qdb"
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
class Mod_Util < Vrobot4::Module::Module
|
||||
def self.type
|
||||
"Utilities"
|
||||
end
|
||||
|
||||
def self.type() "Utilities" end
|
||||
Vrobot4::Module.add_module_type self
|
||||
|
||||
def initialize info
|
||||
|
|
332
source/server.rb
332
source/server.rb
|
@ -1,5 +1,174 @@
|
|||
# Module for vrobot4 server interfaces.
|
||||
module Vrobot4::Server
|
||||
# Generic user information. May be extended.
|
||||
class User
|
||||
attr_reader :name # @return [String] plaintext name of the user
|
||||
attr_reader :roles # @return [String] list of user's roles
|
||||
end
|
||||
|
||||
# Generic channel information. May be extended.
|
||||
class Channel
|
||||
attr_reader :name # @return [String] plaintext name of the channel
|
||||
end
|
||||
|
||||
# Generic event information. May not be extended.
|
||||
class Message
|
||||
attr_reader :msg # @return [String] plaintext of message
|
||||
attr_reader :user # @return [User] user that triggered this message
|
||||
attr_reader :chan # @return [Channel] channel 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.
|
||||
# [:msg] Plaintext of message.
|
||||
# [:user] User that triggered this message.
|
||||
# [:chan] Channel that this message was sent to.
|
||||
# [:serv] Server this message was sent to.
|
||||
# [:reply] Method that sends a message to the specified channel.
|
||||
# [:reply_b] Method that sends a large message to the specified channel.
|
||||
def initialize(**info)
|
||||
@msg = info[:msg]
|
||||
@user = info[:user]
|
||||
@chan = info[:chan]
|
||||
@serv = info[:serv]
|
||||
@reply = info[:reply]
|
||||
@reply_b = info[:reply_b]
|
||||
end
|
||||
|
||||
# Sends a message to the channel this message originated from.
|
||||
def reply *args
|
||||
@reply.call args.join(" ")
|
||||
end
|
||||
|
||||
# Sends a large message to the channel this message originated from.
|
||||
def reply_b *args
|
||||
@reply_b.call args.join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
# Generic server interface.
|
||||
class Server
|
||||
attr_reader :mprm # @return [Hash] module permissions for this server
|
||||
|
||||
# @param info [Hash] arbitrary extra information for this server
|
||||
def initialize info
|
||||
@info = info
|
||||
@modules = [Mod_Base.new(nil)]
|
||||
load_permissions info["permissions"]
|
||||
end
|
||||
|
||||
# Loads and initializes a module into the load list.
|
||||
# @param mod [Vrobot4::Module::Module]
|
||||
def load_mod mod
|
||||
mt = Vrobot4::Module.get_module_type(mod)
|
||||
if mt[:server] and mt[:server] != self.class.type or
|
||||
mt[:servflags] and mt[:servflags] !~ flags then
|
||||
raise ArgumentError, "Module " + mod + " not valid for this server"
|
||||
end
|
||||
@modules << mt[:type].new(@info[mod])
|
||||
end
|
||||
|
||||
# Drops a module from the load list.
|
||||
# @param mod [Vrobot4::Module::Module]
|
||||
def drop_mod mod
|
||||
mt = Vrobot4::Module.get_module_type(mod)
|
||||
@modules.each_index do |i|
|
||||
@modules.delete_at i if @modules[i].is_a? mt[:type]
|
||||
end
|
||||
end
|
||||
|
||||
# Yields for every module loaded in the server.
|
||||
def each_mod
|
||||
@modules.each {|mod| yield mod}
|
||||
end
|
||||
|
||||
# (see Vrobot4::Module::Module#on_message)
|
||||
# Passes information to all modules.
|
||||
def on_message m
|
||||
@modules.each do |mod|
|
||||
break if mod.class.usable_in? m and mod.on_message m
|
||||
end
|
||||
end
|
||||
|
||||
# (see Vrobot4::Module::Module#on_command)
|
||||
# Passes information to all modules.
|
||||
def on_command m, cnam, argv
|
||||
@modules.each do |mod|
|
||||
break if mod.class.usable_in? m and mod.on_command m, cnam, argv
|
||||
end
|
||||
end
|
||||
|
||||
# Connect to the server.
|
||||
def connect
|
||||
raise NotImplementedError, "Server#connect not implemented"
|
||||
end
|
||||
|
||||
# Flags for this server.
|
||||
# [A] Server has audio capabilities and inherits from AudioServer.
|
||||
# [L] Server cannot support large text dumps
|
||||
# (code should assume <=4 lines, 240 characters per as maximum)
|
||||
# @return [String]
|
||||
def flags
|
||||
""
|
||||
end
|
||||
|
||||
# Basic command message handler.
|
||||
#
|
||||
# Checks if the message starts with "." and splits the command
|
||||
# from the arguments, then emits an on_command event.
|
||||
#
|
||||
# Otherwise, it will emit an on_message event.
|
||||
#
|
||||
# @param m [Vrobot4::Server::Message] the message to parse and emit
|
||||
def handle_text_cmd m
|
||||
if m.msg.start_with? '.'
|
||||
arg = m.msg.split(' ', 2)
|
||||
on_command m, arg[0][1 .. -1], arg[1] || ""
|
||||
else
|
||||
on_message m
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
# Implementation defined permission loader.
|
||||
# Loads information into +@mprm+.
|
||||
def load_permissions pinf
|
||||
raise NotImplementedError, "Server#load_permissions not implemented"
|
||||
end
|
||||
end
|
||||
|
||||
# Basis for an audio-enabled server interface.
|
||||
class AudioServer < Server
|
||||
# Joins a voice channel, using a message for context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def voice_join m
|
||||
raise NotImplementedError, "AudioServer#voice_join not implemented"
|
||||
end
|
||||
|
||||
# Quits a voice channel, using a message for context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def voice_quit m
|
||||
raise NotImplementedError, "AudioServer#voice_quit not implemented"
|
||||
end
|
||||
|
||||
# Plays an arbitrary audio file in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
# @param io [IO] audio stream to play
|
||||
def voice_play m, io
|
||||
raise NotImplementedError, "AudioServer#voice_play not implemented"
|
||||
end
|
||||
|
||||
# Check if the bot is playing audio in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def is_playing? m
|
||||
raise NotImplementedError, "AudioServer#is_playing? not implemented"
|
||||
end
|
||||
|
||||
# (see Vrobot4::Server::Server#flags)
|
||||
def flags
|
||||
"A"
|
||||
end
|
||||
end
|
||||
|
||||
class Mod_Base < Vrobot4::Module::Module
|
||||
def initialize info
|
||||
super
|
||||
|
@ -75,169 +244,6 @@ module Vrobot4::Server
|
|||
end
|
||||
private_constant :Mod_Base
|
||||
|
||||
# Generic user information. May be extended.
|
||||
class User
|
||||
attr_reader :name # @return [String] plaintext name of the user
|
||||
attr_reader :roles # @return [String] list of user's roles
|
||||
end
|
||||
|
||||
# Generic channel information. May be extended.
|
||||
class Channel
|
||||
attr_reader :name # @return [String] plaintext name of the channel
|
||||
end
|
||||
|
||||
# Generic event information. May not be extended.
|
||||
class Message
|
||||
attr_reader :msg # @return [String] plaintext of message
|
||||
attr_reader :user # @return [User] user that triggered this message
|
||||
attr_reader :chan # @return [Channel] channel 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.
|
||||
# [:msg] Plaintext of message.
|
||||
# [:user] User that triggered this message.
|
||||
# [:chan] Channel that this message was sent to.
|
||||
# [:serv] Server this message was sent to.
|
||||
# [:reply] Method that sends a message to the specified channel.
|
||||
# [:reply_b] Method that sends a large message to the specified channel.
|
||||
def initialize(**info)
|
||||
@msg = info[:msg]
|
||||
@user = info[:user]
|
||||
@chan = info[:chan]
|
||||
@serv = info[:serv]
|
||||
@reply = info[:reply]
|
||||
@reply_b = info[:reply_b]
|
||||
end
|
||||
|
||||
# Sends a message to the channel this message originated from.
|
||||
def reply *args
|
||||
@reply.call args.join(" ")
|
||||
end
|
||||
|
||||
# Sends a large message to the channel this message originated from.
|
||||
def reply_b *args
|
||||
@reply_b.call args.join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
# Generic server interface.
|
||||
class Server
|
||||
attr_reader :mprm # @return [Hash] module permissions for this server
|
||||
|
||||
# @param info [Hash] arbitrary extra information for this server
|
||||
def initialize info
|
||||
@info = info
|
||||
@modules = [Mod_Base.new(nil)]
|
||||
load_permissions info["permissions"]
|
||||
end
|
||||
|
||||
# Loads and initializes a module into the load list.
|
||||
def load_mod mod
|
||||
mt = Vrobot4::Module.get_module_type(mod)
|
||||
if mt[:server] and mt[:server] != self.class.type or
|
||||
mt[:servflags] and mt[:servflags] !~ flags
|
||||
raise ArgumentError, "Module " + mod + " not valid for this server"
|
||||
end
|
||||
@modules << mt[:type].new(@info[mod])
|
||||
end
|
||||
|
||||
# Drops a module from the load list.
|
||||
def drop_mod mod
|
||||
mt = Vrobot4::Module.get_module_type(mod)
|
||||
@modules.each_index do |i|
|
||||
@modules.delete_at i if @modules[i].is_a? mt[:type]
|
||||
end
|
||||
end
|
||||
|
||||
# Yields for every module loaded in the server.
|
||||
def each_mod
|
||||
@modules.each {|mod| yield mod}
|
||||
end
|
||||
|
||||
# (see Vrobot4::Module::Module#on_message)
|
||||
# Passes information to all modules.
|
||||
def on_message m
|
||||
@modules.each {|mod| break if mod.on_message m}
|
||||
end
|
||||
|
||||
# (see Vrobot4::Module::Module#on_command)
|
||||
# Passes information to all modules.
|
||||
def on_command m, cnam, argv
|
||||
@modules.each {|mod| break if mod.on_command m, cnam, argv}
|
||||
end
|
||||
|
||||
# Connect to the server.
|
||||
def connect
|
||||
raise NotImplementedError, "Server#connect not implemented"
|
||||
end
|
||||
|
||||
# Flags for this server.
|
||||
# [A] Server has audio capabilities and inherits from AudioServer.
|
||||
# [L] Server cannot support large text dumps
|
||||
# (code should assume <=4 lines, 240 characters per as maximum)
|
||||
# @return [String]
|
||||
def flags
|
||||
""
|
||||
end
|
||||
|
||||
# Basic command message handler.
|
||||
#
|
||||
# Checks if the message starts with "." and splits the command
|
||||
# from the arguments, then emits an on_command event.
|
||||
#
|
||||
# Otherwise, it will emit an on_message event.
|
||||
#
|
||||
# @param m [Vrobot4::Server::Message] the message to parse and emit
|
||||
def handle_text_cmd m
|
||||
if m.msg.start_with? '.'
|
||||
arg = m.msg.split(' ', 2)
|
||||
on_command m, arg[0][1 .. -1], (arg[1] or "")
|
||||
else
|
||||
on_message m
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
# Implementation defined permission loader.
|
||||
# Loads information into +@mprm+.
|
||||
def load_permissions pinf
|
||||
raise NotImplementedError, "Server#load_permissions not implemented"
|
||||
end
|
||||
end
|
||||
|
||||
# Basis for an audio-enabled server interface.
|
||||
class AudioServer < Server
|
||||
# Joins a voice channel, using a message for context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def voice_join m
|
||||
raise NotImplementedError, "AudioServer#voice_join not implemented"
|
||||
end
|
||||
|
||||
# Quits a voice channel, using a message for context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def voice_quit m
|
||||
raise NotImplementedError, "AudioServer#voice_quit not implemented"
|
||||
end
|
||||
|
||||
# Plays an arbitrary audio file in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
# @param io [IO] audio stream to play
|
||||
def voice_play m, io
|
||||
raise NotImplementedError, "AudioServer#voice_play not implemented"
|
||||
end
|
||||
|
||||
# Check if the bot is playing audio in a given context.
|
||||
# @param m [Vrobot4::Server::Message] message for context
|
||||
def is_playing? m
|
||||
raise NotImplementedError, "AudioServer#is_playing? not implemented"
|
||||
end
|
||||
|
||||
# (see Vrobot4::Server::Server#flags)
|
||||
def flags
|
||||
"A"
|
||||
end
|
||||
end
|
||||
|
||||
# Adds a server type to the global list.
|
||||
# @param type [Class] server type to add
|
||||
def self.add_server_type type
|
||||
|
|
|
@ -3,10 +3,7 @@ require 'discordrb'
|
|||
# A server implementation for Discord using discordrb.
|
||||
class Sv_Discord < Vrobot4::Server::AudioServer
|
||||
# The server type name.
|
||||
def self.type
|
||||
"Discord"
|
||||
end
|
||||
|
||||
def self.type() "Discord" end
|
||||
Vrobot4::Server.add_server_type self
|
||||
|
||||
attr_reader :bot # The Discordrb::Bot instance.
|
||||
|
@ -15,8 +12,8 @@ class Sv_Discord < Vrobot4::Server::AudioServer
|
|||
def initialize info
|
||||
super
|
||||
|
||||
@ops = info["admins"] or []
|
||||
@hop = info["halfop"] or []
|
||||
@ops = info["admins"] || []
|
||||
@hop = info["halfop"] || []
|
||||
|
||||
@bot = Discordrb::Bot.new \
|
||||
token: info["apikey"],
|
||||
|
@ -113,9 +110,9 @@ class Sv_Discord < Vrobot4::Server::AudioServer
|
|||
if user.is_a? Discordrb::Member
|
||||
if user.owner?
|
||||
@roles += "Ooh"
|
||||
elsif ops and ops.select {|role| user.role? role}.any?
|
||||
elsif ops && ops.select {|role| user.role? role}.any?
|
||||
@roles += "oh"
|
||||
elsif hop and hop.select {|role| user.role? role}.any?
|
||||
elsif hop && hop.select {|role| user.role? role}.any?
|
||||
@roles += "h"
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,10 +3,7 @@ require 'cinch'
|
|||
# A server implementation for IRC using cinch.
|
||||
class Sv_IRC < Vrobot4::Server::Server
|
||||
# The server type name.
|
||||
def self.type
|
||||
"IRC"
|
||||
end
|
||||
|
||||
def self.type() "IRC" end
|
||||
Vrobot4::Server.add_server_type self
|
||||
|
||||
attr_reader :bot # The Cinch::Bot instance.
|
||||
|
@ -18,12 +15,12 @@ class Sv_IRC < Vrobot4::Server::Server
|
|||
this = self
|
||||
@bot = Cinch::Bot.new do
|
||||
configure do |cfg|
|
||||
cfg.server = info["server"]
|
||||
cfg.nick = (info["nick"] or "vrobot4")
|
||||
cfg.port = info["port"] if info.key? "port"
|
||||
cfg.password = info["pass"] if info.key? "pass"
|
||||
cfg.modes = info["modes"] if info.key? "modes"
|
||||
cfg.channels = info["channels"] if info.key? "channels"
|
||||
cfg.server = info["server"]
|
||||
cfg.nick = info["nick"] || "vrobot4"
|
||||
cfg.port = info["port"] if info.key? "port"
|
||||
cfg.password = info["pass"] if info.key? "pass"
|
||||
cfg.modes = info["modes"] if info.key? "modes"
|
||||
cfg.channels = info["channels"] if info.key? "channels"
|
||||
cfg.realname = "vrobot4"
|
||||
cfg.user = "vrobot4"
|
||||
cfg.message_split_start = "… "
|
||||
|
|
Loading…
Reference in New Issue