Skip to content

Commit

Permalink
lessgoo adding image in embed
Browse files Browse the repository at this point in the history
  • Loading branch information
NoLogicAlan authored Feb 22, 2023
1 parent e2642f8 commit dcd62f3
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 221 deletions.
31 changes: 24 additions & 7 deletions commands/generate-image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { CommandBuilder } = require("../Commands.js");
const { ask } = require("../generateImage");
const fetch = require('node-fetch');

module.exports = {
command: new CommandBuilder()
Expand All @@ -10,14 +11,30 @@ module.exports = {
o.setName("prompt")
.setRequired(true)),
run: async function (message, data) {
const prompt = data.get("prompt").value
const prompt = data.get("prompt").value;
const answer = await ask(prompt, message);
if (answer.success) {
answer.imageUrls.forEach(url => {
message.channel.sendMessage(`[Image](${url})`);
const imageUrl = answer.imageUrls[0]; // select the first URL from the array
try {
const response = await fetch(imageUrl);
const buffer = await response.buffer();
const media = await upload.call(this, buffer, prompt);
message.channel.sendMessage({
content: "",
embeds: [{
title: "Image Generation",
description: "Here's your generated image:",
media,
colour: "#bb2525"
}]
});
} else {
message.channel.sendMessage(answer.error);
} catch (error) {
console.error(error);
}
}
}
}

async function upload(buffer, prompt) {
return new Promise((res, rej) => {
this.uploader.upload(buffer, `${prompt}`).then(res).catch(rej);
});
}
16 changes: 8 additions & 8 deletions generateImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Configuration, OpenAIApi } = require("openai");
const fs = require("fs");
const path = require("path");

const API_KEY = "its ur problem";
const API_KEY = "that ur problem";
const DEFAULT_MODEL = "image-alpha-001";
const AVAILABLE_MODELS = [
"image-alpha-001",
Expand All @@ -15,7 +15,7 @@ const AVAILABLE_MODELS = [
"image-jukebox-001",
];

const RATE_LIMIT_COUNT = 1000;
const RATE_LIMIT_COUNT = 100;

const usageDataPath = path.join(__dirname, "usageData.json");

Expand Down Expand Up @@ -54,14 +54,14 @@ async function ask(prompt, message, model = DEFAULT_MODEL) {

// Check if the user has exceeded the usage limit
if (userUsage.count >= RATE_LIMIT_COUNT && now < userUsage.resetTime) {
const timeLeft = Math.ceil((userUsage.resetTime - now) / 1000);
const timeLeft = Math.ceil((userUsage.resetTime - now) / 100);
throw new Error(`Usage limit reached for user ${userId}. Try again in ${timeLeft} seconds.`);
}

userUsage.count++;

// Check if the user has used the API 1000 times
if (userUsage.count === 1000) {
// Check if the user has used the API 100 times
if (userUsage.count === 100) {
userUsage.limitReached = true;
}

Expand All @@ -70,21 +70,21 @@ async function ask(prompt, message, model = DEFAULT_MODEL) {
saveUsageData();

if (userUsage.limitReached) {
return { success: false, error: "User" + `<@${userId}>` + "has reached 1000 API uses." };
return { success: false, error: "User" + `<@${userId}>` + "has reached 100 API uses. want more [support](rvlt.gg/qvJEsmPt) us [images](https://autumn.revolt.chat/attachments/0s377fGbuwcX4AA0aa3b1k-HC2VDJv2YJIGScAKyeB/image.png)" };
}

try {
const response = await openai.createImage({
prompt,
model,
n: 1,
size: "1024x1024"
size: "512x512"
});
const imageUrls = response?.data?.data?.map(({ url }) => url);
return { success: true, imageUrls };
} catch (error) {
console.error(error);
return { success: false, error: error.message + " or bad prompt (maybe)" };
return { success: false, error: error.message + " or bad prompt [maybe](<https://status.openai.com/>)" };
}
}

Expand Down
Loading

0 comments on commit dcd62f3

Please sign in to comment.