# 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:** 1
**Showing post:** 4

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

---

_[View the full topic](https://meta.discourse.org/t/add-script-to-add-user-to-user-group/268478)._
