在类别 discord.py 中创建频道

时间:2021-03-01 11:50:22

标签: python discord discord.py

我知道,我知道它在这里 - Python - Adding channel to category

但是我遇到了一个错误...我使用的是 Python 3.9 和 discord.py 的 1.5.0 版,我想...所以这是我的命令:

@client.command()
async def create ( s, arg: str ):
   guild = s.message.guild
   c = 'FORUM' 
   channel = await guild.create_text_channel ( arg, category = c )

即使存在严格的类别,我也会收到一条错误消息:

Ignoring exception in command create:
Traceback (most recent call last):
File "C:\Python 3.9\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\User1\Gifts.py", line 13, in create
channel = await guild.create_text_channel ( arg, category = c )
File "C:\Python 3.9\lib\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 "C:\Python 3.9\lib\site-packages\discord\guild.py", line 823, in _create_channel
parent_id = category.id if category else None
AttributeError: 'str' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Python 3.9\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Python 3.9\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python 3.9\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'id'

可能是什么问题?我需要做什么?请给我一个解决方案...提前致谢!

3 个答案:

答案 0 :(得分:1)

您传递的是类别名称,但您需要传递类别本身! 您可以通过调用获取类别:

c = get(guild.category_channels, name = "FORUM")

来源: How to get a category by its id?

答案 1 :(得分:1)

类别它不应该是一个字符串,而是一个 CategoryChannel 实例,使用 utils.get 可以通过名称获取它

@client.command()
async def create(s, arg: str):
   guild = s.message.guild
   cat = discord.utils.get(s.guild.categories, name="FORUM")
   channel = await guild.create_text_channel(arg, category=cat)

答案 2 :(得分:1)

您可以使用:

@client.command()
async def create(ctx, arg: str):
   channel = await ctx.guild.create_text_channel(arg, category=discord.utils.get(ctx.guild.categories, name='FORUM'))