Automatically Apply Badges to Group Members (in Hosted Version)

(Andrew Pautler) #1

I’d like to automatically apply certain badges to users in certain groups. In searching the forum, it appears this may be possible via SQL queries, etc. We are on the hosted plan, so that is not possible. In talking with Discourse support, it appears a solution might be to make an app/script that uses the API to apply the badge to users in a groups. Two questions with this:

  1. Has someone done this already and would be willing to share?
  2. How would I implement an app/script that uses the API within the hosted forum setup?

(Sam Saffron) #2

It is possible on hosted plans, just let us know by emailing and we can enabled them.

(Andrew Pautler) #3

@sam Gotcha. What do you think is the better option though? Is it dangerous to begin making SQL queries from within the hosted platform? Makes me a bit nervous because there are other admins in our account and I worry if they accidentally do something in the currently hidden SQL area, it could seriously mess up our database.

(Sam Saffron) #4

If this is a once off I would just make the badges, let us know when you are done and then disable badge SQL.

Should be fine.

I think it makes sense to extend groups to, optionally, automatically grant a badge, so SQL is not involved.

You can also do this via the API but it would require running a regular job on your end.

(cosmo) #5

Is there a way to paste an example code that we can upload on our site?
Is user1 is added to groupX, award badgeX

Simple, nothing more to it

(Steve Combs) #6

Here is the badge SQL, just replace your_group_name

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

(cosmo) #7

This will run via the scheduler correct? It is not triggered be the group add, but just runs daily and awards based on group membership?

(Steve Combs) #8

In the badge settings you need to set the trigger to daily and select the option for revocation daily

(cosmo) #9

@scombs, I had posted in another thread, and someone responded almost at the same time. The code is slightly different. Do you know what the difference would be?

main difference is: gu. vs g.

Other poster’s code:

SELECT gu.user_id, gu.created_at AS granted_at, null AS post_id
FROM group_users AS gu
WHERE gu.group_id = ( SELECT id FROM groups WHERE name ilike 'InsertTheTeamNameHere' )

is there any practical difference?
I’m a complete novice at this, and I just want to give the site owner the code to copy/paste so he doesn’t have to modify it at all.

btw, the group name we are using is: Confirmed_ST_User

(Steve Combs) #10

Not sure. I saw the other post too.

Here is the #howto with the code

(Jay Pfaffman) #11

Should be the same as I explained there. :slight_smile:

(cosmo) #12

thanks @scombs @pfaffman!! :grinning:

(Kane York) #13

The code is identical, just some renaming, capitalization, and formatting differences.