dank-bot-js/bot-modules/4chat/module.js

72 lines
1.4 KiB
JavaScript

const Discord = require("discord.js");
const fetch = require("node-fetch");
const fs = require('fs');
const server = "192.168.1.70";
class Fourchat {
async prompt(input_text) {
const response = await fetch(`http://${server}:7860/run/textgen`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
data: [
`-----
--- 865467536
${input_text}
--- 865467537
`,
200,
true,
1.99,
0.18,
1,
1.15,
30,
0,
0,
1,
0,
1,
false,
]
})
});
const response_json = await response.json();
return response_json;
}
find_response(res) {
const startIndex = res.indexOf("865467537");
const output_only = res.slice(startIndex+9);
const responseIndex = output_only.indexOf(">>865467536");
const s = output_only.slice(responseIndex+12).split("---")[0];
return s.trim();
}
constructor() {
this.matches = [ "4chat", "botmention" ];
}
async action(msg) {
let chan = msg.channel;
try {
const input_text = msg.content.replace("!4chat","").replace(/<@.+?>/,"anon").trim();
console.log(`Input Text: ${input_text}`);
const output_json = await this.prompt(input_text);
const output_text = (output_json.data[0]);
const trimmed_text = this.find_response(output_text);
chan.send(trimmed_text);
} catch (e) {
chan.send(`Error: ${e}`);
}
}
}
module.exports = Fourchat;