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
blockeveryintervalunit(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.
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
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.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twittbot/botpart.rb', line 78 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).
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, = {}, &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() 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.
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_config ⇒ Object
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 |