43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
import config from './config.json' assert {type: "json"};
|
|
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
|
|
import { spawn } from 'node:child_process';
|
|
import fs from "fs";
|
|
|
|
const storage = new SimpleFsStorageProvider("storage.json");
|
|
const client = new MatrixClient(config.homeserver, config.token, storage);
|
|
|
|
AutojoinRoomsMixin.setupOnClient(client)
|
|
client.start().then(() => console.log(`Client has started!\n`));
|
|
|
|
let messageCounter = 0;
|
|
|
|
// ? event listener: logs messages sent into file
|
|
client.on("room.message", (roomId, event) => {
|
|
if (!event["content"] || event["sender"] === config.user) return;
|
|
++messageCounter;
|
|
fs.appendFile('training-matrix.txt', event["content"]["body"] + "\n", function (err) {
|
|
if (err) throw err;
|
|
// console.log(messageCounter + "\t" + event["content"]["body"]);
|
|
});
|
|
|
|
if (lineCount(config.file) < config.size) return; // ? don't start generating messages until a big enough dataset is present
|
|
// ? send message every N messages using the training data
|
|
if (!(messageCounter % config.frequency)) {
|
|
console.log("Generating message...");
|
|
|
|
const python = spawn('python', ["textgen.py", "generate"]);
|
|
|
|
python.stdout.on('data', function (message) {
|
|
message = message.toString();
|
|
client.sendText(roomId, message);
|
|
});
|
|
python.on('close'); // ? close python process when finished
|
|
};
|
|
|
|
// TODO: train AI every Nth message?
|
|
|
|
});
|
|
|
|
function lineCount(text) {
|
|
return fs.readFileSync(text).toString().split("\n").length - 1;
|
|
}
|