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)


70
71
72
# File 'lib/twittbot/botpart.rb', line 70

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.



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

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.



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

def every(interval, unit = :minutes, options = {}, &block)
  raise "Not a Integer: #{interval}" unless interval.is_a? Integer
  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 one of:

    • :tweet

    • :mention

    • :retweet

    • :favorite

    • :friend_list

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

    • :load (when the bot finished initializing)

    • :deleted (when a tweet got deleted, only stores id in object)



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

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.



116
117
118
119
120
121
122
123
124
# File 'lib/twittbot/botpart.rb', line 116

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

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

Defines a new task to be run outside of a running Twittbot process.

Parameters:

  • name (Symbol)

    The name of the task.

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

    A customizable set of options.

Options Hash (options):

  • :desc (String) — default: ""

    Description of this task



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/twittbot/botpart.rb', line 98

def task(name, options = {}, &block)
  name = name.to_s.downcase.to_sym
  task_re = /\A[a-z0-9.:-_]+\z/
  raise "Task already exists: #{name}" if $bot[:tasks].include?(name)
  raise "Task name does not match regexp #{task_re.to_s}" unless name.to_s.match(task_re)

  opts = {
    desc: ''
  }.merge(options)

  $bot[:tasks][name] ||= {
    block: block,
    desc: opts[:desc]
  }
end