I am having an issue fetching active threads on a discord channel. There is not much info about it, or maybe I just couldn't find it. So maybe it's not even possible idk
My code:
import discord
from tools.config import TOKEN
bot = discord.Bot()
@bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
@bot.slash_command(name="chat", description="some desc")
async def chat(ctx, msg):
channel = bot.get_channel(CHANNEL_ID)
user_name = ctx.author
await ctx.response.defer(ephemeral=True)
thread = await channel.create_thread(name=f"Thread was created {user_name}", type=None)
await thread.send("Message in a thread")
await ctx.respond(f'Some message with tagging after creating a thread. {user_name.mention}')
bot.run(TOKEN)
What I am trying to implement is this: user executes slash command, then the bot creates a thread where all the communications with the user are done in, but before creating a thread bot needs to verify that the user is not in any active threads in the current channel. To do that, I need to fetch active threads in the current channel.
First of all, make sure you're using Discord.py 2.0+, there is important changes and features that only exist on newest versions.
I think I found a way to solve your problem. First get all threads from channel using channel.threads
, if I understood well the docs this already get not archived threads (which means they're still active), then check if your user is between the members of threads.
async def user_has_no_thread(ctx):
threads = ctx.channel.threads
if none(ctx.author in thread.members for thread in threads):
return True
return False
you can also define a faster auto_archive_duration
when use channel.create_thread()
to make sure they will be archived soon, by default is around 3 days I guess. Take a look here if you want know more about the function.
Also make sure you have enable members intents for this works
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)