Message collector sending an error when two people send a message at the same time

When two people send a message at the exact same time I get an "unknow message" error, is there any way to prevent that ?
No description
5 Replies
d.js toolkit
d.js toolkit5mo 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!
pik
pikOP5mo ago
My code :
if (interaction.commandName === "start2") {
const player = interaction.options.get('player').value;
const time = interaction.options.get('time').value;
const startingPrice = interaction.options.get('starting_price').value;

let currentPrice = startingPrice;
let lastBidder = null;
let countdown = time;
let countdownInterval = null;
let countdownMessage = null;
let collector = null;

await interaction.reply(`Auction started for **${player}**!\nStarting price: **${startingPrice}**`);

const startCountdown = async () => {
if (countdownInterval) {
clearInterval(countdownInterval);
}

if (countdownMessage) {
await countdownMessage.delete();
}

countdown = time;

countdownMessage = await interaction.channel.send(`Time left: ${countdown} seconds`);

countdownInterval = setInterval(async () => {
if (countdown > 0) {
countdown--;
await countdownMessage.edit(`Time left: ${countdown} seconds`);
} else {
clearInterval(countdownInterval);
collector.stop();
await interaction.channel.send(`Auction ended! Final price: **${currentPrice}** by **${lastBidder || "No one"}**`);
}
}, 1000);
};

const startCollector = () => {
if (collector) {
collector.stop();
}

const filter = (message) => !isNaN(message.content) && message.content.trim().length > 0;
collector = interaction.channel.createMessageCollector({ filter, time: time * 1000 });

collector.on('collect', async (message) => {
const newPrice = parseInt(message.content.trim(), 10);

if (newPrice <= currentPrice) {
return;
}

currentPrice = newPrice;
lastBidder = message.author.username;

await interaction.channel.send(`New bid by **${lastBidder}**: **${currentPrice}**`);

await startCountdown();
startCollector();
});
};

await startCountdown();
startCollector();
}
if (interaction.commandName === "start2") {
const player = interaction.options.get('player').value;
const time = interaction.options.get('time').value;
const startingPrice = interaction.options.get('starting_price').value;

let currentPrice = startingPrice;
let lastBidder = null;
let countdown = time;
let countdownInterval = null;
let countdownMessage = null;
let collector = null;

await interaction.reply(`Auction started for **${player}**!\nStarting price: **${startingPrice}**`);

const startCountdown = async () => {
if (countdownInterval) {
clearInterval(countdownInterval);
}

if (countdownMessage) {
await countdownMessage.delete();
}

countdown = time;

countdownMessage = await interaction.channel.send(`Time left: ${countdown} seconds`);

countdownInterval = setInterval(async () => {
if (countdown > 0) {
countdown--;
await countdownMessage.edit(`Time left: ${countdown} seconds`);
} else {
clearInterval(countdownInterval);
collector.stop();
await interaction.channel.send(`Auction ended! Final price: **${currentPrice}** by **${lastBidder || "No one"}**`);
}
}, 1000);
};

const startCollector = () => {
if (collector) {
collector.stop();
}

const filter = (message) => !isNaN(message.content) && message.content.trim().length > 0;
collector = interaction.channel.createMessageCollector({ filter, time: time * 1000 });

collector.on('collect', async (message) => {
const newPrice = parseInt(message.content.trim(), 10);

if (newPrice <= currentPrice) {
return;
}

currentPrice = newPrice;
lastBidder = message.author.username;

await interaction.channel.send(`New bid by **${lastBidder}**: **${currentPrice}**`);

await startCountdown();
startCollector();
});
};

await startCountdown();
startCollector();
}
Syjalo
Syjalo5mo ago
How do you know to which auction the user is betting to if there're 2 active ones in the channel? You need to separate your auctions. Eg with threads. And for time you can use Discord timestamps <t:1724323611:R>
d.js docs
d.js docs5mo ago
:discord: Reference: API Reference - Message Formatting read more
pik
pikOP5mo ago
It's just a simple bot i'm making for a friend, so there will be only one active at a time
Want results from more Discord servers?
Add your server