text-gen-bot/index.js

77 lines
2.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-15 19:48:20 -04:00
import fs from "fs";
2022-08-22 01:55:10 -04:00
import { PythonShell } from 'python-shell';
2022-09-06 16:06:06 -04:00
import { type } from 'os';
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-17 16:23:49 -04:00
const pyFile = "textgen.py";
2022-08-15 17:28:08 -04:00
2022-08-17 16:23:49 -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-23 13:27:37 -04:00
const messageCounters = new Map(); // room ID, message count
2022-08-23 13:22:26 -04:00
let trainingCounter = 0;
2022-08-15 19:48:20 -04:00
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;
2022-08-17 16:23:49 -04:00
2022-08-23 13:22:26 -04:00
++trainingCounter;
2022-08-23 13:50:21 -04:00
messageCounters.set(roomId, (messageCounters.get(roomId) ?? 0) + 1);
2022-08-17 16:23:49 -04:00
let userMessage = event["content"]["body"].split(" ");
2022-09-06 16:06:06 -04:00
console.log(`COUNTER:\t${messageCounters.get(roomId)}\t${roomId}\t${userMessage.join(" ")}`);
2022-08-23 13:50:21 -04:00
2022-08-17 16:23:49 -04:00
if (userMessage[0].startsWith(config.prefix)) {
userMessage[0] = userMessage[0].replace(config.prefix, '').toLowerCase();
} else {
2022-09-06 16:06:06 -04:00
fs.appendFile(config.file, userMessage.join(" ") + "\n", function (err) {
2022-08-17 16:23:49 -04:00
if (err) throw err;
});
};
2022-08-23 13:22:26 -04:00
// ? send message if:
// ? - enough messages have been sent
// ? - commanded
2022-08-23 13:40:18 -04:00
if (!(messageCounters.get(roomId) % config.frequency) || userMessage[0] === "speak") {
2022-08-16 00:47:38 -04:00
console.log("Generating message...");
2022-08-16 02:36:47 -04:00
2022-08-22 01:55:10 -04:00
userMessage.shift()
2022-09-06 16:06:06 -04:00
userMessage = userMessage.join(" ")
fs.appendFile(config.file, userMessage + "\n", function (err) {
if (err) throw err;
});
const options = { args: ['generate', userMessage] };
2022-08-22 01:55:10 -04:00
PythonShell.run(pyFile, options, (err, message) => {
if (err) throw err;
client.sendText(roomId, message.toString());
console.log("Message sent!");
}); // ? send generated message to room
2022-08-15 19:48:20 -04:00
};
2022-08-16 03:45:11 -04:00
2022-08-23 13:22:26 -04:00
// ? retrain if:
// ? - enough message have been sent
// ? - commanded
if (trainingCounter >= config.retrain || userMessage[0] === "train") {
2022-08-17 16:23:49 -04:00
console.log("Retraining the AI...");
2022-08-23 13:22:26 -04:00
client.sendText(roomId, "Retraining the AI...");
trainingCounter = 0;
const options = { args: ['train'] };
2022-08-16 03:45:11 -04:00
2022-08-23 13:22:26 -04:00
PythonShell.run(pyFile, options, (err, message) => {
if (err) throw err;
console.log(message.toString());
});
2022-08-17 16:23:49 -04:00
console.log("Training finished!");
2022-08-23 13:22:26 -04:00
client.sendText(roomId, "Training finished!");
2022-08-17 16:23:49 -04:00
};
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;
2022-08-17 16:23:49 -04:00
};
2022-08-22 01:55:10 -04:00