nodbot/slash-commands/chat.js

29 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-05-28 00:41:08 +00:00
const { SlashCommandBuilder } = require('@discordjs/builders');
const fn = require('../functions.js');
2023-10-18 21:33:03 +00:00
const strings = require('../strings.json');
2023-05-28 00:41:08 +00:00
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();
2023-06-15 23:07:16 +00:00
await interaction.editReply(fn.embeds.generatingResponse());
2023-05-28 00:41:08 +00:00
const userPrompt = interaction.options.getString("prompt");
const response = await fn.openAI.chatPrompt(userPrompt).catch(e => console.error(e));
2023-10-18 21:33:03 +00:00
const responseText = response.choices[0].message.content;
2023-05-31 12:07:12 +00:00
const usage = {
tokens: response.usage.total_tokens,
2023-10-18 21:33:03 +00:00
usdc: (response.usage.prompt_tokens * (strings.ai.chatPromptCentsPer / strings.ai.chatPromptUnits)) +
(response.usage.completion_tokens * (strings.ai.chatResCentsPer / strings.ai.chatResUnits))
2023-05-31 12:07:12 +00:00
};
const gptEmbed = fn.embeds.gpt(userPrompt, responseText, usage);
2023-05-29 13:22:11 +00:00
await interaction.editReply(gptEmbed);
2023-10-18 21:33:03 +00:00
fn.upload.openai(interaction.user.id, userPrompt, strings.ai.chatModel, usage.tokens, usage.usdc);
2023-05-28 00:41:08 +00:00
},
};