Skip to content

Commit 690eefb

Browse files
author
Ben
committed
Add cookie jar to api requests, fixed some missing fkeys, finally fixed rooms (I think)
1 parent 6c73f93 commit 690eefb

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

src/bot.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Bot extends EventEmitter {
1616
mainRoom,
1717
jar: jar(),
1818
fkey: null,
19+
ws: null,
1920
rooms: {}
2021
});
2122
}
@@ -65,51 +66,38 @@ class Bot extends EventEmitter {
6566
return new WS(`${wsAddress}?l=99999999999`, { origin: BASE_URL });
6667
}
6768
async join(roomid = null) {
69+
const originalRoom = roomid !== null;
6870
if (!this.fkey) {
6971
throw new Error('Not connected');
7072
}
7173
if (!roomid) {
7274
roomid = this.mainRoom;
7375
}
74-
if (this.rooms[roomid]) {
75-
throw new Error(`Already joined room ${roomid}`);
76-
}
7776
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) => {
81-
const json = JSON.parse(message);
82-
for (let [room, data] of Object.entries(json)) {
83-
if (data.e && Array.isArray(data.e) && (data.t != data.d)) {
84-
data.e.forEach(event => {
85-
this.emit('event', { room, event })
86-
});
77+
if(originalRoom) {
78+
ws.on('message', () => ws.close());
79+
} else {
80+
ws.on('error', error => this.emit('error', error));
81+
ws.on('message', (message, flags) => {
82+
const json = JSON.parse(message);
83+
for (let [room, data] of Object.entries(json)) {
84+
if (data.e && Array.isArray(data.e) && (data.t != data.d)) {
85+
data.e.forEach(event => {
86+
this.emit('event', { room, event })
87+
});
88+
}
8789
}
88-
}
89-
});
90-
ws.once('open', () => {
91-
this.emit('room-open', roomid);
90+
});
91+
this.ws = ws;
92+
}
93+
return new Promise(resolve => {
94+
ws.once('open', resolve);
9295
});
9396
}
9497
async leave(roomid = 'all') {
9598
if (!this.fkey) {
9699
throw new Error('Not connected');
97100
}
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-
}
113101
return request({
114102
method: 'POST',
115103
uri: `${BASE_URL}/chats/leave/${roomid}`,
@@ -124,6 +112,7 @@ class Bot extends EventEmitter {
124112
const response = await request({
125113
method: 'POST',
126114
uri: `${BASE_URL}/${path}`,
115+
jar: this.jar,
127116
form
128117
});
129118
return (response && response.length) ? JSON.parse(response) : {};
@@ -132,12 +121,18 @@ class Bot extends EventEmitter {
132121
if (!roomid) {
133122
roomid = this.mainRoom;
134123
}
135-
const path = `/chats/${roomid}/messages/new`;
136-
return this.apiRequest(path, { text }).then(data => data.id);
124+
const path = `chats/${roomid}/messages/new`;
125+
return this.apiRequest(path, {
126+
text,
127+
fkey: this.fkey
128+
}).then(data => data.id);
137129
}
138130
edit(text, messageId) {
139-
const path = `/messages/${messageId}`;
140-
return this.apiRequest(path, { text });
131+
const path = `messages/${messageId}`;
132+
return this.apiRequest(path, {
133+
text,
134+
fkey: this.fkey
135+
});
141136
}
142137
handleEvent({ room, event }) {
143138
console.log(room);

0 commit comments

Comments
 (0)