From e59219db71506a2c0cb7e4cd0994e218573c4697 Mon Sep 17 00:00:00 2001 From: cercopith <61226260+XSPGMike@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:48:32 +0200 Subject: [PATCH 1/4] cmds --- dist/index.js | 25 +++++++++++++++++++++++++ src/index.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/dist/index.js b/dist/index.js index 861c527..9f4fd0a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -319,6 +319,25 @@ class ChatBot { return e; } } + async getHistory(bot) { + try { + await new Promise((resolve) => setTimeout(resolve, 2000)); + let response = await this.makeRequest({ + query: `${queries.chatPaginationQuery}`, + variables: { + before: null, + bot: bot, + last: 25, + }, + }); + for (const { node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) { + console.log(`${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n`); + } + } + catch (e) { + console.log(e); + } + } async getResponse(bot) { let text; let state; @@ -425,6 +444,7 @@ class ChatBot { let helpMsg = "Available commands: !help !exit, !clear, !submit" + "\n!help - show this message" + "\n!exit - exit the chat" + + "\n!history- get the last 25 messages" + "\n!clear - clear chat history" + "\n!submit - submit prompt"; // await this.clearContext(this.chatId); @@ -485,6 +505,11 @@ class ChatBot { } submitedPrompt = ""; } + else if (prompt === "!history") { + spinner.start("Loading history..."); + await this.getHistory(this.bot); + spinner.stop(); + } else { submitedPrompt += prompt + "\n"; } diff --git a/src/index.ts b/src/index.ts index e6e0137..db6ea03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -350,6 +350,28 @@ class ChatBot { } } + public async getHistory(bot: string): Promise { + try { + await new Promise((resolve) => setTimeout(resolve, 2000)); + let response = await this.makeRequest({ + query: `${queries.chatPaginationQuery}`, + variables: { + before: null, + bot: bot, + last: 25, + }, + }); + for(const {node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) { + console.log( + `${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, + `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n` + ) + } + } catch(e) { + console.log(e) + } + } + public async getResponse(bot: string): Promise { let text: string let state: string @@ -462,6 +484,7 @@ class ChatBot { let helpMsg = "Available commands: !help !exit, !clear, !submit" + "\n!help - show this message" + "\n!exit - exit the chat" + + "\n!history- get the last 25 messages" + "\n!clear - clear chat history" + "\n!submit - submit prompt"; @@ -519,6 +542,10 @@ class ChatBot { console.log(response.data); } submitedPrompt = ""; + } else if(prompt === "!history") { + spinner.start("Loading history...") + await this.getHistory(this.bot) + spinner.stop() } else { submitedPrompt += prompt + "\n"; } From 231a29ad3bd03ac1a6848363d6f3e328af6d15d1 Mon Sep 17 00:00:00 2001 From: cercopith <61226260+XSPGMike@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:59:59 +0200 Subject: [PATCH 2/4] indenting --- dist/index.js | 4 ++-- package.json | 2 +- src/index.ts | 45 +++++++++++++++++++++++---------------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9f4fd0a..047d709 100644 --- a/dist/index.js +++ b/dist/index.js @@ -335,7 +335,7 @@ class ChatBot { } } catch (e) { - console.log(e); + console.log("There has been an error while fetching your history!"); } } async getResponse(bot) { @@ -444,7 +444,7 @@ class ChatBot { let helpMsg = "Available commands: !help !exit, !clear, !submit" + "\n!help - show this message" + "\n!exit - exit the chat" + - "\n!history- get the last 25 messages" + + "\n!history - get the last 25 messages" + "\n!clear - clear chat history" + "\n!submit - submit prompt"; // await this.clearContext(this.chatId); diff --git a/package.json b/package.json index 0fd4cab..16c69d9 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "module", "scripts": { "compile": "tsc", - "dev": "npm run compile && node ./dist/index.js", + "dev": "npm run compile && node ./dist/cli.js", "cli": "node ./dist/cli.js" }, "keywords": [ diff --git a/src/index.ts b/src/index.ts index db6ea03..f35ea7e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -351,25 +351,26 @@ class ChatBot { } public async getHistory(bot: string): Promise { - try { - await new Promise((resolve) => setTimeout(resolve, 2000)); - let response = await this.makeRequest({ - query: `${queries.chatPaginationQuery}`, - variables: { - before: null, - bot: bot, - last: 25, - }, - }); - for(const {node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) { - console.log( - `${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, - `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n` - ) + try { + await new Promise((resolve) => setTimeout(resolve, 2000)); + let response = await this.makeRequest({ + query: `${queries.chatPaginationQuery}`, + variables: { + before: null, + bot: bot, + last: 25, + }, + }); + + for(const {node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) { + console.log( + `${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, + `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n` + ) + } + } catch(e) { + console.log("There has been an error while fetching your history!") } - } catch(e) { - console.log(e) - } } public async getResponse(bot: string): Promise { @@ -484,7 +485,7 @@ class ChatBot { let helpMsg = "Available commands: !help !exit, !clear, !submit" + "\n!help - show this message" + "\n!exit - exit the chat" + - "\n!history- get the last 25 messages" + + "\n!history - get the last 25 messages" + "\n!clear - clear chat history" + "\n!submit - submit prompt"; @@ -543,9 +544,9 @@ class ChatBot { } submitedPrompt = ""; } else if(prompt === "!history") { - spinner.start("Loading history...") - await this.getHistory(this.bot) - spinner.stop() + spinner.start("Loading history...") + await this.getHistory(this.bot) + spinner.stop() } else { submitedPrompt += prompt + "\n"; } From 54ee7721384663b93e620133d454f6e9a8e0ffdf Mon Sep 17 00:00:00 2001 From: cercopith <61226260+XSPGMike@users.noreply.github.com> Date: Fri, 31 Mar 2023 18:14:44 +0200 Subject: [PATCH 3/4] no need for 2 sec wait --- src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index f35ea7e..3210399 100644 --- a/src/index.ts +++ b/src/index.ts @@ -352,7 +352,6 @@ class ChatBot { public async getHistory(bot: string): Promise { try { - await new Promise((resolve) => setTimeout(resolve, 2000)); let response = await this.makeRequest({ query: `${queries.chatPaginationQuery}`, variables: { From 9605d1c978ce3083aaf4c8a7105d4227e1fea28b Mon Sep 17 00:00:00 2001 From: cercopith <61226260+XSPGMike@users.noreply.github.com> Date: Fri, 31 Mar 2023 18:15:49 +0200 Subject: [PATCH 4/4] compile --- dist/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 047d709..364e74c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -321,7 +321,6 @@ class ChatBot { } async getHistory(bot) { try { - await new Promise((resolve) => setTimeout(resolve, 2000)); let response = await this.makeRequest({ query: `${queries.chatPaginationQuery}`, variables: {