pythonpython-3.xdiscorddiscord.pypycord

Check if message was sent in a thread (discord.py / pycord)


I need my discord bot to check if slash command is being executed in a thread or not. Couldn't find anything related in the discord.py docs https://discordpy.readthedocs.io/en/stable/api.html and there's no info on that on stackoverflow or anywhere else :(

I basically need something like is_thread() function. If it's even possible. Thanks for your help.


Solution

  • What you're looking for is most likely ctx.channel.type which will tell you the type of channel that the command was executed in. A possible solution for your problem would look something like this:

    async def creativeName(ctx):
      if ctx.channel.type == "public_thread" or "private_thread":
        # Code to execute if it is in a thread goes here
      else:
        # Code, if any, to execute if it is not in a thread