Move basic command handling to Server

master
Marrub 2017-08-13 06:48:00 -04:00
parent e84f48c4a5
commit 34d8744a6a
2 changed files with 30 additions and 24 deletions

View File

@ -180,6 +180,23 @@ module Vrobot4::Server
"" ""
end 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 protected
# Implementation defined permission loader. # Implementation defined permission loader.
# Loads information into +@mprm+. # Loads information into +@mprm+.

View File

@ -31,12 +31,7 @@ class Sv_Discord < Vrobot4::Server::AudioServer
reply: -> (text) {evt.respond text}, reply: -> (text) {evt.respond text},
reply_b: -> (text) {evt.respond "```\n" + text + "```"} reply_b: -> (text) {evt.respond "```\n" + text + "```"}
if m.msg.start_with? '.' handle_text_cmd m
arg = m.msg.split(' ', 2)
on_command m, arg[0][1..-1], (arg[1] or "")
else
on_message m
end
end end
end end
@ -80,33 +75,27 @@ class Sv_Discord < Vrobot4::Server::AudioServer
# (see Vrobot4::Server::Server#load_permissions) # (see Vrobot4::Server::Server#load_permissions)
def load_permissions pinf def load_permissions pinf
@mprm = {chan: {}, role: {}, glob: {}} @mprm = {chan: {}, role: {}, glob: {}}
pinf.each do |perm| pinf.each do |pr|
mod = Vrobot4::Module.get_module_type(perm["module"])[:type] mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
if perm.key? "channel" if pr.key? "channel"
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod @mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod
@mprm[:chan][mod][perm["channel"]] = true @mprm[:chan][mod][pr["channel"]] = true
elsif perm.key? "roles" elsif pr.key? "roles"
@mprm[:role][mod] = perm["roles"] @mprm[:role][mod] = pr["roles"]
elsif perm.key? "enable" elsif pr.key? "enable"
@mprm[:glob][mod] = perm["enable"] @mprm[:glob][mod] = pr["enable"]
end end
end end if pinf
end end
class ChannelPerms class ChannelPerms
def initialize def initialize() @cprm = {} end
@cprm = {}
end
# Returns true if the channel is enabled, false otherwise. # Returns true if the channel is enabled, false otherwise.
def [] chan def [](chan) @cprm[chan.name] or @cprm[chan.real.id] end
@cprm[chan.name] or @cprm[chan.real.id]
end
# Sets a channel's permission on/off. # Sets a channel's permission on/off.
def []= chan, set def []=(chan, set) @cprm[chan] = set end
@cprm[chan] = set
end
end end
private_constant :ChannelPerms private_constant :ChannelPerms