Repository: Pycord-Development/pycord

Issue:
Support newly_created attribute of THREAD_CREATE Websocket event
### Summary

Support differentiation of thread creation from join

### What is the feature request for?

The core library

### The Problem

The Discord service API supports differentiation between thread creations and joins, yet pycord [doesn't](https://docs.pycord.dev/en/master/api.html#discord.on_thread_join):
> Note that from the API’s perspective there is no way to differentiate between a thread being created or the bot joining a thread.

See the official documentation for the [THREAD_CREATE event](https://discord.com/developers/docs/topics/gateway#thread-create):
> Sent when a thread is created, relevant to the current user, or when the current user is added to a thread. The inner payload is a [channel](https://discord.com/developers/docs/resources/channel#channel-object) object.
> * When a thread is created, includes an additional `newly_created` boolean field.

Indeed, this is reflected in my testing where I merely created a new thread for the first time in a channel my bot was present and able to read messages in:
```py
{
  't': 'THREAD_CREATE',
  's': 15,
  'op': 0,
  'd': {
    'type': 11,
    'thread_metadata': {
      'locked': False,
      'create_timestamp': '2022-04-22T14:07:03.955000+00:00',
      'auto_archive_duration': 1440,
      'archived': False,
      'archive_timestamp': '2022-04-22T14:07:03.955000+00:00'
    },
    'rate_limit_per_user': 0,
    'parent_id': '[redacted]',
    'owner_id': '[redacted]',
    'newly_created': True,
    'name': 'adfsdgs',
    'message_count': 0,
    'member_count': 1,
    'last_message_id': None,
    'id': '[redacted]',
    'guild_id': '[redacted]',
    'flags': 0
  }
}
```

### The Ideal Solution

I propose that the current `discord.on_thread_join` event is functionally split into two separate events, depending on the presence of the `newly_created` attributes of the underlying `THREAD_CREATE` Websocket event:
- `on_thread_join` would remain for the case of the bot client is added to an existing thread
- `on_thread_create` would be a new event emitted on creation of a _new_ thread

### The Current Solution

Currently, to detect if a thread was newly created, people working with the bot framework would have to fuzzily compare the `Thread.created_at` attribute to the current time when catching an `on_thread_join` event.

### Additional Context

I came upon this limitation when seeking to implement bot functionality that would detect the creation of a thread in a certain channel and notify the guild's moderators of this activity.


Discussion:

Resolving patch:
diff --git a/discord/state.py b/discord/state.py
--- a/discord/state.py
+++ b/discord/state.py
@@ -897,7 +897,10 @@ def parse_thread_create(self, data) -> None:
         has_thread = guild.get_thread(thread.id)
         guild._add_thread(thread)
         if not has_thread:
-            self.dispatch("thread_join", thread)
+            if data.get("newly_created"):
+                self.dispatch("thread_create", thread)
+            else:
+                self.dispatch("thread_join", thread)
 
     def parse_thread_update(self, data) -> None:
         guild_id = int(data["guild_id"])


Test patch: