From 6ed2f4f1a119304fbc8aca8b3300a220c806922d Mon Sep 17 00:00:00 2001 From: Atridad Lahiji <88056492+atridadl@users.noreply.github.com> Date: Thu, 6 Jul 2023 23:00:28 -0600 Subject: [PATCH] Updates to commands --- package.json | 84 +++++++++++++++++----------------- src/commands/pic.ts | 107 +++++++++++++++++++++++++++----------------- 2 files changed, 109 insertions(+), 82 deletions(-) diff --git a/package.json b/package.json index 35422fe..56db633 100644 --- a/package.json +++ b/package.json @@ -1,43 +1,43 @@ { - "name": "himbot", - "version": "1.0.0", - "engines": { - "node": ">=18.16.0" - }, - "main": "dist/index.js", - "author": "@sapphire", - "license": "UNLICENSE", - "type": "commonjs", - "dependencies": { - "@sapphire/decorators": "^6.0.1", - "@sapphire/discord.js-utilities": "7.0.0", - "@sapphire/framework": "^4.5.0", - "@sapphire/plugin-logger": "^3.0.4", - "@sapphire/utilities": "^3.12.0", - "@skyra/env-utilities": "^1.2.1", - "colorette": "^2.0.20", - "discord.js": "^14.11.0", - "openai": "^3.3.0" - }, - "devDependencies": { - "@sapphire/cli": "^1.6.1", - "@sapphire/prettier-config": "^1.4.5", - "@sapphire/ts-config": "^4.0.0", - "@types/node": "^20.3.3", - "@types/ws": "^8.5.5", - "prettier": "^2.8.8", - "tsc-watch": "^6.0.4", - "typescript": "^5.1.6" - }, - "scripts": { - "sapphire": "sapphire", - "generate": "sapphire generate", - "build": "tsc", - "watch": "tsc -w", - "start": "node dist/index.js", - "dev": "npm run build && npm run start", - "watch:start": "tsc-watch --onSuccess \"npm run start\"", - "format": "prettier --write \"src/\"" - }, - "prettier": "@sapphire/prettier-config" -} \ No newline at end of file + "name": "himbot", + "version": "4.2.0", + "engines": { + "node": ">=18.16.0" + }, + "main": "dist/index.js", + "author": "@sapphire", + "license": "UNLICENSE", + "type": "commonjs", + "dependencies": { + "@sapphire/decorators": "^6.0.1", + "@sapphire/discord.js-utilities": "7.0.0", + "@sapphire/framework": "^4.5.0", + "@sapphire/plugin-logger": "^3.0.4", + "@sapphire/utilities": "^3.12.0", + "@skyra/env-utilities": "^1.2.1", + "colorette": "^2.0.20", + "discord.js": "^14.11.0", + "openai": "^3.3.0" + }, + "devDependencies": { + "@sapphire/cli": "^1.6.1", + "@sapphire/prettier-config": "^1.4.5", + "@sapphire/ts-config": "^4.0.0", + "@types/node": "^20.3.3", + "@types/ws": "^8.5.5", + "prettier": "^2.8.8", + "tsc-watch": "^6.0.4", + "typescript": "^5.1.6" + }, + "scripts": { + "sapphire": "sapphire", + "generate": "sapphire generate", + "build": "tsc", + "watch": "tsc -w", + "start": "node dist/index.js", + "dev": "npm run build && npm run start", + "watch:start": "tsc-watch --onSuccess \"npm run start\"", + "format": "prettier --write \"src/\"" + }, + "prettier": "@sapphire/prettier-config" +} diff --git a/src/commands/pic.ts b/src/commands/pic.ts index cb4e629..9511255 100644 --- a/src/commands/pic.ts +++ b/src/commands/pic.ts @@ -39,38 +39,79 @@ export class UserCommand extends Command { ? await interactionOrMessage.channel.send({ content: '🤔 Thinking... 🤔' }) : await interactionOrMessage.reply({ content: '🤔 Thinking... 🤔', fetchReply: true }); - const response = await fetch(`https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v0-9/text-to-image`, { - method: 'POST', + const creditCountResponse = await fetch(`https://api.stability.ai/v1/user/balance`, { + method: 'GET', headers: { 'Content-Type': 'application/json', - Accept: 'application/json', Authorization: `Bearer ${process.env.STABILITY_API_KEY}` - }, - body: JSON.stringify({ - text_prompts: [ - { - text: prompt - } - ], - cfg_scale: 6, - clip_guidance_preset: 'FAST_BLUE', - height: 1024, - width: 1024, - samples: 1, - steps: 32 - }) + } }); - interface GenerationResponse { - artifacts: Array<{ - base64: string; - seed: number; - finishReason: string; - }>; - } + const balance = (await creditCountResponse.json()).credits || 0; - if (!response.ok) { - const content = `Sorry! I goofed up. Please ask my maker HimbothySwaggins about what could have happened!`; + if (balance > 5) { + const imageGenResponse = await fetch(`https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v0-9/text-to-image`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + Authorization: `Bearer ${process.env.STABILITY_API_KEY}` + }, + body: JSON.stringify({ + text_prompts: [ + { + text: prompt + } + ], + cfg_scale: 6, + clip_guidance_preset: 'FAST_BLUE', + height: 1024, + width: 1024, + samples: 1, + steps: 32 + }) + }); + + interface GenerationResponse { + artifacts: Array<{ + base64: string; + seed: number; + finishReason: string; + }>; + } + + if (!imageGenResponse.ok) { + const content = `Sorry! I goofed up. Please ask my maker HimbothySwaggins about what could have happened!`; + + if (interactionOrMessage instanceof Message) { + return askMessage.edit({ content }); + } + + return interactionOrMessage.editReply({ + content: content + }); + } else { + const responseJSON = (await imageGenResponse.json()) as GenerationResponse; + const imageAttachment = new AttachmentBuilder(Buffer.from(responseJSON.artifacts[0].base64, 'base64')); + + const content = + `Prompt: ${prompt}${ + balance && + balance <= 200 && + `\n\n... also, we are now at ${balance} credits. If you'd like to help fund this command, please type "/support" for details!` + }` || 'ERROR!'; + + if (interactionOrMessage instanceof Message) { + return askMessage.edit({ content, files: [imageAttachment] }); + } + + return interactionOrMessage.editReply({ + content: content, + files: [imageAttachment] + }); + } + } else { + const content = `Oops! We're out of credits for this. If you'd like to help fund this command, please type "/support" for details!`; if (interactionOrMessage instanceof Message) { return askMessage.edit({ content }); @@ -79,20 +120,6 @@ export class UserCommand extends Command { return interactionOrMessage.editReply({ content: content }); - } else { - const responseJSON = (await response.json()) as GenerationResponse; - const imageAttachment = new AttachmentBuilder(Buffer.from(responseJSON.artifacts[0].base64, 'base64')); - - const content = `Prompt: ${prompt}` || 'ERROR!'; - - if (interactionOrMessage instanceof Message) { - return askMessage.edit({ content, files: [imageAttachment] }); - } - - return interactionOrMessage.editReply({ - content: content, - files: [imageAttachment] - }); } } }