Lesson 4: Listen for IRC events
Goal: answer !ping in a channel.
--! name: Ping--! description: Replies pong to !ping--! api: 1--! permissions: sendswizzin.on("message", function(e) if e.text == "!ping" then swizzin.send(e.target, "pong") endend)What it does
Section titled “What it does”swizzin.on(event, fn, opts?) calls fn with an event table for each event of that kind. Useful fields: e.nick (who), e.target (channel), e.text, e.network_name, e.time (milliseconds). Other events include join, part, quit, nick, kick, topic, mode, connect, disconnect and reconnecting: see IRC events.
Expected result
Section titled “Expected result”When someone types !ping in a channel you are in, the script replies pong on the same network.
Good to know
Section titled “Good to know”- By default a script does not receive your own events, so it cannot answer itself in a loop. Pass
{own = true}only if you need that. - Lines replayed by a bouncer are skipped unless you pass
{playback = true}. - Event text is untrusted: it comes from other people.
Common mistakes
Section titled “Common mistakes”- Assuming
e.targetis always a channel: for a private message it is the other nickname, andprivateis a separate event. - Slow handlers: a callback has 250 ms, and events past the queue of 256 are dropped.
Troubleshooting
Section titled “Troubleshooting”- Nothing happens: the script is off, lacks the
sendapproval, or you are not in the channel. - An unknown event name raises an error listing the valid names.