Module: Elephrame::Command
- Includes:
- Reply
- Included in:
- Bots::Command, Bots::GenerativeBot
- Defined in:
- lib/elephrame/streaming/command.rb
Instance Attribute Summary collapse
-
#cmd_hash ⇒ Object
readonly
Returns the value of attribute cmd_hash.
-
#cmd_regex ⇒ Object
readonly
Returns the value of attribute cmd_regex.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#not_found ⇒ Object
readonly
Returns the value of attribute not_found.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Attributes included from Reply
Instance Method Summary collapse
-
#add_command(cmd, &block) ⇒ Object
Adds the command and block into the bot to process later also sets up the command regex.
-
#if_not_found(&block) ⇒ Object
What to do if we don’t match anything.
-
#run_commands ⇒ Object
(also: #run)
Starts loop to process any mentions, running command procs set up earlier.
-
#set_help(usage) ⇒ Object
Shortcut method to provide usage docs in response to help command.
-
#set_prefix(pf) ⇒ Object
sets the prefix for commands.
-
#setup_command(prefix = '!', usage = nil) ⇒ Object
Initializes the
commands
array,cmd_hash
.
Methods included from Reply
#reply, #reply_with_mentions, #run_reply
Instance Attribute Details
#cmd_hash ⇒ Object (readonly)
Returns the value of attribute cmd_hash.
6 7 8 |
# File 'lib/elephrame/streaming/command.rb', line 6 def cmd_hash @cmd_hash end |
#cmd_regex ⇒ Object (readonly)
Returns the value of attribute cmd_regex.
6 7 8 |
# File 'lib/elephrame/streaming/command.rb', line 6 def cmd_regex @cmd_regex end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
5 6 7 |
# File 'lib/elephrame/streaming/command.rb', line 5 def commands @commands end |
#not_found ⇒ Object (readonly)
Returns the value of attribute not_found.
6 7 8 |
# File 'lib/elephrame/streaming/command.rb', line 6 def not_found @not_found end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/elephrame/streaming/command.rb', line 5 def prefix @prefix end |
Instance Method Details
#add_command(cmd, &block) ⇒ Object
Adds the command and block into the bot to process later also sets up the command regex
50 51 52 53 54 55 56 |
# File 'lib/elephrame/streaming/command.rb', line 50 def add_command cmd, &block @commands.append cmd unless @commands.include? cmd @cmd_hash[cmd.to_sym] = block # build up our regex (this regex should be fine, i guess :shrug:) @cmd_regex = /\A#{@prefix}(?<cmd>#{@commands.join('|')})\b(?<data>.*)/m end |
#if_not_found(&block) ⇒ Object
What to do if we don’t match anything
63 64 65 |
# File 'lib/elephrame/streaming/command.rb', line 63 def if_not_found &block @not_found = block end |
#run_commands ⇒ Object Also known as: run
Starts loop to process any mentions, running command procs set up earlier
If a block is passed to this function it gets ran when no commands get matched. Otherwise the framework checks if not_found
exists and runs it
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/elephrame/streaming/command.rb', line 74 def run_commands @streamer.user do |update| next unless update.kind_of? Mastodon::Notification and update.type == 'mention' # set up the status to strip html, if needed update.status.class .module_eval { alias_method :content, :strip } if @strip_html store_mention_data update.status # strip our username out of the status post = update.status.content.gsub(/@#{@username} /, '') # see if the post matches our regex, running the stored proc if it does matches = @cmd_regex.match(post) unless matches.nil? @cmd_hash[matches[:cmd].to_sym] .call(self, matches[:data].strip, update.status) else if block_given? yield(self, update.status) else @not_found.call(self, update.status) unless @not_found.nil? end end end end |
#set_help(usage) ⇒ Object
Shortcut method to provide usage docs in response to help command
37 38 39 40 41 |
# File 'lib/elephrame/streaming/command.rb', line 37 def set_help usage add_command 'help' do |bot| bot.reply("#{usage}") end end |
#set_prefix(pf) ⇒ Object
sets the prefix for commands
28 29 30 |
# File 'lib/elephrame/streaming/command.rb', line 28 def set_prefix pf @prefix = pf end |
#setup_command(prefix = '!', usage = nil) ⇒ Object
Initializes the commands
array, cmd_hash
15 16 17 18 19 20 21 |
# File 'lib/elephrame/streaming/command.rb', line 15 def setup_command(prefix = '!', usage = nil) @commands = [] @cmd_hash = {} set_prefix prefix set_help usage unless usage.nil? end |