Skip to content

Instantly share code, notes, and snippets.

@lynn
Created April 15, 2018 12:20
Show Gist options
  • Save lynn/eb651910cf3e0ba97a0d917eb883a37b to your computer and use it in GitHub Desktop.
Save lynn/eb651910cf3e0ba97a0d917eb883a37b to your computer and use it in GitHub Desktop.
Cute little IRC bot
require 'socket'
server, port, name, channels = ARGV
bot = Hash.new { |hash, key| -> _ { 'Unknown command: ' + key } }
bot['sum'] = -> args { args.map(&:to_i).reduce(0, :+) }
bot['dice'] = -> args { args.map{|n| rand(n.to_i) + 1}.join(' ') }
bot['emoji'] = -> args { (1..5).map{rand(300) + 0x1f300}.pack('U*') }
TCPSocket.open(server, port) do |irc|
irc.puts "NICK #{name}", "USER #{name} 0 * :#{name}"
while irc.gets.chomp do
puts $_
irc.puts "JOIN #{channels}" if ~/^:\S+ 004/ # registered!
irc.puts $_.sub('I', 'O') if ~/^PING :/ # answer pings
irc.puts "PRIVMSG #{$3?$2:$1} :#{bot[$4][$5.split]}" \
if ~/^:(\S+)!\S+ PRIVMSG ((#)?\S+) :#{name}: (\w+) ?(.*)/
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment