Discord.js Bot欢迎成员,分配角色并向他们发送DM

时间:2020-11-08 14:40:20

标签: javascript discord discord.js roles

因此,当新成员加入Guild [discord服务器]时。机器人应在某个频道(ID = 766716351007686696)中发送一条消息,向他们发送一条直接消息,然后添加一个角色(人类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

2 个答案:

答案 0 :(得分:3)

您的代码看起来不错,问题在于未触发该事件。那是因为不和谐默认情况下关闭了“特权意图”。

由于数据的敏感性质,某些意图被定义为“特权”。这些意图是:

  • GUILD_PRESENCES
  • GUILD_MEMBERS

影响之一就是您正在体验的guildMemberAdd事件不正常。

好消息是,您可以通过一个简单的步骤解决此问题。只需在discord developer portal中启用特权网关意图,它就可以正常工作。

enter image description here

如果您想了解更多有关此的信息

答案 1 :(得分:1)

修正:const channel = member.guild.channels.cache.get('CHANNEL ID')

您需要使用member而不是message。因为guildMemberAdd使用member起作用。

client.on('guildMemberAdd', member => {

相关问题