# Checking if post or thread - Ruby

**URL:** https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437
**Category:** Development
**Created:** [April 30, 2019, 1:37pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437 "2019-04-30T13:37:52Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 1:37pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/1 "2019-04-30T13:37:52Z")

</div>

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: [GitHub - jackkelly-bayliss/invictus-discourse-chat-integration · GitHub](https://github.com/jackkelly-bayliss/invictus-discourse-chat-integration)  
File Name: [invictus-discourse-chat-integration/lib/discourse\_chat/provider/discord/discord\_provider.rb at master · jackkelly-bayliss/invictus-discourse-chat-integration · GitHub](https://github.com/jackkelly-bayliss/invictus-discourse-chat-integration/blob/master/lib/discourse_chat/provider/discord/discord_provider.rb)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 1:50pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/2 "2019-04-30T13:50:16Z")

</div>

> [@jackkelly-bayliss](#):
>
> I have attempted to find it already with ‘post\_number’

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

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 1:54pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/3 "2019-04-30T13:54:06Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/meta/original/3X/6/1/6114833ed83e86261a170eda4b1b1d2fc757bfee.png)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 1:57pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/4 "2019-04-30T13:57:05Z")

</div>

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

```plaintext
message

```

lines to

```plaintext
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.

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 1:59pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/5 "2019-04-30T13:59:38Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 2:01pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/6 "2019-04-30T14:01:14Z")

</div>

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: [Discourse Chat Integrations](https://meta.discourse.org/t/chatroom-integration-plugin-discourse-chat-integration/66522)

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 2:17pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/7 "2019-04-30T14:17:32Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/b/3bc1d83256b06e5c46049ff98aa5690b320b55ed.png)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 2:51pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/8 "2019-04-30T14:51:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [April 30, 2019, 3:07pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/9 "2019-04-30T15:07:29Z")

</div>

> [@david](#):
>
> if you can link to the code it will be much easier than a screenshot.

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

````plaintext
    ```
     code goes here
    ```

````

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 3:16pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/10 "2019-04-30T15:16:49Z")

</div>

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?

 ![image](https://global.discourse-cdn.com/meta/original/3X/c/4/c4607b8184c25a919d82488d99c5842a0e163c2d.png)

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 3:17pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/11 "2019-04-30T15:17:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 3:28pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/12 "2019-04-30T15:28:59Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 3:30pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/13 "2019-04-30T15:30:24Z")

</div>

You still need to

> [@david](#):
>
> change the two
> 
> ```plaintext
> message
> 
> ```
> 
> lines to
> 
> ```plaintext
> return message
> 
> ```
> 
> so that it stops execution of the function at that point.

> <https://github.com/jackkelly-bayliss/invictus-discourse-chat-integration/blob/master/lib/discourse_chat/provider/discord/discord_provider.rb#L69>

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 3:42pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/14 "2019-04-30T15:42:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 5:03pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/15 "2019-04-30T17:03:35Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [April 30, 2019, 5:04pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/16 "2019-04-30T17:04:33Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![jackkelly-bayliss](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jackkelly-bayliss/32/139749_2.png) [@jackkelly-bayliss](https://meta.discourse.org/u/jackkelly-bayliss)
#### Post date: [April 30, 2019, 6:13pm UTC](https://meta.discourse.org/t/checking-if-post-or-thread-ruby/116437/17 "2019-04-30T18:13:22Z")

</div>

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](https://global.discourse-cdn.com/meta/original/3X/9/c/9c033d4e956d9e2d9af5b78c8db43a680ffb602a.png)
