How to grant badges using Automation?

AI-generated summary

The discussion revolves around granting badges using Automation in Discourse. Helga_Razinkova asks for help in achieving this, but it’s clarified by joffreyjaffeux that there’s no direct script to grant a badge on an event. However, Tris20 suggests an alternative method by using a SQL Query for the badge, which can be found in the admin panel. This query can be customized to grant badges based on specific conditions, such as joining a group.

JammyDodger notes that custom SQL badges are not available to everyone, depending on their hosting. For those who can’t use SQL queries, JammyDodger suggests using the API to grant custom badges. Alternatively, Tris20 proposes getting the result of an SQL query from the Data Explorer plugin and then using the API to grant the badge.

The discussion concludes with a potential solution for Helga_Razinkova to automatically grant a badge when a user posts their first post.

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:

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.

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 Creating triggered custom badge queries

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

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.