aifordiscord-api
Version:
An advanced npm package for Discord bots providing AI-enhanced random content, memes, jokes, and utilities with comprehensive documentation.
260 lines • 10.9 kB
JavaScript
;
/**
* OpenAI integration service
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AIService = void 0;
const openai_1 = __importDefault(require("openai"));
class AIService {
constructor(apiKey) {
this.enabled = !!apiKey;
if (this.enabled) {
this.openai = new openai_1.default({
apiKey: apiKey || process.env.OPENAI_API_KEY
});
}
}
async enhanceJoke(originalJoke, name) {
if (!this.enabled)
return originalJoke;
try {
const prompt = name
? `Enhance this joke to be funnier and incorporate the name "${name}" naturally: ${originalJoke}`
: `Make this joke funnier while keeping it clean and appropriate: ${originalJoke}`;
const response = await this.openai.chat.completions.create({
model: "gpt-4o", // the newest OpenAI model is "gpt-4o" which was released May 13, 2024. do not change this unless explicitly requested by the user
messages: [
{
role: "system",
content: "You are a comedy writer who enhances jokes to be funnier while keeping them appropriate and clean."
},
{
role: "user",
content: prompt
}
],
max_tokens: 200,
temperature: 0.8
});
return response.choices[0].message.content?.trim() || originalJoke;
}
catch (error) {
console.warn('AI enhancement failed:', error instanceof Error ? error.message : String(error));
return originalJoke;
}
}
async enhanceAdvice(originalAdvice) {
if (!this.enabled)
return originalAdvice;
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o", // the newest OpenAI model is "gpt-4o" which was released May 13, 2024. do not change this unless explicitly requested by the user
messages: [
{
role: "system",
content: "You are a wise mentor who provides thoughtful, actionable advice. Enhance the given advice to be more insightful and practical."
},
{
role: "user",
content: `Enhance this advice to be more thoughtful and actionable: ${originalAdvice}`
}
],
max_tokens: 250,
temperature: 0.7
});
return response.choices[0].message.content?.trim() || originalAdvice;
}
catch (error) {
console.warn('AI enhancement failed:', error instanceof Error ? error.message : String(error));
return originalAdvice;
}
}
async enhanceFact(originalFact) {
if (!this.enabled)
return originalFact;
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o", // the newest OpenAI model is "gpt-4o" which was released May 13, 2024. do not change this unless explicitly requested by the user
messages: [
{
role: "system",
content: "You are an educator who makes facts more interesting and engaging while maintaining accuracy."
},
{
role: "user",
content: `Make this fact more interesting and engaging while keeping it accurate: ${originalFact}`
}
],
max_tokens: 200,
temperature: 0.6
});
return response.choices[0].message.content?.trim() || originalFact;
}
catch (error) {
console.warn('AI enhancement failed:', error instanceof Error ? error.message : String(error));
return originalFact;
}
}
async generateCustomJoke(topic) {
if (!this.enabled) {
throw new Error('AI service not available - please provide OpenAI API key');
}
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o", // the newest OpenAI model is "gpt-4o" which was released May 13, 2024. do not change this unless explicitly requested by the user
messages: [
{
role: "system",
content: "You are a comedian who creates clean, family-friendly jokes on any topic."
},
{
role: "user",
content: `Create a funny, clean joke about: ${topic}`
}
],
max_tokens: 150,
temperature: 0.9
});
return response.choices[0].message.content?.trim() || "Why don't scientists trust atoms? Because they make up everything!";
}
catch (error) {
throw new Error(`Failed to generate custom joke: ${error instanceof Error ? error.message : String(error)}`);
}
}
async enhanceQuote(originalQuote) {
if (!this.enabled)
return originalQuote;
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a wise philosopher who enhances quotes to be more profound and inspiring while maintaining their original meaning."
},
{
role: "user",
content: `Enhance this quote to be more inspiring: ${originalQuote}`
}
],
max_tokens: 200,
temperature: 0.7
});
return response.choices[0].message.content?.trim() || originalQuote;
}
catch (error) {
console.warn('AI enhancement failed:', error instanceof Error ? error.message : String(error));
return originalQuote;
}
}
async generateStory(topic, length = 'short') {
if (!this.enabled) {
throw new Error('AI service not available - please provide OpenAI API key');
}
const maxTokens = length === 'short' ? 150 : length === 'medium' ? 300 : 500;
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a creative storyteller who writes engaging, family-friendly stories."
},
{
role: "user",
content: `Write a ${length} story about: ${topic}`
}
],
max_tokens: maxTokens,
temperature: 0.8
});
return response.choices[0].message.content?.trim() || "Once upon a time, there was a magical adventure waiting to be told...";
}
catch (error) {
throw new Error(`Failed to generate story: ${error instanceof Error ? error.message : String(error)}`);
}
}
async generateRiddle(difficulty = 'medium') {
if (!this.enabled) {
throw new Error('AI service not available - please provide OpenAI API key');
}
try {
const response = await this.openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a riddle master who creates clever riddles. Respond with JSON format: {\"riddle\": \"...\", \"answer\": \"...\"}"
},
{
role: "user",
content: `Create a ${difficulty} riddle with its answer.`
}
],
max_tokens: 200,
temperature: 0.8
});
const content = response.choices[0].message.content?.trim();
try {
const parsed = JSON.parse(content || '{}');
return {
riddle: parsed.riddle || "What gets wetter as it dries?",
answer: parsed.answer || "A towel"
};
}
catch {
return {
riddle: "What gets wetter as it dries?",
answer: "A towel"
};
}
}
catch (error) {
throw new Error(`Failed to generate riddle: ${error instanceof Error ? error.message : String(error)}`);
}
}
async generateCompliment(name) {
if (!this.enabled) {
const generic = [
"You're absolutely amazing!",
"You have a wonderful personality!",
"You bring joy to everyone around you!",
"You're incredibly thoughtful and kind!"
];
return generic[Math.floor(Math.random() * generic.length)];
}
try {
const prompt = name
? `Generate a personalized, genuine compliment for someone named ${name}`
: "Generate a genuine, uplifting compliment";
const response = await this.openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a kind person who gives genuine, heartfelt compliments that make people feel good about themselves."
},
{
role: "user",
content: prompt
}
],
max_tokens: 100,
temperature: 0.8
});
return response.choices[0].message.content?.trim() || "You're an amazing person!";
}
catch (error) {
console.warn('AI enhancement failed:', error instanceof Error ? error.message : String(error));
return "You're absolutely wonderful!";
}
}
isEnabled() {
return this.enabled;
}
}
exports.AIService = AIService;
//# sourceMappingURL=openai.js.map