IRC client protocol module for Deno which aims to provide an easy way to talk with IRC servers.
Any feedback and contributions are welcome.
Now available on JSR.
The first thing to do is to import the Client
:
import { Client } from "https://deno.land/x/irc/mod.ts";
and just instantiate a new client like this:
const client = new Client({
nick: "my_nick",
channels: ["#my_channel"],
});
One instance manages one connection. If you want to connect to many servers, use many instances.
See API Reference to learn more about available options.
Then you can listen to events and send commands:
client.on("join", (msg) => {
if (msg.params.channel === "#my_channel") {
client.privmsg("#my_channel", "Hello world!");
}
});
Finally you have to establish a connection with the server:
await client.connect("irc.libera.chat", 6667);
await client.connect("irc.libera.chat", 7000, true); // with TLS
Note that connecting to servers requires the --allow-net
option:
deno run --allow-net ./code.ts
Events are simple messages which are emitted from the client instance.
They can be received by listening to their event names:
client.on("join", (msg) => {
const { source, params } = msg;
console.log(`${source?.name} has joined ${params.channel}`);
});
Thanks to TypeScript, type of msg
is always inferred from the event name so
you do not have to worry about what is in the object or about the IRC protocol.
client.on("nick", ({ source, params }) => {
console.log(`${source?.name} is now known as ${params.nick}`);
});
client.on("privmsg", ({ source, params }) => {
console.log(`${source?.name} on ${params.target} says ${params.text}`);
});
Some events, like "privmsg"
and "notice"
, can be filtered like this:
client.on("privmsg:channel", ({ source, params }) => {
console.log(`${source?.name} on ${params.target} says ${params.text}`);
});
client.on("notice:private", ({ source, params }) => {
console.log(`${source?.name} notices to you: ${params.text}`);
});
Subscribing to more than one event is also possible by passing an array of event names:
client.on(["part", "kick"], (msg) => {
// msg is PartEvent | KickEvent
});
See API Reference to learn more about events.
Commands are the way to send messages to the server.
They can be sent by just calling them:
client.join("#channel");
client.privmsg("#channel", "Hello world!");
client.quit("Goodbye!");
See API Reference to learn more about commands.
When an error is emitted, it will be thrown by default and causes a crash of the program.
To avoid the client from crashing, it is required to have at least one event
listener for the "error"
event name.
See API Reference to learn more about errors.
This module is mainly built around two patterns:
It involves keeping the core client as minimal as possible and delegates feature implementations to decoupled plugins parts.
The core contains some internal parts related to IRC protocol, TCP sockets and event system. Plugins contain all the extra features built on top of the core client.
In most of the cases, it is quite handy to add new features using plugins without touching the core.
All added parts (core and plugins):
Need help? Type deno task
.