Please explain what is optional in discord.py (or even in python) and how to solve the problem
I want to create a text feed with a command in a specific category. I did everything, but I can't add it to the category. I searched the internet, found nothing (I'm a beginner). I have a category id but if I just insert it:
await ctx.guild.create_text_channel(name=name, overwrites=overwrites, category = 774723755490410516)
Then writes that the wrong type:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'int' object has no attribute 'id'
The documentation says that it is necessary to type Optional
, but I do not know what it is.
Documentation: https://discordpy.readthedocs.io/en/latest/api.html#discord.Guild.create_text_channel
Thank you
1 answers
typing.Optional
indicates that the specified argument can be either of the specified type (in this case, CategoryChannel
) or None
.
To get a category, as well as any other channel by ID, you can use Guild.get_channel(ID)
.
await ctx.guild.create_text_channel(name, category=ctx.guild.get_channel(850289294063283387))
# name - "позиционный" аргумент, его не обязательно указывать по имени ("var" вместо "name = var")
Or you can use the create_text_channel
method of the category object itself: CategoryChannel.create_text_channel
category = ctx.guild.get_channel(850289294063283387)
await category.create_text_channel(name)