Created
August 7, 2011 03:23
-
-
Save jhsu/1130023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PluginHelper | |
module ClassMethods | |
attr_accessor :commands | |
def user_commands(*cmds) | |
@commands = cmds | |
end | |
end | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
# Usage info | |
def help | |
"Overwrite this help message with usage info." | |
end | |
# Method called when cinch regex matches message / event | |
# @param [Cinch::Message] msg message object passed in by cinch | |
# @param [String] cmd command ran by user, first match in regex | |
# @param [String] args any additional args | |
def execute(msg, cmd, args) | |
@msg = msg | |
if callable?(cmd) | |
reply send(cmd, *parse_args(args)) | |
else | |
reply help | |
end | |
rescue | |
reply help | |
end | |
# Send message | |
# @param [String] message to send | |
def reply(message) | |
if @msg | |
@msg.reply "#{message}" | |
else | |
puts "#{message}" | |
end | |
end | |
# Output any logging mid-command | |
# @param [String] info text to display | |
def log(info) | |
reply " #{info}" | |
end | |
# Check if command is available for users | |
# @param [String] cmd command | |
def callable?(cmd) | |
self.class.commands.include?(cmd.to_sym) | |
end | |
# Parse string arguments after a command | |
# @params [String] space seperated args | |
def parse_args(str="") | |
str.strip.split(/\s+/) | |
end | |
end | |
# class SumPlugin | |
# include PluginHelper | |
# | |
# user_commands :heh | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment