# Threads

![](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-Mk6vejx0Y4T_2gRSty8%2F-Mk70eoZJ4vJXhH5Oi_4%2Fimage.png?alt=media\&token=44d449fb-0b1d-4e27-b89d-2dd4e658d0b4)

{% hint style="danger" %}
This guide has been moved to <https://namantech.me/pycord>. Please visit the new guide instead, since this one might be outdated.

The new guide looks better, has better explanations, and is way better overall.
{% endhint %}

Whatever you think of Threads in Discord, you gotta admit they can be useful sometimes. They are a cool feature for support tickets, help channels, etc. Besides, the permissions for threads have been made much better by Discord now. Anyways, in this guide you will learn how to create, use and join threads with Py-cord.

Docs: <https://pycord.readthedocs.io/en/master/api.html#discord.Thread>

![All methods and attributes of Threads](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-Mk6vejx0Y4T_2gRSty8%2F-Mk79GBkFpQ-o230-mHv%2Fimage.png?alt=media\&token=c7cec1fa-cc76-42b0-9ae3-4e0a2f53b6e6)

## Creating Threads

With a few lines of code, we can create threads in Py-cord. Now, here are a few things to keep in mind:

&#x20; \- All public threads need a starting message. This message will start the thread. However, private threads (server boost level 2 required) do not require a starting message.

### Creating thread from a message

```python
message = await something.send("My Starting Message")
await message.create_thread(name="thread name", auto_archive_duration=60)
```

You may also use other ways, say, on\_message on by using buttons.

### Creating thread in a channel

```python
channel = bot.get_channel(...) # define this!
await channel.create_thread(name="Thread Name", message=None, auto_archive_duration=60, type=None, reason=None)
```

A thread type could be news\_thread, public\_thread, private\_thread. You may use it like `type=discord.ChannelType.news_thread`.&#x20;

## Deleting Threads

```python
thread = bot.get_channel(thread_id)  # a thread is a kind of channel too, so we use bot.get_channel
await thread.delete()
```

## Editing Threads

#### Parameters

* **name** ([`str`](https://docs.python.org/3/library/stdtypes.html#str)) – The new name of the thread.
* **archived** ([`bool`](https://docs.python.org/3/library/functions.html#bool)) – Whether to archive the thread or not.
* **locked** ([`bool`](https://docs.python.org/3/library/functions.html#bool)) – Whether to lock the thread or not.
* **invitable** ([`bool`](https://docs.python.org/3/library/functions.html#bool)) – Whether non-moderators can add other non-moderators to this thread. Only available for private threads.
* **auto\_archive\_duration** ([`int`](https://docs.python.org/3/library/functions.html#int)) – The new duration in minutes before a thread is automatically archived for inactivity. Must be one of `60`, `1440`, `4320`, or `10080`.
* **slowmode\_delay** ([`int`](https://docs.python.org/3/library/functions.html#int)) – Specifies the slowmode rate limit for user in this thread, in seconds. A value of `0` disables slowmode. The maximum value possible is `21600`.

```python
thread = bot.get_channel(id)  # a thread is a kind of channel too, so we use bot.get_channel
    await thread.edit(
        name="New Name",
        archived=True,
        locked=True,
        slowmode_delay=10,
        auto_archive_duration=60,
    )
```

## Joining Threads Automatically

If you have a Moderation Bot, you probably want your bot to join all the threads automatically. Only by doing so will you be able to Moderate Threads properly. Thankfully, there's an event for that.

```python
@bot.event
async def on_thread_join(thread):
    await thread.join()
```

## More

There are ton of things you can do with threads. Reading this guide, you must have gotten a hang of how to use these features. Now, you should head to the docs, view all the things you can do and use the features you want accordingly.

Docs: <https://pycord.readthedocs.io/en/master/api.html#discord.Thread>

### Examples + Cheatsheet

{% code title="threads.py" %}

```python
# THREADS CHEATSHEET

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!", Intents=discord.Intents.all())

## Creating Threads

@bot.command()
async def create(ctx):
    ### Creating Threads by message
    message = await ctx.send("I started a public thread!")
    await message.create_thread(name="New Thread", auto_archive_duration=60)

    ### Creating Threads by channel
    channel = bot.get_channel(...)
    await channel.create_thread(
        name="Thread Name",
        message=message,
        auto_archive_duration=60,
        type=None,
        reason="User told me to do so",
    )

@bot.command()
async def delete(ctx, id: int):  # will fail if you forget to make it an integer!
    thread = bot.get_channel(id)  # a thread is a kind of channel too, so we use bot.get_channel
    await thread.delete()

@bot.command()
async def edit(ctx, id: int):  # will fail if you forget to make it an integer!
    thread = bot.get_channel(id)  # a thread is a kind of channel too, so we use bot.get_channel
    await thread.edit(
        name="New Name",
        archived=True,
        locked=True,
        slowmode_delay=10,
        auto_archive_duration=60,
    )

@bot.event
async def on_thread_join(thread):
    await thread.join()

bot.run("TOKEN")
```

{% endcode %}

### Problem Solving

#### Unknown Message

If you get an error looking like`discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 10008): Unknown Message`

There could be multiple reasons, for example,

1. The message does not exist
2. The message already has a thread
3. The message is in channel *x*, you are trying to start a thread in channel *y*.
4. The message was deleted.

#### Forbidden

This means that your bot does not have the necessary permissions to run the command you wanted. Give it the correct permissions, then try again.

#### 'int' object has no attribute 'id'

You probably used `message.id` instead of `message`.

If not, remember that message is an object. You need to get the object, not the specify the message id. You should try to learn about Object Oriented Programming.

#### AttributeError: 'NoneType' object has no attribute *x*

This probably means that the bot was unable to fetch the thread. Check the message id, or use try-except.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brucecodes.gitbook.io/pycord/guide/threads.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
