我正在使用node.js和discord.js制作一个discord bot,我目前正在尝试这样做,以便当用户加入discord服务器时,会发送自定义欢迎消息。这是我的代码:
bot.on("guildMemberAdd" ,(message, member) => {
message.channel.send("Welcome")
});
这是错误:get:
message.channel.send("Welcome")
^
TypeError: Cannot read property 'send' of undefined
感谢您的帮助。
答案 0 :(得分:3)
如果您阅读了documentation,那么就没有message
参数,只有member
。您必须先get the guild's channel ID。
尝试这样的事情:
bot.on('guildMemberAdd', member => {
member.guild.channels.get('channelID').send("Welcome");
});
答案 1 :(得分:1)
我的代码是
bot.on('guildMemberAdd', async member => {
const channel = member.guild.channels.cache.get('channel-id-here');
if (!channel) return;
channel.send("text-here!")
});
希望它有效!
答案 2 :(得分:0)
或将其用于嵌入。
client.on('guildMemberAdd', msg => { // Commands Go Inside The client.on('message',
msg => )
msg.guild.channels.get('484648408372740099').send({embed: {
color: 3447003,
author: {
name: client.user.username,
icon_url: client.user.avatarURL
},
title: "Welcome To ()!",
url: "https://districtservices.net",
description: "@MEMBER",
fields: [{
name: "Fields",
value: "They can have different fields with small headlines."
},
{
name: "Masked links",
value: "You can put [masked links](http://google.com) inside of rich embeds."
},
{
name: "Markdown",
value: "You can put all the *usual* **__Markdown__** inside of them."
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "© Example"
}
}}); });
答案 3 :(得分:0)
client.on('guildMemberAdd', member => {
client.on('message',
var role = member.guild.roles.find('name', 'Beginner role name'); // Variable to get channel ID
member.addRole(role); // Adds the default role to members
member.guild.channels.get('JOIN/LEAVE Channel ID').send({embed: {
color: 3447003,
title: "**SERVER NAME** Welcome Bot!",
url: "WEBSITE URL",
description: "Welcome *" + member + "* to the **Server name** discord server!",
fields: [{
name: "Information",
value: "Some info on the server"
}
],
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL,
text: "© NAME OF SERVER 2018 - 2019"
}
}}); });
这是实际有效的代码:)
答案 4 :(得分:0)
我也在为我的机器人制作一个欢迎功能,下面是代码,它看起来很棒
//Welcome & goodbye messages\\
client.on('guildMemberAdd', member => {
member.roles.add(member.guild.roles.cache.find(i => i.name === 'Among The Server'))
const welcomeEmbed = new Discord.MessageEmbed()
welcomeEmbed.setColor('#5cf000')
welcomeEmbed.setTitle('**' + member.user.username + '** is now Among Us other **' + member.guild.memberCount + '** people')
welcomeEmbed.setImage('https://cdn.mos.cms.futurecdn.net/93GAa4wm3z4HbenzLbxWeQ-650-80.jpg.webp')
member.guild.channels.cache.find(i => i.name === 'greetings').send(welcomeEmbed)
})
client.on('guildMemberRemove', member => {
const goodbyeEmbed = new Discord.MessageEmbed()
goodbyeEmbed.setColor('#f00000')
goodbyeEmbed.setTitle('**' + member.user.username + '** was not the impostor there are **' + member.guild.memberCount + '** left Among Us')
goodbyeEmbed.setImage('https://gamewith-en.akamaized.net/article/thumbnail/rectangle/22183.png')
member.guild.channels.cache.find(i => i.name === 'greetings').send(goodbyeEmbed)
})
//Welcome & goodbye messages end\\