text-gen-bot/index.js

44 lines
1.6 KiB
JavaScript
Raw Normal View History

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