# Add script to add user to user group

**URL:** https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478
**Category:** Feature
**Tags:** automation
**Created:** [June 15, 2023, 3:46pm 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: [June 15, 2023, 3:46pm UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/1 "2023-06-15T15:46:09Z")

</div>

There is currently a script in the Automation plugin to add users to a group based on a custom field. We would like a script to add users to a group, when they’re added to another group.

The scenario is this: The Discourse Experts plugin expects you to only have one user group designated for “experts”. We are running an ambassador program and have two tiers of ambassadors—both of which are “experts” in this case.

We would like to say, anyone added to “Ambassadors” or “Expert Ambassadors” also gets added to the “Experts” user group.

---

<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: [June 19, 2023, 5:50am UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/2 "2023-06-19T05:50:43Z")

</div>

If you are up to sponsoring this I don’t see it as too complicated, we can quote the work. Contact `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: [October 9, 2023, 6:30am UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/3 "2023-10-09T06:30:53Z")

</div>

Also requested in:

> [@An 'add to group' script for the Discourse Automation](https://meta.discourse.org/t/an-add-to-group-script-for-the-discourse-automation/231216/1):
>
> 1. To enable functional sub-groups. For example, if a user is put in a group of Data Scientists, they are also put in the broader group of IT Professionals. If removed from the IT Professionals group, they are also removed from the Data Scientists group.

---

<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: [October 10, 2023, 4:08am UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/4 "2023-10-10T04:08:00Z")

</div>

> [@jordan-violet](#):
>
> We would like to say, anyone added to “Ambassadors” or “Expert Ambassadors” also gets added to the “Experts” user group.

Other than the topic `@JammyDodger` linked to, there have been a few other requests for similar functionality

- [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)

There have also been requests to be able to display groups in a hierarchical manner:

- [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)

I’m noting this just to make sure that adding an Automation script is the best way to deal with this, or if it’s functionality that should get added to the core Discourse groups code.

I’ve been fiddling around with this for fun. It’s kind of related to some recent work I’ve been doing with Zapier. Feel free to ignore the rest of this post.

Assuming an Automation script is the right approach, the only stumbling block is that the Automation plugin’s `group` component only accepts a single group. So either the `group` component needs to be updated to accept more than one group, or a new `groups` component needs to be added. I added a `groups` component to my local dev branch to test it out. With that in place:

Add a user to a parent group when they are added to any of a list of subgroups:

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

Remove a user from a parent group if they have been removed from all of the listed subgroups:

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

If it helps, the triggers and scripts are in a plugin: [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). Note that this code assumes that a `groups` component has been added to [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).

---

<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: [May 9, 2025, 10:44am UTC](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478/5 "2025-05-09T10:44:09Z")

</div>

This plugin now covers this functionality:

> [@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’…
