64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
|
|
import OpenAI from 'openai';
|
|
import config from '../config';
|
|
|
|
const client = new OpenAI({
|
|
apiKey: config.openaiApiKey,
|
|
});
|
|
|
|
const systemPrompt = `You are Gerard Butler, talking to a fan.
|
|
They are fanatical about your work and want to know more about your life.
|
|
They ask you a question, and you respond in a way that is both engaging and informative.
|
|
The fan is eager to hear your response.`;
|
|
|
|
// Initialise the command data.
|
|
export const data = new SlashCommandBuilder()
|
|
.setName('gerard')
|
|
.setDescription('Talk with Gerard Butler')
|
|
.addStringOption((option) =>
|
|
option
|
|
.setName('question')
|
|
.setDescription('The question to ask Gerard Butler')
|
|
.setRequired(true)
|
|
);
|
|
|
|
console.log(`Loaded ${data.name} command.`);
|
|
|
|
/**
|
|
* Generate a response based on a prompt and send it back to the user.
|
|
* @param interaction The interaction that triggered the command.
|
|
* @returns A promise that resolves when the command is finished executing.
|
|
*/
|
|
export async function execute(
|
|
interaction: ChatInputCommandInteraction
|
|
): Promise<void> {
|
|
await interaction.deferReply();
|
|
|
|
const prompt = interaction.options.getString('question', true);
|
|
|
|
try {
|
|
// Create a chat completion.
|
|
const result = await client.chat.completions.create({
|
|
model: 'gpt-4o-mini',
|
|
messages: [
|
|
{ role: 'system', content: systemPrompt },
|
|
{ role: 'user', content: prompt },
|
|
],
|
|
});
|
|
|
|
// Extract the response.
|
|
const response = result.choices[0]!.message?.content;
|
|
|
|
if (!response) {
|
|
await interaction.editReply('No response was generated.');
|
|
return;
|
|
}
|
|
|
|
await interaction.editReply(response);
|
|
} catch (error: any) {
|
|
await interaction.editReply(
|
|
`An error occurred while generating the response: ${error.message || 'Unknown error'}`
|
|
);
|
|
}
|
|
}
|