繁体   English   中英

在类别 discord.py 中创建通道

[英]Create channel in Category discord.py

我知道,我知道它在这里 - Python - 将频道添加到类别

但是我遇到了一个错误...我认为 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'

可能是什么问题? 我需要做什么? 请给我一个解决方案...在此先感谢!

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

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

来源: 如何通过 id 获取类别?

类别不应该是字符串,而是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)

您可以使用:

@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'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM