Skip to content

Instantly share code, notes, and snippets.

@liliakai
Last active August 29, 2015 13:59
Show Gist options
  • Save liliakai/10443308 to your computer and use it in GitHub Desktop.
Save liliakai/10443308 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'cinch'
RECENT_ENOUGH_MINS = 120
recent = Hash.new
class Song
def words
@words ||= []
end
def method_missing(method)
words.push(method.to_s)
self
end
def hear!(message)
message.split.each do |word|
words.shift if word.strip.gsub(/[?.!,;']?/, '') == words.first
end
end
def sung?
words.empty?
end
end
class StandupSong < Song
include Cinch::Plugin
listen_to :channel
def listen(m)
hear! m.message
if sung?
m.reply "So it begins."
self.its.standup.time.its.standup.time.its.standup.standup.standup.time
end
end
end
class Tell
class TellStruct < Struct.new(:whom, :who, :what, :time)
def to_s
"#{whom}: at #{time.asctime} #{who} said: #{what}"
end
end
include Cinch::Plugin
listen_to :channel
match /tell (\S+) (.*)/
def initialize(*args)
super
@messages = {}
end
def listen(m)
Array(@messages.delete(m.user.nick)).each do |tell|
m.reply tell.to_s
end
end
def execute(m, nick, message)
if nick == @bot.nick
m.reply "That's me!"
elsif nick == m.user.nick
m.reply "That's you!"
else
@messages[nick] ||= []
@messages[nick].push TellStruct.new(nick, m.user.nick, message, Time.now)
m.reply "Ok, I'll tell #{nick}"
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.freenode.org"
c.channels = ["##whisper"]
c.nick = "fickleknifebot"
c.plugins.plugins = [Tell, StandupSong]
end
on :channel, /fi+c+k+l+e+ +k+n+i+f+e/i do |m|
candidates = m.channel.users.keys.reject { |user|
(user.nick == bot.nick) or (recent.has_key?(user) and ((DateTime.now - recent[user]) * (24*60)).to_i <= RECENT_ENOUGH_MINS)
}
puts "Chosen candidates: #{candidates.inspect}"
recent.delete_if { |user, timestamp|
((DateTime.now - timestamp) * (24*60)).to_i > RECENT_ENOUGH_MINS
}
chosen_one = candidates.sample
if chosen_one.nil?
m.reply "the fickle knife no longer cares to spin."
else
m.reply "The fickle knife of fate lands on #{chosen_one.nick}."
end
recent[chosen_one] = DateTime.now
end
end
bot.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment