🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

modernirc

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

modernirc

IRC library for creating modern IRC bots

1.1.0
latest
npm
Version published
Maintainers
1
Created
Source

ModernIRC - The no-dependency NodeJS library to make easy IRC bots

Current Version Ko-Fi

Installation

npm i modernirc

Usage

import { createBot } from 'modernirc'

async function main() {
  await createBot({
    uplink: {
      host: 'localhost',
      port: 6667,
    },
    autojoin: [{ name: '#Home' }],
  })
}

main().catch(console.error)

Congratulations, the bot is now started.

Configuration

The configuration object that is passed as the first and only argument of the init function has the following default properties :

{
  "uplink": {
    "host": "localhost",
    "port": 6667,
    "password": null,
    "tls": false,
    "rejectUnauthorized": false,
    "autoPong": true
  },
  "identity": {
    "nickname": "modernbot",
    "username": "modernbot",
    "realname": "modernbot",
    "password": null
  },
  "autojoin": [],
  "modules": {},
  "logger": {},
  "api": {
    "enabled": false,
    "listen": {
      "port": 9999,
      "host": ""
    },
    "auth": {
      "key": null
    }
  }
}

The uplink configuration contains all the details of the connection to be made.

The host property can be the domain name or the IP address of the IRC server.

Identity configuration

On an IRC server, three properties are defining an user identity :

  • the nickname: the displayed name in the users list
  • the username: internal name used by the server to identify an user whatever the nickname is.
  • the realname: "real" name (can contain spaces and accents unlink nickname and username)

The password property is used by the bot if there is a services operator (like Anope or Atheme) to identify against a registered nickname. If the property is defined, the bot will wait for a specific message from the NickServ service before sending the identify command.

Auto Join

You can force the bot to automatically join channels at startup like this :

{
  autojoin: [
    { name: '#Foo' },
  ]
}

And if the channel needs a key :

{
  autojoin: [
    { name: '#Foo', key: 'bar' }
  ]
}

Modules

By default, the bot is doing just... nothing. It connects, takes some configuration from the server first messages but it will not react to anything.

This is the responsibility of the modules added to the bot.

Create a new module

In your project folder, start by creating a modules folder at the root. It will contain all the modules you'll add to the bot. Inside this new folder, create a new folder which will contain the actual module you're creating.

In our example, we'll create a module that will greet every people joining a channel where the bot is present.

mkdir -p modules/helloworld
// modules/helloworld/index.js

export function init(bot, options) {
  return {
    onJoin(message) {
      if (message.prefix.nickname !== bot.identity.nickname) {
        bot.message(message.params[0], `Hello, ${message.prefix.nickname}!`)
      }
    },
  }
}

The name of the function is built from the event name prefixed by on. For example, the join event will trigger the onJoin method, and a privmsg message will trigger a onPrivmsg method, etc.

Message structure

The message is a JSON object containing all the information relative to the incoming message, for example :

:foo!foo@bar.baz PRIVMSG #Home :Hello World !
{
  "prefix": {
    "nickname": "foo",
    "username": "foo",
    "hostname": "bar.baz"
  },
  "code": "PRIVMSG",
  "command": "privmsg",
  "params": ["#Home"],
  "message": "Hello World !",
  "self": false
}

The self property is set to true if the incoming message is a message from the bot itself. It can happen on join and channel notice events.

Event Names

IRC commands are alphanumeric codes that are translated in the library into some human-readable events.

First, there is a special event that handles every message recieved from the server, the message event.

Then, please find below the complete list of event names available:

CommandEvent Name
PRIVMSGprivmsg
JOINjoin
QUITquit
PARTpart
NOTICEnotice
NICKnick
KICKkick
TOPICtopic
MODEmode
INVITEinvite
001welcome
002yourhost
003created
004myinfo
005serverconfig
300none
302userhost
303ison
301away
305unaway
306noaway
311whoisuser
312whoisserver
313whoisoperator
317whoisidle
318endofwhois
319whoischannels
314whomasuser
369endofwhowas
321liststart
322list
323listend
324channelmodeis
325uniqopis
331notopic
332topic
341inviting
342summoning
346invitelist
347endofinvitelist
348exceptlist
349endofexceptlist
351version
352whoreply
315endofwho
353namreply
366endofnames
364links
365endoflinks
367banlist
368endofbanlist
371info
374endofinfo
375motdstart
372motd
376endofmotd
381youreoper
382rehashing
383youreservice
391time
392usersstart
393users
394endofusers
395nousers
200tracelink
201traceconnecting
202tracehandshake
203traceunknown
204traceoperator
205traceuser
206traceserver
207traceservice
208tracenewtype
209traceclass
210tracereconnect
261tracelog
262traceend
211statslinkinfo
212statscommands
213statscline
214statsnline
215statsiline
216statskline
218statsyline
219endofstats
241statsline
242statsuptime
243statsoline
244statshline
221umodeis
251luserclient
252luserop
253luserunknown
254luserchannels
255luserme
256adminme
257adminloc1
258adminloc2
259adminemail
263tryagain
401nosuchnick
402nosuchserver
403nosuchchannel
404cannotsendtochan
405toomanychannels
406wasnosuchnick
407toomanytargets
408nosuchservice
409noorigin
411norecipient
412notexttosend
413notoplevel
414wildtoplevel
421unknowncommand
422nomotd
423noadmininfo
424fileerror
431nonicknamegiven
432erroneousnickname
433nicknameinuse
436nickcollision
437unavailresource
441usernotinchannel
442notonchannel
443useronchannel
444nologin
445summondisabled
446usersdisabled
451notregistered
461needmoreparams
462alreadyregistered
463nopermforhost
464passwdmismatch
465yourebannedcreep
467keyset
471channelisfull
472unknownmode
473inviteonlychan
474bannedfromchan
475badchannelkey
476badchanmask
477nochanmodes
478banlistfull
481noprivileges
482chanoprivsneeded
483cantkillserver
484restricted
485uniqopprivsneeded
491nooperhost
501umodeunknownflag
502usersdontmatch
209traceclass
231serviceinfo
232endofservices
233service
235servlistend
316whoischanop
362closing
373infostart
466youwillbebanned
217statsqline
234servlist
361killdone
363closeend
384myportis
476badchanmask
492noservicehost

All events can be ignored or handled according to what you want to do with your bot.

Please note that even if all RFC 1459/2812 events have been mapped in the library, some of them are only triggered between servers or for IRCOps, so your bot will never receive them.

Logging

A logger is embedded in the robot to allow unified logs. It can write logs in console, in a file and to an HTTP request (as JSON or URL-encoded).

{
  // ...
  "logger": {
    "level": "info",
    "levels": ["error", "warning", "info", "log", "debug"],
    "transports": [{
      "type": "console",
      "level": "debug",
      "opts": { "pretty": true }
    }],
    "name": "app"
  }
}

By default, it logs to the console every message with level more important than info, so info, warning and error. You can customise the whole thing, and create custom transports if you need.

Child loggers

When a module uses the embedded logger (options.logger), it will automatically append the name of the module to the name in the configuration.

By default, the name is app. In a module hello, the name property will be app/hello. You can create your own logger children if you want with the child method.

// assuming the logger name is `app`
const child = logger.child('childName')

// this child will log with name `app/childName`

Create custom transport

A transport in the ModernIRC logger is a function taking the name and the opts object. It returns an object with a single function output(_level: string, _raw: string) doing the real job of outputting the log.

The output function can be sync or async, it will never be awaited to avoid I/O issues.

Example :

// configuration
{
  //...
  logger: {
    //...
    transports: [{
      type: 'custom',
      level: 'debug',
      builder: (name, opts) => ({
        output(_level: string, _raw: string) {
          console.log(`${_level}: ${_raw}`)
        },
      }),
    }],
  },
}

Custom format

If you don't want to write a full custom transport, but only format the output string, you can custom the log string format by giving a formatter property to a default transport metadata. A formatter is a function taking an object with the following properties :

  • date: Date of the log
  • name: Name of the logger (app or child)
  • level: Log level
  • text: Text message of the log

This function must return a string.

Example :

{
  //...
  logger: {
    //...
    transports: [{
      type: 'console',
      level: 'debug',
      formatter: ({ date, name, level, text }) => `${level} ${date} - ${text}\n`,
    }],
  },
}

HTTP Controller

You can control some actions of the bot from an HTTP interface. You must enable api in the configuration first, and define an authentication key.

{
  "api": {
    "enabled": true,
    "auth": {
      "key": "123456"
    }
  }
}

The key can be any string, but we recommend using UUIDs or a strong random bytes string. For every call done to this HTTP gateway, the key must be set in an HTTP header X-Key and is mandatory. If a call is done without this key, a 403 Forbidden will be returned.

Endpoints

GET /identity

{
  "nickname": "modernbot",
  "username": "modernbot",
  "realname": "modernbot"
}

This can be helpful to get the current identity of the bot.

Note that if you change the nickname, this will change it there as well.

GET /channels

{
  "#Bots": {
    "name": "#Bots",
    "users": [
      {
        "nickname": "chaksoft",
        "mode": ""
      },
      {
        "nickname": "modernbot",
        "mode": ""
      }
    ]
  }
}

Gets the full list of channels and users where the bot is present as well. This list is up-to-date according to the different join, part, kick and quit events.

Note that the bot will always be in all the users list.

GET /modules

[
  {
    "name": "hello",
    "enabled": true
  }
]

Gets the list of all the modules in the bot configuration and if they are enabled or not. In the future, the control API will be able to enable and disable modules on runtime.

Modifier endpoints

All the modifiers are just shortcuts for sending commands. Please note that all these endpoints will return 200 if the command has been successfully transmitted to the bot. But if the IRC server refused the command for whatever reason, it will not be bubbled back to the API.

In case of 200, all below endpoints will return a simple JSON with { ok: true }.

POST /nick

JSON Body

{
  "nickname": "ModernBot2"
}

Changes the nickname.

POST /join

JSON Body

{
  "channel": "#Foo",
  "key": "bar"
}

Joins a channel, key is optional if there is no key for the channel.

POST /part

JSON Body

{
  "channel": "#Foo"
}

Leaves the channel. If the bot is not in the channel, this command does nothing.

POST /privmsg

JSON Body

{
  "target": "#Bots",
  "message": "Hello there !"
}

Sends a message to the designated target.

POST /notice

JSON Body

{
  "target": "chaksoft",
  "message": "hello there, this is a notice !"
}

Sends a notice to the designated target.

POST /kick

JSON Body

{
  "channel": "#Bots",
  "target": "chaksoft",
  "reason": "Chop Chop !"
}

Kicks the designated target from the designated channel. Please note that if the bot has not enough privileges, the command will do nothing.

POST /module

JSON Body

{
  "moduleName": "hello",
  "someparam": "somevalue"
}

Sends a message to a specific module. The moduleName property is mandatory, if empty it will return a 404 error. The other properties of the body depends on what the module is waiting for in its onApiRequest handler.

export async function init(bot, options) {
  return {
    onApiRequest(body) {
      // do something here with the body (will not contain the `moduleName` property)
    },
  }
}

The onApiRequest can return a JSON object that will be serialized into the HTTP response (in the result property). If you return nothing, the response will be { "ok": true, "result": null }.

API Reference

Bot client object

bot.send(str)

Sends a raw command string to the server.

bot.message(target, str)

Sends a text message to the target. Target can be a channel or a private channel with another user.

bot.notice(target, str)

Sends a notice to the target. Target usually is an user but can also be a channel.

bot.join(channel, [key = null])

Joins a channel with the given key if applicable. Check for the error codes in the table above to check for available answers.

bot.changeNickname(nickname)

Changes the nickname of the bot. This change is temporary and is not saved. When the bot restarts, it will take the configured identity to register itself.

bot.part(channel, [reason = ''])

Makes the bot leave a specified channel and optionally a reason.

bot.kick(channel, target, [reason = ''])

Kicks a target (usually an user) from a channel. Please note that this command will do nothing if the bot has no operator rights in the specified channel. Otherwise, the CHANOPRIVSNEEDED error is sent by the server as an failure notification.

bot.joinedChannels

Array of channels where the bot is present. The list of users inside each channel is kept updated following the part, kick and quit events.

bot.serverConfig

Object containing all the supported modes and flags from the server. At startup, a server will send a full list of all supported modes and specific flags enabled.

Module options

options.logger

This property contains the child logger affected to the module. In order to have unified logs, it's recommended to use this one instead of console.log.

Keywords

irc

FAQs

Package last updated on 26 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts