3

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

3 Answers 3

4

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}>`)})
  }
})

Sign up to request clarification or add additional context in comments.

Comments

2

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>
  }
})

Comments

0

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

4 Comments

I've try your code, and i've this error now: [ERROR] TypeError: Cannot read property 'send' of undefined :c
@SplatingWorld, did you understand my answer? (its not only copy-paste)
Yeah, sorry... :c
Isn't channellog a function? Or isn't there something like guild.GetChannelLogs() instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.