Class: Twittbot::BotPart

Inherits:
Object
  • Object
show all
Defined in:
lib/twittbot/botpart.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ BotPart

Returns a new instance of BotPart.

Parameters:

  • name (Symbol)

    The name of the botpart. Should be the same as the file name without the extension.



7
8
9
10
11
12
13
14
15
16
# File 'lib/twittbot/botpart.rb', line 7

def initialize(name, &block)
  @botpart_config_path = File.expand_path("./etc/#{name}.yml")
  @config = $bot[:config].merge(if File.exist? @botpart_config_path
                                  YAML.load_file @botpart_config_path
                                else
                                  {}
                                end)
  instance_eval &block
  $bot[:botparts] << self
end

Instance Method Details

#clientTwitter::REST::Client Also known as: bot

Returns:

  • (Twitter::REST::Client)


68
69
70
# File 'lib/twittbot/botpart.rb', line 68

def client
  $bot[:client]
end

#cmd(name, options = {}, &block) ⇒ Object

Defines a new direct message command.

Parameters:

  • name (Symbol)

    The name of the command. Can only contain alphanumerical characters. The recommended maximum length is 4 characters.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :admin (Boolean) — default: true

    Require admin status for this command.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/twittbot/botpart.rb', line 78

def cmd(name, options = {}, &block)
  raise "Command already exists: #{name}" if $bot[:commands].include? name
  raise "Command name does not contain only alphanumerical characters" unless name.to_s.match /\A[A-Za-z0-9]+\z/

  opts = {
      admin: true
  }.merge(options)

  $bot[:commands][name] ||= {
      admin: opts[:admin],
      block: block
  }
end

#every(interval, unit = :minutes, options = {}, &block) ⇒ Object

Runs block every interval unit(s).

Parameters:

  • interval (Fixnum)
  • unit (Symbol) (defaults to: :minutes)

    the time unit. Can be one of:

    • :minute or :minutes

    • :hour or :hours

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :run_at_start (Boolean) — default: true

    Run the code in block when the bot finished starting.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/twittbot/botpart.rb', line 45

def every(interval, unit = :minutes, options = {}, &block)
  raise "Not a Fixnum: #{interval}" unless interval.is_a? Fixnum
  raise "Interval less than 1" if interval < 1

  opts = {
      run_at_start: true
  }.merge(options)

  case unit
    when :min, :mins, :minute, :minutes
    when :hr, :hrs, :hour, :hours, :horse
      interval *= 60
    else
      raise "Unknown unit: #{unit}"
  end
  $bot[:periodic] << {
      interval: interval,
      remaining: opts[:run_at_start] ? 0 : interval,
      block: block
  }
end

#on(name, *args, &block) ⇒ Object

Adds a new callback to name.

Parameters:

  • name (Symbol)

    The callback type. Can be (at least) one of:

    • :tweet

    • :mention

    • :retweet

    • :favorite

    • :friend_list

    • :direct_message (i.e. not command DMs, see #cmd for that)



28
29
30
31
32
33
34
# File 'lib/twittbot/botpart.rb', line 28

def on(name, *args, &block)
  $bot[:callbacks][name] ||= []
  $bot[:callbacks][name] << {
      args: args,
      block: block
  }
end

#save_configObject

Saves the botpart’s configuration. This is automatically called when Twittbot exits.



94
95
96
97
98
99
100
101
102
# File 'lib/twittbot/botpart.rb', line 94

def save_config
  botpart_config = Hash[@config.to_a - $bot[:config].to_a]

  unless botpart_config.empty?
    File.open @botpart_config_path, 'w' do |f|
      f.write botpart_config.to_yaml
    end
  end
end