Skip to content

Instantly share code, notes, and snippets.

@ololduck
Created March 17, 2012 15:32
Show Gist options
  • Save ololduck/2061237 to your computer and use it in GitHub Desktop.
Save ololduck/2061237 to your computer and use it in GitHub Desktop.
Ruby IRC bot
#!/usr/bin/env ruby
require 'socket'
class BotIRC
# initialize:
# @param: server Le serveur auquel se connecter
# pseudo Le pseudo IRC du bot. Sera aussi utilisé en USER
# chans Une liste des chans auquels le bot doit se connecter
# port Port par défaut
def initialize(server="irc.netrusk.net",
pseudo="RubyTest",
chan=["#testbot"],
port=6667
)
puts("Init...")
@server = server
@port = port
@pseudo = pseudo
@chan = chan
end
# serve: Méthode lançant le bot
def serve()
self.connect()
self.mainloop(0.05)
end
# connect: Méthode connectant et identifiant le bot
def connect()
@irc = TCPSocket.new(@server, @port)
@irc.puts("NICK #{@pseudo}\n")
@irc.puts("USER #{(@pseudo+" ")*3} :#{@pseudo}\n")
end
# mainloop SERVE FOREVER
def mainloop(freq=0.1)
while(line = @irc.gets())
puts(line)
if(line.match("PING :"))
@irc.puts("PONG : #{line.split(':')[1]}\n")
elsif(line.match(":End of /MOTD command."))
@chan.each do |c|
@irc.puts("JOIN #{c}")
end
end
sleep(freq)
end
end
end
if(__FILE__ == $0)
bot = BotIRC.new()
bot.serve()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment