← back to browse
goat command

edit

AI image editor

0
views
0
likes
0
installs
raw source
const axios = require("axios");
const FormData = require("form-data");
const fs = require("fs-extra");
const path = require("path");

module.exports.config = {
  name: "edit",
  version: "1.0.0",
  author: "🔰𝐑𝐀𝐇𝐀𝐓 𝐈𝐒𝐋𝐀𝐌🔰",
  countDown: 10,
  role: 0,
  shortDescription: {
    en: "AI image editor"
  },
  description: {
    en: "AI image editor"
  },
  category: "image",
  guide: {
    en: "[reply to image] <prompt>"
  }
};

module.exports.onStart = async function ({ api, event, args, message }) {
  const { threadID, messageID, messageReply } = event;

  let imageUrl = null;

  if (
    messageReply &&
    messageReply.attachments &&
    messageReply.attachments.length > 0
  ) {
    const attachment = messageReply.attachments.find(
      item => item.type === "photo" || item.type === "image"
    );

    if (attachment) {
      imageUrl = attachment.url || attachment.image_data?.url;
    }
  }

  if (!imageUrl && event.attachments && event.attachments.length > 0) {
    const attachment = event.attachments.find(
      item => item.type === "photo" || item.type === "image"
    );

    if (attachment) {
      imageUrl = attachment.url || attachment.image_data?.url;
    }
  }

  if (!imageUrl) {
    return message.reply(
      "❌ একটি ছবির মেসেজে reply করে command দিন।\n\nউদাহরণ:\n!edit make the background beautiful"
    );
  }

  const prompt = args.join(" ").trim();

  if (!prompt) {
    return message.reply(
      "❌ Prompt দিন।\n\nউদাহরণ:\n!edit make the sky sunset"
    );
  }

  const cacheDir = path.join(__dirname, "cache");
  await fs.ensureDir(cacheDir);

  const inputPath = path.join(cacheDir, `imgedit_input_${Date.now()}.jpg`);
  const outputPath = path.join(cacheDir, `imgedit_output_${Date.now()}.jpg`);

  try {
    await message.reply("🪒please wait bara...");

    const imageResponse = await axios.get(imageUrl, {
      responseType: "arraybuffer",
      timeout: 30000
    });

    await fs.writeFile(inputPath, imageResponse.data);
    const form = new FormData();

    form.append("image", fs.createReadStream(inputPath), {
      filename: "image.jpg",
      contentType: "image/jpeg"
    });

    form.append("prompt", prompt);
    form.append("resolution", "2K");
    form.append("ratio", "match_input_image");

    // তোমার API
    const apiResponse = await axios.post(
      "https://xrahat-image-edit.vercel.app/api/edit",
      form,
      {
        headers: {
          ...form.getHeaders()
        },
        timeout: 120000,
        maxContentLength: Infinity,
        maxBodyLength: Infinity
      }
    );

    const data = apiResponse.data || {};

    if (!data.success || !data.imageUrl) {
      throw new Error(data.error || "Image generation failed");
    }

    const outputResponse = await axios.get(data.imageUrl, {
      responseType: "arraybuffer",
      timeout: 60000
    });

    await fs.writeFile(outputPath, outputResponse.data);

    // Messenger এ image পাঠানো (GoatBot style attachment reply)
    return message.reply({
      body:
        `✅ Image Edit Complete!\n` +
        `📝 Prompt: ${prompt}`,
      attachment: fs.createReadStream(outputPath)
    });

  } catch (error) {
    console.error("imgedit error:", error);

    let errorMessage = "❌ Image edit failed.";

    if (error.response?.data?.error) {
      errorMessage += `\n\n${error.response.data.error}`;
    } else if (error.message) {
      errorMessage += `\n\n${error.message}`;
    }

    return message.reply(errorMessage);

  } finally {
    setTimeout(async () => {
      try {
        if (await fs.pathExists(inputPath)) {
          await fs.remove(inputPath);
        }

        if (await fs.pathExists(outputPath)) {
          await fs.remove(outputPath);
        }
      } catch (e) {
        console.error("Cleanup error:", e.message);
      }
    }, 5000);
  }
};

View raw file