vanish/index.js

47 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

#!/usr/bin/env node
process.env.UV_THREADPOOL_SIZE = 128;
2023-01-09 13:23:14 -05:00
import config from './config.json' assert {type: "json"};
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
import * as file from "fs";
2023-01-09 13:23:14 -05:00
const storage = new SimpleFsStorageProvider("storage.json");
const client = new MatrixClient(config.homeserver, config.token, storage);
const logs = "./deleted.log";
2023-01-09 13:23:14 -05:00
AutojoinRoomsMixin.setupOnClient(client);
2023-01-09 15:58:19 -05:00
client.start().then(() => console.log(`Vanishing messages...`));
2023-01-09 13:23:14 -05:00
2023-01-09 15:58:19 -05:00
let timeInMS = config.time * 3_600_000 //? converts hours to milliseconds
2023-01-09 13:23:14 -05:00
2023-01-09 15:58:19 -05:00
client.on("room.message", (roomId, event) => {
let table = [
["Room ID", roomId],
["Message", event["content"]["body"]]
]
2023-01-09 15:58:19 -05:00
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
2023-01-09 15:58:19 -05:00
setTimeout(redactMessage, timeInMS)
2023-01-09 13:23:14 -05:00
}
})