【发布时间】:2020-10-26 23:41:22
【问题描述】:
因此,我正在尝试根据我找到的教程向我的机器人添加一个自动角色功能,但是在加入不和谐的 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
【问题讨论】:
标签: javascript node.js bots discord discord.js