如何从用户discord.js v12中删除所有角色?

时间:2020-09-09 08:31:21

标签: javascript node.js discord discord.js

我正在执行一条命令,该命令将从用户中删除所有角色,并给他指定一个角色。但是,我遇到了一个问题,即它不会删除角色,给了我[INVALID_TYPE]: Supplied roles is not an Array or Collection of Roles or Snowflakes.错误。我的代码如下:

const { DiscordAPIError } = require("discord.js");
const Discord = require("discord.js");
const fs = require("fs");
const ms = require("ms");
let antagonistRoleList = JSON.parse(
  fs.readFileSync("./roleIDs/antagonistRole.json", "utf-8")
);
const commands = require("./commands");

module.exports = {
  name: "antagonise",
  description: "The bot will antagonise the mentioned user",
  execute(message, args) {
    let antagonistRoleList = JSON.parse(
      fs.readFileSync("./roleIDs/antagonistRole.json", "utf-8")
    );
    let guildID = message.guild.id;
    let antagonistRole = antagonistRoleList[guildID].antagonistRoleList;
    var member = message.mentions.members.first(); // || message.guild.members.cache.get(args[0]);
    var role = message.guild.roles.cache.get(
      `${antagonistRoleList[guildID].antagonistRoleList}`
    );

    if (!member) {
      var member = message.guild.members.cache.get(args[0]);
      var antagReason = args.join(" ").slice(18);
    } else {
      var antagReason = args.join(" ").slice(22);
    }

    if (!member) {
      return message.reply(
        "please specify the user that should be antagonised"
      );
    }

    if (!role) {
      return message.reply(
        "there is not a role set as the antagonist role, set it by doing &setantagonistrole (that requires manage server permissions)"
      );
    }

    if (!antagReason) {
      antagReason = "no reason given";
    }

    if (member.id === message.author.id) {
      return message.reply("can not allow self-harm");
    }
    if (
      message.member.roles.highest.comparePositionTo(member.roles.highest) <= 0
    ) {
      return message.reply(
        "you can not antagonise a user with the same (or higher) permissions as you"
      );
    }
    if (!message.member.permissions.has("MANAGE_ROLES")) {
      return message.reply(
        `you don't have manage roles permissions that are required to execute this command.`
      );
    }

    console.log(`${role}`);
    console.log(`${antagonistRole}`);

    if (member.roles.cache.has(`${antagonistRole}`)) {
      return message.reply("this user is already antagonised!");
    }
    member.roles.remove(member.roles).catch((error) => {
      console.log(error);
    });
    member.roles
      .add(role)
      .then((memberAdded) => {
        message.reply(
          `you have succesfully antagonised <@${member.id}> with the reason **${antagReason}**`
        );
      })
      .catch((error) => {
        console.log(error);
      });

    const embed = new Discord.MessageEmbed()

      .setColor("#FF0000")
      .setTitle(`You were antagonised in ${message.guild.name}`)
      //.setAuthor(`${userinfoget.user.tag}`, userinfoget.user.displayAvatarURL())
      .addFields({ name: `Antagonised by`, value: `${message.author.tag}` })
      .addFields({ name: `Reason`, value: `${antagReason}` })
      .setTimestamp();

    member.user.send(embed).catch((error) => {
      message.channel.send(
        `Failed to DM <@${member.id}> the info about this action`
      );
      console.log(error);
    });

    console.log(`${antagonistRoleList[guildID].antagonistRoleList}`);
    console.log(`${message.guild.name}`);
    console.log(`${message.author.tag}`);
    console.log(`${antagReason}`);
  },
};

我做错了什么?我敢肯定member.roles.remove(member.roles)行中有一个错误,但是我应该将其更改为什么?

1 个答案:

答案 0 :(得分:3)

member.roles.remove方法将“角色”,“角色数组”或“角色集合”作为参数。您已为该方法提供了member.roles,根据文档,该方法的类型为GuildMemberRoleManager,这不是remove方法的可接受参数类型。

尝试将member.roles.remove(member.roles)更改为member.roles.remove(member.roles.cache),因为cache property返回角色集合。