简体   繁体   中英

Welcome message with Discord.js - “Cannot read property 'get' of undefined”

I'm learning JavaScript with the Library Discord.js/Node.js and I'm building a Discord bot to exercise myself.

I'm trying to send a message to a new member of my discord server. The property I was looking for was .find , I didn't find it anywhere on the wiki of Discord.js and I've got an error that said Cannot read property 'get' of undefined .

So I decided to use a property that Ii found on the Wiki of Discord.js .Get . And I've got the same error. I don't know how to tell to the bot to send a message to the new user when he joins the server for the first time.

Here's my code:

bot.on("guildMemberAdd", MemberAdd => {
   MemberAdd.guild.channel.get("enter-leave").send("HI, welcome on my server.")
   console.log("enter");
});

The answer was that I've forgot the S for .channels . .channel was another property.

So my code is

bot.on("guildMemberAdd", MemberAdd => {

MemberAdd.guild.channels.find("name", "general").send("Bienvenu dans ma taverne mon chou :heart:.")

console.log("enter"); });

for discord.js v12 you do:

bot.on("guildMemberAdd", MemberAdd => {
   let channel = MemberAdd.guild.channels.cache.find(channel => channel.name === "enter-leave")
   channel.send("HI, welcome on my server.")
   console.log("enter");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM