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
3 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - Node.js Discord Bot : TypeError: Cannot read property 'send' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741248861a2365413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论