我正在尝试为我的机器人在discord.js中创建一个autorole函数,但是它不起作用

时间:2020-07-06 19:18:35

标签: javascript node.js bots discord discord.js

因此,我试图根据我发现的教程向我的机器人添加autorole函数,但在将alt加入不和谐状态进行测试时,它会返回错误消息。

我的index.js文件中与autorole相关的代码:

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);
    });

我的autorole.js文件:

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}**`);
        }
    }
}

设置角色可以很好地工作,只有在加入服务器时才会出现问题。这很可能是在分配角色时引起的,因为在注释member.addRole(role);发出时它不会出错(但是它的功能也会丢失)。

我收到的错误消息:

(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.

编辑:我正在使用discord.js v11

1 个答案:

答案 0 :(得分:0)

这是相关行:

(node:18844) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions

您的漫游器没有足够的权限来授予该成员角色。再次尝试管理角色