Created
March 5, 2018 00:03
-
-
Save DanielFGray/2f0e510cca79f0fc7bcb1680c73d557b to your computer and use it in GitHub Desktop.
ircjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require('net') | |
const { Observable } = require('rxjs') | |
const R = require('ramda') | |
const { | |
T, | |
concat, | |
cond, | |
drop, | |
equals, | |
once, | |
pipe, | |
prop, | |
replace, | |
split, | |
test, | |
} = R | |
const id = x => x | |
const isString = R.is(String) | |
const tap = (...fn) => v => (pipe(...fn)(v), v) // eslint-disable-line no-sequences | |
const log = tap(console.log) | |
const client = net.connect({ host: 'irc.rizon.net', port: '6667' }) | |
client.setEncoding('utf-8') | |
const nick = 'dysfiguredBot' | |
const write = pipe( | |
tap(x => client.write(`${x}\r\n`)), | |
x => log(`--> ${x}`), | |
) | |
const connection$ = Observable | |
.create(o => { | |
client.on('data', x => o.next(x)) | |
client.on('error', x => o.next(x)) | |
}) | |
.map(cond([ | |
[isString, pipe(replace('\r', ''), split('\n'))], | |
[T, pipe(log, Observable.of)], | |
])) | |
.flatMap(id) | |
.filter(id) | |
.do(x => log(`<-- ${x}`)) | |
.publishReplay() | |
.refCount() | |
connection$ | |
.filter(test(/hostname/)) | |
.flatMap(once(() => Observable.of(`NICK ${nick}`, `USER ${nick} a b :${nick}`))) | |
.subscribe(write, console.error) | |
connection$ | |
.filter(pipe(split(' '), prop(1), equals('001'))) | |
.mapTo('JOIN #dysfigured') | |
.subscribe(write, console.error) | |
connection$ | |
.filter(test(/^PING/)) | |
.map(pipe(drop(5), concat('PONG '))) | |
.subscribe(write, console.error) | |
const processOn = (events, fn) => events.forEach(e => process.on(e, fn)) | |
processOn(['SIGTERM', 'SIGINT', 'exit'], () => { | |
write('QUIT Bot terminated') | |
process.exit() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment