1- const { EventEmitter} = require ( 'events' ) ;
2- const { stringify} = require ( 'querystring' ) ;
1+ const { EventEmitter } = require ( 'events' ) ;
2+ const { stringify } = require ( 'querystring' ) ;
33
44const Promise = require ( 'bluebird' ) ;
55const WS = require ( 'ws' ) ;
6- const { jar} = require ( 'request' ) ;
6+ const { jar } = require ( 'request' ) ;
77const request = require ( 'request-promise' ) ;
88const cheerio = require ( 'cheerio' ) ;
99
@@ -15,9 +15,8 @@ class Bot extends EventEmitter {
1515 Object . assign ( this , {
1616 mainRoom,
1717 jar : jar ( ) ,
18- ws : null ,
1918 fkey : null ,
20- rooms : new Set ( )
19+ rooms : { }
2120 } ) ;
2221 }
2322 async auth ( email , password ) {
@@ -49,82 +48,78 @@ class Bot extends EventEmitter {
4948 return body ;
5049 }
5150 async createWsConnection ( roomid , fkey ) {
52- const form = stringify ( { roomid, fkey} ) ;
51+ const form = stringify ( { roomid, fkey } ) ;
5352 const body = await request ( {
5453 method : 'POST' ,
5554 uri : `${ BASE_URL } /ws-auth` ,
5655 jar : this . jar ,
5756 body : form ,
58- headers : {
57+ headers : {
5958 Origin : BASE_URL ,
6059 Referer : `${ BASE_URL } /rooms/${ roomid } ` ,
6160 'Content-Length' : form . length ,
6261 'Content-Type' : 'application/x-www-form-urlencoded'
6362 }
6463 } ) ;
6564 const wsAddress = JSON . parse ( body ) . url ;
66- return new WS ( `${ wsAddress } ?l=99999999999` , { origin : BASE_URL } ) ;
65+ return new WS ( `${ wsAddress } ?l=99999999999` , { origin : BASE_URL } ) ;
6766 }
68- async listen ( roomid ) {
69- if ( ! this . fkey ) {
67+ async join ( roomid = null ) {
68+ if ( ! this . fkey ) {
7069 throw new Error ( 'Not connected' ) ;
7170 }
72- if ( ! roomid ) {
71+ if ( ! roomid ) {
7372 roomid = this . mainRoom ;
7473 }
75- this . rooms . add ( roomid ) ;
76- this . ws = await this . createWsConnection ( roomid , this . fkey ) ;
77- this . ws . on ( 'error' , error => this . emit ( 'error' , error ) ) ;
78- this . ws . on ( 'message' , ( message , flags ) => {
74+ if ( this . rooms [ roomid ] ) {
75+ throw new Error ( `Already joined room ${ roomid } ` ) ;
76+ }
77+ const ws = await this . createWsConnection ( roomid , this . fkey ) ;
78+ this . rooms [ roomid ] = ws ;
79+ ws . on ( 'error' , error => this . emit ( 'error' , error ) ) ;
80+ ws . on ( 'message' , ( message , flags ) => {
7981 const json = JSON . parse ( message ) ;
80- for ( let [ room , data ] of Object . entries ( json ) ) {
81- if ( data . e && Array . isArray ( data . e ) && ( data . t != data . d ) ) {
82+ for ( let [ room , data ] of Object . entries ( json ) ) {
83+ if ( data . e && Array . isArray ( data . e ) && ( data . t != data . d ) ) {
8284 data . e . forEach ( event => {
83- this . emit ( 'event' , { room, event} )
85+ this . emit ( 'event' , { room, event } )
8486 } ) ;
8587 }
8688 }
8789 } ) ;
88- this . ws . once ( 'open' , ( ) => {
89- this . emit ( 'open' ) ;
90+ ws . once ( 'open' , ( ) => {
91+ this . emit ( 'room- open' , roomid ) ;
9092 } ) ;
9193 }
92- async join ( roomid ) {
93- if ( this . rooms . has ( roomid ) ) {
94- throw new Error ( `Already joined room ${ roomid } ` ) ;
95- }
96- this . rooms . add ( roomid ) ;
97- const ws = await this . createWsConnection ( roomid , this . fkey ) ;
98- // TODO: implement rooms, for now just close the connection because it's not supported
99- ws . on ( 'open' , ( ) => ws . close ( ) ) ;
100- }
101- leaveAll ( ) {
102- if ( ! this . fkey ) {
94+ async leave ( roomid = 'all' ) {
95+ if ( ! this . fkey ) {
10396 throw new Error ( 'Not connected' ) ;
10497 }
98+ if ( ! this . rooms [ roomid ] ) {
99+ throw new Error ( `Not connected to room ${ roomid } ` ) ;
100+ }
101+ if ( roomid === 'all' ) {
102+ for ( const ws of Object . values ( this . rooms ) ) {
103+ if ( ws && ws . readyState !== WS . CLOSED ) {
104+ ws . close ( ) ;
105+ }
106+ }
107+ } else {
108+ const ws = this . rooms [ roomid ] ;
109+ if ( ws && ws . readyState !== WS . CLOSED ) {
110+ ws . close ( ) ;
111+ }
112+ }
105113 return request ( {
106114 method : 'POST' ,
107- uri : `${ BASE_URL } /chats/leave/all ` ,
115+ uri : `${ BASE_URL } /chats/leave/${ roomid } ` ,
108116 jar : this . jar ,
109117 form : {
110118 quiet : true ,
111119 fkey : this . fkey
112120 }
113121 } ) ;
114122 }
115- quit ( leave = false ) {
116- if ( this . ws && this . ws . readyState !== WS . CLOSED ) {
117- this . ws . close ( ) ;
118- }
119- if ( this . fkey ) {
120- if ( leave ) {
121- return this . leaveAll ( ) . then ( ( ) => this . emit ( 'close' ) ) ;
122- } else {
123- this . emit ( 'close' ) ;
124- return Promise . resolve ( ) ;
125- }
126- }
127- }
128123 async apiRequest ( path , form ) {
129124 const response = await request ( {
130125 method : 'POST' ,
@@ -134,17 +129,17 @@ class Bot extends EventEmitter {
134129 return ( response && response . length ) ? JSON . parse ( response ) : { } ;
135130 }
136131 send ( text , roomid ) {
137- if ( ! roomid ) {
132+ if ( ! roomid ) {
138133 roomid = this . mainRoom ;
139134 }
140135 const path = `/chats/${ roomid } /messages/new` ;
141- return this . apiRequest ( path , { text} ) . then ( data => data . id ) ;
136+ return this . apiRequest ( path , { text } ) . then ( data => data . id ) ;
142137 }
143138 edit ( text , messageId ) {
144139 const path = `/messages/${ messageId } ` ;
145- return this . apiRequest ( path , { text} ) ;
140+ return this . apiRequest ( path , { text } ) ;
146141 }
147- handleEvent ( { room, event} ) {
142+ handleEvent ( { room, event } ) {
148143 console . log ( room ) ;
149144 console . log ( event ) ;
150145 }
0 commit comments