You want to create an IRC bot quickly? Then Isaac is you. It will be. At some point, at least. But you shall be welcome to try it out and help me extend and beautify it. Be aware, the code is not stellar by any measure, most likely it is very crude and a large portion of the IRC standard has not been implemented, simply because I haven’t needed it yet. Oh, and a lot of concepts were borrowed from Sinatra (sinatrarb.com). Thanks.
-
Wraps parsing of incoming messages and raw IRC commands in simple constructs.
-
Hides all the ugly regular expressions of matching IRC commands. Leaves only the essentials for you to match.
-
Takes care of dull stuff such as replying to PING-messages and avoiding excess flood.
An Isaac-bot needs a few basics:
require 'isaac' config do |c| c.nick = "AwesomeBot" c.server = "irc.freenode.net" c.port = 6667 end
That’s it. Run ruby bot.rb
and it will connect to the specified server.
After the bot has connected to the IRC server you might want to join some channels:
on :connect do join "#awesome_channel", "#WesternBar" end
Joining a channel and sitting idle is not much fun. Let’s repeat everything being said in these channels:
on :channel, /.*/ do msg channel, message end
Notice the channel
and message
variables. Additionally nick
and match
is available for channel-events. nick
being the sender of the message, match
being a MatchData object returned by the regular expression you specified:
on :channel, /^quote this: (.*)/ do msg channel, "Quote: '#{match[1]}' by #{nick}" end
If you want to match private messages use the +on :private+ event:
on :private, /^login (\S+) (\S+)/ do username = match[1] password = match[2] # do something to authorize or whatevz. msg nick, "Login successful!" end
Helpers should not be defined in the top level, but instead using the helpers
-constructor:
helpers do def rain_check(meeting) msg nick, "Can I have a rain check on the #{meeting}?" end end on :private, /date/ do rain_check("romantic date") end
Errors, as specified by RFC 1459, can be reacted upon as well. If you e.g. try to send a message to a non-existant nick you will get error 401: “No such nick/channel”.
on :error, 401 do # Do something. end
Available variables: nick
and channel
.
You might want to send messages, join channels etc. without it strictly being the result of an on()-event, e.g. send a message every time a RSS feed is updated or whatever. You can use Isaac.execute
for that, and all your normal commands, msg
, join
, topic
etc. will be available:
class K def smoke(brand) Isaac.execute { msg "harryjr", "you should smoke #{brand} cigarettes" } end end on :connect do k = K.new k.smoke("Lucky Strike") end
The source is hosted at GitHub: github.com/ichverstehe/isaac
------------------------------------------------------------------------------ "THE BEER-WARE LICENSE" (Revision 42): <ichverstehe@gmail.com> wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. ------------------------------------------------------------------------------