Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  142] [ 4]  / answers: 1 / hits: 6868  / 4 Years ago, wed, september 9, 2020, 12:00:00

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. 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?


More From » node.js

 Answers
1

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.


[#2716] Thursday, September 3, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kieraelsies

Total Points: 718
Total Questions: 103
Total Answers: 104

Location: England
Member since Sun, May 21, 2023
1 Year ago
kieraelsies questions
Tue, Aug 3, 21, 00:00, 3 Years ago
Tue, Feb 23, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Mon, Sep 16, 19, 00:00, 5 Years ago
Fri, Sep 13, 19, 00:00, 5 Years ago
;