2022-10-16 20:09:48 +02:00
|
|
|
import { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ActionRowBuilder, ButtonStyle } from 'discord.js';
|
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import os from 'node:os';
|
|
|
|
|
2022-10-16 22:58:31 +02:00
|
|
|
const { stableHordeApi, stableHordeID } = process.env;
|
2022-10-16 22:30:00 +02:00
|
|
|
|
2022-10-16 20:09:48 +02:00
|
|
|
export default {
|
|
|
|
data: new SlashCommandBuilder()
|
2022-10-17 21:30:29 +02:00
|
|
|
.setName('txt2img')
|
2022-10-17 16:24:38 +02:00
|
|
|
.setDescription('AI generated image with stable diffusion (If credit are low it may be slow)')
|
2022-10-16 20:09:48 +02:00
|
|
|
.addStringOption(option =>
|
|
|
|
option.setName('prompt')
|
|
|
|
.setDescription('What do you want the AI to generate?')
|
|
|
|
.setRequired(true)),
|
2022-10-17 21:30:29 +02:00
|
|
|
category: 'AI',
|
2022-10-16 20:09:48 +02:00
|
|
|
async execute(interaction, args, client) {
|
|
|
|
await interaction.deferReply();
|
2022-10-16 22:37:34 +02:00
|
|
|
generate(interaction, args.prompt, client);
|
2022-10-16 20:09:48 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-16 22:37:34 +02:00
|
|
|
async function generate(i, prompt, client) {
|
2022-10-16 20:09:48 +02:00
|
|
|
const body = {
|
|
|
|
prompt: prompt,
|
|
|
|
params: {
|
|
|
|
n: 1,
|
|
|
|
width: 512,
|
|
|
|
height: 512,
|
|
|
|
},
|
2022-10-17 21:32:36 +02:00
|
|
|
cfg_scale: 9,
|
2022-10-17 16:24:38 +02:00
|
|
|
use_gfpgan: true,
|
|
|
|
use_real_esrgan: true,
|
|
|
|
use_ldsr: true,
|
|
|
|
use_upscaling: true,
|
|
|
|
steps: 50,
|
|
|
|
nsfw: i.channel.nsfw ? true : false,
|
|
|
|
censor_nsfw: i.channel.nsfw ? true : false,
|
2022-10-16 20:09:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const fetchParameters = {
|
|
|
|
method: 'post',
|
|
|
|
body: JSON.stringify(body),
|
2022-10-16 22:30:00 +02:00
|
|
|
headers: { 'Content-Type': 'application/json', 'apikey': stableHordeApi },
|
2022-10-16 20:09:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let response = await fetch('https://stablehorde.net/api/v2/generate/sync', fetchParameters);
|
|
|
|
|
|
|
|
response = await response.json();
|
2022-10-16 22:58:31 +02:00
|
|
|
|
|
|
|
let creditResponse = await fetch(`https://stablehorde.net/api/v2/users/${stableHordeID}`);
|
|
|
|
creditResponse = await creditResponse.json();
|
|
|
|
|
2022-10-16 20:09:48 +02:00
|
|
|
const stableEmbed = new EmbedBuilder()
|
|
|
|
.setColor(i.member ? i.member.displayHexColor : 'Navy')
|
|
|
|
.setTitle(prompt)
|
|
|
|
.setURL('https://aqualxx.github.io/stable-ui/')
|
2022-10-16 22:58:31 +02:00
|
|
|
.setFooter({ text: `**Credit left: ${creditResponse.kudos}** Seed: ${response.generations[0].seed} worker ID: ${response.generations[0].worker_id} worker name: ${response.generations[0].worker_name}` });
|
2022-10-16 20:09:48 +02:00
|
|
|
|
|
|
|
const row = new ActionRowBuilder()
|
|
|
|
.addComponents(
|
|
|
|
new ButtonBuilder()
|
|
|
|
.setCustomId(`regenerate${i.user.id}`)
|
2022-10-16 22:37:34 +02:00
|
|
|
.setLabel('🔄 Regenerate')
|
2022-10-16 20:09:48 +02:00
|
|
|
.setStyle(ButtonStyle.Primary),
|
|
|
|
);
|
|
|
|
|
|
|
|
fs.writeFileSync(`${os.tmpdir()}/${i.id}.png`, response.generations[0].img, 'base64');
|
|
|
|
|
|
|
|
await i.editReply({ embeds: [stableEmbed], components: [row], files: [`${os.tmpdir()}/${i.id}.png`] });
|
2022-10-16 22:37:34 +02:00
|
|
|
|
|
|
|
client.once('interactionCreate', async (interactionMenu) => {
|
|
|
|
if (i.user !== interactionMenu.user) return;
|
|
|
|
if (!interactionMenu.isButton) return;
|
|
|
|
if (interactionMenu.customId === `regenerate${interactionMenu.user.id}`) {
|
|
|
|
await interactionMenu.deferReply();
|
|
|
|
await generate(interactionMenu, prompt, client);
|
|
|
|
}
|
|
|
|
});
|
2022-10-16 20:09:48 +02:00
|
|
|
}
|