admin管理员组

文章数量:1278822

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

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

Share Improve this question edited Jul 12, 2020 at 15:49 x00 13.9k3 gold badges20 silver badges41 bronze badges asked Sep 4, 2017 at 20:12 SplatingSplating 311 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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}>`)})
  }
})

javascriptdiscord.jsdiscordsendnode.jstypeerror

This might be late, but try using await

I think you're using Disord v11 not 12, I would remend 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>
  }
})

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

本文标签: javascriptNodejs Discord BotTypeError Cannot read property 39send39 of undefinedStack Overflow