dscrdbt/events/interacionAiPost.js

59 lines
1.8 KiB
JavaScript

const { Events } = require('discord.js');
const { generateCaption, generateImage, generatePostImage } = require('../utils/newsProcessor');
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
if (!interaction.isModalSubmit()) return;
if (interaction.customId !== 'aipostModal') return;
try {
const newsLink = interaction.fields.getTextInputValue('newsLinkInput');
await interaction.deferReply();
// Process the article with status updates
// await interaction.editReply('📰 Reading article...');
// const articleData = await scrapeArticle(newsLink);
await interaction.editReply('✍️ Generating image copy...');
const imageText = await generateImage(newsLink);
await interaction.editReply('🎨 Creating AI image...');
const imageUrl = await generatePostImage(imageText);
await interaction.editReply('📝 Generating Instagram post...');
const postText = await generateCaption(newsLink);
// First message with the AI-generated image and its copy
await interaction.editReply({
content: `📸 **Generated Image Copy:**\n${imageText}`,
files: [imageUrl],
});
// Second message with the Instagram post copy
await interaction.followUp({
content: `📱 **Generated Instagram Post:**\n${postText}`,
});
}
catch (error) {
console.error('Error in modal submission:', error);
const errorMessage = error.message.includes('Failed to')
? error.message
: 'An unexpected error occurred while generating the post.';
if (interaction.deferred) {
await interaction.editReply({
content: `${errorMessage}\nPlease try again or use a different article.`,
ephemeral: true,
});
}
else {
await interaction.reply({
content: `${errorMessage}\nPlease try again or use a different article.`,
ephemeral: true,
});
}
}
},
};