leave empty rooms
This commit is contained in:
parent
242d32639a
commit
39d97343a5
92
src/bot.js
92
src/bot.js
@ -42,21 +42,26 @@ class OcrccBot {
|
|||||||
|
|
||||||
async sendMessage(roomId, content) {
|
async sendMessage(roomId, content) {
|
||||||
logger.log("info", `SENDING MESSAGE: ${content.body}`)
|
logger.log("info", `SENDING MESSAGE: ${content.body}`)
|
||||||
return this.client.sendMessage(roomId, content).catch(err => {
|
try {
|
||||||
|
await this.client.sendMessage(roomId, content)
|
||||||
|
} catch(err) {
|
||||||
switch (err["name"]) {
|
switch (err["name"]) {
|
||||||
case "UnknownDeviceError":
|
case "UnknownDeviceError":
|
||||||
Object.keys(err.devices).forEach(userId => {
|
Object.keys(err.devices).forEach(userId => {
|
||||||
Object.keys(err.devices[userId]).map(async deviceId => {
|
Object.keys(err.devices[userId]).map(async deviceId => {
|
||||||
|
try {
|
||||||
await this.client.setDeviceVerified(userId, deviceId, true);
|
await this.client.setDeviceVerified(userId, deviceId, true);
|
||||||
|
} catch(err) {
|
||||||
|
logger.log("error", `ERROR VERIFYING DEVICE: ${err}`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return this.sendMessage(roomId, content);
|
return this.sendMessage(roomId, content);
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
logger.log("error", `ERROR SENDING MESSAGE: ${err}`);
|
logger.log("error", `ERROR SENDING MESSAGE: ${err}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inviteUserToRoom(roomId, member) {
|
inviteUserToRoom(roomId, member) {
|
||||||
@ -98,7 +103,7 @@ class OcrccBot {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (chatOffline) {
|
if (chatOffline) {
|
||||||
logger.log('info', "CHAT OFFLINE!")
|
logger.log('info', "NO FACILITATORS ONLINE")
|
||||||
this.sendTextMessage(roomId, this.config.CHAT_OFFLINE_MESSAGE);
|
this.sendTextMessage(roomId, this.config.CHAT_OFFLINE_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,6 +190,9 @@ class OcrccBot {
|
|||||||
const command = content.body.substring("!bot".length).trim();
|
const command = content.body.substring("!bot".length).trim();
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
|
case "purge rooms":
|
||||||
|
this.leaveEmptyRooms(senderId);
|
||||||
|
break;
|
||||||
case "transcript":
|
case "transcript":
|
||||||
this.sendTranscript(senderId, roomId);
|
this.sendTranscript(senderId, roomId);
|
||||||
break;
|
break;
|
||||||
@ -216,6 +224,26 @@ class OcrccBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async leaveEmptyRooms(senderId) {
|
||||||
|
try {
|
||||||
|
const roomData = await this.client.getJoinedRooms()
|
||||||
|
const joinedRoomsIds = roomData["joined_rooms"]
|
||||||
|
joinedRoomsIds.forEach(async roomId => {
|
||||||
|
const room = this.client.getRoom(roomId)
|
||||||
|
if (room && room.getJoinedMemberCount() === 1) {
|
||||||
|
try {
|
||||||
|
logger.log('info', "LEAVING EMPTY ROOM => " + roomId)
|
||||||
|
await this.client.leave(roomId)
|
||||||
|
} catch(err) {
|
||||||
|
logger.log('error', "ERROR LEAVING EMPTY ROOM => " + err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch(err) {
|
||||||
|
logger.log("error", `ERROR GETTING JOINED ROOMS: ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async sendTranscript(senderId, roomId) {
|
async sendTranscript(senderId, roomId) {
|
||||||
try {
|
try {
|
||||||
const transcriptFile = this.activeChatrooms[roomId]
|
const transcriptFile = this.activeChatrooms[roomId]
|
||||||
@ -278,22 +306,10 @@ class OcrccBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async leaveOldRooms() {
|
async trackJoinedRooms() {
|
||||||
const roomData = await this.client.getJoinedRooms()
|
const roomData = await this.client.getJoinedRooms()
|
||||||
const joinedRoomsIds = roomData["joined_rooms"]
|
this.joinedRooms = roomData["joined_rooms"]
|
||||||
this.joinedRooms = joinedRoomsIds
|
logger.log("info", "JOINED ROOMS => " + this.joinedRooms)
|
||||||
|
|
||||||
logger.log("info", `LEAVING ROOMS ${joinedRoomsIds}`)
|
|
||||||
|
|
||||||
joinedRoomsIds.forEach(async(roomId) => {
|
|
||||||
if (roomId === this.config.FACILITATOR_ROOM_ID) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.client.leave(roomId)
|
|
||||||
} catch(err) {
|
|
||||||
logger.log("error", `ERROR LEAVING ROOM => ${err}`)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async setMembershipListeners() {
|
async setMembershipListeners() {
|
||||||
@ -304,16 +320,17 @@ class OcrccBot {
|
|||||||
member.userId === this.config.BOT_USERID &&
|
member.userId === this.config.BOT_USERID &&
|
||||||
!this.joinedRooms.includes(member.roomId)
|
!this.joinedRooms.includes(member.roomId)
|
||||||
) {
|
) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.log("info", "Auto-joining room " + member.roomId);
|
|
||||||
const room = await this.client.joinRoom(member.roomId)
|
const room = await this.client.joinRoom(member.roomId)
|
||||||
|
logger.log("info", "AUTO JOINED ROOM" + room.roomId)
|
||||||
this.sendTextMessage(
|
this.sendTextMessage(
|
||||||
this.config.FACILITATOR_ROOM_ID,
|
this.config.FACILITATOR_ROOM_ID,
|
||||||
`A support seeker requested a chat (Room ID: ${member.roomId})`
|
`A support seeker requested a chat (Room ID: ${room.roomId})`
|
||||||
);
|
);
|
||||||
this.inviteFacilitators(member.roomId)
|
this.inviteFacilitators(room.roomId)
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
logger.log("error", err);
|
logger.log("error", "ERROR JOINING ROOM =>" + err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,19 +386,23 @@ class OcrccBot {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
member.membership === "leave" &&
|
member.membership === "leave" &&
|
||||||
member.userId !== this.config.BOT_USERID &&
|
member.userId !== this.config.BOT_USERID
|
||||||
this.activeChatrooms[member.roomId] &&
|
|
||||||
member.userId === this.activeChatrooms[member.roomId].facilitator
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
if (this.activeChatrooms[member.roomId] && member.userId === this.activeChatrooms[member.roomId].facilitator) {
|
||||||
this.sendTextMessage(
|
this.sendTextMessage(
|
||||||
member.roomId,
|
member.roomId,
|
||||||
`${member.name} has left the chat.`
|
`${member.name} has left the chat.`
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// leave if there is nobody in the room
|
||||||
const room = this.client.getRoom(member.roomId)
|
const room = this.client.getRoom(member.roomId)
|
||||||
|
if (!room) return
|
||||||
|
|
||||||
const memberCount = room.getJoinedMemberCount()
|
const memberCount = room.getJoinedMemberCount()
|
||||||
|
|
||||||
if (memberCount === 1) {
|
if (memberCount === 1) { // just the bot
|
||||||
logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
||||||
this.client.leave(event.roomId)
|
this.client.leave(event.roomId)
|
||||||
}
|
}
|
||||||
@ -407,6 +428,18 @@ class OcrccBot {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async leaveOldRooms() {
|
||||||
|
const roomData = await this.client.getJoinedRooms()
|
||||||
|
|
||||||
|
roomData["joined_rooms"].forEach(async roomId => {
|
||||||
|
try {
|
||||||
|
await this.client.leave(roomId)
|
||||||
|
} catch(err) {
|
||||||
|
logger.log('error', "ERROR LEAVING ROOM => " + err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
const localStorage = this.createLocalStorage();
|
const localStorage = this.createLocalStorage();
|
||||||
|
|
||||||
@ -429,10 +462,9 @@ class OcrccBot {
|
|||||||
|
|
||||||
this.client = matrix.createClient(opts);
|
this.client = matrix.createClient(opts);
|
||||||
await this.deleteOldDevices()
|
await this.deleteOldDevices()
|
||||||
await this.leaveOldRooms();
|
await this.trackJoinedRooms()
|
||||||
await this.client.initCrypto()
|
await this.client.initCrypto()
|
||||||
|
await this.setMembershipListeners();
|
||||||
this.setMembershipListeners();
|
|
||||||
this.setMessageListeners();
|
this.setMessageListeners();
|
||||||
this.client.startClient({ initialSyncLimit: 0 })
|
this.client.startClient({ initialSyncLimit: 0 })
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
|
Loading…
Reference in New Issue
Block a user