Automatically grant a badge for all members of a group

(Sam Saffron) #1

I just created a badge for all the awesome plugin authors.

Creating a badge targeting a group is quite easy, just type this into the SQL box:

select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = (
  select from groups g where = 'GROUP_NAME_IN_LOWER_HERE'

I will add a trigger for this at some point so granting is faster, in the mean time just go to /sidekiq/scheduler and kick off the badge job to rush it.

Badge Granting in bulk or for a group of users
What cool badge queries have you come up with?
What are Badges?
Automatically Apply Badges to Group Members (in Hosted Version)
Plugin Author Badge Qualifications
Add custom badge by querying an api
Automatically Apply Badges to Group Members (in Hosted Version)
Discourse Patreon Integration
How to set Minimum Topic Body Length?
Badges / record of real life meeting attendance
Configuring Patreon integration with Discourse
Discourse Patreon Integration
Granting Manual Badges
(Michael Downey) #2

If someone is removed from the group, does the badge go away?

(Sam Saffron) #3

If you select “Run revocation query daily” then yes.

(Tobias Eigen) #4

This is not accurate - on my site I have “Volunteers” as a group and I have to put “Volunteers” (same case) in the query for it to work.

Love this feature. Thank you!

(Eric Trowbridge) #5

When this option is checked, is there a certain time that it’s scheduled to re-run?

(Sam Saffron) #6

If stuff is not triggered it runs daily, you can see when if you look at sitename/sidekiq in the scheduler tab at the badge grant job.

(Kane York) #7

You can also manually start the badge grant from there, as well, @etrowbridge.

(Alessio Fattorini) #8

Can you generalize it for all members of all groups?

(Sam Saffron) #9

That is a weird question, users in all groups == all users … there is a tl0 group you know…

(Marco) #10

I receive this error:

(Kane York) #11

Switch it to “run daily”

(Stephen Gates) #12

Are there plans to add this trigger in the near future or should the techniques described in triggered custom badge queries be used to grant group membership immediately?

Could the trigger when a user is edited or created be used to grant a badge for group membership immediately? Does adding a user to a group count as an edit?

How could the code provided above be adjusted to immediately grant group membership badges?

select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = (
  select from groups g where = 'GROUP_NAME_IN_LOWER_HERE'

EDIT: Pardon my SQL but would this work?

select user_id, current_timestamp granted_at
from users u
where IN (
 select user_id
 from group_users
 where group_id = (
  select from groups g where = 'oktest'
and (:backfill OR IN (:user_ids) )


I find having to wait for the badge grant scheduler to run a bit of a pain especially if it’s a daily grant and creating a new badge. As we also don’t have access to sidekiq on the hosting the ability to run the process manually isn’t available. It would be useful if the job ran as you saved a new or edited a badge query.

(Sam) #14

I’ve been using this version for a while now, runs fine (slight modification of original query):

select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = ( 
  select from groups g where = 'GROUP_NAME' 
AND  (:backfill OR user_id IN (:user_ids))

With the When a user is edited or created trigger (options: revoke daily, allow to be used as a title, show badge on public badges page). It gives the badge immediately upon group membership which is basically what I’m looking for, although it still only revokes the badge with the daily job.

(Stephen Gates) #15

Thank you @Yuun - that works perfectly and as you describe. Is there a way to find out when the daily badge revocation job runs on a hosted discourse instance?

(Sam) #16

Are you able to go to on your instance? I think the revocation query is part of the regular Badge Grant job, just look for when it’s next scheduled to occur.

(Jay Pfaffman) #17

No, not on his hosted instance.

(Stephen Gates) #18

I checked hourly and noticed a change at 11:40pm Brisbane Australia = 2pm London = 9am Washington.

Revocation worked fine :smile: Thanks for your help

(Harold Martin) #19

Working on creating manual, trigger-driven badges for our hosted instance. I have a few questions if someone could help answer them so that we fully understand.

Before I ask my questions, I have just a general suggestion for badge creations. Would it be possible to implement just a “tool-tip” for each field explaining just a bit about what it is…what it does, etc.,?

So, now for my specific question is…

  1. What is the purpose of the “Group” field, when creating a new field? I’ve read @sam’s post regarding assigning a badge for everyone that’s part of a group, but what if we don’t really have, or want, any “group” assigned to users who are awarded badges? For instance, for our discourse instance, we have a custom user field created which each user completes as part of their initial profile setup. The selection they make in this field determines what specific “badge” they get (automatically). For instance, if they select that they are a “Type 1 Diabetic”, they would get a “badge” automatically for Type 1. The true purpose of the badge is so that they can use the “title” that comes along with it…which they can make visible in our forums. So, in our instance, what group would even be relevant?? I am thinking “Getting Started”, but I just don’t know which one (if any) my badges would fit into and why it’s significant? Also, what are the implications of assigning groups? Will the groups appear on the Group listing? If so, would that mean that all members of a group could be messaged?

And IF we have to have a group applied…given my scenario that I listed…which would you suggest we use as the “group” assigned to these badges?


(Sam Saffron) #20

Sure, I support this if anyone wants to send through a PR

For context you are talking about this:

The group denotes the “group” this badge appears on in the badges page:

You use it to “group” together certain badges on the badge page. I agree the word group is not a great word here and we should probably rename it to “section”