generated from array-in-a-matrix/matrix-bot-template
write deleted messages to log file + increase threadpool
This commit is contained in:
parent
3c2170d0f5
commit
fa4bfb1720
33
index.js
33
index.js
|
@ -1,8 +1,13 @@
|
|||
#!/usr/bin/env node
|
||||
process.env.UV_THREADPOOL_SIZE = 128;
|
||||
|
||||
import config from './config.json' assert {type: "json"};
|
||||
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
|
||||
import * as file from "fs";
|
||||
|
||||
const storage = new SimpleFsStorageProvider("storage.json");
|
||||
const client = new MatrixClient(config.homeserver, config.token, storage);
|
||||
const logs = "./deleted.log";
|
||||
|
||||
AutojoinRoomsMixin.setupOnClient(client);
|
||||
client.start().then(() => console.log(`Vanishing messages...`));
|
||||
|
@ -10,10 +15,32 @@ client.start().then(() => console.log(`Vanishing messages...`));
|
|||
let timeInMS = config.time * 3_600_000 //? converts hours to milliseconds
|
||||
|
||||
client.on("room.message", (roomId, event) => {
|
||||
let table = [
|
||||
["Room ID", roomId],
|
||||
["Message", event["content"]["body"]]
|
||||
]
|
||||
if (event["sender"] === config.user) {
|
||||
if (event["content"]["body"].startsWith(config.key)) return; //? if message starts with the key do NOT delete that message
|
||||
console.table([roomId, event["content"]["body"]])
|
||||
function redactMessage(){client.redactEvent(roomId, event["event_id"], "Vanish")} //? define callback function
|
||||
console.warn(`The message "${table[1][1]}" in room ${table[0][1]} will be deleted.`)
|
||||
function redactMessage() {
|
||||
console.table(table)
|
||||
try {
|
||||
file.accessSync(logs, file.constants.R_OK | file.constants.W_OK)
|
||||
file.appendFile(logs, `Room: ${table[0][1]} \nMessage: "${table[1][1]}"\n\n`, (err) => {
|
||||
if (err != null) {
|
||||
console.error(err)
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
file.writeFile(logs, `Room: ${table[0][1]} \nMessage: "${table[1][1]}"\n\n`, (err) => {
|
||||
if (err != null) {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
client.redactEvent(roomId, event["event_id"], "Vanish")
|
||||
} //? define callback function
|
||||
setTimeout(redactMessage, timeInMS)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue