Skip to content

Commit

Permalink
✨ custom preview file format & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ImDarkTom committed Jan 13, 2024
1 parent f713269 commit 07b184b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 22 deletions.
7 changes: 5 additions & 2 deletions botConfig.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"devs": [
"youruserid"
],
"showImageAuthor": true,
"progressUpdateIntervalMs": 2500
"generation": {
"showImageAuthor": true,
"progressUpdateIntervalMs": 2500,
"imagePreviewFormat": "jpg"
}
}
11 changes: 6 additions & 5 deletions src/utils/SD/base64ToBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
const sharp = require('sharp');

/**
*
* @param {String} encoded Base64 encoded image string.
* Turns a Base64 encoded image into a buffer.
* @param {String} encodedString Base64 encoded image string.
* @param {keyof sharp.FormatEnum} resultFormat The image format to return the buffer as, default png.
* @returns {Promise<Buffer>}
*/
module.exports = async (encoded) => {
const buffer = Buffer.from(encoded, 'base64');
module.exports = async (encodedString, resultFormat = "png") => {
const buffer = Buffer.from(encodedString, 'base64');

const imagePromise = sharp(buffer).png().toBuffer();
const imagePromise = sharp(buffer).toFormat(resultFormat).toBuffer();

return imagePromise;
};
4 changes: 3 additions & 1 deletion src/utils/SD/createImageEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ module.exports = async (data, settings = {upscaleBtn: true, redoBtn: false, addi
.setFooter({ text: imageParams.infotexts[0].match(/Model: ([^,]+)/)[1] })
.setColor(cancelled ? "#bb0000" : "#00bb00")

if (botConfig.showImageAuthor) { embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`}); }
if (botConfig.generation.showImageAuthor) {
embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`});
}

const row = new ActionRowBuilder()

Expand Down
17 changes: 12 additions & 5 deletions src/utils/SD/getProgressEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = async (user, context = "", addCancelButton = true) => {
.setColor('Yellow')
.setTitle('Starting...')

if (botConfig.showImageAuthor) { embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`}); }
if (botConfig.generation.showImageAuthor) {
embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`});
}

return {content: "", embeds: [embed]};
}
Expand All @@ -37,17 +39,22 @@ module.exports = async (user, context = "", addCancelButton = true) => {
const progress = Math.round(data.progress*100);
const progressBarCompletion = Math.ceil(progress/10);

const imageBuffer = await base64ToBuffer(data.current_image);
const imagePreviewFormat = botConfig.generation.imagePreviewFormat;
const previewImageFilename = 'progress.' + imagePreviewFormat;

const imageBuffer = await base64ToBuffer(data.current_image, imagePreviewFormat);

const image = new AttachmentBuilder(imageBuffer, {name: 'progress.png'});
const image = new AttachmentBuilder(imageBuffer, {name: previewImageFilename});

const embed = new EmbedBuilder()
.setTitle(`${context}${progress}%\n|${"▓".repeat(progressBarCompletion)}${"░".repeat(10 - progressBarCompletion)}|`)
.setFooter({text: `ETA: ${data.eta_relative.toFixed(1)}s`})
.setImage('attachment://progress.png')
.setImage(`attachment://${previewImageFilename}`)
.setColor("Yellow")

if (botConfig.showImageAuthor) { embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`}); }
if (botConfig.generation.showImageAuthor) {
embed.setAuthor({name: user.username, iconURL: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}?size=256`});
}

//Buttons
const row = new ActionRowBuilder();
Expand Down
4 changes: 2 additions & 2 deletions src/utils/SD/progressUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const { ChatInputCommandInteraction, ButtonInteraction } = require("discord.js")
module.exports = async (imagePromise, interaction) => {
let interval;

if (botConfig.progressUpdateIntervalMs !== -1) {
if (botConfig.generation.progressUpdateIntervalMs !== -1) {
interval = setInterval( async () => {

interaction.editReply(await getProgressEmbed(interaction.user, "Generating... ", true));

}, botConfig.progressUpdateIntervalMs);
}, botConfig.generation.progressUpdateIntervalMs);
}

const imageData = await imagePromise;
Expand Down
20 changes: 13 additions & 7 deletions src/utils/SD/sendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ const { baseUrl, port } = require('../../../sdConfig.json');
const axios = require('axios');

/**
* Make a request to the SD API.
*
* @param {string} path API URL path e.g 'sdapi/v1/txt2img'
* @param {object} data Data to be sent with request.
* @param {import('axios').Method} method
* @returns {Promise<object>}
*/
module.exports = async (path, data = {}, method = "post") => {
const response = await axios({
method: method,
url: `${baseUrl}:${port}/${path}`,
data: data
});

return response.data;
try {
const response = await axios({
method: method,
url: `${baseUrl}:${port}/${path}`,
data: data
});

return response.data;
} catch (error) {
console.error(`Request to ${path} failed:`, error.message);
return null;
}
};

0 comments on commit 07b184b

Please sign in to comment.