How to grant badges using Automation?

sorry, me again :slight_smile:

You mentioned that Automation plugin can help with granting badges. Maybe you have some hints on how to do so? I cannot find anything relatable in the dropdown:

image

1 Like

Looking at the commit, it seems it should be available for send_pms and zapier_webhook scripts:

This trigger needs a badge as required field and accepts an optional only_on_first_grant field. ATM it’s usable for: send_pms and zapier_webhook scripts.

3 Likes

Sorry it might not be super clear, so let me explain it.

ATM we only have a trigger for granted badge, which means we can calls scripts when a user is awarded a badge. But we don’t exactly have what you currently ask for a script to grant a badge which would be triggered on some event (joining a specific group? ). As explained, our best solution so far is Triggered custom badge queries

3 Likes

Thank you so much for your help!

I really hope that we’ll manage to set an API to grant a badge every time a user posts their first post :pray:t2:

Thank you!

This is very helpful!

Hey Helga,

If you want to automatically grant a badge, the best way to do this is via the SQL Query for the badge.
This can be found In admin->badges

Then scroll down to find the SQL query.

If you search through meta.discourse.org you might be lucky enough to find someone has already written the query you are looking for.

My favourite query grants a badge when someone joins a group. The example below grants the badge within 24 hours of the person joining the “discourse-admin” group

select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = (
  select g.id from groups g where g.name = 'discourse-admin'
)

I use the following settings with the query for the badge
image

For that you would need to enable custom SQL badges, which isn’t available to everyone depending on their hosting:

Though using the API is also suggested as an alternative.

Oh wow, I didn’t know about that. Thanks for the tip @JammyDodger!

I guess it would also be possible to get the result of an SQL query from the Data Explorer plugin, then use the api to grant the badge.

1 Like