我在 discord.js v12.5.3 静音命令上遇到错误

时间:2021-07-08 18:54:35

标签: javascript node.js discord.js

当我运行这段代码时,它给了我一个错误,我在互联网上四处寻找解决这个问题的方法,但我就是无法让它工作!请如果你想帮助我,请做!

var Discord = require('discord.js');
var ms = require('ms');

exports.run = async(client, msg, args) => {
    if(!msg.member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You can\'t use that!');

    var user = msg.mentions.users.first();
    if(!user) return msg.reply('You didn\'t mention anyone!');

    var member;

    try {
        member = await msg.guild.members.fetch(user);
    } catch(err) {
        member = null;
    }

    if(!member) return msg.reply('They aren\'t in the server!');
    if(member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You cannot mute that person!');

    var rawTime = args[1];
    var time = ms(rawTime);
    if(!time) return msg.reply('You didn\'t specify a time!');

    var reason = args.splice(2).join(' ');
    if(!reason) return msg.reply('You need to give a reason!');

    var channel = msg.guild.channels.cache.find(c => c.name === 'potato');

    var log = new Discord.MessageEmbed()
    .setTitle('User Muted')
    .addField('User:', user, true)
    .addField('By:', msg.author, true)
    .addField('Expires:', rawTime)
    .addField('Reason:', reason)
    msg.channel.send(log);

    var embed = new Discord.MessageEmbed()
    .setTitle('You were muted!')
    .addField('Expires:', rawTime, true)
    .addField('Reason:', reason, true);

    try {
        user.send(embed);
    } catch(err) {
        console.warn(err);
    }

    var role = msg.guild.roles.cache.find(r => r.id === '862761416255602718');

    member.roles.add(role);

    setTimeout(async() => {
        member.roles.remove(role);
    }, time);

    msg.channel.send(`**${user}** has been muted by **${msg.author}** for **${rawTime}**!`);
}

这是我得到的错误:(这是我运行时得到的错误)

(node:23408) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at RequestHandler.execute (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RequestHandler.push (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async GuildMemberRoleManager.add (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:96:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23408) 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:23408) [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.
(node:23408) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions

这是我在 10 秒后得到的代码:

at RequestHandler.execute (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RequestHandler.push (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async GuildMemberRoleManager.remove (D:\spil\Firlingdon & xEpic_Wolf Bot\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:125:7)
(node:23408) 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)

1 个答案:

答案 0 :(得分:1)

第一个想法:您提供了错误的使用许可。机器人的合适权限是:'MANAGE_ROLES',当用户的合适权限是:'MUTE_MEMBERS'时,我强烈建议这样做。

 if(!msg.guild.me.hasPermission('MANAGE_ROLES'){
    return msg.reply(`blahblah`)
}

第二个想法:您实际上可以将自己或管理员静音。在无权使用“MUTE_MEMBERS”的人身上进行测试。我非常强烈建议您在 ALT 上执行此操作,因为它可以帮助解决很多问题。

第三个想法:你给你的机器人的角色是不够的,也许让它成为服务器中最高的角色(比如在服务器的顶部)并测试谁没有权限或ALT。

第四个想法:您的角色可能是错误的,我强烈建议使用此代码:

let role = msg.guild.roles.cache.get(`id`)

memeber.roles.add(role)

member.roles.remove(role)

或者您的会员代码有误,请尝试这样做:


member = await msg.guild.members.cache.get(user.id)

并在错误代码上执行此操作:

member = null
console.log(err) //so you know what errors it out here.

希望这些想法对您有所帮助! <3