Automatically Assigning Titles Based on Trust Level in Discourse

Hi everyone,

I have set up a ranking system with custom titles, but as you know, Discourse doesn’t have a built-in way to assign a default title to all new users. So, I created a workaround where:

  • New members are in Group 1
  • Active members are in Group 2
  • And so on…

The method I used is as follows:

cd /var/discourse
./launcher enter app
rails c

User.where(trust_level: 0).update_all(title: "عضو جديد")
User.where(trust_level: 1).update_all(title: "عضو جديد")
User.where(trust_level: 2).update_all(title: "عضو مشارك")
User.where(trust_level: 3).update_all(title: "عضو مبدع")

The Problem

I have to run these commands manually every time a new user joins. If I run the script today and a new member posts tomorrow, they won’t get the title automatically, so I have to log in to the server and re-run the commands.

I tried automating it with a Ruby script and calling it via crontab, but I get an error.

I asked ChatGPT, and it told me:

“The User model is not loaded when you run the script directly with Ruby outside of the Discourse environment.”

The issue is that I don’t have the proper config/environment setup inside the Discourse files.

I added It, but it didn’t work.

My Question

Is there a way to automate this without using crontab? Or how can I properly execute a script that updates user titles without manually entering the Rails console each time?

Thanks in advance!

1 Like

Any help ?

Maybe it’s possible by creating custom automation for Discourse Automation & Create custom Automations, but I don’t know how it works.

1 Like

You can achieve this with a custom automation script using the User Event and User Promoted Event triggers. When a new user is created (User Event) or their trust level changes (User Promoted Event), you can trigger a script to automatically assign them a title based on their trust level.

5 Likes

Thanks @jahan_gagan ! any help to make this script ?

Wow! Nice hacking! :clap:

It does seem unfortunate that some group settings are not available for the automated groups like trust levels. It should be possible to set the user title based on the trust level group they are in but it is not.

You could try doing this using the badge system. Create a badge that is granted when a member reaches the trust level you want, and then set the “Allow badge to be used as a title”.

You can then create an automation using the “User group membership through badge” script that runs every day and updates the user’s title and flair.

3 Likes