首页 > 解决方案 > 频道未在类别 Discord.py 中创建

问题描述

我想在用户喜欢的类别中创建频道,但是当我尝试运行程序时,它不会在我想要的类别中创建频道。

这是我的代码:

if sly.content.startswith("sly ctchan"):
x=sly.content.split("sly ctchan ",1)[1]
await sly.channel.send("Category Name")
y=await client.wait_for('message', timeout=15.0)
z=discord.utils.get(sly.guild.categories, name =y)
await sly.guild.create_text_channel(x, overwrites=None, category=z)

标签: pythondiscorddiscord.py

解决方案


脱离上下文,很难看出你的代码是如何工作的。on_message您是否为所有“命令”使用事件?看看Commnads,它们非常整洁。

正如 Taku 在评论中指出的那样,await client.wait_for('message')返回一个Message Object。您知道使用.content来获取消息,但第二个问题:所有类别名称都显示为大写,但大小写可能会有所不同,用户不会知道。我们需要对此进行纠正,并使检查不关心套管。

除此之外,你已经把一切都整理好了。我在那里添加了一个检查,所以它只会接受来自发起频道制作的用户的消息。否则任何人都可以写任何东西,它会开始寻找一个类别。您还可以添加最后一条if语句,仅当频道确实找到类别时才创建频道。

    await sly.channel.send("Name of category:")

    # Check for the right user
    def check(m):
        return m.author == sly.author
        
    categoryName = await client.wait_for('message', check=check, timeout=15)

    categoryChannel = discord.utils.find(lambda cat : cat.name.upper() = categoryName.content.upper(), sly.guild.categories)

    # If it found a category
    if categoryChannel:
        await sly.guild.create_text_channel(channelName, category=categoryChannel)
        return

    # Didn't find a category? Tell the user it didn't find one, don't create a channel
    await sly.channel.send(f"Couldn't find a category by the name __**{categoryName}**__. Aborting")


推荐阅读