Making IRC bots is incredibly simple in Raku, thanks to IRC::Client. It allows you to create a very simple bot in about 20 lines of code. There’s a plugin system that allows easy re-use of code between multiple bots, and adding customized features can be as easy as dropping in an anonymous class.
So, let’s get to it!
Get your dependencies installed
Raku uses zef as the standard module installer, and if you’re reading this, I’m assuming you have it available to you. Install IRC::Client with zef, and you should be good to get started.
zef install IRC::Client
Setting up the bot
To set up the bot, we’ll need to have a nickname to use, a server to connect to and a list of channels to join. To make it easier to run this is a program from your shell, I’ll be using a MAIN sub as well.
use IRC::Client; sub MAIN () { IRC::Client.new( nick => 'raku-advent', host => 'irc.darenet.org', channels => < #advent >, ).run; }
Let’s save this in a file called bot.pl6, and run it.
perl6 bot.pl6
This will run, and if you’re in the channel you specified in channels, you should see the bot joining in a short moment. However, the program itself doesn’t seem to provide any output. It would be highly convenient, especially during development, to show what it’s doing. This is possible by enabling the debug mode. Adding this to the new method call, making it look as follows.
IRC::Client.new( nick => 'raku-advent', host => 'irc.darenet.org', channels => < #advent >, debug => True, ).run;
If you restart the application now, you will see there’s a lot of output all of a sudden, showcasing the IRC commands the bot is receiving and sending in response. Now all we need to do is add some functionality.
Making the bot work
As described earlier, functionality of the bot is added in using plugins. These can be any class that implements the right method names. For now, we’ll stick to irc-to-me, which is a convenience method which is triggered whenever the bot is spoken to in a private message, or directly addressed in a channel.
The simplest example to get started with here is to simply have it respond with the message you sent to the bot. Let’s do this by adding an anonymous class as a plugin to the new method call.
IRC::Client.new( nick => 'raku-advent', host => 'irc.darenet.org', channels => < #advent >, debug => True, plugins => [ class { multi method irc-to-me ($e) { $e.text } } ], ).run;
When you restart the bot and talk to it on IRC, you will see it responding to you with the same message you sent it.
<@tyil> raku-advent: hi <raku-advent> tyil, hi <@tyil:> raku-advent: how are you doing <raku-advent> tyil, how are you doing
Adding some real features
So, you’ve seen how easy it is to get started with a simple IRC bot in just over a dozen lines. Let’s add two features that you may want your bot to support.
For convenience sake, I will only cover the class implementing the features, not the entire IRC::Client.new block.
Uptime
First off, let’s make the bot able to show the time its been running for. For this, I’ll make it respond to people asking it for “uptime”. We can use the irc-to-me convenience method for this again. After all, we probably don’t want it to respond every time someone discusses uptime, only when the bot is asked directly about it.
In Raku, there’s a special variable called $*INIT-INSTANT, which contains an Instant of the moment the program started. We can use this to easily get the Duration that the program has been running for.
class { multi method irc-to-me ($ where *.text eq 'uptime') { my $response = "I've been alive for"; my ($seconds, $minutes, $hours, $days, $weeks) = (now - $*INIT-INSTANT).polymod(60, 60, 24, 7); $response ~= " $weeks weeks" if $weeks; $response ~= " $days days" if $days; $response ~= " $hours hours" if $hours; $response ~= " $minutes minutes" if $minutes; $response ~= " $seconds seconds" if $seconds; $response ~ '.'; } }
Now, whenever you ask the bot for uptime, it will respond with a human friendly uptime notification.
<@tyil> uptime <@tyil> raku-advent: uptime <raku-advent> tyil, I've been alive for 5 minutes 8 seconds.
User points
Most channels have a bot that keeps track of user points, or karma as it’s sometimes referred to. There’s a module already that does this for us, called IRC::Client::Plugin::UserPoints. We don’t have to do much apart from installing it and adding it to the list of plugins.
zef install IRC::Client::Plugin::UserPoints
Once this finishes, the module can be used in your code. You will need to import it with a use statement, which you can put directly under the use IRC::Client line.
use IRC::Client; use IRC::Client::Plugin::UserPoints;
Now, in the list of plugins, add it as a new entry.
plugins => [ IRC::Client::Plugin::UserPoints.new, class { ... }, ],
This plugin makes the bot respond to !scores, !sum and whenever a nick is
given points using a ++ suffix, for instance, tyil++.
<@tyil> raku++ <@tyil> raku++ <@tyil> !scores <raku-advent> tyil, « raku » points: main: 2
Finding plugins
All plugins for IRC::Client that are shared on the community have the prefix IRC::Client::Plugin::, so you can search for that on modules.perl6.org to find plugins to use. Of course, you can easily add your own plugins to the ecosystem as well!
Winding down
As you can see, with some very simple code you can add some fun or important
tools to your IRC community using the Raku programming language. Try it out and
have some fun, and share your ideas with others!
The posts are interesting reading, including this one, but it seems to be a recurring problem that in code containing the lists-within-angle-brackets construct, it gets taken out by WordPress. This makes it harder to follow along since you have to guess what’s supposed to go there.
LikeLike
It’s indeed quite frustrating to see WordPress handle text inbetween less/greather than characters. I hope I got them all fixed on this particular post!
LikeLike