vanish/index.js

47 lines
1.8 KiB
JavaScript

#!/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...`));
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.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)
}
})