diff --git a/functions.js b/functions.js index 025c020..2d2650e 100644 --- a/functions.js +++ b/functions.js @@ -21,6 +21,14 @@ const Discord = require('discord.js'); // Fuzzy text matching for db lookups const FuzzySearch = require('fuzzy-search'); +// OpenAI +const { Configuration, OpenAIApi } = require("openai"); + +const configuration = new Configuration({ + apiKey: process.env.OPENAI_API_KEY, +}); +const openai = new OpenAIApi(configuration); + // Various imports from other files const config = require('./config.json'); const strings = require('./strings.json'); @@ -530,6 +538,22 @@ const functions = { } } }, + openAI: { + chatPrompt(userPrompt) { + return new Promise(async (resolve, reject) => { + const response = await openai.createCompletion({ + model: 'text-davinci-003', + prompt: userPrompt, + temperature: 0, + max_tokens: 250 + }).catch(e => { + reject(e); + return null; + }); + resolve(response); + }); + } + }, // Parent-Level functions (miscellaneuous) closeRequest(requestId, interaction) { if (interaction.user.id == ownerId) { diff --git a/package.json b/package.json index 7c84570..05f8e83 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nodbot", - "version": "3.1.0", - "description": "Nods and Nod Accessories.", + "version": "3.2.0", + "description": "Nods and Nod Accessories, now with ChatGPT!", "main": "main.js", "dependencies": { "@discordjs/builders": "^0.16.0", @@ -12,10 +12,11 @@ "dotenv": "^10.0.0", "fuzzy-search": "^3.2.1", "mysql": "^2.18.1", + "openai": "^3.2.1", "tenorjs": "^1.0.10" }, "engines": { - "node": "16.x" + "node": "18.x" }, "devDependencies": { "eslint": "^7.32.0" diff --git a/slash-commands/chat.js b/slash-commands/chat.js new file mode 100644 index 0000000..848efe8 --- /dev/null +++ b/slash-commands/chat.js @@ -0,0 +1,20 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const fn = require('../functions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('chat') + .setDescription('Send a message to ChatGPT') + .addStringOption(o => + o.setName("prompt") + .setDescription("Prompt to send to ChatGPT") + .setRequired(true) + ), + async execute(interaction) { + await interaction.deferReply(); + const userPrompt = interaction.options.getString("prompt"); + const response = await fn.openAI.chatPrompt(userPrompt).catch(e => console.error(e)); + const responseText = response.data.choices[0].text; + await interaction.editReply(`${responseText}`); + }, +}; \ No newline at end of file