-1

I am trying to make a poll for my discord bot that have the poll and the results as an embedded message. I can get the poll in plain text, but when I try to make the embeds, I get this error

TypeError: MessageEmbed is not a constructor

Here is the code I am using:

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 pretty new to coding (very new!!) and would really apperciate it if someone could update my current code to be correct but also explain why. current mood: UGH!! Thanks in advance!

I tried updating all of my packages, different variations of 'MessageEmbed', Discord.MessageEmbed' etc.

delyrious
  • 11
  • 3

1 Answers1

-1

If you're using a newer version of discord.js you need to use EmbedBuilder instead of MessageEmbed

const { EmbedBuilder } = require('discord.js');
const pollEmbed = new EmbedBuilder()
      .setColor('#0099ff')
      .setTitle('Poll')
      .setDescription(question);

options.forEach((option, index) => {
    pollEmbed.addField({ name: `Option ${index + 1}`, value: option});
});

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
NoobDev
  • 151
  • 10