1
votes

So I'm trying to add an autorole function to my bot based on a tutorial I found but it returns an error message when joining the discord on an alt to test it.

Code related to autorole in my index.js file:

client.on("guildMemberAdd", member => {
    let autorole = JSON.parse(fs.readFileSync("./autorole.json", "utf8"));
    if (!autorole[member.guild.id]) { 
        autorole[member.guild.id] = {
            autorole: client.config.autorole
        };
    }
    var role = autorole[member.guild.id].role;
    if (!role) return;
    member.addRole(role);
    });

My autorole.js file:

module.exports = {
    name: "autorole", // set command name
    aliases: [ "ar" ], // set command aliases
    permissions: [ "MANAGE_ROLES" ], // set command permissions
    exec: async (client, message, args) => {
        const fs = require("fs");

        let autorole = JSON.parse(fs.readFileSync("./autorole.json", "utf8"))
        if (!args[0]) {
            autorole.message.guild.id = {
                role: 0
            };
            fs.writeFile("./autorle.json", JSON.stringify(autorole), (err) => {
                if (err) console.log(err);
            });
            message.channel.send(`**${message.author.username}** 1`)
        }

        if(args[0]) {
            let roles = args.join(" ");
            let role = message.guild.roles.find(role => role.name === roles);
            autorole[message.guild.id] = {
                role: role.id
            };
            fs.writeFile("./autorole.json", JSON.stringify(autorole), (err) => {
                if (err) console.log(err);
            });
            message.channel.send(`**${message.author.username}** the autorole is et to **${role.name}**`);
        }
    }
}

Setting the role works perfectly fine, the problem only occurres when joining the server. It's most likely caused when assigning the role because when comment member.addRole(role); out it gives no error (but ofc the function of it is lost too).

The error message I get:

(node:18844) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at C:\Users\ocsko\Desktop\bot\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15
    at C:\Users\ocsko\Desktop\bot\node_modules\discord.js\node_modules\snekfetch\src\index.js:215:21
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:18844) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:18844) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Edit: I'm using discord.js v11

1

1 Answers

0
votes

This is the relevant line:

(node:18844) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions

Your bot does not have enough permissions to give that member a role. Check if it has the Manage Roles an try again.