最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove all roles from an user discord.js v12? - Stack Overflow

programmeradmin1浏览0评论

I'm making a mand 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. 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 mands = require("./mands");

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.highestparePositionTo(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 mand.`
      );
    }

    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?

I'm making a mand 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. 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 mands = require("./mands");

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.parePositionTo(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 mand.`
      );
    }

    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?

Share Improve this question edited Sep 9, 2020 at 10:25 Lioness100 8,4226 gold badges20 silver badges50 bronze badges asked Sep 9, 2020 at 8:31 PandiconPandicon 4303 gold badges17 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The member.roles.remove methods takes a Role, Array of Roles or Collection of Roles as an argument. 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.

Try changing member.roles.remove(member.roles) to member.roles.remove(member.roles.cache), since the cache property returns a Collection of Roles.

This works for me (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);
发布评论

评论列表(0)

  1. 暂无评论