Module: Elephrame::Command

Includes:
Reply
Included in:
Bots::Command, Bots::GenerativeBot
Defined in:
lib/elephrame/streaming/command.rb

Instance Attribute Summary collapse

Attributes included from Reply

#on_reply

Instance Method Summary collapse

Methods included from Reply

#reply, #reply_with_mentions, #run_reply

Instance Attribute Details

#cmd_hashObject (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_regexObject (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

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/elephrame/streaming/command.rb', line 5

def commands
  @commands
end

#not_foundObject (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

#prefixObject (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

Parameters:

  • cmd (String)

    a command to add

  • block (Proc)

    the code to execute when cmd is recieved



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

Parameters:

  • block (Proc)

    a block to run when we don’t match a command



63
64
65
# File 'lib/elephrame/streaming/command.rb', line 63

def if_not_found &block
  @not_found = block
end

#run_commandsObject 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

Parameters:

  • usage (String)


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

Parameters:

  • pf (String)

    the prefix



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

Parameters:

  • prefix (String) (defaults to: '!')

    sets the command prefix, defaults to ‘!’

  • usage (String) (defaults to: nil)

    the response to the help command



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