Skip to content

Instantly share code, notes, and snippets.

@pylixonly
Created December 18, 2022 13:53
Show Gist options
  • Save pylixonly/16b400aeeefb1ed23f3f90e7a8a36199 to your computer and use it in GitHub Desktop.
Save pylixonly/16b400aeeefb1ed23f3f90e7a8a36199 to your computer and use it in GitHub Desktop.
Upload static image to Discord CDN through webhooks
// 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