Browse files

Moved bot definitions into core, updated most examples to autoruns, a…

…dded an 'all.bot' that includes all appropriate bots in a stack
  • Loading branch information...
1 parent de500be commit 2167b580cef5eb945f9a59068e682d3c58b27774 @raggi committed Nov 13, 2009
View
7 examples/all.bot
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+require 'talker_bot/autorun'
+
+use TalkerBot::Bots::Dice.new
+use TalkerBot::Bots::Fortune.new
+use TalkerBot::Bots::Greeter.new
+use TalkerBot::Bots::Inspector.new
View
13 examples/autorun.bot
@@ -1,14 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-class Inspector
- def respond_to?(name)
- true if name.to_s =~ /^on_/
- end
-
- def method_missing(name, *args)
- p [name, args]
- end
-end
-
-use Inspector.new
+use TalkerBot::Bots::Inspector.new
View
15 examples/cat.bot
@@ -1,16 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-class Cat
- def client=(client)
- Thread.new do
- while true
- client.send_message $stdin.readline.chomp
- end
- end
- rescue EOFError
- exit!
- end
-end
-
-use Cat.new
+use TalkerBot::Bots::Cat.new
View
12 examples/config.bot
@@ -1,11 +1 @@
-class Inspector
- def respond_to?(name)
- true if name.to_s =~ /^on_/
- end
-
- def method_missing(name, *args)
- p [name, args]
- end
-end
-
-use Inspector.new
+use TalkerBot::Bots::Inspector.new
View
21 examples/dice.bot
@@ -1,22 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-class Dice
- attr_accessor :client
- def on_message(sender, content, event)
- return if sender['name'] == 'bot'
- case content
- when /(\d+)[dD](\d+)(?:\+(\d+))?/
- dice, faces, bonus = $1.to_i, $2.to_i, $3
- if dice >= 1000 || faces >= 10000
- return client.send_message("#{dice}d#{faces} is too large")
- end
- results = Array.new(dice){ 1+rand(faces) }
- total = results.inject { |t,v| t + v }
- total += bonus.to_i if bonus
- client.send_message "#{total} from #{results.inspect} #{"+#{bonus}" if bonus}"
- end
- end
-end
-
-use Dice.new
+use TalkerBot::Bots::Dice.new
View
14 examples/fortune.bot
@@ -1,15 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-class Fortune
- attr_accessor :client
- def on_message(sender, content, event)
- return if sender['name'] == 'bot'
- case content
- when /!fortune/
- client.send_message `fortune`
- end
- end
-end
-
-use Fortune.new
+use TalkerBot::Bots::Fortune.new
View
23 examples/greeter.bot
@@ -1,24 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-class Greeter
- attr_accessor :client
-
- def on_message(sender, content, event)
- return if sender['name'] == 'bot'
- case content
- when /^@*bot:*\s+/
- client.send_message "Hi #{sender['name']}!"
- when /botsnack/
- client.send_message "botsnack, nom nom"
- end
- end
-
- def on_join(*users)
- users.each do |user|
- client.send_message "Welcome #{user['name']}, nice to see you!"
- end
- end
-end
-
-use Greeter.new
+use TalkerBot::Bots::Greeter.new
View
23 examples/youtube.bot
@@ -1,24 +1,3 @@
#!/usr/bin/env ruby
require 'talker_bot/autorun'
-
-abort "bzzzt, known broken atm"
-
-require 'youtube'
-
-class YouTube
- attr_accessor :client
-
- def initialize
- @toobes = YouTube::Client.new(File.read(ENV['HOME'] + '/.youtube').strip)
- end
-
- def on_message(sender, content, event)
- return if sender['name'] == 'bot'
- case content
- when /(.*) on youtube/
- client.send_message @toobes.search(:query => $1).first.url
- end
- end
-end
-
-use YouTube.new
+use TalkerBot::Bots::YouTube.new
View
10 lib/talker_bot.rb
@@ -1,11 +1,21 @@
require 'talker'
require 'talker/cli'
+require 'subload'
class TalkerBot
VERSION = '1.0.0'
TALKER_OPTS = [:host, :port, :room, :token]
TALKER_CALLBACKS = Talker.instance_methods.grep(/^on_/).map { |m| m.to_sym }
UNDERSCORE = '_'
+
+ module Bots
+ subload :Cat
+ subload :Youtube
+ subload :Greeter
+ subload :Fortune
+ subload :Dice
+ subload :Inspector
+ end
def self.start(config = {}, &block)
bot = new(config)
View
11 lib/talker_bot/bots/cat.rb
@@ -0,0 +1,11 @@
+class TalkerBot::Bots::Cat
+ def client=(client)
+ Thread.new do
+ while true
+ client.send_message $stdin.readline.chomp
+ end
+ end
+ rescue EOFError
+ exit!
+ end
+end
View
17 lib/talker_bot/bots/dice.rb
@@ -0,0 +1,17 @@
+class TalkerBot::Bots::Dice
+ attr_accessor :client
+ def on_message(sender, content, event)
+ return if sender['name'] == 'bot'
+ case content
+ when /(\d+)[dD](\d+)(?:\+(\d+))?/
+ dice, faces, bonus = $1.to_i, $2.to_i, $3
+ if dice >= 1000 || faces >= 10000
+ return client.send_message("#{dice}d#{faces} is too large")
+ end
+ results = Array.new(dice){ 1+rand(faces) }
+ total = results.inject { |t,v| t + v }
+ total += bonus.to_i if bonus
+ client.send_message "#{total} from #{results.inspect} #{"+#{bonus}" if bonus}"
+ end
+ end
+end
View
10 lib/talker_bot/bots/fortune.rb
@@ -0,0 +1,10 @@
+class TalkerBot::Bots::Fortune
+ attr_accessor :client
+ def on_message(sender, content, event)
+ return if sender['name'] == 'bot'
+ case content
+ when /!fortune/
+ client.send_message `fortune`
+ end
+ end
+end
View
19 lib/talker_bot/bots/greeter.rb
@@ -0,0 +1,19 @@
+class TalkerBot::Bots::Greeter
+ attr_accessor :client
+
+ def on_message(sender, content, event)
+ return if sender['name'] == 'bot'
+ case content
+ when /^@*bot:*\s+/
+ client.send_message "Hi #{sender['name']}!"
+ when /botsnack/
+ client.send_message "botsnack, nom nom"
+ end
+ end
+
+ def on_join(*users)
+ users.each do |user|
+ client.send_message "Welcome #{user['name']}, nice to see you!"
+ end
+ end
+end
View
9 lib/talker_bot/bots/inspector.rb
@@ -0,0 +1,9 @@
+class TalkerBot::Bots::Inspector
+ def respond_to?(name)
+ true if name.to_s =~ /^on_/
+ end
+
+ def method_missing(name, *args)
+ p [name, args]
+ end
+end
View
18 lib/talker_bot/bots/youtube.rb
@@ -0,0 +1,18 @@
+require 'youtube'
+
+class TalkerBot::Bots::YouTube
+ attr_accessor :client
+
+ def initialize
+ abort "bzzzt, known broken atm"
+ @toobes = YouTube::Client.new(File.read(ENV['HOME'] + '/.youtube').strip)
+ end
+
+ def on_message(sender, content, event)
+ return if sender['name'] == 'bot'
+ case content
+ when /(.*) on youtube/
+ client.send_message @toobes.search(:query => $1).first.url
+ end
+ end
+end

0 comments on commit 2167b58

Please sign in to comment.