Home > Software engineering >  Can't send a message to a specific channel
Can't send a message to a specific channel

Time:12-02

I'm trying to create a modmail system and whenever I try to make it, it says "channel.send is not a function, here is my code."

const Discord = require("discord.js")
const client = new Discord.Client()
const db = require('quick.db')
// ...
client.on('message', message => {
  if(db.fetch(`ticket-${message.author.id}`)){
    if(message.channel.type == "dm"){
      const channel = client.channels.cache.get(id => id.name == `ticket-${message.author.id}`)
      channel.send(message.content)
    }
  }
})

I'm trying this with version 12.0.0 // ... client.login("MYTOKEN")

CodePudding user response:

You are trying to find it with a function. Use .find for that instead:

const channel = client.channels.cache.find(id => id.name == `ticket-${message.author.id}`)

CodePudding user response:

As MrMythical said, you should use the find function instead of get. I believe the issue is that you're grabbing a non-text channel, since channel is defined, you just can't send anything to it.

You could fix this by adding an additional catch to ensure you are getting a text channel, and not a category or voice channel. I would also return (or do an error message of sorts) if channel is undefined.

Discord.js v12:

const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'text');

Discord.js v13:

const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'GUILD_TEXT');

Edit: You can tell channel is defined because if it weren't it would say something along the lines of: Cannot read property 'send' of undefined.

  • Related