频道未在类别Discord.py中创建

2024-06-08 13:51:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的密码:

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)

Tags: 用户程序send密码ifchannelcontentawait
1条回答
网友
1楼 · 发布于 2024-06-08 13:51:06

断章取义,很难看出代码是如何工作的。您是否对所有“命令”都使用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")

相关问题 更多 >