Home > Software engineering >  How to make `CommandInteraction.reply()` return a `Message` using the discord API
How to make `CommandInteraction.reply()` return a `Message` using the discord API

Time:01-02

I have the following (typescript) code snippet that generates an embed in response to an interaction, and sends it:

const embed = await this.generateEmbed(...);
await interaction.reply({embeds: [embed]});
const sentMessage: Message = <Message<boolean>> await interaction.fetchReply();
await sentMessage.react('⬅');

However, this fails at runtime because TypeError: sentMessage.react is not a function.

This is somewhat explained in the discord.js docs:

Returns the raw message data if the webhook was instantiated as a WebhookClient or if the channel is uncached, otherwise a Message will be returned

However, this happens even if I cache everything:

const bot = new Client({
    makeCache: Options.cacheEverything(),
    ...
});

Also note that I'm not using a WebhookClient here.

Why is CommandInteraction.reply() returning a raw message here, and how can I make it return a full Message object, so that I can use the .react() method?

CodePudding user response:

I've done quite a bit of testing and I've come to the conclusion that this is happening because you're not requesting the GUILDS intent. I'm assuming that without the GUILDS intent, the GuildChannel cannot be cached, nor the Message, so it returns the raw data from the API.

const { Client, Intents } = require('discord.js');

const bot = new Client({
    intents: [Intents.FLAGS.GUILDS],
    ...
})
  • Related