pik
DIAdiscord.js - Imagine an app
•Created by pik on 8/22/2024 in #djs-questions
Message collector sending an error when two people send a message at the same time
It's just a simple bot i'm making for a friend, so there will be only one active at a time
7 replies
DIAdiscord.js - Imagine an app
•Created by pik on 8/22/2024 in #djs-questions
Message collector sending an error when two people send a message at the same time
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();
}
7 replies