Add Gerard

This commit is contained in:
2024-09-17 22:04:17 +00:00
parent e4a391930f
commit 54335fa676
5 changed files with 234 additions and 6 deletions

63
src/commands/gerard.ts Normal file
View File

@@ -0,0 +1,63 @@
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'}`
);
}
}

View File

@@ -8,6 +8,7 @@ interface Config {
giphyApiKey: string;
plantnetApiKey: string;
omdbApiKey: string;
openaiApiKey: string;
replicateApiKey: string;
mongodbUri: string;
registerSlashCommands: boolean;
@@ -27,6 +28,7 @@ const config: Config = {
giphyApiKey: getEnv('GIPHY_API_KEY') as string,
plantnetApiKey: getEnv('PLANTNET_API_KEY') as string,
omdbApiKey: getEnv('OMDB_API_KEY') as string,
openaiApiKey: getEnv('OPENAI_API_KEY') as string,
replicateApiKey: getEnv('REPLICATE_API_KEY') as string,
mongodbUri: getEnv('MONGODB_URI') as string,
registerSlashCommands:

View File

@@ -4,6 +4,7 @@ import * as corrupt from '../commands/corrupt';
import * as countdown from '../commands/countdown';
import * as eyecandy from '../commands/eyecandy';
import * as game from '../commands/game';
import * as gerard from '../commands/gerard';
import * as image from '../commands/image';
import * as imdb from '../commands/imdb';
import * as kanye from '../commands/kanye';
@@ -29,6 +30,7 @@ export function getCommands(): Collection<string, Command> {
commands.set(countdown.data.name, countdown);
commands.set(eyecandy.data.name, eyecandy);
commands.set(game.data.name, game);
commands.set(gerard.data.name, gerard);
commands.set(image.data.name, image);
commands.set(imdb.data.name, imdb);
commands.set(kanye.data.name, kanye);