Search code examples
discorddiscord.jsdiscord-buttons

Discord.js v14 Sending Message in Separate Channel Not Working


I am creating a command, where on a button click it is supposed to send a message in a specific channel.

To achieve this, I have used the code given in the discord.js documentations faq;

const channel = client.channels.cache.get('id');
channel.send('content');

However, on clicking the button, the console gives the following error;

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

I'm not very well versed at discord.js whatsoever, so I have no idea what to do.

Here's the full code for the button interaction;

   const filter = i => i.customId === 'accept' && i.member.roles.cache.has('1111582370265563200');
    const collector = interaction.channel.createMessageComponentCollector({ filter });
    collector.on('collect', async i => {
        if (i.customId === 'accept'){
         accept.setDisabled(true)
            const channel = client.channels.cache.get('1111616693358301237');
            channel.send({embeds: [embedAccept]});
         i.update({embeds: [yesembed]});
        }
    });

Any help is appreciated. Thanks in advance.


Solution

  • The issue is that you're trying to access the channels property of an undefined client.

    For slash commands, you can access your client instance by using interaction.client.

    const channel = interaction.client.channels.cache.get('1111616693358301237');