简体   繁体   English

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

[英]How to remove all roles from an user discord.js v12?

I'm making a command that would remove all roles from a user and give him one specified role.我正在制作一个命令,该命令将从用户中删除所有角色并给他一个指定的角色。 However, I've run into a problem that it doesn't remove the roles, giving me the [INVALID_TYPE]: Supplied roles is not an Array or Collection of Roles or Snowflakes.但是,我遇到了一个问题,它不会删除角色,给我[INVALID_TYPE]: Supplied roles is not an Array or Collection of Roles or Snowflakes. error.错误。 My code looks like this:我的代码如下所示:

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

What have I done wrong?我做错了什么? I'm pretty sure it's a mistake in the member.roles.remove(member.roles) line, but what should I change it to?我很确定这是member.roles.remove(member.roles)行中的错误,但是我应该将其更改为什么?

The member.roles.remove methods takes a Role, Array of Roles or Collection of Roles as an argument. member.roles.remove方法将角色、角色数组或角色集合作为参数。 You've supplied the method with member.roles , which according to the docs, is of type GuildMemberRoleManager , which isn't an accepted argument type for the remove method.您所提供与方法member.roles ,它根据该文档,是类型的GuildMemberRoleManager ,这是不针对一个公认的参数类型remove方法。

Try changing member.roles.remove(member.roles) to member.roles.remove(member.roles.cache) , since the cache property returns a Collection of Roles.尝试将member.roles.remove(member.roles)更改为member.roles.remove(member.roles.cache) ,因为cache property返回角色集合。

It's working for me (discord.js v12):它对我有用(discord.js v12):

let role = message.member.guild.roles.cache.find(role => role.name === "RoleName");
if (role) message.guild.members.cache.get(message.author.id).roles.remove(role);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM