![](/img/trans.png)
[英]Discord.js : Add a role on a specific server to a user that DM the bot
[英]Discord.js Bot Welcomes Member, Assign a Role and send them a DM
因此,当新成员加入Guild
[discord 服务器] 时。 Bot 应该在某个频道(ID = 766716351007686696)中发送消息,直接发送消息给他们,然后添加一个角色(Human Bean)。 这是我现在拥有的代码,它不起作用,底部有错误
client.on('guildMemberAdd', member =>{
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
const channelwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription(`${member} just joined the discord! Make sure to read #rules!`)
.setTimestamp();
channel.send(channelwelcomeEmbed);
const dmwelcomeEmbed = new Discord.MessageEmbed()
.setColor('#ffd6d6')
.setTitle('Welcome!')
.setDescription("For Help Using @Pro Bot#7903, Send The Command `!help` In Server")
.setTimestamp();
member.send(dmwelcomeEmbed);
let role6 = message.guild.roles.cache.find(role => role.name == "Human Bean"); //BASIC ROLE, EVERYONE GETS IT
if(!role6) return message.reply("Couldn't find that Role .")
member.roles.add(role6);
});
错误信息是;
const channel = message.guild.channels.cache.find(c => c.id === "766716351007686696")
^
ReferenceError: message is not defined
您的代码看起来不错,问题是未触发事件。 那是因为不和谐默认关闭了“特权意图”。
由于数据的敏感性,某些意图被定义为“特权”。 这些意图是:
GUILD_PRESENCES
GUILD_MEMBERS
一种影响就是您所经历的,即不工作的guildMemberAdd
事件。
好消息是您可以通过一个简单的步骤解决此问题。 只需在Discord 开发人员门户中启用Privileged Gateway Intents ,它就可以正常工作。
修复: const channel = member.guild.channels.cache.get('CHANNEL ID')
您需要使用member
而不是message
。 因为guildMemberAdd
函数使用member
。
client.on('guildMemberAdd', member => {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.