ディスコースにおける信頼レベルに基づく自動タイトル割り当て

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!

「いいね!」 2

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

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

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.

「いいね!」 5

Would a theme component that added this info to the post/user card be more flexible? That way users can still use the title field for other things as intended (badges, group identifiers, something custom you may want to add), and having it added by a theme component would mean they couldn’t change/remove it.

「いいね!」 1

Hi,

I’m trying to implement this using the badge system automation, but I’m facing a limitation.

To run the user_group_membership_through_badge script, I must specify a group. However, I already use the default system groups: Trust Level 1, 2, 3, 4. These groups are built-in and cannot be selected in the automation tool.

If I create new groups to use with this automation, I’m afraid all current members will not be carried over automatically — and I will lose the accurate mapping of users to their trust levels.

Here’s a screenshot showing the error when the group field is left empty:

Is there any way to apply the badge-to-title update without requiring a group?
Or is there a workaround to target the Trust Level groups directly within this automation?

@xkhalid I’ve put together a quick plugin (also my first!) that:

  • Assigns a title to new users
  • Changes the title to the corresponding one with their Trust Level. E.g. TL1 → TL2: users get the TL2 title now, instead of the TL1 title.
  • Updates user titles every 12 hours (plans to make the frequency configurable)

All titles configurable via settings :slightly_smiling_face:.

「いいね!」 4

Thanks !

After I add this to app.yml

- git clone https://github.com/NateDhaliwal/add-title-based-on-trust-level.git

and rebuild, I encountered a 502 Bad Gateway error. Any solution?
For now, I’ve removed it from app.yml and everything works well.

Huh, odd. IIRC that error may sometimes appear after a rebuild, but waiting it out will solve it (or another rebuild).

Yeah, I know — but this issue only happened after I added the Git repo and rebuilt. It gave me this error.

However, I’ll try it again later.


What about this issue? Could it be related?

「いいね!」 1

I’ll take another look at this, thanks for informing me.

「いいね!」 1

@xkhalid could you give it a shot now? Just updated it.

「いいね!」 1

Thanks ! It works now.

But it’s not reflected for all users. For example, this user is at trust level 1 and is a new user, but there’s no title assigned.

It will take time or ?

@xkhalid I’ve pushed a fix. Please tell me if it works now.
Thanks!

I want to thank @NateDhaliwal for his plugin !

It was very helpful ! and the title updated every 12 hours.

「いいね!」 1

Hi, thank you for your great plugin!

I have a more complex need:
I’d like to set user titles automatically based on a combination of group membership (for example: group A or group B) and trust level (for example: TL2, TL3, etc.). For example, a user in group A with TL2 would get the title “A - TL2”, and so on.

Would it be possible to extend your plugin to support this feature, or do you have any advice on how to implement it?
I don’t have coding experience myself, so I’d greatly appreciate any suggestions or help.

Thank you very much!

@joo Are these groups the user’s primary groups?

Yes, I want to use the user’s primary group for deciding the title.