Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now with Achievements! #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bot.yml
41 changes: 15 additions & 26 deletions Readme.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,22 @@ 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:
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
39 changes: 14 additions & 25 deletions chatbot.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions helpers.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions plugins/over_achiever.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'net/http'
require "uri"

module OverAchiever
def self.generate_badge(points, msg)
email = "[email protected]"

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
21 changes: 21 additions & 0 deletions plugins/over_achiever_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative './over_achiever'

class OverAchieverListener
include Cinch::Plugin

$help_messages << "achieve: <number> <message> Generate an achievement with <number> points"
$help_messages << "achievement: <number> <message> Generate an achievement with <number> points"
$help_messages << "achievement unlocked: <number> <message> Generate an achievement with <number> points"

listen_to :channel

def listen(m)
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)
m.reply badge.gsub('{{user}}', m.user.nick)
end
end
end
end