Discord.py Создание нового канала и роли из шаблона

#python #discord.py

#python #discord.py

Вопрос:

Итак, я пытаюсь создать канал и роль на основе того, что пользователь запросил в команде.

В качестве примера:

 User: >newrealm (realmname) (emoji)
  

Пока у меня есть это:

 @commands.command()
  async def newrealm(self, ctx,* , reason):
    reason = reason.split(' ')
    realm, emoji = reason
    author = ctx.message.author
    guild = ctx.message.guild
    color = discord.Colour(0x3498DB)
    await guild.create_role(name= realm   " OP", color = color)
    await guild.create_text_channel(realm   emoji, category = 777360644252762123) #issue here
    await ctx.send("Created Channel and Role!")
  

Но я получаю там обратную трассировку.

 Ignoring exception in command newrealm:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/runner/SolarisTurtleMain/cogs/RealmCMD.py", line 55, in newrealm
    await guild.create_text_channel(realm   emoji, category = 777360644252762123)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/guild.py", line 905, in create_text_channel
    data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/guild.py", line 823, in _create_channel
    parent_id = category.id if category else None
AttributeError: 'int' object has no attribute 'id'
  

Комментарии:

1. В документации для create_text_channel говорится, что категория должна быть CategoryChannel . Откуда 777360644252762123 берется?

2. Это идентификатор категории

Ответ №1:

category Аргумент ожидает CategoryChannel объект, но вы передаете его int . Вы можете использовать get_channel, чтобы получить CategoryChannel :

 category_channel = guild.get_channel(777360644252762123)
  

Затем передайте его create_text_channel функции:

 await guild.create_text_channel(realm   emoji, category = category_channel)