Class: Twittbot::BotPart
- Inherits:
-
Object
- Object
- Twittbot::BotPart
- Defined in:
- lib/twittbot/botpart.rb
Instance Method Summary collapse
- #client ⇒ Twitter::REST::Client (also: #bot)
-
#cmd(name, options = {}, &block) ⇒ Object
Defines a new direct message command.
-
#every(interval, unit = :minutes, options = {}, &block) ⇒ Object
Runs
block
everyinterval
unit
(s). -
#initialize(name, &block) ⇒ BotPart
constructor
A new instance of BotPart.
-
#on(name, *args, &block) ⇒ Object
Adds a new callback to
name
. -
#save_config ⇒ Object
Saves the botpart’s configuration.
-
#task(name, options = {}, &block) ⇒ Object
Defines a new task to be run outside of a running Twittbot process.
Constructor Details
#initialize(name, &block) ⇒ BotPart
Returns a new instance of BotPart.
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.("./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
#client ⇒ Twitter::REST::Client Also known as: bot
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.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/twittbot/botpart.rb', line 80 def cmd(name, = {}, &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() $bot[:commands][name] ||= { admin: opts[:admin], block: block } end |
#every(interval, unit = :minutes, options = {}, &block) ⇒ Object
Runs block
every interval
unit
(s).
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, = {}, &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() 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
.
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_config ⇒ Object
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.
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, = {}, &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() $bot[:tasks][name] ||= { block: block, desc: opts[:desc] } end |