Threads

Whatever you think of Threads in Discord, they are a cool feature for support tickets, help channels, etc. in this guide you will learn how to use threads with Py-cord.

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.

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

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:

- 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

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

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.

Deleting Threads

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) – The new name of the thread.

  • archived (bool) – Whether to archive the thread or not.

  • locked (bool) – Whether to lock the thread or not.

  • invitable (bool) – Whether non-moderators can add other non-moderators to this thread. Only available for private threads.

  • auto_archive_duration (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) – Specifies the slowmode rate limit for user in this thread, in seconds. A value of 0 disables slowmode. The maximum value possible is 21600.

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.

@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

threads.py
# 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")

Problem Solving

Unknown Message

If you get an error looking likediscord.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.

Last updated