Как исправить: роли не определены (Discord.js )

#javascript #node.js #discord #discord.js #repl.it

#javascript #node.js #Discord #discord.js #repl.it

Вопрос:

Я пытаюсь добавить роль участнику, когда он присоединяется к моему серверу, вот код, который у меня есть:

Проблема, с которой я "ReferenceError: roles is not defined" сталкиваюсь, может кто-нибудь, пожалуйста, помочь мне решить это?

 client.on("guildMemberAdd", (member) => {
    console.log("User "   member.user.username   " has joined the server!");
    var role = member.guild.roles.cache.find((role) => role.name === "Javjajjaj");
    roles.add(role);
});
  

Комментарии:

1. Вы никогда не определяете roles в своем коде?

Ответ №1:

Проблема в том, что roles переменная никогда не определяется в вашем фрагменте кода.


 client.on("guildMemberAdd", GuildMember => {
    // Logging when a GuildMember enters the Guild.
    console.log(`${GuildMember.user.tag} joined ${GuildMember.guild.name}!`);

    const Role = GuildMember.guild.roles.cache.find(role => role.name == "Javjajjaj"); // Finding the Role by name in the GuildMember's Guild.

    // Checking if the role exists.
    if (!Role) return console.error("Couldn't find the role!");
    // Trying to add the Role to the GuildMember and catching any error(s).
    GuildMember.roles.add(Role).catch(error => console.error(`Couldn't add the Role to the GuildMember. | ${error}`));
});