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! 😄
12 Replies
d.js toolkit
d.js toolkit•10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - ✅ Marked as resolved by OP
SirShadowShark
SirShadowSharkOP•10mo ago
Discord.js: 14.14.1 Node: 19.0.0
monbrey
monbrey•10mo ago
403 is the Forbidden code so yeah, you dont have access
SirShadowShark
SirShadowSharkOP•10mo ago
Would this just be caused due to limiting? Or is it due to the new way images are handled?
monbrey
monbrey•10mo ago
What limiting are you referring to? Its likely the expiration
SirShadowShark
SirShadowSharkOP•10mo ago
That's what I was thinking, I was maybe thinkning I could've been rate limited, but I wasn't sure, so definitely looking for guidance.
SirShadowShark
SirShadowSharkOP•10mo ago
https://sourceb.in/s8ArU393Mf That's the entire class, for reference.
SourceBin
ticketclose class.
Instantly share your code with the world.
SirShadowShark
SirShadowSharkOP•10mo ago
Greatly apologize for the spaghetti code, just been trying to do fixes.
monbrey
monbrey•10mo ago
Ratelimiting is a 429, not a 403
SirShadowShark
SirShadowSharkOP•10mo ago
Gothca, 403 is just strictly forbidden access, right? I took the link that it returned and I put it in my browser and it gave me the This content is no longer available, I put it in discord and came up just fine Also, I did try and check the discord.js website, I couldnt find anything on the new expiration, so if there's docs on it, I'd happily take a look.
duck
duck•10mo ago
I mean there isn't really anything for us to document, since this is just how discord behaves as mentioned in #djs-help-v14, you'll want to fetch the message itself if you're trying to access it programmatically as for why it works if you send it in discord, https://discord.com/channels/222078108977594368/992166350640386090/1157372621764182058 mentions that links in message content will automatically be updated
SirShadowShark
SirShadowSharkOP•10mo ago
I think I understand, thank you Duck, I apologize I about #djs-help-v14, wasn't as clear as I could've been. I'm going to try and change the way I grab images to instead get the message, not the direct url. Previously, I thought that was the only feasible way to do so. I'll give it a different go 🙂
Want results from more Discord servers?
Add your server