Skip to content

Commit

Permalink
v0.1.1 - First Functional Release
Browse files Browse the repository at this point in the history
Added default command parser, `send()`, and `sendToChannel()`. 
Apart from docs, this makes the library useable.
  • Loading branch information
diingus authored Jul 3, 2020
2 parents 17ba980 + 1686d4c commit bfad2ab
Show file tree
Hide file tree
Showing 3 changed files with 765 additions and 14 deletions.
84 changes: 75 additions & 9 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"use strict";
import api from './api.js';
import api from '@bitwave/chat-client';
import $log from './log.js';

import TurndownService from 'turndown';
const turndownService = new TurndownService();

let config = {
room: 'global',
Expand Down Expand Up @@ -41,36 +45,98 @@ const handleMessages = ( self, ms ) => {

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

const unescapeHtml = unsafe => {
return unsafe
.replace( /&amp;/g, `&` )
.replace( /&lt;/g, `<` )
.replace( /&gt;/g, `>` )
.replace( /&quot;/g, `"` )
.replace( /&#39;/g, `'` );
};

// <p></p>
mp.message = mp.message.replace( /<\/?p[\w =#"':\/\\.\-?]*>/gi, "" );

// <a>gets left</a>
mp.message = mp.message.replace( /<\/?a[\w ="':\/\\.]*>/gi, "" );
// Custom links are text in <a>, and then the link in <kbd>
mp.message = mp.message.replace( /<\/?a[\w -=#"':\/\\.\-?]*>/gi, "" );
mp.message = mp.message.replace( "<kbd>", "" );
mp.message = mp.message.replace( "</kbd>", "" );

// <img> to :emotes:
//mp.message = mp.message.replace( /<img[\w ="':\/\\.\-?]*>/gi, m => {
// return m.search( /https:\/\/[\/\w\.\-?]*/ );
//});
mp.message = mp.message.replace( /<img[\w -=#"':\/\\.\-?]*>/gi, m => {
// (((, ))), :), :( are rendered differently
// (They dont even show up in the emote list)
//
// It wouldn't be so bad if the two echos were 'echol' and 'echor', but
// one echo is flipped with CSS.
if( m.includes('alt="echo"') ) {
return m.includes('scaleX(-1)') ? "(((" : ")))";
}
return m.match( /alt="([\w:()]+)"/ )[1];
});

mp.message = turndownService.turndown( mp.message );

return mp;
};

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

const commandParserSettings = {
prefix: '!',
commands: new Map(),
};
const isCommand = m => {
return m.message.startsWith( commandParserSettings.prefix ) ? m : false;
};
const commandParser = m => {
// removes prefix
m.message = m.message.replace( commandParserSettings.prefix, "" );
const args = m.message.split(' ');

const command = commandParserSettings.commands.get(args[0]);
if( command ) {
command( m, args.splice(1, 1) );
} else {
$log.info( `No command ${args[0]} found` );
}
};

export default {

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

get commandParserSettings() { return commandParserSettings; },
set commandParserSettings(s) { commandParserSettings = s; },

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

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

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

send( msg ) {
api.sendMessage({
message: msg,
channel: this.config.room,
global: this.config.global,
showBadge: true,
});
},

sendToChannel( msg, channel ) {
this.config.room = m.channel;
this.send( msg );
this.config.room = oldRoom;
}

};
Loading

0 comments on commit bfad2ab

Please sign in to comment.