python - 频道未在类别 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)
解决方案
脱离上下文,很难看出你的代码是如何工作的。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")
推荐阅读
- go - 使用多个 goroutine 从通道中拉取
- android-studio - Android Studio 中的模块名称
- vue.js - 调用Vue过滤器动态崩溃组件
- corda - 如何向 Corda 帐户添加额外信息?
- python - Python读取字符串对象并搜索格式
- list - 如何为箭头写下序列?
- python - 将多个值发布到 django rest 框架中的单个字段?
- excel - 如何在 excel 中编辑超链接以同时打开 .doc 和 .docx 版本
- python - 从每一行的文本文件中提取子字符串?
- javascript - 使用什么方法来存储简单的 Web 应用程序项目的数据?