From 34d8744a6a9faea6b524c573aea7e5760e5b368d Mon Sep 17 00:00:00 2001 From: Marrub Date: Sun, 13 Aug 2017 06:48:00 -0400 Subject: [PATCH] Move basic command handling to Server --- source/server.rb | 17 +++++++++++++++++ source/servers/sv_discord.rb | 37 +++++++++++++----------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/source/server.rb b/source/server.rb index f2afea6..48340de 100644 --- a/source/server.rb +++ b/source/server.rb @@ -180,6 +180,23 @@ module Vrobot4::Server "" 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+. diff --git a/source/servers/sv_discord.rb b/source/servers/sv_discord.rb index f8691f5..8a86986 100644 --- a/source/servers/sv_discord.rb +++ b/source/servers/sv_discord.rb @@ -31,12 +31,7 @@ class Sv_Discord < Vrobot4::Server::AudioServer reply: -> (text) {evt.respond text}, reply_b: -> (text) {evt.respond "```\n" + text + "```"} - 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 + handle_text_cmd m end end @@ -80,33 +75,27 @@ class Sv_Discord < Vrobot4::Server::AudioServer # (see Vrobot4::Server::Server#load_permissions) def load_permissions pinf @mprm = {chan: {}, role: {}, glob: {}} - pinf.each do |perm| - mod = Vrobot4::Module.get_module_type(perm["module"])[:type] - if perm.key? "channel" + pinf.each do |pr| + mod = Vrobot4::Module.get_module_type(pr["module"])[:type] + if pr.key? "channel" @mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod - @mprm[:chan][mod][perm["channel"]] = true - elsif perm.key? "roles" - @mprm[:role][mod] = perm["roles"] - elsif perm.key? "enable" - @mprm[:glob][mod] = perm["enable"] + @mprm[:chan][mod][pr["channel"]] = true + elsif pr.key? "roles" + @mprm[:role][mod] = pr["roles"] + elsif pr.key? "enable" + @mprm[:glob][mod] = pr["enable"] end - end + end if pinf end class ChannelPerms - def initialize - @cprm = {} - end + def initialize() @cprm = {} end # Returns true if the channel is enabled, false otherwise. - def [] chan - @cprm[chan.name] or @cprm[chan.real.id] - end + def [](chan) @cprm[chan.name] or @cprm[chan.real.id] end # Sets a channel's permission on/off. - def []= chan, set - @cprm[chan] = set - end + def []=(chan, set) @cprm[chan] = set end end private_constant :ChannelPerms