Ryuujo
Ryuujo

Reputation: 623

How to send discord message to another channel? (Discord JS)

So the flow is, I send !beep command to #general channel, then I want bot send a reply to #announcement-channel so let's assume that my channel ID is 668*****8*********. But when I tried to find the channel and send it there, It responds with an error:

TypeError: Cannot read property 'send' of undefined

Here's the code that I wrote

const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
  name: 'beep',
  description: 'Beep!',
  execute(message) {
    const channel = client.channels.get('668*****8*********');
    channel.send('Yahoo');
  }
};

I tried several times from multiple answers in another questions, but none of them worked. Changed from string to integer also don't work. I also tried to console.log(client.channels.get('668*****8*********')), but it returns nothing.

Upvotes: 1

Views: 320

Answers (1)

Cipher
Cipher

Reputation: 2722

You can do with follow code:

You cannot use the client argument function here because don't pass it on. It is also impossible to create a client in each command file. 1 token - 1 client

const Discord = require('discord.js');

module.exports = {
  name: 'beep',
  description: 'Beep!',
  execute(message) {
      if(message.channel.type === 'dm') return
    const channel = message.guild.channels.get('668*****8*********');
    if(!channel) return
    channel.send('Yahoo');
  }
};

Upvotes: 3

Related Questions