const { getStreamFromURL, uploadImgbb } = global.utils; const fs = require("fs").promises; const path = require('path'); const short = require('tinyurl'); const axios = require("axios"); const API = 'https://midjourney-pv6b.onrender.com'; const apiKeys = { "keys": [ "f080f3e4-bbee-4048-ba23-10fb9b9f68ba" ] }; let currentKey= 0; function getKey() { if (currentKey >= apiKeys.keys.length) { currentKey= 0; } const nextKey = apiKeys.keys[currentKey]; currentKey++; return nextKey; } module.exports = { config: { name: "pussyx", version: "1.0", author: "Akash Xzh", countDown: 20, role: 0, longDescription: "Generate unique and captivating images using the Midjourney API. Combine prompts and optional image attachments to create visually stunning results.", category: "ai imagination", guide: { en: "Usage: {pn} prompt or {pn} image attachment + prompt." }, }, onStart: async function ({ api, args, event, message }) { try { const permission = ["100032778441480", "100077553281922", "100066407270658", "100080352260736", "100070029543010", "100017822257155", "100087595555128", "100084713389776"]; if (!permission.includes(event.senderID)) { api.sendMessage("~OhBaka!Seemsyoudon'thavepermissiontousethiscmd!ðŸ�±", event.threadID, event.messageID); return; } let promptX; let apiKeyX = getKey(); let imageUrl = null; if (event.type === "message_reply" && ["photo", "sticker"].includes(event.messageReply.attachments?.[0]?.type)) { const s = await uploadImgbb(event.messageReply.attachments[0].url); imageUrl = s.image.url; } const prompt = args.join(' '); if (!prompt) { throw new Error("Please provide a prompt...!"); } if (imageUrl) { promptX = imageUrl + ' ' + prompt; } else { promptX = prompt; } const ID = event.messageID; const w = await message.reply("⛵ Initiating request to Midjourney.\n\n⏰ Please be patient...!"); message.reaction('⏰', ID); const resultData = await processTask(promptX, message, w, apiKeyX, ID); const { ImageID: tid, URL: img } = resultData.data; const count = resultData.count; await message.reply({ body: `Gen ID: ${count} \n\nAvailable Actions: U1,U2,U3,U4,V1,V2,V3,V4`, attachment: await getStreamFromURL(img), }, async (err, info) => { if (err) { console.error(err); message.reply(`Error: ${err}`); return; } message.reaction('✅', ID); global.GoatBot.onReply.set(info.messageID, { commandName: this.config.name, messageID: info.messageID, author: event.senderID, type: "reply", Y: tid, Z: img, X: count, }); }); } catch (error) { console.error(error); message.reply(`Error: ${error.message}`); } }, onReply: async function ({ event, Reply, message }) { try { const { author, Y, X, Z, type } = Reply; const { senderID, body } = event; if (senderID != author) { return; } if (type == "reply") { const sides = ["U1", "U2", "U3", "U4"]; const varModes = ["V1", "V2", "V3", "V4"]; if (sides.includes(body)) { const pose = sidesIn(body); const upURL = API + `/upscale?side=${pose}&image_url=${Z}`; try { const w = await message.reply(`Processing Action\n\nID: ${X}`); const res = await axios.get(upURL); if (res.data.error) { throw new Error(`Error:\n${res.data.error}\nID: ${res.data.task_id}`); } const imgX = await short.shorten(res.data.image_url); message.reply({ body: `Image Upscaled\n\nGen ID: ${X}\n\nDownload: ${imgX}`, attachment: await getStreamFromURL(imgX), }, (err, info) => { if (err) { console.error(err); message.reply(`Error: ${err.message || "Unknown error"}`); return; } global.GoatBot.onReply.set(info.messageID, { commandName: this.config.name, messageID: info.messageID, author: event.senderID, type: "upscale" }); }); await message.unsend(w.messageID); } catch (error) { console.error(error); message.reply(`Error: ${error.message || "Unknown error"}`); } } else if (varModes.includes(body)) { return message.reply("Variation disabled!"); const pose = sidesIn(body); try { const w = await message.reply(`Processing Action\n\nID: ${X}`); const resultData = await variationTask(Y, pose, w, ID, message, name); if (resultData.data.status === "failed") { message.reply(`Request failed!\n\nReasons: ${resultData.data.task_result.error_messages}`); message.reaction('â�Œ', ID); return; } let info; const { process_time: time, task_id: tid, task_result: { image_url: img }, task_result: { quota_used: quota } } = resultData.data; const count = resultData.count; if (resultData.data.meta.process_mode === "relax") { info = 'R:' + quota; } else { info = 'F:' + quota; } await message.reply({ body: `Gen ID: ${count} \n\nGen Time: ${time} seconds\n\nStatus: ${info}\n\nAvailable Actions: U1,U2,U3,U4,V1,V2,V3,V4`, attachment: await getStreamFromURL(img), }, (err, info) => { if (err) { console.error(err); return message.reply(`Error: ${err.message || "Unknown error"}`); } message.reaction('✅', ID); global.GoatBot.onReply.set(info.messageID, { commandName: this.config.name, messageID: info.messageID, author: event.senderID, type: "reply", Y: tid, Z: img, X: count }); }); } catch (error) { console.error(error); message.reply(`Error: ${error.message || "Unknown error"}`); } } else { message.reply("Invalid action.\n\nPlease choose from: U1, U2, U3, U4, V1, V2, V3, V4."); } } else if (type == "upscale") { const { TID, count } = Reply; const upOPT = ["1", "2"]; const zoom = ["3", "4"]; if (upOPT.includes(body)) { const pose = sidesIn(body); const url = API +`/upscale?taskID=${TID}&side=${pose}`; try { const w = await message.reply(`Processing your request.\n\nGen ID: ${X}`); const res = await axios.get(url); if (res.data.error) { throw new Error(`Error:\n${res.data.error}\nID: ${res.data.task_id}`); } const { image_url: img, time_taken: time } = res.data; message.reply({ body: `Image Upscaled (${pose})\n\nGen ID: ${X}\n\nTime taken: ${time} seconds`, attachment: await getStreamFromURL(img) }); await message.unsend(w.messageID); } catch (error) { console.error(error); message.reply(`Error: ${error.message || "Unknown error"}`); } } else if (zoom.includes(body)) { const pose = sidesIn(body); const url = API +`/outpaint?taskID=${TID}&action=${pose}`; try { const w = await message.reply(`Processing your request.\n\nGen ID: ${count}`); const res = await axios.get(url); if (res.data.error) { throw new Error(`Error:\n${res.data.error}\nID: ${res.data.task_id}`); } count++; const { image_url: img, time_taken: time } = data; const countFilePath = path.join('requestCount.json'); await fs.writeFile(countFilePath, JSON.stringify({ count })); message.reply({ body: `Image Zoomed Out (${pose}x)\n\nGen ID: ${count}\n\nTime taken: ${time} seconds`, attachment: await getStreamFromURL(img) }); await message.unsend(w.messageID); } catch (error) { console.error(error); message.reply(`Error: ${error.message || "Unknown error"}`); } } else { return; } } } catch (error) { console.error(error); message.reply(`Error: ${error.message || "Unknown error"}`); } } }; function sidesIn(body) { switch (body) { case '1': return '2x'; case '2': return '4x'; case '3': return 1.5; case '4': return 2; case 'U1': return 1; case 'V1': return 1; case 'U2': return 2; case 'V2': return 2; case 'U3': return 3; case 'V3': return 3; case 'U4': return 4; case 'V4': return 4; } } async function fetchResultData(taskId, message, w, ID, apiKeyX) { let Processing = false; let count = 1; while (true) { const res = await axios.post('https://visioncraft.top/midjourney/result', { task_id: taskId.toString(), token: apiKeyX }); if (res.data.Status === "generating" && !Processing) { const countFilePath = path.join('requestCount.json'); try { const countData = await fs.readFile(countFilePath, 'utf-8'); count = JSON.parse(countData).count + 1; } catch (error) {} await fs.writeFile(countFilePath, JSON.stringify({ count })); message.reply(`Processing your request...\n\nRequest ID: ${count} `); message.reaction('⚙', ID); await message.unsend(w.messageID); Processing = true; } if (res.data.Status === "failed" || res.data.Status === "success") { return { data: res.data, count: count }; } await new Promise(resolve => setTimeout(resolve, 30000)); } } async function processTask(promptX, message, w, apiKeyX, ID) { try { const response = await axios.post('https://visioncraft.top/midjourney', { prompt: promptX, token: apiKeyX }); const taskId = response.data.data; const resultData = await fetchResultData(taskId, message, w, ID, apiKeyX); return resultData; } catch (error) { console.log(error.message); throw new Error(error.message); } } async function variationTask(message, w, ID, Y, pose, name) { try { const apiReqUrl = Base + "variation"; const requestData = { origin_task_id: Y, index: pose, }; const response = await axios.post(apiReqUrl, requestData); if (response.data.status !== "success") { throw new Error(response.data.message); } const taskId = response.data.task_id; try { const resultData = await fetchResultData(taskId, message, w, ID, name); return resultData; } catch (resultError) { throw new Error(`Error fetching result data: ${resultError.message}`); } } catch (error) { if (error.response && error.response.status === 403) { throw new Error("Unable to process the request."); } else if (error.response && error.response.status === 400) { return { count: "N/A", data: { status: "failed", task_id: "N/A", task_result: { error_messages: error.message } } }; } else { throw error; } } }