Created
December 18, 2022 13:53
-
-
Save pylixonly/16b400aeeefb1ed23f3f90e7a8a36199 to your computer and use it in GitHub Desktop.
Upload static image to Discord CDN through webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// upload images on discord to use through webhook avatar | |
// inb4 used to load images on discord but now I use application | |
async uploadExternalImagesFromWebhook(link: string, webhookUrl: string) { | |
const params = { | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ | |
avatar_url: link, | |
content: `<${link}>` | |
}), | |
method: "POST" | |
}; | |
let response = await fetch(`${webhookUrl}?wait=true`, params).then(res => res.json()); | |
let messsageId = response.webhook_id; | |
let avatarHash = response.author.avatar; | |
if (!avatarHash) { // might do a while loop here until xx retries | |
this.logger.warn("Avatar hash is null, likely still unloaded, attempting to fetch it again.."); | |
response = await fetch(`${webhookUrl}/messages/${response.id}`).then(res => res.json()); | |
avatarHash = response.author.avatar; | |
} | |
// you can delete the webhook here ig | |
return `mp:avatars/${messsageId}/${avatarHash}.png`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment