From 9b64761370871faaede12745eee255e9f818f1e4 Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Wed, 30 Jan 2013 15:07:39 -0600 Subject: [PATCH 1/6] Add an Achievement badge generator --- plugins/over_achiever.rb | 22 ++++++++++++++++++++++ plugins/over_achiever_listener.rb | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 plugins/over_achiever.rb create mode 100644 plugins/over_achiever_listener.rb diff --git a/plugins/over_achiever.rb b/plugins/over_achiever.rb new file mode 100644 index 0000000..4b91ad1 --- /dev/null +++ b/plugins/over_achiever.rb @@ -0,0 +1,22 @@ +require 'net/http' +require "uri" + +module OverAchiever + def self.generate_badge(points, msg) + email = "m8r-pgofgf@mailinator.com" + + uri = URI.parse("http://www.justachieveit.com/jai/code.jsp") + response = Net::HTTP.post_form(uri, { type: 1, email: email, text: msg, score: points }) + + if response.is_a?(Net::HTTPRedirection) && response['location'] + badge_response = Net::HTTP.get_response(URI.parse(response['location'])) + if badge_response.is_a?(Net::HTTPOK) && matches = badge_response.body.match(/src="(http:\/\/cdn\.justachieveit\.com\/[^"]+)"/) + matches[1] + else + "Sorry {{user}} I can't find the badge's URL" + end + else + "Sorry {{user}} I couldn't figure out where to get the badge's URL" + end + end +end diff --git a/plugins/over_achiever_listener.rb b/plugins/over_achiever_listener.rb new file mode 100644 index 0000000..a8c6aff --- /dev/null +++ b/plugins/over_achiever_listener.rb @@ -0,0 +1,19 @@ +require_relative './over_achiever' + +class OverAchieverListener + include Cinch::Plugin + + $help_messages << "achieve: Generate an achievement with points" + + listen_to :channel + + def listen(m) + if matches = m.message.match(/^achieve: (\d*)(\D.*)/) + points = matches[1].empty? 5 : matches[1] + msg = matches[2].strip + if badge = OverAchiever.generate_badge(points, msg) + m.reply badge.gsub('{{user}}', m.user.nick) + end + end + end +end From b11d6b6faa16eee99f8903284b2f3a45e698f854 Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Wed, 30 Jan 2013 16:28:56 -0600 Subject: [PATCH 2/6] fix typo --- plugins/over_achiever_listener.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/over_achiever_listener.rb b/plugins/over_achiever_listener.rb index a8c6aff..ba508c7 100644 --- a/plugins/over_achiever_listener.rb +++ b/plugins/over_achiever_listener.rb @@ -9,7 +9,7 @@ class OverAchieverListener def listen(m) if matches = m.message.match(/^achieve: (\d*)(\D.*)/) - points = matches[1].empty? 5 : matches[1] + points = matches[1].empty? ? 5 : matches[1] msg = matches[2].strip if badge = OverAchiever.generate_badge(points, msg) m.reply badge.gsub('{{user}}', m.user.nick) From 918702cc8f795fb1b1695d457e8761e6b2def33d Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Thu, 31 Jan 2013 11:37:56 -0600 Subject: [PATCH 3/6] Move plugin definitions to bot.yml --- Readme.rdoc | 39 ++++++++++++++------------------------- chatbot.rb | 39 ++++++++++++++------------------------- helpers.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 helpers.rb diff --git a/Readme.rdoc b/Readme.rdoc index 2e8fa9c..1151d83 100644 --- a/Readme.rdoc +++ b/Readme.rdoc @@ -6,31 +6,20 @@ A community built chat bot, just for fun. Send pull requests! It runs with straight Ruby. You'll need the Cinch and Cinch-Identify gems and Ruby 1.9, and you'll want to specify the nick and channel you want to use in a YAML file: - bot.yml +bot.yml:: settings: - nick: my_bot - channel: ecruby - nickserv_pass: sekrit - -If you plan to use the Karma plugin, you'll need a bit more: - - bot.yml - settings: - nick: my_bot - channel: ecruby - nickserv_pass: sekrit - persistence_url: ecruby.org - username: some_user - password: sekrit - -Then just run it with + server: 'irc.freenode.org' + nick: &nick 'my_bot' + channel: '#ecruby' + plugins: + "Repeater": + "OverAchieverListener": + "Cinch::Plugins::Identify": + require: 'cinch/plugins/identify' + nick: *nick + nickserv_pass: sekrit + type: nickserv + +Then just run it with: ruby chatbot.rb - - -== Things we can do - -* More games -* Database persistence -* Record links to Tumblr? -* Whatever else sounds like fun diff --git a/chatbot.rb b/chatbot.rb index 06708ba..9b81847 100644 --- a/chatbot.rb +++ b/chatbot.rb @@ -1,38 +1,27 @@ require 'rubygems' require 'cinch' -require 'cinch/plugins/identify' require 'yaml' +require './helpers' -begin - $settings = YAML.load(File.read("bot.yml")) -rescue - puts "create bot.yml and populate it with values. See the readme file!" -end - -$help_messages = [] +raise "create bot.yml and populate it with values. See the readme file!" unless File.exists?("bot.yml") -require './plugins/karma' -require './plugins/link_catcher' -require './plugins/repeater' +initialize_globals! +cinch_plugins = include_plugins($settings["settings"]['plugins']) -@irc = Cinch::Bot.new do - +@irc = Cinch::Bot.new do configure do |c| - c.server = "irc.freenode.org" - c.nick = $settings["settings"]["nick"] - c.channels = [$settings["settings"]["channel"]] - c.plugins.plugins = [Karma, LinkCatcher, Repeater, Cinch::Plugins::Identify] - c.plugins.options[Cinch::Plugins::Identify] = { - :username => $settings['settings']['nick'], - :password => $settings['settings']['nickserv_pass'], - :type => :nickserv - } + c.server = $settings["settings"]["server"] + c.nick = $settings["settings"]["nick"] + c.channels = [$settings["settings"]["channel"]] + c.plugins.plugins = cinch_plugins.keys + cinch_plugins.each do |plugin_class, options| + c.plugins.options[plugin_class] = options + end end - + on :message, /^!help/ do |m| - $help_messages.each{|message| m.user.send message } + $help_messages.each{ |message| m.user.send message } end - end @irc.start diff --git a/helpers.rb b/helpers.rb new file mode 100644 index 0000000..5688082 --- /dev/null +++ b/helpers.rb @@ -0,0 +1,26 @@ +require 'active_support' + +def include_plugins(plugin_list = {}) + plugin_list ||= {} + + plugins = plugin_list.inject({}) do |plugin_classes, plugin| + plugin_name, plugin_options = plugin + underscored_name = ActiveSupport::Inflector.underscore(plugin_name) + if plugin_options && path = plugin_options.delete('require') + require path + else + require "./plugins/#{underscored_name}" + end + plugin_class = ActiveSupport::Inflector.constantize(plugin_name) + plugin_classes[plugin_class] = (plugin_options || {}) + + plugin_classes + end + + return plugins +end + +def initialize_globals! + $settings = YAML.load(File.read("bot.yml")) + $help_messages = [] +end From aa13e1ed234d133ec63fb9ae6d8fa68d2744c36f Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Thu, 31 Jan 2013 11:47:09 -0600 Subject: [PATCH 4/6] Update new gem dependency in Readme --- Readme.rdoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.rdoc b/Readme.rdoc index 1151d83..211de6a 100644 --- a/Readme.rdoc +++ b/Readme.rdoc @@ -4,9 +4,9 @@ A community built chat bot, just for fun. Send pull requests! === Running it -It runs with straight Ruby. You'll need the Cinch and Cinch-Identify gems and Ruby 1.9, and you'll want to specify the nick and channel you want to use in a YAML file: +It runs with straight Ruby. You'll need the Cinch, Cinch-Identify and ActiveSupport gems and Ruby 1.9, and you'll want to specify the nick and channel you want to use in a YAML file: -bot.yml:: +[bot.yml]: settings: server: 'irc.freenode.org' nick: &nick 'my_bot' From 118b39db16c94d3cb8a5bc1e56545646b1b4cd26 Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Tue, 5 Feb 2013 14:36:55 -0600 Subject: [PATCH 5/6] allow for more options to trigger achievements --- plugins/over_achiever_listener.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/over_achiever_listener.rb b/plugins/over_achiever_listener.rb index ba508c7..6fc4da7 100644 --- a/plugins/over_achiever_listener.rb +++ b/plugins/over_achiever_listener.rb @@ -4,11 +4,13 @@ class OverAchieverListener include Cinch::Plugin $help_messages << "achieve: Generate an achievement with points" + $help_messages << "achievement: Generate an achievement with points" + $help_messages << "achievement unlocked: Generate an achievement with points" listen_to :channel def listen(m) - if matches = m.message.match(/^achieve: (\d*)(\D.*)/) + if matches = m.message.match(/^(?:ach[ie]{2}ve(?:ment)?|ach[ie]{2}vement unlocked): (\d*)(\D.*)/) points = matches[1].empty? ? 5 : matches[1] msg = matches[2].strip if badge = OverAchiever.generate_badge(points, msg) From 1db9cd2c38df00b0a7d015f447d37b44f7067299 Mon Sep 17 00:00:00 2001 From: Mike Weber Date: Tue, 5 Feb 2013 14:41:07 -0600 Subject: [PATCH 6/6] ignore bot.yml to keep config files local --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4b064d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bot.yml \ No newline at end of file