更改discord.js中的昵称时,缺少权限错误

时间:2020-05-16 18:09:28

标签: javascript node.js discord.js

每次我尝试使用bot更改服务器中其他人的昵称时(甚至是我自己在朋友服务器中),都会失败,并出现以下错误:

(node:11620) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at RequestHandler.execute (C:\Users\(User)\Desktop\Discord Bot\Nick-Cycle\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:11620) 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: 1)
(node:11620) [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.

我检查了一下,它确实具有“管理昵称”,并且它的角色就在所有者的下面。我要更改昵称的人,他的最高职责远远低于机器人的职责。

Image of Bot's role, position, and Manage Nickname's permission.

这是代码:

client.on('message', async message => {
    const prefix = ";"

    if (message.author.bot) return;
    if (!message.guild) return;
    if (!message.content.startsWith(prefix)) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const cmd = args.shift().toLowerCase();

    switch(cmd) {
        case "setnick": 
        if (!message.guild.me.hasPermission("MANAGE_NICKNAMES")) return message.channel.send("I don't have Manage Nicknames!");
        if (!args[0]) return message.channel.send("Not enough arguments!");
        if (args[0] === getUserFromMention(args[0])) {
            const user = getUserFromMention(args[0])
            if (!args[1]) return message.channel.send("Not enough arguments!");

            message.guild.members.fetch(user).setNickname(args[1]);
        }
        else {
            message.member.setNickname(args[0]);
        }
        break;
    }
    
});

function getUserFromMention(mention) {
	if (!mention) return;

	if (mention.startsWith('<@') && mention.endsWith('>')) {
		mention = mention.slice(2, -1);

		if (mention.startsWith('!')) {
			mention = mention.slice(1);
		}

		return client.users.cache.get(mention);
	}
}

怎么了?有故障吗?角色有问题吗?还是我的代码有问题?

2 个答案:

答案 0 :(得分:0)

好消息!您的代码不是问题。问题在于您的漫游器角色的位置以及其拥有的权限。确保该机器人能够更改其他人的名字,并且其角色高于受害者的角色。希望这会有所帮助!

enter image description here

打开此

enter image description here

答案 1 :(得分:0)

我解决了。事实证明,它并没有接受应有的争论,而是一直转移到其他地方,在那里它会更改您自己的昵称,在我的服务器中,我是所有者,在这里我的昵称无法被机器人更改。通过重写if语句以在继续之前专门检查提及来修复此问题。现在工作!