# 添加脚本以将用户加入用户组

**URL:** https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478
**Category:** Feature
**Tags:** automation
**Created:** [2023年六月15日 15:46 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478 "2023-06-15T15:46:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jordan-violet](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan-violet/32/281428_2.png) [@jordan-violet](https://meta.discourse.org/u/jordan-violet)
#### Post date: [2023年六月15日 15:46 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/1 "2023-06-15T15:46:09Z")

</div>

目前有一个自动化插件中的脚本，用于根据自定义字段将用户添加到组。我们希望有一个脚本，在用户被添加到另一个组时，将他们添加到某个组。

场景如下：Discourse Experts 插件要求您只有一个用户组被指定为“专家”。我们正在运行一个大使计划，并且有两个级别的大使——在这种情况下，两者都是“专家”。

我们希望这样说：任何被添加到“大使”或“专家大使”的用户，也应被添加到“专家”用户组。

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2023年六月19日 05:50 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/2 "2023-06-19T05:50:43Z")

</div>

如果您愿意赞助此事，我认为这并不复杂，我们可以为这项工作报价。请联系 `team`。

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [2023年十月9日 06:30 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/3 "2023-10-09T06:30:53Z")

</div>

还要求：

> [@nathank](#):
>
> 1. 启用功能性子组。例如，如果一个用户被分到一个数据科学家组，他们也被分到一个更广泛的IT专业人员组。如果他们被移出IT专业人员组，他们也会被移出数据科学家组。

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2023年十月10日 04:08 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/4 "2023-10-10T04:08:00Z")

</div>

> [@jordan-violet](#):
>
> 我们想说的是，添加到“大使”或“专家大使”的任何人也将被添加到“专家”用户组。

除了 `@JammyDodger` 链接到的主题之外，还有一些其他关于类似功能的请求：

- [Hierarchical group membership (groups inside groups)](https://meta.discourse.org/t/hierarchical-group-membership-groups-inside-groups/182263)
- [Parent groups and sub-groups](https://meta.discourse.org/t/parent-groups-and-sub-groups/119766)
- [https://meta.discourse.org/t/creating-subgroups/200812](https://meta.discourse.org/t/creating-subgroups/200812)

还有人请求能够以分层方式显示组：

- [Group groups for groups page?](https://meta.discourse.org/t/group-groups-for-groups-page/58232)
- [Organize groups like we organize categories or tags](https://meta.discourse.org/t/organize-groups-like-we-organize-categories-or-tags/120303)

我注意到这一点只是为了确保添加自动化脚本是处理此问题的最佳方法，或者这是否是应添加到核心 Discourse 组代码的功能。

我一直在随意摆弄这个。这有点与我最近使用 Zapier 进行的一些工作有关。您可以忽略此帖子的其余部分。

假设自动化脚本是正确的方法，唯一的障碍是自动化插件的 `group` 组件只接受一个组。因此，要么需要更新 `group` 组件以接受多个组，要么需要添加一个新的 `groups` 组件。我在本地开发分支中添加了一个 `groups` 组件进行测试。有了它：

当用户被添加到列表中的任何子组时，将其添加到父组：

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/7/7/d77a61b45c99e3c6218b63705e6b57e76a46f0b6.png)

如果用户已从所有列出的子组中移除，则将其从父组中移除：

 ![image](https://global.discourse-cdn.com/meta/original/4X/0/f/9/0f93dd8ae6e366b0bf6098f1d18ab7a8f5e25ba1.png)

如果对您有帮助，触发器和脚本位于一个插件中：[automation-script-example/plugin.rb at main · scossar/automation-script-example · GitHub](https://github.com/scossar/automation-script-example/blob/main/plugin.rb#L13-L98%E3%80%82%E8%AF%B7%E6%B3%A8%E6%84%8F%EF%BC%8C%E6%AD%A4%E4%BB%A3%E7%A0%81%E5%81%87%E5%AE%9A%E5%B7%B2%E5%B0%86) `groups` 组件添加到 [https://github.com/discourse/discourse-automation/tree/main/assets/javascripts/discourse/components/fields。](https://github.com/discourse/discourse-automation/tree/main/assets/javascripts/discourse/components/fields%E3%80%82)

---

<div class="post-metadata">

### Author: ![nathank](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nathank/32/290039_2.png) [@nathank](https://meta.discourse.org/u/nathank)
#### Post date: [2025年五月9日 10:44 UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/5 "2025-05-09T10:44:09Z")

</div>

此插件现已涵盖此功能：

> [@Discourse Dynamic Groups](https://meta.discourse.org/t/discourse-dynamic-groups/365342):
>
> information_sourceSummary Automatically manage group membershiphammer_and_wrenchRepository Link [https://github.com/communiteq/discourse-dynamic-groups](https://github.com/communiteq/discourse-dynamic-groups)open_bookInstall Guide [How to install plugins in Discourse](https://meta.discourse.org/t/install-plugins-in-discourse/19157)warning This plugin should be considered beta warningFeatures Discourse communities often need to automate group memberships based on conditions like badge ownership or existing group memberships. Common use cases include: Users not in the ‘subscribers’…
