discord imagine | | discord stability proxy | Search

The patchInteractionImageAttachment function is an asynchronous function that patches an interaction image attachment with the given prompt, image buffer, token, and content type. It returns a promise that resolves to the result of the patch request.

Run example

npm run import -- "update discord interaction with attachments"

update discord interaction with attachments

const {DEFAULT_APPLICATION} = importer.import("discord configuration")
const { safeurl } = importer.import("domain cache tools")
const mime = require('mime-types');
const {requestAuthQ} = importer.import("discord request")


async function patchInteractionImageAttachment(prompt, buff, token, content = 'image/png') {
  return await requestAuthQ({
    headers: {
      'Content-Type': 'multipart/form-data; boundary=boundary123'
    },
    method: 'PATCH',
    url: `webhooks/${DEFAULT_APPLICATION}/${token}/messages/@original`,
    body: `--boundary123
Content-Disposition: form-data; name="payload_json"
Content-Type: application/json

${JSON.stringify({
  content: 'Your ' + (content.includes('image') ? 'image' : content) + ' delivered: *' + prompt + '*',
  attachments: [{
    "id": 0,
    "description": prompt,
    "filename": safeurl(prompt) + '.' + mime.extension(content)
  }],
})}
--boundary123
Content-Disposition: form-data; name="files[0]"; filename="${safeurl(prompt)}.${mime.extension(content)}"
Content-Transfer-Encoding: base64
Content-Type: ${content}

${buff.toString('base64')}
--boundary123--`
  })
}


module.exports = {
  patchInteractionImageAttachment
}

What the code could have been:

import { DEFAULT_APPLICATION } from 'discord configuration';
import { safeUrl } from 'domain cache tools';
import mime from'mime-types';
import { requestAuthQ } from 'discord request';

/**
 * Patches an interaction image attachment with the given prompt and buffer.
 * 
 * @param {string} prompt The prompt to be displayed with the image.
 * @param {Buffer} buff The image buffer.
 * @param {string} token The token for the application.
 * @param {string} [content='image/png'] The content type of the image.
 * @returns {Promise} The response from the request.
 */
async function patchInteractionImageAttachment(prompt, buff, token, content = 'image/png') {
  // Set the content type and boundary for the request
  const contentType ='multipart/form-data; boundary=boundary123';
  const boundary = '--boundary123';

  // Define the payload JSON
  const payloadJson = {
    content: `Your ${content.includes('image')? 'image' : content} delivered: *${prompt}*`,
    attachments: [{
      id: 0,
      description: prompt,
      filename: `${safeUrl(prompt)}.${mime.extension(content)}`,
    }],
  };

  // Convert the payload to JSON string
  const payloadJsonStr = JSON.stringify(payloadJson);

  // Set the file content type and transfer encoding
  const fileContentType = content;
  const fileTransferEncoding = 'base64';

  // Convert the buff to base64 string
  const fileContent = buff.toString('base64');

  // Construct the request body
  const requestBody = `${boundary}
Content-Disposition: form-data; name="payload_json"
Content-Type: application/json

${payloadJsonStr}
${boundary}
Content-Disposition: form-data; name="files[0]"; filename="${safeUrl(prompt)}.${mime.extension(content)}"
Content-Transfer-Encoding: ${fileTransferEncoding}
Content-Type: ${fileContentType}

${fileContent}
${boundary}--`;

  // Make the request
  const response = await requestAuthQ({
    headers: {
      'Content-Type': contentType,
    },
    method: 'PATCH',
    url: `webhooks/${DEFAULT_APPLICATION}/${token}/messages/@original`,
    body: requestBody,
  });

  return response;
}

export { patchInteractionImageAttachment };

Code Breakdown

Imports

  • DEFAULT_APPLICATION and requestAuthQ are imported from discord configuration and discord request respectively.
  • safeurl is imported from domain cache tools.
  • mime is required from mime-types.

Function patchInteractionImageAttachment

  • An asynchronous function that patches an interaction image attachment.
  • Parameters:
    • prompt: The prompt for the image attachment.
    • buff: The buffer of the image attachment.
    • token: The token for the interaction.
    • content: The content type of the image attachment (default: 'image/png').
  • Returns: A promise that resolves to the result of the patch request.

Request Body

  • The function constructs a multipart/form-data request body with the following parts:
    • payload_json: A JSON object containing the content and attachment information.
    • files[0]: The base64 encoded image attachment buffer.

Module Exports

  • The function patchInteractionImageAttachment is exported as a module.

Example Usage

const { patchInteractionImageAttachment } = require('./patchInteractionImageAttachment');

const prompt = 'Example Image';
const buff = Buffer.from('image data', 'utf8');
const token = 'interaction token';
const content = 'image/png';

patchInteractionImageAttachment(prompt, buff, token, content)
 .then((result) => {
    console.log(result);
  })
 .catch((error) => {
    console.error(error);
  });