Splating
Splating

Reputation: 31

Node.js Discord Bot : TypeError: Cannot read property 'send' of undefined

I want my public bot to sendMessage in specific channel, and specific server. But, I have an error... this is my code:

client.on('message', msg => {
  if (msg.content.startsWith('+specifictest')) {
    var channellog = msg.client.channels.get('352496750327496725');
    var guiiild = msg.client.guilds.get('343913599686934539').channellog;
    guiiild.send({
      embed: new Discord.RichEmbed()
                        .setColor("#FFFFFF")
                        .setAuthor("Dessin")
                        .setDescription(`Demandé par <@${msg.author.id}>`)
    })
  }
})

And, my error: TypeError: Cannot read property 'send' of undefined

Upvotes: 3

Views: 23656

Answers (3)

kuso.sama
kuso.sama

Reputation: 336

This might be late, but try using await

I think you're using Disord v11 not 12, I would recommend upgrade to 12 but that's your decision

V11

client.on('message', async (msg) => {
  if (msg.content.startsWith('+specifictest')) {
    const channel = await client.channels.get("352496750327496725")
    channel.send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
 }
})

v12

client.on('message', async(msg) => {
  if (msg.content.startsWith('+specifictest')) {
    const channel = await client.channels.cache.find(x => x.id == "352496750327496725")
    channel.send(new Discord.MessageEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par ${msg.author}`)}) //this is the same as <@ID>
  }
})

Upvotes: 2

TheThirdDimension
TheThirdDimension

Reputation: 311

You could try this:

client.channels.get("ID").send("Your message")

ID would be the id of the channel you want to send the message to. So in your case, try:

client.on('message', msg => {
  if (msg.content.startsWith('+specifictest')) {
    client.channels.get("352496750327496725").send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
  }
})

Upvotes: 4

EMX
EMX

Reputation: 6211

This error (in your context) means that your variable guiiild has not been correctly populated hence the unexpected failure when it tries to use an unexisting property (in this case the send function).

Try/Catch

You can wrap it in a try/catch block :

client.on('message', msg => {
  if (msg.content.startsWith('+specifictest')) {
   try{
    var channellog = msg.client.channels.get('352496750327496725');
    var guiiild = msg.client.guilds.get('343913599686934539').channellog;
    guiiild.send({embed: new Discord.RichEmbed().setColor("#FFFFFF").setAuthor("Dessin").setDescription(`Demandé par <@${msg.author.id}>`)})
   }catch(e){console.log("[ERROR]",e)}
  }
})

But it will still give you errors if msg.client.guilds.get('343913599686934539').channellog doesnt return anything containing .send

Upvotes: 0

Related Questions