Skip to content

Instantly share code, notes, and snippets.

@bf4
Last active December 26, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/7102266 to your computer and use it in GitHub Desktop.
Save bf4/7102266 to your computer and use it in GitHub Desktop.
my irc bot

My irc bot

ruby irc.rb &

Outputs server messages to irc-regular.log and channel PRIVMSG to irc-messages.log

source 'https://rubygems.org'
gem 'cinch'
gem 'god'
GEM
remote: https://rubygems.org/
specs:
cinch (2.0.9)
god (0.13.3)
PLATFORMS
ruby
DEPENDENCIES
cinch
god
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'god' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../Gemfile",
Pathname.new(__FILE__).realpath)
require 'rubygems'
require 'bundler/setup'
load Gem.bin_path('god', 'god')
require 'fileutils'
CURRENT_DIRECTORY = File.expand_path(File.dirname(__FILE__))
# pid_file_directory = File.join(CURRENT_DIRECTORY, 'pid')
# FileUtils.mkdir_p(pid_file_directory)
# God.pid_file_directory = pid_file_directory
def path(filename)
File.join(CURRENT_DIRECTORY, filename)
end
def start_cmd
p "bundle exec ruby #{path('irb.rb')} &"
end
# def stop_cmd(pid_file)
# p "kill -9 $(cat #{pid_file})"
# end
# def pid(pid_file)
# p File.read(pid_file)
# end
God.watch do |w|
w.name = "irc"
w.start = start_cmd
p w.pid_file
# w.stop = stop_cmd(w.pid_file)
# w.restart = [stop_cmd(w.pid_file), start_cmd].join(' && ')
w.keepalive(:memory_max => 50.megabytes,
:cpu_max => 10.percent)
end
require 'logger'
require 'cinch'
require 'cinch/logger/zcbot_logger'
class OutputLogger < Logger
def puts(msg)
info(msg)
end
end
class RegularOutputLogger < OutputLogger
def puts(msg)
super if msg =~ /\AREGULAR/
end
end
class MessageOutputLogger < OutputLogger
def puts(msg)
super unless msg =~ /\AREGULAR/
end
end
class MessageLogger < Cinch::Logger
def initialize(filename, logger=OutputLogger)
super(filename)
@output = logger.new(filename)
output.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end
end
# include Cinch::Plugin
# plugin_name = "testplugin"
def log(messages, event, level=event)
messages = Array(messages).map do |m|
if m.is_a?(Cinch::Message)
# next unless m.command == 'PRIVMSG'
[m.command, m.channel, m.user, m.message].map(&:to_s).join("\t")
else
"REGULAR: #{m}"
end
end.compact
super
end
end
bot = Cinch::Bot.new do
def regular_output_logger
log_path = File.expand_path('../irc-regular.log', __FILE__)
logger = RegularOutputLogger
MessageLogger.new(log_path, logger)
end
def message_output_logger
log_path = File.expand_path('../irc-messages.log', __FILE__)
logger = MessageOutputLogger
MessageLogger.new(log_path, logger)
end
self.loggers.clear
self.loggers << regular_output_logger
self.loggers << message_output_logger
configure do |c|
c.server = "irc.freenode.org"
c.nick = 'irclogger'
c.verbose = false
c.channels = %w(#yaml #railsrumble #jruby #rom-rb #rails-contrib #rubylang)
# c.plugins.options = {@plugin => {:key => :value}}
end
on :message do |m|
incoming m
end
end
bot.start
#!/usr/bin/env bash
# http://godrb.com/
god -c ./irc.god
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment