|
| 1 | +# deno-irc |
| 2 | + |
| 3 | +IRC client protocol module for [Deno](https://deno.land/). |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +IRC is not dead yet. |
| 8 | + |
| 9 | +This module aims to provide to Deno community an easy way to communicate with |
| 10 | +IRC through an abstraction built on top of the client protocol. |
| 11 | + |
| 12 | +Semantic Versioning will be used but breaking changes are expected on minor versions prior to `1.0.0`. |
| 13 | + |
| 14 | +Any feedback and contributions are welcome. |
| 15 | + |
| 16 | +## Contents |
| 17 | + |
| 18 | +- [Usage](#usage) |
| 19 | +- API |
| 20 | +- [Contributing](#contributing) |
| 21 | +- [License](#license) |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +Code is better than words: |
| 26 | + |
| 27 | +```ts |
| 28 | +import { Client } from "https://deno.land/x/irc/mod.ts"; |
| 29 | + |
| 30 | +const client = new Client({ nick: "my_nick" }); |
| 31 | +client.connect("irc.freenode.net", 6667); |
| 32 | +client.on("register", () => client.join("#my_chan")); |
| 33 | +``` |
| 34 | + |
| 35 | +Note that this code above requires the `--allow-net` option. |
| 36 | + |
| 37 | +It is also better to have at least one `"error"` event listener to avoid the client from crashing: |
| 38 | + |
| 39 | +```ts |
| 40 | +client.on("error", (error) => console.error(error)); |
| 41 | +``` |
| 42 | + |
| 43 | +There are only two main concepts to know to use this module: [events](#events) and [commands](#commands). |
| 44 | + |
| 45 | +### Events |
| 46 | + |
| 47 | +Events are simple messages which are emitted from the client instance. |
| 48 | + |
| 49 | +They can be received by listening to their event names: |
| 50 | + |
| 51 | +```ts |
| 52 | +client.on("join" (msg) => { |
| 53 | + console.log(`${msg.origin.nick} joins ${msg.channel}`); |
| 54 | +}); |
| 55 | + |
| 56 | +client.on("nick" (msg) => { |
| 57 | + console.log(`${msg.origin.nick} is now known as ${msg.nick}`); |
| 58 | +}); |
| 59 | + |
| 60 | +client.on("msg:channel", (msg) => { |
| 61 | + console.log(`${msg.origin.nick} talks to ${msg.channel}: ${msg.text}`); |
| 62 | +}); |
| 63 | + |
| 64 | +client.on("msg:private", (msg) => { |
| 65 | + console.log(`${msg.origin.nick} talks to you: ${msg.text}`); |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +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 pecificities of the protocol. |
| 70 | + |
| 71 | +### Commands |
| 72 | + |
| 73 | +Commands are the way to send messages to the server. |
| 74 | + |
| 75 | +They can be sent by calling them: |
| 76 | + |
| 77 | +```ts |
| 78 | +client.join("#channel"); |
| 79 | + |
| 80 | +client.msg("#channel", "Hello world!"); |
| 81 | + |
| 82 | +client.nick("new_nick"); |
| 83 | + |
| 84 | +client.topic("#channel", "New topic of the channel"); |
| 85 | + |
| 86 | +client.quit("Goodbye!"); |
| 87 | +``` |
| 88 | + |
| 89 | +### Uncaught errors |
| 90 | + |
| 91 | +When an error is thrown, it causes a crash. |
| 92 | + |
| 93 | +By listening to `"error"` events, errors will no longer be thrown and you will be |
| 94 | +able to handle them properly: |
| 95 | + |
| 96 | +```ts |
| 97 | +client.on("error", (error) => { |
| 98 | + // deal with the error message |
| 99 | + |
| 100 | + switch (error.name) { |
| 101 | + case "ConnectError": |
| 102 | + case "SendError": |
| 103 | + console.log(`${error.name}: ${error.message}`); |
| 104 | + break; |
| 105 | + default: |
| 106 | + console.error(error); |
| 107 | + } |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +This behavior is heavily inspired by the Node.js event emitter pattern. |
| 112 | + |
| 113 | +## Contributing |
| 114 | + |
| 115 | +_This is a first draft of a contributing section._ |
| 116 | + |
| 117 | +This module is built around two patterns: event driven architecture and plugins. |
| 118 | + |
| 119 | +It involves keeping the core as minimal as possible and delegates implementation of features to small independent parts (which are called plugins). |
| 120 | + |
| 121 | +The core contains some internal parts related to main parsers, sockets and event emitter. The plugins contain all the extra features built on top of the core client. |
| 122 | + |
| 123 | +In most of the cases, it is quite handy to add new features using plugins without touching the core. |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +MIT |
0 commit comments