如何从匹配正则表达式的所有角色中删除成员?

How do I remove a member from all roles that match a regex?

如何从匹配正则表达式的所有角色(角色名称)中删除成员?我是 discord.js 的新手,我不太了解 JavaScript,所以任何帮助将不胜感激。

我正在创建一个允许用户更改颜色的机器人,因此我希望命令在添加新角色之前删除所有现有的颜色角色。

正则表达式是 /^#[0-9a-zA-Z]{6}$/,如果有帮助的话。

好吧,既然您已经有了正则表达式,那么您已经完成了一半。我采用的一种方法是获取成员的角色并过滤与正则表达式匹配的角色。然后我会简单地调用用户的 removeRoles 方法来删​​除所有角色。

您可以在下面找到一些示例代码,尝试一下,让我知道它是怎么回事。

client.on("message", async message =>
{
  // Get the user
  let member = message.member;

  // Get all the roles which match the regex
  let filteredRoles = member.roles.filter((role) => /^#[0-9a-zA-Z]{6}$/.test(role.name));

  // Remove the roles from the member
  member.removeRoles(filteredRoles);
});