From ac9a59a055bfea787406af1bb4684880d91d6544 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sat, 30 Dec 2023 11:11:57 -0500 Subject: [PATCH] Not quite MVP, but function files are here + boots --- .vscode/launch.json | 2 +- example.env => Example.env | 0 Dockerfile => src/Dockerfile | 0 {data => src/data}/config.json | 0 {data => src/data}/strings.json | 0 main.js => src/main.js | 0 {modules => src/modules}/_cleanInput.js | 0 .../modules}/_deploySlashCmdsGlobal.js | 0 .../modules}/_deploySlashCmdsLocal.js | 0 {modules => src/modules}/_eraseSlashCmds.js | 0 {modules => src/modules}/functions.js | 2 +- {modules => src/modules}/input.txt | 0 package-lock.json => src/package-lock.json | 0 package.json => src/package.json | 0 src/slash-commands/chat.js | 29 ++++++++++++++ src/slash-commands/dalle.js | 40 +++++++++++++++++++ src/slash-commands/ping.js | 11 +++++ .../slash-commands}/template | 0 18 files changed, 82 insertions(+), 2 deletions(-) rename example.env => Example.env (100%) rename Dockerfile => src/Dockerfile (100%) rename {data => src/data}/config.json (100%) rename {data => src/data}/strings.json (100%) rename main.js => src/main.js (100%) rename {modules => src/modules}/_cleanInput.js (100%) rename {modules => src/modules}/_deploySlashCmdsGlobal.js (100%) rename {modules => src/modules}/_deploySlashCmdsLocal.js (100%) rename {modules => src/modules}/_eraseSlashCmds.js (100%) rename {modules => src/modules}/functions.js (95%) rename {modules => src/modules}/input.txt (100%) rename package-lock.json => src/package-lock.json (100%) rename package.json => src/package.json (100%) create mode 100644 src/slash-commands/chat.js create mode 100644 src/slash-commands/dalle.js create mode 100644 src/slash-commands/ping.js rename {slash-commands => src/slash-commands}/template (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json index c52eeff..c9e16ec 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,7 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/main.js" + "program": "${workspaceFolder}/src/main.js" } ] } \ No newline at end of file diff --git a/example.env b/Example.env similarity index 100% rename from example.env rename to Example.env diff --git a/Dockerfile b/src/Dockerfile similarity index 100% rename from Dockerfile rename to src/Dockerfile diff --git a/data/config.json b/src/data/config.json similarity index 100% rename from data/config.json rename to src/data/config.json diff --git a/data/strings.json b/src/data/strings.json similarity index 100% rename from data/strings.json rename to src/data/strings.json diff --git a/main.js b/src/main.js similarity index 100% rename from main.js rename to src/main.js diff --git a/modules/_cleanInput.js b/src/modules/_cleanInput.js similarity index 100% rename from modules/_cleanInput.js rename to src/modules/_cleanInput.js diff --git a/modules/_deploySlashCmdsGlobal.js b/src/modules/_deploySlashCmdsGlobal.js similarity index 100% rename from modules/_deploySlashCmdsGlobal.js rename to src/modules/_deploySlashCmdsGlobal.js diff --git a/modules/_deploySlashCmdsLocal.js b/src/modules/_deploySlashCmdsLocal.js similarity index 100% rename from modules/_deploySlashCmdsLocal.js rename to src/modules/_deploySlashCmdsLocal.js diff --git a/modules/_eraseSlashCmds.js b/src/modules/_eraseSlashCmds.js similarity index 100% rename from modules/_eraseSlashCmds.js rename to src/modules/_eraseSlashCmds.js diff --git a/modules/functions.js b/src/modules/functions.js similarity index 95% rename from modules/functions.js rename to src/modules/functions.js index db2e9ac..4b10f11 100644 --- a/modules/functions.js +++ b/src/modules/functions.js @@ -14,7 +14,7 @@ const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = Discord; // Various imports from other files const config = require('../data/config.json'); const strings = require('../data/strings.json'); -const slashCommandFiles = fs.readdirSync('./slash-commands/').filter(file => file.endsWith('.js')); +const slashCommandFiles = fs.readdirSync('./src/slash-commands/').filter(file => file.endsWith('.js')); const functions = { // Functions for managing and creating Collections diff --git a/modules/input.txt b/src/modules/input.txt similarity index 100% rename from modules/input.txt rename to src/modules/input.txt diff --git a/package-lock.json b/src/package-lock.json similarity index 100% rename from package-lock.json rename to src/package-lock.json diff --git a/package.json b/src/package.json similarity index 100% rename from package.json rename to src/package.json diff --git a/src/slash-commands/chat.js b/src/slash-commands/chat.js new file mode 100644 index 0000000..6ff3fdc --- /dev/null +++ b/src/slash-commands/chat.js @@ -0,0 +1,29 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const fn = require('../modules/functions.js'); +const strings = require('../data/strings.json'); + +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(); + await interaction.editReply(fn.embeds.generatingResponse()); + const userPrompt = interaction.options.getString("prompt"); + const response = await fn.openAI.chatPrompt(userPrompt).catch(e => console.error(e)); + const responseText = response.choices[0].message.content; + const usage = { + tokens: response.usage.total_tokens, + usdc: (response.usage.prompt_tokens * (strings.ai.chatPromptCentsPer / strings.ai.chatPromptUnits)) + + (response.usage.completion_tokens * (strings.ai.chatResCentsPer / strings.ai.chatResUnits)) + }; + const gptEmbed = fn.embeds.gpt(userPrompt, responseText, usage); + await interaction.editReply(gptEmbed); + fn.upload.openai(interaction.user.id, userPrompt, strings.ai.chatModel, usage.tokens, usage.usdc); + }, +}; \ No newline at end of file diff --git a/src/slash-commands/dalle.js b/src/slash-commands/dalle.js new file mode 100644 index 0000000..bde2078 --- /dev/null +++ b/src/slash-commands/dalle.js @@ -0,0 +1,40 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const fn = require('../modules/functions.js'); +const strings = require("../data/strings.json"); + +module.exports = { + data: new SlashCommandBuilder() + .setName('dalle') + .setDescription('Generate an image with DALL-e') + .addStringOption(o => + o.setName("prompt") + .setDescription("Prompt to send to DALL-e") + .setRequired(true) + ) + .addStringOption(o => + o.setName("size") + .setDescription("1024x1024, 512x512, 256x256") + .setRequired(false) + .addChoices( + { name: "1024x1024 (2¢)", value: "1024x1024" }, + { name: "512x512 (1.8¢)", value: "512x512" }, + { name: "256x256 (1.6¢)", value: "256x256" } + )), + async execute(interaction) { + try { + await interaction.deferReply(); + await interaction.editReply(fn.embeds.generatingResponse()); + const userPrompt = interaction.options.getString("prompt"); + const size = interaction.options.getString("size") ? interaction.options.getString("size") : "512x512"; + + const imageUrl = await fn.openAI.imagePrompt(userPrompt, size); + const dalleEmbed = fn.embeds.dalle(userPrompt, imageUrl, size); + await interaction.editReply(dalleEmbed); + fn.upload.openai(interaction.user.id, userPrompt, "dalle", 0, strings.costs.dalle[size]); + } catch (err) { + const errorId = fn.generateErrorId(); + console.error(`${errorId}: ${err}`); + await interaction.editReply(`An error has occured. Error ID: ${errorId}\n${err}`); + } + }, +}; \ No newline at end of file diff --git a/src/slash-commands/ping.js b/src/slash-commands/ping.js new file mode 100644 index 0000000..df71548 --- /dev/null +++ b/src/slash-commands/ping.js @@ -0,0 +1,11 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const fn = require('../modules/functions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('ping') + .setDescription('Check that the bot is alive and responding.'), + async execute(interaction) { + await interaction.reply({ content: 'Pong!', ephemeral: true }); + }, +}; \ No newline at end of file diff --git a/slash-commands/template b/src/slash-commands/template similarity index 100% rename from slash-commands/template rename to src/slash-commands/template