From 6f0040e9a5b0a3c0b18a5d4feb6991e407158748 Mon Sep 17 00:00:00 2001 From: cameron Date: Wed, 8 Mar 2023 00:30:50 -0500 Subject: [PATCH] Added working 4chan-gpt integration --- bot-modules/4chat/module.js | 71 +++++++++++++++++++++++++++++++++++++ bot-modules/pepe/module.js | 6 ++-- main.js | 15 ++++++-- package.json | 16 ++++----- 4 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 bot-modules/4chat/module.js diff --git a/bot-modules/4chat/module.js b/bot-modules/4chat/module.js new file mode 100644 index 0000000..bc6bcb6 --- /dev/null +++ b/bot-modules/4chat/module.js @@ -0,0 +1,71 @@ +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; diff --git a/bot-modules/pepe/module.js b/bot-modules/pepe/module.js index 1797b96..0d91048 100644 --- a/bot-modules/pepe/module.js +++ b/bot-modules/pepe/module.js @@ -17,18 +17,16 @@ class Pepe { //returns a dank pepe pepe() { - let dankPepe = new Discord.RichEmbed(); - let selection = Math.floor(Math.random() * (this.pepesList.length)); + let dankPepe = new Discord.EmbedBuilder().setImage(this.pepesList[selection]); - dankPepe.setImage(this.pepesList[selection]); return dankPepe; } async action(msg) { let chan = msg.channel; let pepeImg = this.pepe(); - chan.send(pepeImg); + chan.send({ embeds: [pepeImg] }); } } diff --git a/main.js b/main.js index 3f0caa1..ced1134 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,11 @@ const Discord = require("discord.js"); const Gamedig = require('gamedig'); -const client = new Discord.Client({ partials: ['REACTION', 'CHANNEL', 'MESSAGE'] }); +const client = new Discord.Client({ partials: [Discord.Partials.Message, Discord.Partials.Channel, Discord.Partials.Reaction], intents: [ + Discord.GatewayIntentBits.Guilds, + Discord.GatewayIntentBits.GuildMessages, + Discord.GatewayIntentBits.MessageContent, + Discord.GatewayIntentBits.GuildMembers, + ] }); const fs = require('fs'); const request = require('request'); const rp = require('request-promise-native'); @@ -34,11 +39,13 @@ async function main() { } + console.log(triggers); + client.login('MzYyNzU1MzE3MTczNTgzODcy.DK3R1A.8M22ZkNaaoHy-ifBQwVivSU7svY'); client.on('ready', () => { console.log(`Logged into Discord as ${client.user.tag}`); }); - client.on('message', async (msg) => { + client.on('messageCreate', async (msg) => { for(const t in triggers) { if (msg.content.startsWith(`!${t}`)) { console.log(`+ Bot module ${triggers[t]} triggered.`); @@ -46,6 +53,10 @@ async function main() { break; } } + if (msg.mentions.has(client.user)) { + console.log(`Bot mentioned.`); + await bot_modules[triggers['botmention']].action(msg); + } }); client.on('messageReactionAdd', async (reaction, user) => { if (reaction.partial) { diff --git a/package.json b/package.json index 293f4c5..c89caf2 100644 --- a/package.json +++ b/package.json @@ -4,16 +4,16 @@ "description": "", "main": "main.js", "dependencies": { - "discord.js": "^12.5.1", - "gamedig": "^1.0.37", - "glob": "^7.1.2", + "discord.js": "^14.7.1", + "gamedig": "^1.0.49", + "glob": "^7.2.3", "markov": "0.0.7", - "nodemon": "^1.12.1", + "nodemon": "^1.19.4", "npm-init": "^1.3.1", - "request": "^2.83.0", - "request-promise-native": "^1.0.5", - "sqlite3": "^5.0.2", - "striptags": "^3.1.0", + "request": "^2.88.2", + "request-promise-native": "^1.0.9", + "sqlite3": "^5.1.4", + "striptags": "^3.2.0", "twitter": "^1.7.1" }, "devDependencies": {},