Hey everyone! I’m working on a Discord bot for music playback. I’ve got a skip feature, but I’m stuck on how to stop users from voting multiple times. Here’s what I’ve got so far:
function handleSkip(message, server) {
const superSkipper = message.member.roles.cache.some(r => r.name === 'Super Skipper');
if (superSkipper) {
voteCount = 0;
skipSong(server);
return message.channel.send('Super Skipper skipped the song!');
}
voteCount++;
if (voteCount < 5) {
message.channel.send(`${5 - voteCount} more votes needed to skip.`);
} else {
voteCount = 0;
skipSong(server);
message.channel.send('Song skipped!');
}
}
function skipSong(server) {
if (server.player) {
server.player.stop();
}
}
Any ideas on how to make sure each user can only vote once? Thanks for the help!
To prevent multiple votes from a single user, you could implement a Set to keep track of users who’ve already voted. Here’s a suggested modification:
const skipVotes = new Set();
function handleSkip(message, server) {
const userId = message.author.id;
if (skipVotes.has(userId)) {
return message.reply('You have already voted to skip.');
}
skipVotes.add(userId);
const voteCount = skipVotes.size;
// Rest of your logic here
// Reset votes when song is skipped
if (voteCount >= 5) {
skipVotes.clear();
}
}
This approach ensures each user can only vote once per song. Remember to clear the Set when starting a new song or after a successful skip.
hey mate, i had a similar problem with my bot. you could use an array to store user IDs who voted. something like:
let voters = [];
function handleSkip(message, server) {
if (voters.includes(message.author.id)) {
return message.reply('u already voted dude');
}
voters.push(message.author.id);
// rest of ur code
}
dont forget to clear the array when song changes or is skipped!
I’ve dealt with a similar issue in my own Discord bot project. One approach that worked well for me was using a Map to track votes. Here’s a quick breakdown:
const skipVotes = new Map();
function handleSkip(message, server) {
const userId = message.author.id;
const currentTime = Date.now();
if (skipVotes.has(userId) && currentTime - skipVotes.get(userId) < 300000) {
return message.reply('You can only vote once every 5 minutes.');
}
skipVotes.set(userId, currentTime);
const voteCount = skipVotes.size;
// Your existing logic here
if (voteCount >= 5) {
skipVotes.clear();
skipSong(server);
}
}
This method not only prevents multiple votes but also implements a cooldown period. It’s been pretty effective in my experience, and users seem to appreciate the fairness. Just remember to clear the Map when starting a new song too.