From fa853dbd86aefec0266ca57361f0d734b0c1b54e Mon Sep 17 00:00:00 2001
From: array-in-a-matrix <nova.neutrino@protonmail.com>
Date: Mon, 22 Aug 2022 01:55:10 -0400
Subject: [PATCH] now using pythonshell lib

---
 README.md      |  7 +++----
 index.js       | 26 ++++++++++----------------
 package.json   |  3 ++-
 pnpm-lock.yaml |  7 +++++++
 4 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md
index 2ae3704..0e9870a 100644
--- a/README.md
+++ b/README.md
@@ -37,20 +37,19 @@ List of directories and files created by the project and its dependencies:
 
 The project is split into 2 parts `index.js` and `textgen.py`. The `index.js` file contains the code that interacts with the user on Matrix and sends text generated by the `textgen.py` file.
 
-Install [JavaSript SDK](https://github.com/turt2live/matrix-bot-sdk):
+Install [matrix-bot-sdk](https://github.com/turt2live/matrix-bot-sdk) and [python-shell](https://github.com/extrabacon/python-shell) (JS):
 
 ```sh
 > pnpm add matrix-bot-sdk
+> pnpm add python-shell
 ```
 
-Install [Python module](https://github.com/minimaxir/aitextgen):
+Install [aitextgen](https://github.com/minimaxir/aitextgen) (PY):
 
 ```sh
 > pip3 install aitextgen
 ```
 
-
-
 ### Configurations
 
 Before a bot can be used the fields in the `config.json` file must be populated with valid information. Values in angled brackets (stared below) must be supplied before usage.
diff --git a/index.js b/index.js
index 4662a62..02d4629 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,7 @@
 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";
+import { PythonShell } from 'python-shell';
 
 const storage = new SimpleFsStorageProvider("storage.json");
 const client = new MatrixClient(config.homeserver, config.token, storage);
@@ -32,31 +32,25 @@ client.on("room.message", (roomId, event) => {
     if ((!(messageCounter % config.frequency) && !(lineCount(config.file) < config.size)) || userMessage[0] === "speak") {
         console.log("Generating message...");
 
-                userMessage.shift()
-                const python = spawn('python', [pyFile, "generate", userMessage.join(' ')]);
-
-        python.stdout.on('data', (message) => {
-            message = message.toString();
-            client.sendText(roomId, message); // ? send generated message to room
-        });
-        console.log("Message sent!");
-        python.on('close'); // ? close python process when finished
+        userMessage.shift()
+        const options = { args: ['generate'] };
+        PythonShell.run(pyFile, options, (err, message) => {
+            if (err) throw err;
+            client.sendText(roomId, message.toString());
+            console.log("Message sent!");
+        }); // ? send generated message to room
     };
 
     if (trainCounter >= config.retrain || userMessage[0] === "train") {
         console.log("Retraining the AI...");
 
         trainCounter = 0;
-        const python = spawn('python', [pyFile, "train"]);
-
-        python.stdout.on('data', function (message) {
-            console.log(message.toString());
-        });
+        // TODO: exec training function
         console.log("Training finished!");
-        python.on('close'); // ? close python process when finished
     };
 });
 
 function lineCount(text) {
     return fs.readFileSync(text).toString().split("\n").length - 1;
 };
+
diff --git a/package.json b/package.json
index bda985f..90325fc 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,7 @@
 {
 	"dependencies": {
-		"matrix-bot-sdk": "^0.6.1"
+		"matrix-bot-sdk": "^0.6.1",
+		"python-shell": "^3.0.1"
 	},
 	"type": "module"
 }
\ No newline at end of file
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0afc41d..92ec4d4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2,9 +2,11 @@ lockfileVersion: 5.4
 
 specifiers:
   matrix-bot-sdk: ^0.6.1
+  python-shell: ^3.0.1
 
 dependencies:
   matrix-bot-sdk: 0.6.1
+  python-shell: 3.0.1
 
 packages:
 
@@ -843,6 +845,11 @@ packages:
     engines: {node: '>=6'}
     dev: false
 
+  /python-shell/3.0.1:
+    resolution: {integrity: sha512-TWeotuxe1auhXa5bGRScxnc2J+0r41NBntSa6RYZtMBLtAEsvCboKrEbW6DvASosWQepVkhZZlT3B5Ei766G+Q==}
+    engines: {node: '>=0.10'}
+    dev: false
+
   /qs/6.10.3:
     resolution: {integrity: sha512-wr7M2E0OFRfIfJZjKGieI8lBKb7fRCH4Fv5KNPEs7gJ8jadvotdsS08PzOKR7opXhZ/Xkjtt3WF9g38drmyRqQ==}
     engines: {node: '>=0.6'}
-- 
2.49.0