All files mineflayer-chatgpt.js

100% Statements 45/45
100% Branches 11/11
100% Functions 3/3
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 461x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 7x 7x 7x 7x 7x 9x 9x 9x 5x 5x 3x 3x 3x 5x 1x 1x 3x 5x 2x 2x 2x 9x 9x 1x 1x 1x 1x 1x 1x  
"use strict";
import Client from "./client.js";
import Memory from "./memory.js";
import moderator from "./moderator.js";
 
let memory;
 
function chatgpt(bot) {
 
  let client;
  let enableModeration;
  let enableLogging;
 
  bot.chatgpt = {};
 
  bot.chatgpt.setConfig = (apiKey, opts) => {
    opts = opts || {};
    enableModeration = opts.enableModeration || true;
    enableLogging = opts.enableLogging || false;
    client = new Client(apiKey, opts);
    memory = new Memory(opts.historySize);
  };
 
  bot.chatgpt.sendMessage = async (player, message) => {
    try {
      let reply = await client.chat(memory, player, message);
      if (enableModeration === true) {
        reply = moderator.sanitiseProfanity(reply);
      }
      if (enableLogging === true) {
        console.log(`Player ${player} received a reply from ChatGPT: ${reply}`);
      }
      return reply;
    } catch (error) {
      console.error(`An unexpected error has occurred: ${error.message}`);
      throw error;
    }
  };
}
 
const exports = {
  chatgpt: chatgpt,
};
 
export { exports as default };