Search code examples
discord.js

How to make discord bot react to it's own reply?


I was trying to make my bot to react an emoji to it's own reply. so I tried

    const message = interaction.reply({
      ephemeral: true,
      content: reply,
      fetchReply: true,
    })
    message.react('👍🏽')

But message has type 'Promise<APIMessage | Message>', and method react doesn't exist on type 'APIMessage'.

Tried the exact same code on discord.js guide, still the same error.

Is there any way to solve this?

Thanks!

Full code:

Property 'react' does not exist on type 'APIMessage | Message<boolean>'.
  Property 'react' does not exist on type 'APIMessage'.

Here's my code:

import { Message } from 'discord.js'
import { ICommand } from 'wokcommands'

export default {
  category: 'Testing',
  description: 'Replies with pong', // Required for slash commands

  slash: true, // Create both a slash and legacy command
  testOnly: false, // Only register a slash command for the testing guilds

  // if interaction.reply didn't await would cause error
  callback: async ({ interaction, message }) => {
    const reply = 'pong'

    // interaction is provided for slash commands
    const replyMessage = await interaction.reply({
      content: reply,
      fetchReply: true,
    })

    replyMessage.react('👍🏽')
  },
} as ICommand


Solution

  • So after researching for hours, I found that my actual question is how to narrow down variable type to the type that has the method 'react'. In this case it's message. So changing the code to this works.

        const replyMessage = await interaction.reply({
          content: reply,
          fetchReply: true,
        })
        if (replyMessage instanceof Message) {
          replyMessage.react('👋🏼')
        }
    

    This article has pretty detailed information about type guard. FYI https://rangle.io/blog/how-to-use-typescript-type-guards/