Skip to content

Commit

Permalink
Add: comments for bufferHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthedang committed Sep 19, 2024
1 parent e8498f3 commit c8e58fd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/handlers/bufferHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Attachment } from "discord.js"

/**
* Method to convert a Discord attachment url to an array buffer
*
* @param url Discord Attachment Url
* @returns array buffer from Attachment Url
*/
async function getAttachmentBuffer(url: string): Promise<ArrayBuffer> {
// Get the data from the image
const response = await fetch(url)
Expand All @@ -12,6 +18,12 @@ async function getAttachmentBuffer(url: string): Promise<ArrayBuffer> {
return await response.arrayBuffer()
}

/**
* Method to convert an array buffer to a Base64 String
*
* @param buffer Array Buffer from attachment
* @returns converted Base64 string
*/
function arrayBufferToBase64(buffer: ArrayBuffer): string {
// Converting to Uint8Array
const uint8Array = new Uint8Array(buffer)
Expand All @@ -25,6 +37,12 @@ function arrayBufferToBase64(buffer: ArrayBuffer): string {
return Buffer.from(binary, 'binary').toString('base64')
}

/**
* Method to retrieve the Base64 Array of provided Message Attachment
*
* @param attachment Message Attachment from Discord
* @returns Base64 string array
*/
export async function getAttachmentData(attachment: Attachment | undefined): Promise<string[]> {
const url: string = attachment !== undefined ? attachment.url : "Missing Url"

Expand Down

0 comments on commit c8e58fd

Please sign in to comment.