Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
diingus committed Jul 2, 2020
0 parents commit 6d2fcf4
Show file tree
Hide file tree
Showing 5 changed files with 887 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
76 changes: 76 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";
import api from './api.js';

let config = {
room: 'global',
global: false,
credentials: null,
};

const id = a => a;

const compose = ( f, g ) => {
return x => {
return f( g( x ) );
};
};

// maybe monad composition, basically
const maybeCompose = ( f, g ) => {
return x => {
if( !f || !g ) return undefined;

const y = g( x );
if( !y ) return undefined;
else return f( x );
};
};

const handleMessages = ( self, ms ) => {
const transform = self.transformers.reduce( (x, y) => maybeCompose(x, y), id );
const filter = self. filters.reduce( (x, y) => maybeCompose(x, y), id );
for( const m of ms ) {
const mp = transform( m );
if( mp ) {
if( filter( mp ) ) {
self.consumer( mp );
}
}
}
};

const reduceHtml = m => {
const mp = m;
// <p>
//mp.message = m.message.substring( 3, m.message.length-5 );

// <a>gets left</a>
mp.message = mp.message.replace( /<\/?a[\w ="':\/\\.]*>/gi, "" );

// <img> to :emotes:
//mp.message = mp.message.replace( /<img[\w ="':\/\\.\-?]*>/gi, m => {
// return m.search( /https:\/\/[\/\w\.\-?]*/ );
//});
return mp;
};

const roomCheck = ( m ) => {
return ( m.channel !== config.room && !config.global ) ? undefined : m;
};

export default {

get config() { return config; },
set config(c) { config = c; },

transformers: [ reduceHtml ],
filters: [ roomCheck ],

consumer( m ) { console.log( m ); },

async init() {
api.rcvMessageBulk = ms => handleMessages( this, ms );
await api.init( this.config.room, this.config.credentials );
}

};
15 changes: 15 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
prefix: '[bitwave.tv bot] ',

info( message ) {
console.log( this.prefix + message );
},

warn( message ) {
console.warn( this.prefix + '[WARN] ' + message );
},

error( message ) {
console.error( this.prefix + '[ERROR] ' + message );
},
};
Loading

0 comments on commit 6d2fcf4

Please sign in to comment.