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.
npm run import -- "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
}
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
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
.patchInteractionImageAttachment
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').payload_json
: A JSON object containing the content and attachment information.files[0]
: The base64 encoded image attachment buffer.patchInteractionImageAttachment
is exported as a module.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);
});