diff --git a/.gitignore b/.gitignore index 44e33d96542450d3531f99bb3a0602d9b4f9e0e8..b966bc7cba7ef613d111e32b12ae8a985c62ba35 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 cca61ef6d7f23248f5520d535874011f31ff4554..6cbb4889b42c1a9cbcfbb0f6d146ffe1b75875a5 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 3bf83fc4b68fda00b49fe04138d75ca2335d8ef9..121c881ebee6813d51203263549041475714c30c 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 700c58cc82ae4e235f4bc2a38ca340d2b959229c..8369a0f2caf980d734840bce197b85aca6b2b203 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 33e689337f268c80a6a07a7a626f58ab97aa1286..e031ead88d62b1932ffe629e3017e84bc0678ce2 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 0000000000000000000000000000000000000000..e27d6b19e780fa5cae33bd7e5a6c421cceb4e777 --- /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 0cab47698eb3d5910fdbd759985161e4d29338b9..2c7eeeb586a7278a119e6dc4e6e6b763891eb32e 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 ec13d87fad4c7e69a9b13d4f6f6682347b389e0a..11cbe19807a627a412b87be5d0620c44eeae12f4 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 11902c566db9a47e07b380a7730cc151d02a30b7..7039162b864931962a81b1709e2e769adc2374a3 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" } }