Permalink
Browse files

Remove loudbot from examples

  • Loading branch information...
1 parent cc743fb commit fe67388f44e4d20e1d8050e1c8f8fd2a78f691db @Nerdmaster committed Jan 31, 2012
Showing with 3 additions and 221 deletions.
  1. +3 −3 README.md
  2. +0 −114 examples/loudbot/bot_runner.rb
  3. +0 −87 examples/loudbot/loudbot.rb
  4. +0 −17 examples/loudbot/shuffle.rb
View
@@ -20,9 +20,9 @@ I started my project.
Example Usage
======
-Need an example? For a separate project you can play with that relies on Net::YAIL, check out
-https://github.com/Nerdmaster/superloud. This is based on the code in the examples directory,
-but is easier to clone, run, and tinker with because it's a separate github project.
+Need a slightly more complex example? For a separate project you can play
+with that relies on Net::YAIL, check out https://github.com/Nerdmaster/superloud
+(fair warning: this code is not even remotely appropriate!).
For the nitty-gritty, you can see all this stuff in the [Net::YAIL docs](http://ruby-irc-yail.nerdbucket.com/)
page, as well as more complete documentation about the system. For a complete bot,
@@ -1,114 +0,0 @@
-# This is a demonstration of net-yail just to see what a "real" bot could do without much work.
-# Chances are good that you'll want to put things in classes and/or modules rather than go this
-# route, so take this example with a grain of salt.
-#
-# Yes, this is a very simple copy of an existing "loudbot" implementation, but using YAIL to
-# demonstrate the INCREDIBLE POWER THAT IS Net::YAIL. Plus, plagiarism is a subset of the cool
-# crime of stealing.
-#
-# Example of running this thing:
-# ruby bot_runner.rb --network irc.somewhere.org --channel "#bots"
-
-require 'rubygems'
-
-# Want a specific version of net/yail? Try uncommenting this:
-# gem 'net-yail', '1.x.y'
-
-require 'net/yail'
-require 'getopt/long'
-
-# Hacks Array#shuffle and Array#shuffle! for people not using the latest ruby
-require 'shuffle'
-
-# Pulls in all of loudbot's methods - filter/callback handlers for IRC events
-require 'loudbot'
-
-# User specifies network, channel and nick
-opt = Getopt::Long.getopts(
- ['--network', Getopt::REQUIRED],
- ['--channel', Getopt::REQUIRED],
- ['--nick', Getopt::REQUIRED],
- ['--debug', Getopt::BOOLEAN]
-)
-
-# Create bot object
-@irc = Net::YAIL.new(
- :address => opt['network'],
- :username => 'Frakking Bot',
- :realname => 'John Botfrakker',
- :nicknames => [opt['nick'] || "SUPERLOUD"]
-)
-
-# Loud messages can be newline-separated strings in louds.txt or an array or hash serialized in
-# louds.yml. If messages are an array, we convert all of them to hash keys with a score of 1.
-@messages = FileTest.exist?("louds.yml") ? YAML.load_file("louds.yml") :
- FileTest.exist?("louds.txt") ? IO.readlines("louds.txt") :
- {"ROCK ON WITH SUPERLOUD" => 1}
-if Array === @messages
- dupes = @messages.dup
- @messages = {}
- dupes.each {|string| @messages[string.strip] = 1}
-end
-
-@random_messages = @messages.keys.shuffle
-@last_message = nil
-@dirty_messages = false
-
-# If --debug is passed on the command line, we spew lots of filth at the user
-@irc.log.level = Logger::DEBUG if opt['debug']
-
-#####
-#
-# To learn the YAIL, begin below with attentiveness to commented wording
-#
-#####
-
-# This is a filter. Because it's past-tense ("heard"), it runs after the server's welcome message
-# has been read - i.e., after any before-filters and the main hanler happen.
-@irc.heard_welcome { |e| @irc.join(opt['channel']) if opt['channel'] }
-
-# on_xxx means it's a callback for an incoming event. Callbacks run after before-filters, and
-# replaces any existing incoming invite callback. YAIL has very few built-in callbacks, so
-# this is a safe operation.
-@irc.on_invite { |e| @irc.join(e.channel) }
-
-# This is just another callback, using the do/end block form. We auto-message the channel on join.
-@irc.on_join do |e|
- @irc.msg(e.channel, "WHATS WRONG WITH BEING SEXY") if e.nick == @irc.me
-end
-
-# You should *never* override the on_ping callback unless you handle the PONG manually!!
-# Filters, however, are perfectly fine.
-#
-# Here we're using the ping filter to actually do the serialization of our messages hash. Since
-# we know pings are regular, this is kind of a hack to serialize every few minutes.
-@irc.heard_ping do
- unless @dirty_messages
- File.open("louds.yml", "w") {|f| f.puts @messages.to_yaml}
- @dirty_messages = false
- end
-end
-
-# This is a before-filter - using the present tense means it's a before-filter, and using a tense
-# of "hear" means it's for incoming messages (as opposed to "saying" and "said", where we'd filter
-# our outgoing messages). Here we intercept all potential commands and send them to a method.
-@irc.hearing_msg {|e| do_command($1, e) if e.message =~ /^!(.*)$/ }
-
-# Another filter, but in-line this time - we intercept messages directly to the bot. The call to
-# +handled!+ tells the event not to run any more filters or the main callback.
-@irc.hearing_msg do |e|
- if e.message =~ /^#{@irc.me}/
- random_message(e.channel)
- e.handled!
- end
-end
-
-# This is our primary message callback. We know our filters have caught people talking to us and
-# any command-style messages, so we don't need to worry about those situations here. The decision
-# to make this the primary callback is pretty arbitrary - do what makes the most sense to you.
-#
-# Note that this is a proc-based filter - we handle the message entirely in incoming_message.
-@irc.on_msg self.method(:incoming_message)
-
-# Start the bot - the bang (!) calls the version of start_listening that runs an endless loop
-@irc.start_listening!
@@ -1,87 +0,0 @@
-# Stores a LOUD message into the hash and responds.
-def it_was_loud(message, channel)
- @irc.log.debug "IT WAS LOUD! #{message.inspect}"
-
- @messages[message] ||= 1
- random_message(channel)
-end
-
-# This is our main message handler.
-#
-# We store and respond if messages meet the following criteria:
-# * It is long (11 characters or more)
-# * It has at least one space
-# * It has no lowercase letters
-# * At least 60% of the characters are uppercase letters
-def incoming_message(e)
- # We don't respond to "private" messages
- return if e.pm?
-
- text = e.message
-
- return if text =~ /[a-z]/
-
- # Count various exciting things
- len = text.length
- uppercase_count = text.scan(/[A-Z]/).length
- space_count = text.scan(/\s/).length
-
- if len >= 11 && uppercase_count >= (len * 0.60) && space_count >= 1
- it_was_loud(e.message, e.channel)
- end
-end
-
-# Pulls a random message from our messages array and sends it to the given channel. Reshuffles
-# the main array if the randomized array is empty.
-def random_message(channel)
- @random_messages = @messages.keys.shuffle if @random_messages.empty?
- @last_message = @random_messages.pop
- @irc.msg(channel, @last_message)
-end
-
-# Just keepin' the plagiarism alive, man. At least in my version, size is always based on requester.
-def send_dong(channel, user_hash)
- old_seed = srand(user_hash)
- @irc.msg(channel, "8" + ('=' * (rand(20).to_i + 8)) + "D")
- srand(old_seed)
-end
-
-# Adds +value+ to the score of the last message, if there was one. If the score goes too low, we
-# remove that message forever.
-def vote(value)
- return unless @last_message
-
- @messages[@last_message] += value
- if @messages[@last_message] <= -1
- @last_message = nil
- @messages.delete(@last_message)
- end
- @dirty_messages = true
-end
-
-# Reports the last message's score
-def score(channel)
- if !@last_message
- @irc.msg(channel, "NO LAST MESSAGE OR IT WAS DELETED BY !DOWNVOTE")
- return
- end
-
- @irc.msg(channel, "#{@last_message}: #{@messages[@last_message]}")
-end
-
-# Handles a command (string begins with ! - to keep with the pattern, I'm making our loudbot only
-# respond to loud commands)
-def do_command(command, e)
- case command
- when "DONGME" then send_dong(e.channel, e.msg.user.hash + e.msg.host.hash)
- when "UPVOTE" then vote(1)
- when "DOWNVOTE" then vote(-1)
- when "SCORE" then score(e.channel)
- when "HELP" then @irc.msg(e.channel, "I HAVE COMMANDS AND THEY ARE !DONGME !UPVOTE !DOWNVOTE !SCORE AND !HELP")
- end
-
- # Here we're saying that we don't want any other handling run - no filters, no handler. For
- # commands, I put this here because I know I don't want any other handlers having to deal with
- # strings beginning with a bang.
- e.handled!
-end
@@ -1,17 +0,0 @@
-# Dynamically adds Array shuffling as described in http://www.ruby-forum.com/topic/163649
-class Array
- # Shuffle the array
- def shuffle!
- n = length
- for i in 0...n
- r = Kernel.rand(n-i)+i
- self[r], self[i] = self[i], self[r]
- end
- self
- end
-
- # Return a shuffled copy of the array
- def shuffle
- dup.shuffle!
- end
-end

0 comments on commit fe67388

Please sign in to comment.