Sending attachment with message edit

Hi guys, I need a hand i'm struggling af. I took a 2-4 month old code, added webpack and update libs, what worked before is now crashing. I have a message that i edit regulary. Getting information to send in the message
export async function updateMessage(initialInteractionId) {
const game = selectGameById(store.getState(), initialInteractionId);
if (!game) return;
const round = selectCurrentRound(store.getState(), initialInteractionId);

const timeLeft = Math.max(0, Math.ceil((round.startTime + game.timeout * 1000 - Date.now()) / 1000));
store.dispatch(updateRoundTimeLeft({ interactionId: initialInteractionId, timeLeft: timeLeft }));
let messageContent = {
content: timeLeft > 0 ?
`## 🚦 Round ${game.rounds.length} ⏳ **${timeLeft}** seconds remaining... ⏳\n\nπŸ”₯ *Type the sentence shown in the image.*` :
`## 🚦 Round ${game.rounds.length}\n\n🏁 **Time's up!** 🏁`
};
if(timeLeft <= 0){
collectors[initialInteractionId].stop();
}

let embeds = [];
if (Object.keys(round.userAttempts).length > 0) {
embeds.push(buildAttemptsEmbed(round.userAttempts));
}
if (round.ended) {
embeds.push(buildRoundResultsEmbed(round.raceResults));
}
messageContent.embeds = embeds;

// Conditionally add the button only if the round has not ended
if (!round.ended) {
const button = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId(`show_last_attempt|${game.interactionData.id}`)
.setLabel('πŸ‘€ Last Attempt')
.setStyle(ButtonStyle.Secondary)
);
messageContent.components = [button];
} else {
// Explicitly set components to an empty array to ensure they are removed when the round ends
messageContent.components = [];
}
const attachment = await getMessageAttachment(initialInteractionId);
if (attachment){
messageContent.files = [attachment];
}

try {
await sendFollowUpMessage(initialInteractionId, messageContent);
} catch (error) {
console.error('Error sending follow-up message:', error);
}

}
export async function updateMessage(initialInteractionId) {
const game = selectGameById(store.getState(), initialInteractionId);
if (!game) return;
const round = selectCurrentRound(store.getState(), initialInteractionId);

const timeLeft = Math.max(0, Math.ceil((round.startTime + game.timeout * 1000 - Date.now()) / 1000));
store.dispatch(updateRoundTimeLeft({ interactionId: initialInteractionId, timeLeft: timeLeft }));
let messageContent = {
content: timeLeft > 0 ?
`## 🚦 Round ${game.rounds.length} ⏳ **${timeLeft}** seconds remaining... ⏳\n\nπŸ”₯ *Type the sentence shown in the image.*` :
`## 🚦 Round ${game.rounds.length}\n\n🏁 **Time's up!** 🏁`
};
if(timeLeft <= 0){
collectors[initialInteractionId].stop();
}

let embeds = [];
if (Object.keys(round.userAttempts).length > 0) {
embeds.push(buildAttemptsEmbed(round.userAttempts));
}
if (round.ended) {
embeds.push(buildRoundResultsEmbed(round.raceResults));
}
messageContent.embeds = embeds;

// Conditionally add the button only if the round has not ended
if (!round.ended) {
const button = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId(`show_last_attempt|${game.interactionData.id}`)
.setLabel('πŸ‘€ Last Attempt')
.setStyle(ButtonStyle.Secondary)
);
messageContent.components = [button];
} else {
// Explicitly set components to an empty array to ensure they are removed when the round ends
messageContent.components = [];
}
const attachment = await getMessageAttachment(initialInteractionId);
if (attachment){
messageContent.files = [attachment];
}

try {
await sendFollowUpMessage(initialInteractionId, messageContent);
} catch (error) {
console.error('Error sending follow-up message:', error);
}

}
Sending/Editing the message :
export async function sendFollowUpMessage(initialInteractionId, messageOptions) {
const messageData = await selectRoundMessage(store.getState(), initialInteractionId);

if (messageData == null){
const channel = selectGameChannel(store.getState(),initialInteractionId);
// Send a new message in the game.interactionData.channelId
const sentMessage = await channel.send(messageOptions);
store.dispatch(updateRoundMessage({
interactionId: initialInteractionId,
messageId : sentMessage.id,
channelId: sentMessage.channelId
}));
}
else{
const message = await getMessage(messageData);

try {
await message.edit(messageOptions);
} catch (error) {
console.error('Error editing message:', error);
}
}
}
export async function sendFollowUpMessage(initialInteractionId, messageOptions) {
const messageData = await selectRoundMessage(store.getState(), initialInteractionId);

if (messageData == null){
const channel = selectGameChannel(store.getState(),initialInteractionId);
// Send a new message in the game.interactionData.channelId
const sentMessage = await channel.send(messageOptions);
store.dispatch(updateRoundMessage({
interactionId: initialInteractionId,
messageId : sentMessage.id,
channelId: sentMessage.channelId
}));
}
else{
const message = await getMessage(messageData);

try {
await message.edit(messageOptions);
} catch (error) {
console.error('Error editing message:', error);
}
}
}
Retrieving the attachment
async function getMessageAttachment(initialInteractionId){
const messageData = await selectRoundMessage(store.getState(), initialInteractionId);
if(messageData == null) return null;

const message = await getMessage(messageData);

//filter attachments by name
const attachment = message.attachments.filter(attachment => attachment.name === "race-text.png").first();
return attachment;
}
async function getMessageAttachment(initialInteractionId){
const messageData = await selectRoundMessage(store.getState(), initialInteractionId);
if(messageData == null) return null;

const message = await getMessage(messageData);

//filter attachments by name
const attachment = message.attachments.filter(attachment => attachment.name === "race-text.png").first();
return attachment;
}
I'm using redux for state managment i don't think the problem is coming from there.
node v18.1.0
node v18.1.0
13 Replies
d.js toolkit
d.js toolkitβ€’5mo 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
CYPH ERZZ
CYPH ERZZβ€’5mo ago
Here's the stack trace :
arg1: TypeError: terminated
at Fetch.onAborted (C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:2034:49)
at Fetch.emit (node:events:527:28)
at Fetch.terminate (C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:93:10)
at C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:506:30
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: terminated
at Fetch.onAborted …ions (node:internal/process/task_queues:95:5)', message: 'terminated', cause: TypeError [ERR_INVALID_ARG_TYPE]: The "stream"…t must be an instance of Stream. Received an …}
code: 'ERR_INVALID_ARG_TYPE'
message: 'The "stream" argument must be an instance of Stream. Received an instance of ReadableStream'
stack: 'TypeError [ERR_INVALID_ARG_TYPE]: The "stream" argument must be an instance of Stream. Received an instance of ReadableStream
at new NodeError (node:internal/errors:377:5)
at eos (node:internal/streams/end-of-stream:62:11)
at fetchFinale (C:\\Dev\\CryptoGamersMinigames\
ode_modules\\undici\\lib\\web\\fetch\\index.js:1093:5)
at mainFetch (C:\\Dev\\CryptoGamersMinigames\
ode_modules\\undici\\lib\\web\\fetch\\index.js:760:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)'
arg1: TypeError: terminated
at Fetch.onAborted (C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:2034:49)
at Fetch.emit (node:events:527:28)
at Fetch.terminate (C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:93:10)
at C:\Dev\CryptoGamersMinigames
ode_modules\undici\lib\web\fetch\index.js:506:30
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {stack: 'TypeError: terminated
at Fetch.onAborted …ions (node:internal/process/task_queues:95:5)', message: 'terminated', cause: TypeError [ERR_INVALID_ARG_TYPE]: The "stream"…t must be an instance of Stream. Received an …}
code: 'ERR_INVALID_ARG_TYPE'
message: 'The "stream" argument must be an instance of Stream. Received an instance of ReadableStream'
stack: 'TypeError [ERR_INVALID_ARG_TYPE]: The "stream" argument must be an instance of Stream. Received an instance of ReadableStream
at new NodeError (node:internal/errors:377:5)
at eos (node:internal/streams/end-of-stream:62:11)
at fetchFinale (C:\\Dev\\CryptoGamersMinigames\
ode_modules\\undici\\lib\\web\\fetch\\index.js:1093:5)
at mainFetch (C:\\Dev\\CryptoGamersMinigames\
ode_modules\\undici\\lib\\web\\fetch\\index.js:760:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)'
I guess it's coming from the attachment being resent, never had this problem months ago, and the attachment is sent correctly the first time...
Unknown User
Unknown Userβ€’5mo ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown Userβ€’5mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docsβ€’5mo ago
AbortError: The user aborted a request. A request took longer than the specified timeout (15 seconds default), and was aborted to not lock up the request handler. - This can be caused by an internal server error on Discord's side, or just a slow connection. - In case of a slow connection, the timeout option in RESTOptions can be increased to prevent future AbortErrors.
Unknown User
Unknown Userβ€’5mo ago
Message Not Public
Sign In & Join Server To View
CYPH ERZZ
CYPH ERZZβ€’5mo ago
i'm updating. You're so damn right ! i wasn't on the same pc neither and checking the production server it's already in 21.6.2
Unknown User
Unknown Userβ€’5mo ago
Message Not Public
Sign In & Join Server To View
CYPH ERZZ
CYPH ERZZβ€’5mo ago
trying ahah MAN.
CYPH ERZZ
CYPH ERZZβ€’5mo ago
Thanks a lot ! ❀️
Unknown User
Unknown Userβ€’5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server