如何从用户 discord.js v12 中删除所有角色?
Posted
技术标签:
【中文标题】如何从用户 discord.js v12 中删除所有角色?【英文标题】:How to remove all roles from an user discord.js v12? 【发布时间】:2020-12-27 15:46:14 【问题描述】:我正在创建一个命令,该命令将删除用户的所有角色并给他一个指定的角色。但是,我遇到了一个问题,它没有删除角色,给了我[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】:这对我有用(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);
【讨论】:
【参考方案2】:member.roles.remove
方法将角色、角色数组或角色集合作为参数。您已经为该方法提供了member.roles
,根据文档,该方法的类型为GuildMemberRoleManager
,这不是remove
方法可接受的参数类型。
尝试将member.roles.remove(member.roles)
更改为member.roles.remove(member.roles.cache)
,因为cache property
返回一个角色集合。
【讨论】:
请注意,member.roles.set([])
是一种更快的删除某人所有角色的方法。
@Lioness100 不错的建议。为了扩展这一点,将其写为member.roles.set([role])
结合了删除所有角色和添加对抗角色
是的,这是个好主意!以上是关于如何从用户 discord.js v12 中删除所有角色?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Discord.JS v12 中查看用户正在玩的游戏
Discord.js V12 用户信息 createdAt /joinedAt 未定义