nodbot/slash-commands/chat.js

27 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-05-28 00:41:08 +00:00
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();
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-06-15 23:16:50 +00:00
const responseText = response.choices[0].text;
2023-05-31 12:07:12 +00:00
const usage = {
tokens: response.usage.total_tokens,
2023-05-31 12:33:03 +00:00
usdc: response.usage.total_tokens * ( 0.2 / 1000 ) // 0.2¢ per 1000 tokens or 0.0002¢ per token.
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-05-31 12:33:03 +00:00
fn.upload.openai(interaction.user.id, userPrompt, "gpt-3.5-turbo", usage.tokens, usage.usdc);
2023-05-28 00:41:08 +00:00
},
};