Skip to content

Anatomy of an IRC Event

JR edited this page Mar 3, 2023 · 15 revisions

Parsing and IRCEvents

The IRC parsing has been split into a separate library. Please see the dialect wiki entry for more information on IRCEvents.

Plugins

Plugins will only ever have to deal with being passed IRCEvents. Given an IRCEvent event, you can easily write together some code that deals with event.sender, event.content, event.channel and so forth, without knowing anything but the mere basics of how IRC communication works.

A skeletal plugin is less than 30 lines of code. You write it as a D module, with module-level functions as you want your implementation to be. Then you merely annotate said functions with an IRCEventHandler struct, enumerating what kind of IRCEvent.Type you want the function to be triggered on. You don't need to register any callbacks, though it is possible. Both delegate and Fiber callbacks are technically supported.

The below plugin (or the onHello function of it) triggers on the message "!hello":

module kameloso.plugins.myplugin;

import kameloso.plugins;
import kameloso.plugins.common;
import kameloso.messaging;
import dialect.defs;

mixin ModuleRegistration!MyPlugin;

final class MyPlugin : IRCPlugin
{
    mixin IRCPluginImpl;
}

@(IRCEventHandler()
    .onEvent(IRCEvent.Type.CHAN)
    .permissionsRequired(Permissions.ignore)
    .addCommand(
        IRCEventHandler.Command()
            .word("hello")
            .policy(PrefixPolicy.prefixed)
    )
)
void onHello(MyPlugin plugin, const ref IRCEvent event)
{
    chan(plugin.state, event.channel, "Hello World!");
}

The ModuleRegistration mixin will generate module constructors that register your plugin to be instantiated on program startup/connect.

See the Seen plugin for an example plugin with comments.