update bot ot use groups
This commit is contained in:
parent
e48b001486
commit
42866ecfc7
60
src/bot.js
60
src/bot.js
@ -18,7 +18,7 @@ const MAX_RETRIES = 3;
|
||||
class OcrccBot {
|
||||
constructor() {
|
||||
this.awaitingFacilitator = {};
|
||||
this.client = matrix.createClient(process.env.MATRIX_SERVER_URL);
|
||||
this.client = null;
|
||||
this.joinedRooms = [];
|
||||
this.activeChatrooms = {};
|
||||
}
|
||||
@ -150,7 +150,7 @@ class OcrccBot {
|
||||
})
|
||||
.catch(err => {
|
||||
this.handleBotCrash(roomId, err);
|
||||
logger.log("error", `ERROR GETTING ROOM MEMBERS: ${err}`);
|
||||
logger.log("error", `ERROR GETTING FACILITATORS: ${err}`);
|
||||
});
|
||||
}
|
||||
|
||||
@ -281,15 +281,14 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
deleteOldDevices() {
|
||||
const doDelete = (oldDevices, auth=null, retries=0) => {
|
||||
if (retries > MAX_RETRIES) {
|
||||
throw new Error("Exceeded max retries deleting old devices")
|
||||
}
|
||||
logger.log("info", `ATTEMPTING TO DELETE OLD DEVICES: ${oldDevices}`);
|
||||
this.client.deleteMultipleDevices(oldDevices, auth)
|
||||
.then(() => logger.log("info", "DELETED OLD DEVICES"))
|
||||
return this.client.getDevices().then(data => {
|
||||
const currentDeviceId = this.client.getDeviceId();
|
||||
const allDeviceIds = data.devices.map(d => d.device_id);
|
||||
const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
|
||||
logger.log("info", `DELETING OLD DEVICES: ${oldDevices}`);
|
||||
this.client
|
||||
.deleteMultipleDevices(oldDevices)
|
||||
.catch(err => {
|
||||
if (err['errcode'] === undefined && err['data']) {
|
||||
const auth = {
|
||||
session: err.data.session,
|
||||
type: "m.login.password",
|
||||
@ -297,35 +296,24 @@ class OcrccBot {
|
||||
identifier: { type: "m.id.user", user: process.env.BOT_USERID },
|
||||
password: process.env.BOT_PASSWORD
|
||||
};
|
||||
|
||||
doDelete(oldDevices, auth)
|
||||
} else if (err['errcode'] === 'M_LIMIT_EXCEEDED') {
|
||||
const retryCount = retries + 1
|
||||
const delay = err['retry_after_ms'] ? err['retry_after_ms'] : retryCount * 1000
|
||||
logger.log("error", `RATE LIMIT EXCEEDED, RETRYING IN ${delay} MS`);
|
||||
this.client
|
||||
.deleteMultipleDevices(oldDevices, auth)
|
||||
.then(() => logger.log("info", "DELETED OLD DEVICES"))
|
||||
.catch(err => {
|
||||
if (err.errcode === "M_LIMIT_EXCEEDED") {
|
||||
const delay = err.retry_after_ms || 2000
|
||||
logger.log("info", `RETRYING DELETE OLD DEVICES: ${oldDevices}`);
|
||||
setTimeout(() => {
|
||||
doDelete(oldDevices, auth, retryCount)
|
||||
this.client.deleteMultipleDevices(oldDevices)
|
||||
}, delay)
|
||||
} else {
|
||||
logger.log("error", `ERROR DELETING OLD DEVICES ON RETRY ${retries}: ${JSON.stringify(err)}`)
|
||||
doDelete(oldDevices, auth, retries + 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return this.client.getDevices()
|
||||
.then(data => {
|
||||
const currentDeviceId = this.client.getDeviceId();
|
||||
const allDeviceIds = data.devices.map(d => d.device_id);
|
||||
const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
|
||||
doDelete(oldDevices)
|
||||
})
|
||||
.catch(err => {
|
||||
this.handleBotCrash(undefined, err);
|
||||
logger.log(
|
||||
"error",
|
||||
`ERROR DELETING OLD DEVICES: ${JSON.stringify(err.data)}`
|
||||
)
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -430,8 +418,9 @@ class OcrccBot {
|
||||
|
||||
start() {
|
||||
const localStorage = this.createLocalStorage();
|
||||
const tmpClient = matrix.createClient(process.env.MATRIX_SERVER_URL);
|
||||
|
||||
this.client
|
||||
tmpClient
|
||||
.login("m.login.password", {
|
||||
user: process.env.BOT_USERNAME,
|
||||
password: process.env.BOT_PASSWORD,
|
||||
@ -440,6 +429,7 @@ class OcrccBot {
|
||||
.then(data => {
|
||||
const accessToken = data.access_token;
|
||||
const deviceId = data.device_id;
|
||||
logger.log('info', `LOGIN DATA ==> ${JSON.stringify(data)}`)
|
||||
|
||||
// create new client with full options
|
||||
|
||||
@ -456,7 +446,9 @@ class OcrccBot {
|
||||
.catch(err => {
|
||||
logger.log("error", `ERROR WITH LOGIN: ${err}`);
|
||||
})
|
||||
.then(() => this.deleteOldDevices())
|
||||
.then(() => {
|
||||
this.deleteOldDevices()
|
||||
})
|
||||
.then(() => this.client.initCrypto())
|
||||
.catch(err => logger.log("error", `ERROR STARTING CRYPTO: ${err}`))
|
||||
.then(() =>
|
||||
|
Loading…
Reference in New Issue
Block a user