|
| 1 | +// commands/image-edit.js |
1 | 2 | import { EmbedBuilder, AttachmentBuilder } from "discord.js"; |
2 | | -import axios from "axios"; |
3 | 3 | import { toFile } from "openai"; |
4 | 4 |
|
5 | 5 | async function imageEdit(interaction, client) { |
6 | | - // Immediately defer the reply to avoid timeout issues |
7 | 6 | await interaction.deferReply(); |
8 | 7 |
|
9 | 8 | try { |
10 | | - // Log the interaction for debugging |
11 | | - console.log("Image edit command received:", { |
12 | | - user: interaction.user.tag, |
13 | | - options: interaction.options.data |
14 | | - }); |
15 | | - |
16 | 9 | const description = interaction.options.getString("description"); |
17 | 10 | const imageAttachment = interaction.options.getAttachment("image"); |
18 | 11 |
|
19 | | - // Validate inputs after deferring to ensure we can respond properly |
20 | | - if (!description) { |
21 | | - return await interaction.editReply({ |
22 | | - content: "Please provide an image description." |
23 | | - }); |
| 12 | + if (!imageAttachment?.contentType?.startsWith("image/")) { |
| 13 | + return await interaction.editReply("Veuillez fournir une image valide (PNG, JPEG, WebP)."); |
24 | 14 | } |
25 | 15 |
|
26 | | - if (!imageAttachment) { |
27 | | - return await interaction.editReply({ |
28 | | - content: "Please provide a valid image attachment." |
29 | | - }); |
30 | | - } |
| 16 | + console.log(`[IMAGE EDIT] ${interaction.user.username}: "${description}"`); |
| 17 | + console.log(`[IMAGE EDIT] Input image: ${imageAttachment.name} (${imageAttachment.size} bytes)`); |
31 | 18 |
|
32 | | - // Log the attachment details |
33 | | - console.log("Attachment details:", { |
34 | | - name: imageAttachment.name, |
35 | | - contentType: imageAttachment.contentType, |
36 | | - size: imageAttachment.size, |
37 | | - url: imageAttachment.url |
38 | | - }); |
39 | | - |
40 | | - if (!imageAttachment.contentType?.startsWith("image/")) { |
41 | | - return await interaction.editReply({ |
42 | | - content: "The provided attachment is not an image. Please upload a PNG, JPEG, or WebP image." |
43 | | - }); |
44 | | - } |
45 | | - |
46 | | - // Download the image from Discord |
47 | | - console.log("Downloading image from Discord..."); |
48 | | - const response = await axios.get(imageAttachment.url, { responseType: 'arraybuffer' }); |
49 | | - const imageBuffer = Buffer.from(response.data); |
50 | | - console.log(`Image downloaded, size: ${imageBuffer.length} bytes`); |
51 | | - |
52 | | - // Convert to proper format for OpenAI |
53 | | - console.log("Converting image for OpenAI..."); |
54 | | - const imageFile = await toFile(imageBuffer, imageAttachment.name || "image.png", { |
| 19 | + const response = await fetch(imageAttachment.url); |
| 20 | + const buffer = await response.arrayBuffer(); |
| 21 | + const imageFile = await toFile(buffer, imageAttachment.name, { |
55 | 22 | type: imageAttachment.contentType |
56 | 23 | }); |
57 | | - console.log("Image converted successfully"); |
58 | 24 |
|
59 | | - // Call the edit endpoint |
60 | | - console.log("Calling OpenAI edit API with prompt:", description); |
61 | 25 | const result = await client.openai.images.edit({ |
62 | 26 | model: "gpt-image-1", |
63 | 27 | image: imageFile, |
64 | 28 | prompt: description, |
| 29 | + // Removed response_format - no longer supported |
65 | 30 | }); |
66 | 31 |
|
67 | | - console.log("OpenAI API response received:", result); |
68 | | - |
69 | | - if (!result?.data?.[0]?.b64_json) { |
70 | | - throw new Error("No image data returned from API"); |
| 32 | + // Check if response has base64 data or URL |
| 33 | + let outputBuffer; |
| 34 | + if (result.data[0].b64_json) { |
| 35 | + // Base64 response |
| 36 | + outputBuffer = Buffer.from(result.data[0].b64_json, "base64"); |
| 37 | + console.log(`[IMAGE EDIT] Received base64 edited image`); |
| 38 | + } else if (result.data[0].url) { |
| 39 | + // URL response - download the image |
| 40 | + console.log(`[IMAGE EDIT] Downloading from URL: ${result.data[0].url}`); |
| 41 | + const imageResponse = await fetch(result.data[0].url); |
| 42 | + const arrayBuffer = await imageResponse.arrayBuffer(); |
| 43 | + outputBuffer = Buffer.from(arrayBuffer); |
| 44 | + } else { |
| 45 | + throw new Error("No image data in response"); |
71 | 46 | } |
72 | 47 |
|
73 | | - // Convert base64 to buffer for Discord attachment |
74 | | - const outputBuffer = Buffer.from(result.data[0].b64_json, "base64"); |
75 | | - const attachment = new AttachmentBuilder(outputBuffer, { name: "edited-image.png" }); |
| 48 | + const attachment = new AttachmentBuilder(outputBuffer, { name: "edited.png" }); |
76 | 49 |
|
77 | 50 | const embed = new EmbedBuilder() |
78 | | - .setTitle("Image edited successfully") |
| 51 | + .setTitle("Image modifiée") |
79 | 52 | .setDescription(`Prompt: ${description}`) |
80 | | - .setImage("attachment://edited-image.png") |
| 53 | + .setImage("attachment://edited.png") |
81 | 54 | .setTimestamp(); |
82 | 55 |
|
83 | | - await interaction.editReply({ |
84 | | - content: null, |
85 | | - embeds: [embed], |
86 | | - files: [attachment] |
87 | | - }); |
88 | | - |
89 | | - console.log("Successfully sent edited image response"); |
| 56 | + console.log(`[IMAGE EDIT] Success - Edited image sent to ${interaction.user.username}`); |
| 57 | + await interaction.editReply({ embeds: [embed], files: [attachment] }); |
90 | 58 | } catch (error) { |
91 | | - console.error("Error in imageEdit function:", error); |
92 | | - |
93 | | - // Check for specific API errors |
94 | | - const errorMessage = error.response?.data?.error?.message || error.message || "Unknown error"; |
95 | | - console.error("Detailed error:", errorMessage); |
96 | | - |
97 | | - // Always use editReply since we deferred at the beginning |
98 | | - await interaction.editReply({ |
99 | | - content: `Failed to edit the image. Error: ${errorMessage}`, |
100 | | - embeds: [], |
101 | | - files: [] |
102 | | - }); |
| 59 | + console.error("[IMAGE EDIT] Error:", error); |
| 60 | + await interaction.editReply("Échec de modification d'image. " + (error.message || "")); |
103 | 61 | } |
104 | 62 | } |
105 | 63 |
|
|
0 commit comments