leave rooms when empty, use localStorage
This commit is contained in:
43
src/bot.js
43
src/bot.js
@@ -17,7 +17,6 @@ class OcrccBot {
|
||||
this.awaitingFacilitator = {};
|
||||
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
||||
this.joinedRooms = [];
|
||||
this.activeChatrooms = {};
|
||||
}
|
||||
|
||||
createLocalStorage() {
|
||||
@@ -56,7 +55,7 @@ class OcrccBot {
|
||||
}
|
||||
});
|
||||
});
|
||||
return this.sendMessage(roomId, content);
|
||||
await this.sendMessage(roomId, content);
|
||||
default:
|
||||
logger.log("error", `ERROR SENDING MESSAGE: ${err}`);
|
||||
break;
|
||||
@@ -82,7 +81,7 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async inviteFacilitators(roomId) {
|
||||
this.awaitingFacilitator[roomId] = true;
|
||||
this.localStorage.setItem(`${roomId}-waiting`, 'true')
|
||||
let chatOffline = true;
|
||||
|
||||
try {
|
||||
@@ -114,7 +113,7 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async uninviteFacilitators(roomId) {
|
||||
this.awaitingFacilitator[roomId] = false;
|
||||
this.localStorage.removeItem(`${roomId}-waiting`)
|
||||
|
||||
try {
|
||||
const groupUsers = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID)
|
||||
@@ -173,7 +172,7 @@ class OcrccBot {
|
||||
const time = date.toLocaleTimeString("en-GB", {
|
||||
timeZone: "America/New_York"
|
||||
});
|
||||
const filepath = this.activeChatrooms[roomId].transcriptFile;
|
||||
const filepath = this.localStorage.getItem(`${roomId}-transcript`)
|
||||
const message = `${sender} [${time}]: ${content.body}\n`;
|
||||
|
||||
fs.appendFileSync(filepath, message, "utf8");
|
||||
@@ -190,9 +189,6 @@ class OcrccBot {
|
||||
const command = content.body.substring("!bot".length).trim();
|
||||
|
||||
switch (command) {
|
||||
case "purge rooms":
|
||||
this.leaveEmptyRooms(senderId);
|
||||
break;
|
||||
case "transcript":
|
||||
this.sendTranscript(senderId, roomId);
|
||||
break;
|
||||
@@ -246,9 +242,8 @@ class OcrccBot {
|
||||
|
||||
async sendTranscript(senderId, roomId) {
|
||||
try {
|
||||
const transcriptFile = this.activeChatrooms[roomId]
|
||||
? this.activeChatrooms[roomId].transcriptFile
|
||||
: false;
|
||||
const transcriptFile = this.localStorage.getItem(`${roomId}-transcript`)
|
||||
|
||||
if (!transcriptFile) {
|
||||
this.sendTextMessage(
|
||||
roomId,
|
||||
@@ -320,17 +315,16 @@ class OcrccBot {
|
||||
member.userId === this.config.BOT_USERID &&
|
||||
!this.joinedRooms.includes(member.roomId)
|
||||
) {
|
||||
|
||||
try {
|
||||
const room = await this.client.joinRoom(member.roomId)
|
||||
logger.log("info", "AUTO JOINED ROOM" + room.roomId)
|
||||
logger.log("info", "AUTO JOINED ROOM => " + room.roomId)
|
||||
this.sendTextMessage(
|
||||
this.config.FACILITATOR_ROOM_ID,
|
||||
`A support seeker requested a chat (Room ID: ${room.roomId})`
|
||||
);
|
||||
this.inviteFacilitators(room.roomId)
|
||||
} catch(err) {
|
||||
logger.log("error", "ERROR JOINING ROOM =>" + err)
|
||||
logger.log("error", "ERROR JOINING ROOM => " + err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,11 +333,9 @@ class OcrccBot {
|
||||
if (
|
||||
member.membership === "join" &&
|
||||
member.userId !== this.config.BOT_USERID &&
|
||||
this.awaitingFacilitator[member.roomId]
|
||||
this.localStorage.getItem(`${member.roomId}-waiting`)
|
||||
) {
|
||||
this.activeChatrooms[member.roomId] = {
|
||||
facilitator: member.userId
|
||||
};
|
||||
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId)
|
||||
const event = {
|
||||
getType: () => {
|
||||
return "m.room.power_levels";
|
||||
@@ -380,7 +372,7 @@ class OcrccBot {
|
||||
});
|
||||
const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`;
|
||||
const filepath = path.resolve(path.join("transcripts", filename));
|
||||
this.activeChatrooms[member.roomId].transcriptFile = filepath;
|
||||
this.localStorage.setItem(`${member.roomId}-transcript`, filepath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -388,8 +380,8 @@ class OcrccBot {
|
||||
member.membership === "leave" &&
|
||||
member.userId !== this.config.BOT_USERID
|
||||
) {
|
||||
|
||||
if (this.activeChatrooms[member.roomId] && member.userId === this.activeChatrooms[member.roomId].facilitator) {
|
||||
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
|
||||
if (member.userId === facilitatorId) {
|
||||
this.sendTextMessage(
|
||||
member.roomId,
|
||||
`${member.name} has left the chat.`
|
||||
@@ -404,13 +396,15 @@ class OcrccBot {
|
||||
|
||||
if (memberCount === 1) { // just the bot
|
||||
logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
||||
this.client.leave(event.roomId)
|
||||
this.client.leave(member.roomId)
|
||||
this.localStorage.removeItem(`${member.roomId}-facilitator`)
|
||||
this.localStorage.removeItem(`${member.roomId}-transcript`)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setMessageListeners() {
|
||||
async setMessageListeners() {
|
||||
// encrypted messages
|
||||
this.client.on("Event.decrypted", (event, err) => {
|
||||
if (err) {
|
||||
@@ -442,6 +436,7 @@ class OcrccBot {
|
||||
|
||||
async start() {
|
||||
const localStorage = this.createLocalStorage();
|
||||
this.localStorage = localStorage
|
||||
|
||||
try {
|
||||
const auth = {
|
||||
@@ -465,7 +460,7 @@ class OcrccBot {
|
||||
await this.trackJoinedRooms()
|
||||
await this.client.initCrypto()
|
||||
await this.setMembershipListeners();
|
||||
this.setMessageListeners();
|
||||
await this.setMessageListeners();
|
||||
this.client.startClient({ initialSyncLimit: 0 })
|
||||
} catch(err) {
|
||||
this.handleBotCrash(undefined, err);
|
||||
|
||||
Reference in New Issue
Block a user