SirShadowShark
SirShadowShark
DIAdiscord.js - Imagine a boo! 👻
Created by SirShadowShark on 2/11/2024 in #djs-questions
403 Error due to new image expiration?
Hey all, I'm unfortunately having issues with a ticket transcript system that I created for my discord bot. I've done some researching and I've come to the conclusion that I may not have access to the content I'm trying to fetch, or I've been flagged and limited. What boggles my mind is that I never had an issue in the past 3-4 months with this approach, but I am now, so I'm more so leaning towards I'm being limited, or not requesting an image correctly for discord. I will admit it is rather primitive on how I made this function, so, apologies. Here is my code that seems to be causing my issue, as well as an abbreviated error code that I receive when calling it. I did do some more digging and found that there was a new discord image expiration, or link / attachment expiration due to not wanting malware to be spread. Currently I am unsure how to request this new type of format with a bot. fetchImage:
async function fetchImage(url) {
try {
console.log(`Attempting to fetch image from URL: ${url}`);
const response = await axios.get(url, {
responseType: 'arraybuffer',
headers: {
'User-Agent': 'RemovedforPrivacy/1.0',
'Authorization': `Bot ${botToken}` //try bot token for auth, no work.
}
});
console.log(`Image fetched successfully: ${url}`);
return Buffer.from(response.data, 'binary');
} catch (error) {
console.error(`Error fetching image from URL: ${url}`, error);

//Retry once after a delay if it's a 5xx server error
if (error.response && error.response.status >= 500) {
console.log(`Retrying image fetch for URL: ${url} after delay...`);
await delay(1000); // Wait for 1 second before retrying
try {
const retryResponse = await axios.get(url, {
responseType: 'arraybuffer',
headers: {
'Authorization': `Bot ${botToken}` //did not work.
}
});
console.log(`Image fetched successfully after retry: ${url}`);
return Buffer.from(retryResponse.data, 'binary');
} catch (retryError) {
console.error(`Retry failed for image URL: ${url}`, retryError);
return null;
}
} else {
return null;
}
}
}
async function fetchImage(url) {
try {
console.log(`Attempting to fetch image from URL: ${url}`);
const response = await axios.get(url, {
responseType: 'arraybuffer',
headers: {
'User-Agent': 'RemovedforPrivacy/1.0',
'Authorization': `Bot ${botToken}` //try bot token for auth, no work.
}
});
console.log(`Image fetched successfully: ${url}`);
return Buffer.from(response.data, 'binary');
} catch (error) {
console.error(`Error fetching image from URL: ${url}`, error);

//Retry once after a delay if it's a 5xx server error
if (error.response && error.response.status >= 500) {
console.log(`Retrying image fetch for URL: ${url} after delay...`);
await delay(1000); // Wait for 1 second before retrying
try {
const retryResponse = await axios.get(url, {
responseType: 'arraybuffer',
headers: {
'Authorization': `Bot ${botToken}` //did not work.
}
});
console.log(`Image fetched successfully after retry: ${url}`);
return Buffer.from(retryResponse.data, 'binary');
} catch (retryError) {
console.error(`Retry failed for image URL: ${url}`, retryError);
return null;
}
} else {
return null;
}
}
}
little annoying error: Error fetching image from URL: (dicsord image url) AxiosError: Request failed with status code 403 code: 'ERR_BAD_REQUEST', I'm fairly certain I've overlooked something rather simple, so apologies in advance, but any and all help is greatly appreciated! 😄
16 replies