Keeping A Channel Polls Only

I'm trying to make a specific channel polls only. Since Discord doesn't have a permission set for this I've setup my bot to detect the difference in a poll message and a regular message. Regular messages get deleted from the channel and the user a DM stating why. Polls get ignored. This has been successful. What isn't successful though is when the poll ends and Discord sends out the notification message stating the poll has ended it gets deleted as well. Is there a way to detect poll end messages too? I tried having the bot see if the messages contained specific text, but no luck.
// The channel ID you want to monitor
const pollChannelId = '1292516925494267954';

// Check if the message is a poll ending message (adjust based on your poll ending format)
function isPollEndMessage(message) {
return message.content.includes("has closed"); // Customize this if necessary
}

client.on('messageCreate', async (message) => {
// Ignore messages from bots
if (message.author.bot) return;

// Only check messages from the specified channel
if (message.channel.id === pollChannelId) {
// If it's a poll, process as normal
if (message.poll) {
// Check if the poll has more than 5 options
if (message.poll.answers && message.poll.answers.size > 4) {
try {
// Delete the poll
await message.delete();
// Notify the user via DM
await message.author.send(`Your poll was deleted because it exceeded the limit of 4 answers.`);
} catch (error) {
console.error(`Error deleting poll or sending DM: ${error}`);
}
}
return; // End execution if the message is a valid poll
}

// If the message is the poll ending message, don't delete it
if (isPollEndMessage(message)) {
return; // Do nothing if it's the poll end message
}

// If it's not a poll or an ending message, delete the message
try {
await message.delete();
await message.author.send(`Your message was deleted because ${message.channel} is for polls only.`);
} catch (error) {
console.error(`Error deleting message or sending DM: ${error}`);
}
}
});
// The channel ID you want to monitor
const pollChannelId = '1292516925494267954';

// Check if the message is a poll ending message (adjust based on your poll ending format)
function isPollEndMessage(message) {
return message.content.includes("has closed"); // Customize this if necessary
}

client.on('messageCreate', async (message) => {
// Ignore messages from bots
if (message.author.bot) return;

// Only check messages from the specified channel
if (message.channel.id === pollChannelId) {
// If it's a poll, process as normal
if (message.poll) {
// Check if the poll has more than 5 options
if (message.poll.answers && message.poll.answers.size > 4) {
try {
// Delete the poll
await message.delete();
// Notify the user via DM
await message.author.send(`Your poll was deleted because it exceeded the limit of 4 answers.`);
} catch (error) {
console.error(`Error deleting poll or sending DM: ${error}`);
}
}
return; // End execution if the message is a valid poll
}

// If the message is the poll ending message, don't delete it
if (isPollEndMessage(message)) {
return; // Do nothing if it's the poll end message
}

// If it's not a poll or an ending message, delete the message
try {
await message.delete();
await message.author.send(`Your message was deleted because ${message.channel} is for polls only.`);
} catch (error) {
console.error(`Error deleting message or sending DM: ${error}`);
}
}
});
10 Replies
d.js toolkit
d.js toolkit2mo 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!
[RS] JesseUCAVedYou [EN]
-_- and my question/code gets removed. le sigh
souji
souji2mo ago
not sure what you mean by that, it's in the post starter where it should be?
NyR
NyR2mo ago
Check also for the message type
d.js docs
d.js docs2mo ago
:dtypes: v10: MessageType - PollResult read more
NyR
NyR2mo ago
Ignore if it is of that type ^
[RS] JesseUCAVedYou [EN]
Discord must be messing up on my end then :/
No description
[RS] JesseUCAVedYou [EN]
So here I am looking like a fool lol
souji
souji2mo ago
interesting :kek: it's def there
[RS] JesseUCAVedYou [EN]
:PES_Happy: Will try. Thanks! Works wonderfully thanks to you. Will post my code here for future reference of others.
// The channel ID you want to monitor
const pollChannelId = '1292516925494267954';

client.on('messageCreate', async (message) => {
// Ignore messages from bots
if (message.author.bot) return;

// Only check messages from the specified channel
if (message.channel.id === pollChannelId) {
// If the message is a poll result (type 46), do nothing (allow it)
if (message.type === 46) {
return; // End the execution, don't delete the poll result message
}

// If the message is a poll
if (message.poll) {
// Check if the poll has more than 5 options
if (message.poll.answers && message.poll.answers.size > 5) {
try {
// Delete the message (poll)
await message.delete();
// Notify the user via DM
await message.author.send(`Your poll was deleted because it exceeded the limit of 5 answers.`);
} catch (error) {
console.error(`Error deleting poll or sending DM: ${error}`);
}
}
return; // End execution here if it's a valid poll
}

// If it's not a poll, delete the message and send a DM
try {
await message.delete();
await message.author.send(`Your message was deleted because ${message.channel} is for polls only.`);
} catch (error) {
console.error(`Error deleting message or sending DM: ${error}`);
}
}
});
// The channel ID you want to monitor
const pollChannelId = '1292516925494267954';

client.on('messageCreate', async (message) => {
// Ignore messages from bots
if (message.author.bot) return;

// Only check messages from the specified channel
if (message.channel.id === pollChannelId) {
// If the message is a poll result (type 46), do nothing (allow it)
if (message.type === 46) {
return; // End the execution, don't delete the poll result message
}

// If the message is a poll
if (message.poll) {
// Check if the poll has more than 5 options
if (message.poll.answers && message.poll.answers.size > 5) {
try {
// Delete the message (poll)
await message.delete();
// Notify the user via DM
await message.author.send(`Your poll was deleted because it exceeded the limit of 5 answers.`);
} catch (error) {
console.error(`Error deleting poll or sending DM: ${error}`);
}
}
return; // End execution here if it's a valid poll
}

// If it's not a poll, delete the message and send a DM
try {
await message.delete();
await message.author.send(`Your message was deleted because ${message.channel} is for polls only.`);
} catch (error) {
console.error(`Error deleting message or sending DM: ${error}`);
}
}
});
Want results from more Discord servers?
Add your server