Luke
Luke

Reputation: 81

Discord Js - Bot can't react to message

I want to make a poll command, where the bot send an embed with the poll and afterwards reacts to it with some emojis. I added the reaction Intent to my index.js but still get an error everytime I call the command. This is my code:

const { SlashCommandBuilder } = require('@discordjs/builders')
const { MessageEmbed } = require('discord.js')

module.exports = {
    data: new SlashCommandBuilder()
        .setName("poll")
        .setDescription("Create a quick poll.")
        .addStringOption(option => option.setName("question").setDescription("The question").setRequired(true)),
    async execute(interaction) {
        const question = interaction.options.getString("question")
        const message = await interaction.reply({embeds: [
            new MessageEmbed()
                .setColor('GREEN')
                .setTitle(`Discord Poll`)
                .setAuthor(`Requested by ${interaction.member.user.tag}`, interaction.member.user.avatarURL())
                .setDescription(question)
                .setThumbnail('https://pngimg.com/uploads/question_mark/question_mark_PNG126.png')
                .setTimestamp()
                .setFooter('Bot by Iuke#4681', 'https://cdn.discordapp.com/emojis/912052946592739408.png?size=96')
        ]})
        message.react(':check:')
        .then(() => message.react(':neutral:')
        .then(() => message.react(':xmark:')))
    }
}

And get this error:

TypeError: Cannot read properties of undefined (reading 'react')

Upvotes: 1

Views: 831

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23189

You will need to fetch the reply. You can use the fetchReply option and set it to true.

module.exports = {
  data: new SlashCommandBuilder()
    .setName('poll')
    .setDescription('Create a quick poll.')
    .addStringOption((option) =>
      option
        .setName('question')
        .setDescription('The question')
        .setRequired(true),
    ),
  async execute(interaction) {
    const question = interaction.options.getString('question');
    const message = await interaction.reply({
      embeds: [
        new MessageEmbed()
          .setColor('GREEN')
          .setTitle(`Discord Poll`)
          .setAuthor(
            `Requested by ${interaction.member.user.tag}`,
            interaction.member.user.avatarURL(),
          )
          .setDescription(question)
          .setThumbnail(
            'https://pngimg.com/uploads/question_mark/question_mark_PNG126.png',
          )
          .setTimestamp()
          .setFooter(
            'Bot by Iuke#4681',
            'https://cdn.discordapp.com/emojis/912052946592739408.png?size=96',
          ),
      ],
      fetchReply: true,
    });

    // you could use await to react in sequence instead of then()s
    await message.react(':check:');
    await message.react(':neutral:');
    await message.react(':xmark:');
  },
};

Upvotes: 2

Turtlepaw
Turtlepaw

Reputation: 595

According to the docs you need to add the fetchReply option on interaction.reply like this:

const message = await interaction.reply({ content: 'Pong!', fetchReply: true });

Upvotes: 1

Related Questions