I'm attempting to create a poll feature for my discord bot that displays both the poll question and results as an embedded message. While I've managed to get the poll information in plain text format, I'm encountering an error when trying to generate embeds.
TypeError: MessageEmbed is not a constructor
The following is the code snippet I'm currently working with:
const { SlashCommandBuilder } = require('discord.js');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('poll')
.setDescription('Create a poll')
.addStringOption(option =>
option
.setName('question')
.setDescription('The poll question')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('options')
.setDescription('The poll options, separated by commas')
.setRequired(true)
)
.addIntegerOption(option =>
option
.setName('duration')
.setDescription('The duration of the poll in seconds')
.setRequired(true)
),
run: async ({ interaction, client }) => {
const question = interaction.options.getString('question');
const options = interaction.options.getString('options').split(',');
const duration = interaction.options.getInteger('duration');
const pollEmbed = new MessageEmbed()
.setColor('#0099ff')
.setTitle('Poll')
.setDescription(question);
options.forEach((option, index) => {
pollEmbed.addField(`Option ${index + 1}`, option);
});
await interaction.reply({ embeds: [pollEmbed] });
setTimeout(async () => {
const channel = client.channels.cache.get(interaction.channelId);
const sentMessages = await channel.messages.fetch({ limit: 1 });
const sentMessage = sentMessages.first();
sentMessage.reactions.cache.forEach(async reaction => {
const optionIndex = options.findIndex(option => option === reaction.emoji.name);
if (optionIndex !== -1) {
const users = await reaction.users.fetch();
const nonBotUsers = users.filter(user => !user.bot);
const userTags = nonBotUsers.map(user => user.tag).join(', ');
channel.send(`Option ${optionIndex + 1} received ${nonBotUsers.size} vote(s) from: ${userTags}`);
}
});
}, duration * 1000);
},
};
I'm relatively new to coding, so any assistance in rectifying my current code and providing explanations would be greatly appreciated! Feeling frustrated right now but hopeful for a solution soon!
I've experimented with updating all my packages and exploring different variations of 'MessageEmbed', 'Discord.MessageEmbed', etc.