CYPH ERZZ
CYPH ERZZ
DIAdiscord.js - Imagine a boo! 👻
Created by CYPH ERZZ on 6/21/2024 in #djs-questions
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
17 replies
DIAdiscord.js - Imagine a boo! 👻
Created by CYPH ERZZ on 4/26/2024 in #djs-questions
Opening handshake timeout
Hi, I'm testing my bot and i make reboots often. I have and Opening handshake has timed out when trying to launch client.login(<botToken>) method, not sure if it comes from me or the API for hitting rate limit or whatever. Resetting bot token does not help. Currently I emptied my node_modules folder to reinstall from fresh. Versions - discord.js : v14.14.1 - node : v20.10.0 Can someone help on this ? Some details below.
16 replies