Checking if post or thread - Ruby

Hello fellow Discourse users,

I’m currently attempting to build upon an already existing plugin called ‘discourse-chat-integration’ for which I’ve forked and attempted to customize for a certain community but I’m having a wee mind blank. How would I specifically test if it’s a post or thread? I’m trying to build two seperate notification blocks, one for a post and another for a thread.

I have attempted to find it already with ‘post_number’ but it doesn’t seem to work. Any ideas? Thank you.

GitHub Link: https://github.com/jackkelly-bayliss/invictus-discourse-chat-integration
File Name: https://github.com/jackkelly-bayliss/invictus-discourse-chat-integration/blob/master/lib/discourse_chat/provider/discord/discord_provider.rb

1 Like

That’s the right approach, but the first post in a topic has post_number==1, not post_number==0

4 Likes

Hello David,

Thank you for your reply and response my topic. Would this be the correct method in completing the task? The first block should only send if it’s a thread and the second block should only send if it’s a post.

Yes, that looks roughly correct. I think you will need to change the two

message

lines to

return message

so that it stops execution of the function at that point.

What are you trying to achieve here? It might be possible to do something using the existing configuration options, rather than maintaining a fork of the plugin.

1 Like

Thank you for your reply, I will change the stated lines and test now. Currently the plugin will send a Webhook to Discord on any new topics or posts. I am quite new to Ruby so I apologize for the lack of knowledge with this situation.

You can control whether you want “topic” and/or “post” notifications in the admin user interface. More information is in the “configuring rules” section here: Chatroom Integration Plugin (discourse-chat-integration)

1 Like

I came across that option previously but I want a totally seperate notification layout for each. I’ve changes the lines you stated previously but it’s throwing an error at me now which suggests that no message is being returned from the previous screenshot.

Can you share the error message? Also, if you can link to the code it will be much easier than a screenshot.

2 Likes

Or if linking is inconvenient you can paste code here like this:

    ```
     code goes here
    ```
3 Likes

Thank you for responding. I did link the file I am referring to in my thread description. These are the errors I am receiving, I would assume that I need to include some kind of file. Any thoughts?

Oh, I see. You need to use post.post_number instead of just post_number.

3 Likes

I’ve just changed the variables now and the ‘New Post’ section works but the top code block for new threads does not. Any ideas?

You still need to

2 Likes

Ahh, I forgot that important step haha. That’s worked perfectly, thank you very much for your time, effort and patience haha.

1 Like

I apologise for bumping this thread but integrating with this code, how would I be able to retrieve a user’s custom field value? Would this even be possible at all?

Which user? The one who wrote the post?

If so, you can do post.user.custom_fields["custom_field_name"]

(replace custom_field_name with the custom field you want to access)

1 Like

Yes, if that’s possible. I’ve created a custom field named ‘Embed Colour’ and added it into the code but it appears to be blank when it’s retrieved. Any suggestions?

image