1

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)
    }
  }
})
// ...
client.login("MYTOKEN")

I'm trying this with version 12.0.0

EDIT: I found my issue, for some reason the saved ID is the bots ID, not my ID

Cmb
  • 334
  • 2
  • 18

2 Answers2

3

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.

Angoose
  • 56
  • 5
  • Its only defined in the creation of the ticket (Inside another client.on() function) – Cmb Dec 02 '21 at 21:08
  • I just got the error `Cannot read property 'send' of undefined` when I tried your fix – Cmb Dec 02 '21 at 21:11
  • And you're 100% sure the channel you are trying to find exists and your bot has access to it? – Angoose Dec 04 '21 at 06:29
  • After taking another look at your code I decided to mark it as the correct answer. – Cmb Apr 24 '22 at 15:59
1

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}`)
MrMythical
  • 8,908
  • 2
  • 17
  • 45