From f4ace1f2ac1ea1637cff3c05dfe2aa1420296f24 Mon Sep 17 00:00:00 2001 From: aa <aaaa> Date: Tue, 14 Nov 2023 12:46:53 +0100 Subject: [PATCH] WIP --- .gitignore | 3 +- commands/CommandInfoCovid.js | 2 +- commands/CommandParser.js | 7 +- commands/DatabaseCommand.js | 4 +- commands/InformeMoi.js | 14 ++-- commands/OpenAI.js | 127 +++++++++++++++++++++++++++++++++++ index.js | 7 +- package-lock.json | 51 ++++++++++++-- package.json | 3 +- 9 files changed, 194 insertions(+), 24 deletions(-) create mode 100644 commands/OpenAI.js diff --git a/.gitignore b/.gitignore index 44e33d9..b966bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.json +channels_conf.json /node_modules -/database/complot_db.json \ No newline at end of file +/database/complot_db.json diff --git a/commands/CommandInfoCovid.js b/commands/CommandInfoCovid.js index cca61ef..6cbb488 100644 --- a/commands/CommandInfoCovid.js +++ b/commands/CommandInfoCovid.js @@ -96,7 +96,7 @@ class CommandInfoCovid{ } shouldRespond(message){ - return this.infoCovidRegExp.test(message.content); + return this.infoCovidRegExp.test(message.content) && !message.content.includes('?'); } } diff --git a/commands/CommandParser.js b/commands/CommandParser.js index 3bf83fc..121c881 100644 --- a/commands/CommandParser.js +++ b/commands/CommandParser.js @@ -4,6 +4,7 @@ const CommandVoteParser = require('./CommandVoteParser.js'); const CommandQui = require('./CommandQui.js'); const CommandPourquoi = require('./CommandPourquoi.js'); const CommandInfoCovid = require('./CommandInfoCovid.js'); +const OpenAI = require('./OpenAI.js'); class CommandParser { @@ -16,8 +17,10 @@ class CommandParser { this.commandQui = new CommandQui(client); this.commandPourquoi = new CommandPourquoi(client); this.commandInfoCovid = new CommandInfoCovid(client); + this.openAI = new OpenAI(client); } + parse(message) { if (message.author.id !== this.client.application.id) { this.informeMoi.parse(message); @@ -27,7 +30,9 @@ class CommandParser { this.commandPourquoi.parse(message); this.commandInfoCovid.parse(message); } + //this.openAI.parse(message); + } } -module.exports = CommandParser; \ No newline at end of file +module.exports = CommandParser; diff --git a/commands/DatabaseCommand.js b/commands/DatabaseCommand.js index 700c58c..8369a0f 100644 --- a/commands/DatabaseCommand.js +++ b/commands/DatabaseCommand.js @@ -4,7 +4,7 @@ const complotDB = require('../database/ComplotDB'); class DatabaseCommand{ constructor(client){ this.client = client; - this.channel = '892676980699455500'; + this.channel = '1163914178042527844'; this.commandList = '!complot list'; this.commandAddAction = '!complot add action '; this.commandAddReason = '!complot add reason '; @@ -68,4 +68,4 @@ class DatabaseCommand{ } } -module.exports = DatabaseCommand; \ No newline at end of file +module.exports = DatabaseCommand; diff --git a/commands/InformeMoi.js b/commands/InformeMoi.js index 33e6893..e031ead 100644 --- a/commands/InformeMoi.js +++ b/commands/InformeMoi.js @@ -27,11 +27,11 @@ class InformeMoi{ } getRandomSentenceFilled(sentence){ - sentence = this.fillAllRandomObject(sentence, this.actorRE, complotDB.getRandomActor.bind(complotDB)); sentence = this.fillAllRandomObject(sentence, this.actionRE, complotDB.getRandomAction.bind(complotDB)); sentence = this.fillAllRandomObject(sentence, this.reasonRE, complotDB.getRandomReason.bind(complotDB)); sentence = this.fillAllRandomObject(sentence, this.sourceRE, complotDB.getRandomSource.bind(complotDB)); - + sentence = this.fillAllRandomObject(sentence, this.actorRE, complotDB.getRandomActor.bind(complotDB)); + sentence = sentence.replaceAll(this.wantRE, 'veut'); /**Todo, change */ return sentence; } @@ -49,15 +49,15 @@ class InformeMoi{ this.client.channels.cache.get(channelsID.welcome).send(message); } + + parse(message){ - if(this.shouldRespond(message)){ + let lowercase = message.content.toLowerCase(); + if(!lowercase.includes("informe moi sur ") && lowercase.includes("informe moi")){ message.reply(this.getRandomComplot()); } } - shouldRespond(message){ - return message.content.toLowerCase().includes("informe moi"); - } } -module.exports = InformeMoi; \ No newline at end of file +module.exports = InformeMoi; diff --git a/commands/OpenAI.js b/commands/OpenAI.js new file mode 100644 index 0000000..e27d6b1 --- /dev/null +++ b/commands/OpenAI.js @@ -0,0 +1,127 @@ + +const {OPENAI_API_KEY} = require('../config.json'); +const { Configuration, OpenAIApi } = require("openai"); +const configuration = new Configuration({ + apiKey: OPENAI_API_KEY, +}); + +const COMPLOBOT_LINE_PREFIX = "Complobot (façon complotiste):"; +/* +class Message{ + constructor(message, listMessage){ + this.content = message.content; + this.user = message.author.username; + this.id = message.id; + this.parent = null; + if(message.reference && message.reference.messageId){ + for(let i = 0; i < listMessage.length ; i++){ + let m = listMessage[i]; + if(m.id === message.reference.messageId){ + this.parent = m; + break; + } + } + } + + } + + toString(){ + return `${this.user}: ${this.content}\n`; + } + + dialogue(){ + let item = this; + let dialog = ""; + while(item !== null){ + let itemStr = item.toString(); + let newStr = itemStr + dialog; + + if(newStr > 950){ + item = null; + break; + }else{ + dialog = newStr; + item = item.parent; + } + } + + dialog = `Ecrit le dialogue de Complobot.\n${dialog}${COMPLOBOT_LINE_PREFIX}`; + + return dialog; + } + +} + +*/ +class OpenAI{ + constructor(client) { + this.client = client; + this.openai = new OpenAIApi(configuration); + this.questionRE = new RegExp('^[^?]+\\?$'); + // this.messages = []; + + + } + + async getRandomTheorieOpenAI(subject){ + const completion = await this.openai.createCompletion("text-davinci-002", { + prompt: `Ecrit une théorie du complot sur ${subject}.`, + temperature: 0.7, + max_tokens: 500 + }); + return completion.data.choices[0].text ; + } + + async getRandomAnswerOpenAI(question){ + const completion = await this.openai.createCompletion("text-davinci-002", { + prompt: `Répond de manière complotiste à la question "${question}"`, + temperature: 0.7, + max_tokens: 500 + }); + return completion.data.choices[0].text ; + } +/* + async getRandomDialogOpenAI(message){ + const promptInput = message.dialogue(); + const completion = await this.openai.createCompletion("text-davinci-002", { + prompt:promptInput , + temperature: 0.7, + max_tokens: 500 + }); + const reponse = completion.data.choices[0].text; + return reponse; + } +*/ + parse(discordMessage){ + /*const message = new Message(discordMessage, this.messages); + + this.messages.push(message); + + if(this.messages.length > 200){ + this.messages.shift(); + } +*/ + if(discordMessage.author.id === this.client.application.id){ + return; + } + + if(discordMessage.content.toLowerCase().startsWith('informe moi sur ')){ + this.getRandomTheorieOpenAI(discordMessage.content).then(reponse=>{ + discordMessage.reply(reponse); + }) + }else if(this.questionRE.test(discordMessage.content)){ + /*if(message.parent !== null){ + this.getRandomDialogOpenAI(message).then(reponse=>{ + discordMessage.reply(reponse); + }); + }else{*/ + this.getRandomAnswerOpenAI(discordMessage.content).then((reponse)=>{ + discordMessage.reply(reponse); + }); + // } + } + + } +} + +module.exports = OpenAI; diff --git a/index.js b/index.js index 0cab476..2c7eeeb 100644 --- a/index.js +++ b/index.js @@ -21,10 +21,9 @@ client.once('ready', () => { client.on("messageCreate", (message) => { - if (!parser) return; - if (message.author.id !== clientId) { - parser.parse(message); - } +// console.log("MESSAGE", message.partial, message.content); + if (!parser) return; + parser.parse(message); }); client.on('messageReactionAdd', async (reaction) => { diff --git a/package-lock.json b/package-lock.json index ec13d87..11cbe19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "discord-api-types": "^0.23.1", "discord.js": "^13.1.0", "dotenv": "^10.0.0", - "node": "^16.10.0" + "node": "^16.10.0", + "openai": "^2.0.5" } }, "node_modules/@discordjs/builders": { @@ -271,9 +272,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.14.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", - "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==", + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", "funding": [ { "type": "individual", @@ -365,6 +366,23 @@ "node": "4.x || >=6.0.0" } }, + "node_modules/openai": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/openai/-/openai-2.0.5.tgz", + "integrity": "sha512-M/drXV2QYdRhmB5EhAINpeA4oGzj0M6mSVfi8o8Rex3Re+yEnSBMUExtGZSlbVRAKpIvTLbq0oC7bgHBaSnjLw==", + "dependencies": { + "axios": "^0.26.0", + "form-data": "^4.0.0" + } + }, + "node_modules/openai/node_modules/axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "dependencies": { + "follow-redirects": "^1.14.8" + } + }, "node_modules/ow": { "version": "0.27.0", "resolved": "https://registry.npmjs.org/ow/-/ow-0.27.0.tgz", @@ -640,9 +658,9 @@ "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" }, "follow-redirects": { - "version": "1.14.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", - "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==" + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" }, "form-data": { "version": "4.0.0", @@ -698,6 +716,25 @@ "whatwg-url": "^5.0.0" } }, + "openai": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/openai/-/openai-2.0.5.tgz", + "integrity": "sha512-M/drXV2QYdRhmB5EhAINpeA4oGzj0M6mSVfi8o8Rex3Re+yEnSBMUExtGZSlbVRAKpIvTLbq0oC7bgHBaSnjLw==", + "requires": { + "axios": "^0.26.0", + "form-data": "^4.0.0" + }, + "dependencies": { + "axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "requires": { + "follow-redirects": "^1.14.8" + } + } + } + }, "ow": { "version": "0.27.0", "resolved": "https://registry.npmjs.org/ow/-/ow-0.27.0.tgz", diff --git a/package.json b/package.json index 11902c5..7039162 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "discord-api-types": "^0.23.1", "discord.js": "^13.1.0", "dotenv": "^10.0.0", - "node": "^16.10.0" + "node": "^16.10.0", + "openai": "^2.0.5" } } -- GitLab